For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

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 use if not pos: (in fact, I tried modifying my if not pos: to be if 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:

    1. Only place an order if no mainside order has been opened in the last 10 bars

    or

    1. 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 in notify_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.


  • administrators

    @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:

    1. 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 to minutes=50, the numbers of bars for the last 50 minutes may change and it will for sure change if you change the timeframe)

    You need to keep the len as the clock (the Strategy 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"

    1. When the order is notified as Submitted (i.e.: it has gone into the broker), record the len
    2. In your logic, check the actual len against the recorded length for a given order.
    3. If it's larger than your desired value (10), cancel the order.