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

Submitted StopTrail is dead but then somehow Completed?



  • Hello,
    I somehow have a bt.Order.StopTrail that refuses to be canceled. I am working on a shorting strategy where I buy a short based on some indicators. The position will only be closed when a trailing stop is satisfied or self.cross_up is satisfied.

    Below is the code for notify_order(). I have tried in several places within the method to cancel the trailing stop (self.trail_stop')

        def notify_order(self, order):
            # https://www.backtrader.com/docu/order-creation-execution/order-creation-execution/
            # Check the order status. 
            print('{}: Order ref: {} / Type {} / Status {}'.format(
                    self.data.datetime.datetime(0),
                    order.ref, 'Buy' * order.isbuy() or 'Sell',
                    order.getstatusname()))
            
            if (self.cross_up and self.trail_stop):
                self.cancel(self.trail_stop)
            
            # If it is submitted/accepted, leave the fucntion 
            if order.status in [order.Submitted, order.Accepted]:
                if self.cross_up:
                    self.cancel(self.trail_stop)
                    print(f'{self.datas[0].datetime.datetime(0)} trying to cancel {self.trail_stop}')
                return
    

    In the output log, ref: 2 is my StopTrail since we see that there is a trail amount. You can see that the bt.Order.StopTrail is dead (Alive: False), yet it is still completed? How is that possible?

    2019-11-01 09:00:00: Order ref: 2 / Type Buy / Status Accepted
    2019-11-01 09:00:00 trying to cancel Ref: 2
    OrdType: 0
    OrdType: Buy
    Status: 4
    Status: Completed
    Size: 6
    Price: None
    Price Limit: None
    TrailAmount: 10.97938953872858
    TrailPercent: None
    ExecType: 5
    ExecType: StopTrail
    CommInfo: <backtrader.comminfo.CommInfoBase object at 0x7fae48146e50>
    End of Session: 737363.9999999999
    Info: AutoOrderedDict([('ref', '999')])
    Broker: None
    Alive: False
    2019-11-01 09:00:00: Order ref: 3 / Type Buy / Status Accepted
    2019-11-01 09:00:00 trying to cancel Ref: 2
    OrdType: 0
    OrdType: Buy
    Status: 4
    Status: Completed
    Size: 6
    Price: None
    Price Limit: None
    TrailAmount: 10.97938953872858
    TrailPercent: None
    ExecType: 5
    ExecType: StopTrail
    CommInfo: <backtrader.comminfo.CommInfoBase object at 0x7fae48146e50>
    End of Session: 737363.9999999999
    Info: AutoOrderedDict([('ref', '999')])
    Broker: None
    Alive: False
    2019-11-01 09:00:00: Order ref: 2 / Type Buy / Status Completed
    

Log in to reply
 

});