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

Doubt relating the combination of StopTrail and target order!



  • Hello folks! I am trying to develop a strategy and was confused about something in the back trader framework. In spite of going through the combination thoroughly I couldn't make any conclusions on what's the potential outcome of my strategy.

    so, in my def next(self): method I defined a condition to enter my trades and used the exec=type as the StopTrail Order along with trail amount and valid time as follows
    ```

    def next(self):
            if self.order:
                return
            if not self.position:
                if condition:
                    self.order = self.buy(price=self.dataopen[0], exectype=bt.Order.StopTrail,                   trailamount=self.p.trail_stop, valid = datetime.datetime.now()+datetime.timedelta(minutes=self.p.valid_mts))
                            
            else:
                if (self.data.open[0] > self.p.targetprice):
                     self.order = self.close() #self.sell()
    

    So, my doubt is, am I supposed to use self.close() to close the position or self.sell() given that I want all the orders associated with the buy order to be cancelled( most importantly the stop trail order and any partially unfilled buy and stop trail orders associated with them). Actually, this is the best way I managed to verify if the target is being reached after every time step. Why is there no order like a Bracket order with a trailing stop order?? [where we could place all the 3 orders, namely Limit Entry Price, Stop Loss trigger Limit which executes at market price and the Target Price] Am I missing something here? Please enlighten me. Thank you



  • I am not really sure, if I understood you correctly. But it seems you want to use self.sell_bracket(...) and self.buy_bracket(...)

    https://www.backtrader.com/blog/posts/2017-04-01-bracket/bracket/



  • So, Is it possible for me to use the Buy bracket order along with a Trailing Stop loss, i.e. where the exectype is StopTrail?



  • yes, just set the exec types accordingly.
    Also for your close vs sell question, close will use the whole open position size while sell will allow to enter any arbitary size.



  • But, when I try to use the notify order, with the following code, the compiler throws and Attribute error: 'Position Object has no attribute ''is closed'. How could I print the log if I can't access if the position is closed, I need this because I am running a long-short strategy and would like to keep track of what's happening in the algo with different logs

        def notify_order(self, order):
             if order.status in [order.Submitted, order.Accepted]:
                   return
    
             if order.status in [order.Completed]:
    
                if order.isbuy():
                    self.log(
                        'BUY EXECUTED, Price: %.5f, Cost: %.5f, Comm %.5f' %
                        (order.executed.price,
                        order.executed.value,
                        order.executed.comm))
    
                    self.buyprice = order.executed.price
                    self.buycomm = order.executed.comm      
    
                if order.issell():
                    self.log(
                        'SELL EXECUTED, Price: %.5f, Cost: %.5f, Comm %.5f' %
                        (order.executed.price,
                        order.executed.value,
                        order.executed.comm))
    
                    self.sellprice = order.executed.price
                    self.sellcomm = order.executed.comm     
    
                if self.position.isclosed():
                    self.log('Position Closed, Price: %.5f, Cost: %.5f, Comm %.5f' %
                            (order.executed.price,
                            order.executed.value,
                            order.executed.comm))


  • you have an error in your code. the position object does not have an isclosed method. to know if an position is open check position.size.



  • you can check if an trade was closed by calling:

    def notify_trade(self, trade):
            if not trade.isclosed:
                return
    


  • @dasch said in Doubt relating the combination of StopTrail and target order!:

    yes, just set the exec types accordingly.

    That works, thank you



  • @dasch Even this isn't helping

    if order.isclosed:
        self.log('Position Closed, Price: %.5f, Cost: %.5f, Comm %.5f' %
             (order.executed.price,
             order.executed.value,
             order.executed.comm))
    

    The traceback is

    AttributeError: 'AutoInfoClass_OrderBase_Order_BuyOrder' object has no attribute 'isclosed'
    


  • order will not have isclosed, trade will have.

    you can check the order.status for order.Completed



  • @NikolaTesla Okay, I think it's only possible to use order.isclosed if we use simple orders like self.buy() and self.sell() and it doesn't work for self.buy_bracket() and self.sell_bracket(). Is it?



  • no, you get the difference between order, trade and position wrong.
    check out the documentation for trades, orders and positions.

    https://www.backtrader.com/docu/order/
    https://www.backtrader.com/docu/position/
    https://www.backtrader.com/docu/trade/



  • My bad, so if I were to use buy and sell orders along with trailing stops and if the respective conditions are True, something like

    if not position:
         if buy_condition:
             self.buy(price, exectype=trailstop, trailstop value)
    
        if sell_condition:
             self.sell(price, exectype=trailstop, trailstop value)
    
    else:
         if (open_price[0] > buy_price+target) or (open_price[0] < sell_price-target):
            self.order = self.close()
    

    Then will I be able to use check if a position is closed using some condition?


Log in to reply
 

});