Monitoring recently placed orders in next() and canceling based on conditions
-
Hi,
I'm trying to figure out how to conditionally not place trades, or cancel previous trades, based on some custom logic in
next()
, much like we useif not pos:
(in fact, I tried modifying myif not pos:
to beif not pos and not self.order:
but this doesn't change the behavior at all).Here's the two things I'd like to experiment with:
- Only place an order if no mainside order has been opened in the last 10 bars
or
- If the strategy wants to place an order, cancel any open main side orders placed in the last 10 bars.
I would like to do this in
next()
, but I'm open to doing this innotify_order()
I've now spent hours on this, and it seems like it should be relatively simple to get something like:
for order in open_orders: print(order) if order.date_created < (datetime.now() - timedelta(minutes=50)): cancel(order)
I've read all the docs on canceling orders, but I'm a little hung up~
Thanks for any help.
-
@tw00000 said in Monitoring recently placed orders in next() and canceling based on conditions:
if not pos: to be if not pos and not self.order: b
You are probably referring to
self.position
(which gets you the position for the 1st data feed in the system)@tw00000 said in Monitoring recently placed orders in next() and canceling based on conditions:
- Only place an order if no mainside order has been opened in the last 10 bars
@tw00000 said in Monitoring recently placed orders in next() and canceling based on conditions:
it seems like it should be relatively simple to get something like:
if order.date_created < (datetime.now() - timedelta(minutes=50)):That's wrong.
datetime.now()
may have nothing to do with the actual timestamps of the data (for example if you backtest things which are 20 years in the past) and there is nothing there which takes into account the number of bars (even if you believe it to be so, because you restrict it tominutes=50
, the numbers of bars for the last50
minutes may change and it will for sure change if you change thetimeframe
)You need to keep the
len
as the clock (theStrategy
offers you a general system clock and each data feed has its own clock which can vary if the data feeds are not synchronized or have different timeframes)See: Docs - Platform Concepts and the section "Lines
len
"- When the order is notified as
Submitted
(i.e.: it has gone into the broker), record thelen
- In your logic, check the actual
len
against the recorded length for a given order. - If it's larger than your desired value (
10
), cancel the order.