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

Cancel order problems



  • I'm learning the broker.cancel function and did the following small test. I find the market order and stop trail order cannot be canceled after the order is accepted.

                    self.cancel(o)
                    self.cancel(st)
    

    From my point of view, since the market buy order and stoptrail order will be completed on the next bar, why not cancel on present bar in backtesting? Hope to get help.

    from __future__ import (absolute_import, division, print_function,
                            unicode_literals)
    
    import argparse
    import datetime
    
    import backtrader as bt
    
    class St(bt.Strategy):
        params = dict(
            ma=bt.ind.SMA,
            p1=10,
            p2=30,
            stoptype=bt.Order.StopTrail,
            trailamount=0.0,
            trailpercent=0.0,
        )
    
        def __init__(self):
            ma1, ma2 = self.p.ma(period=self.p.p1), self.p.ma(period=self.p.p2)
            self.crup = bt.ind.CrossUp(ma1, ma2)
            # self.order = None
    
        def next(self):
            if not self.position:
                if self.crup:
                    o = self.buy()
                    st= self.sell(exectype=self.p.stoptype,
                           # trailamount=self.p.trailamount,
                           trailpercent=self.p.trailpercent)
                    self.cancel(o)
                    self.cancel(st)
                    # self.order = None
                    
                    print('*' * 50)
    


  • I am not an author and it might be a bug, but it seems right: order should be submitted and accepted by broker, only after it can be cancelled. Notifications about order changes happen after the current bar close. So therefore bt can not see the order on the bar when it was issued. Also Market order can'not be cancelled after it is submitted, broker accepts and executes it momentarily if there are enough funds.


  • administrators

    @da-zhang said in Cancel order problems:

    From my point of view,

    Interesting point of view. Go to any exchange, send a Market order and try to cancel it.

    Come then back and let us know how things worked out.

    @da-zhang said in Cancel order problems:

    why not cancel on present bar in backtesting?

    Because "present" in "present bar" is a fallacy. That bar is already the PAST, because if you see that it has ALREADY happened.

    If you go to the market thinking in terms of bars your money will be gone very soon. The market is made of TICKS which is a single price point representing an execution (in FOREX not even that, as most data providers provide the Bid price even if no execution has happened).

    Your Market order may actually be the one that generates the next tick.

    Using bars is simply a representation of the market.

    NOTE: The StopTrail order as sent to the broker is also immediately executed because it will be immediately triggered with the given parameters.



  • thanks for your reply, I understand it now. From the log information, I see the buy order is submitted, accepted and completed on the next bar, So it cannot be cancelled. The system is designed wisely.



  • @backtrader @ab_trader
    thanks for your reply, I understand it now. From the log information, I see the buy order is submitted, accepted and completed on the next bar, So it cannot be cancelled. The system is designed wisely.


Log in to reply
 

});