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

Order Execution



  • Hello all,

    I am facing an issue regarding executing stop loss and take profit orders based on values in the order.executed.price.

    1. My next method issues a buy order based on a signal:
    def next(self):
    
      if not self.position:
          if self.trade_sig < 0:
                   short_order = self.sell(size = self.units)
                   short_order.addinfo(name = 'MARKET_SELL')
          
          elif self.trade_sig > 0:
                   buy_order = self.buy(size = self.units)
                   buy_order.addinfo(name='MARKET_BUY')
    
    1. My notify_order() has the logic to execute the stop loss and take profit orders in order.Completed:
    if order.info['name'] == 'MARKET_BUY':
           sell_order = self.sell(size = self.units, exectype = bt.Order.Stop, price = (order.executed.price - self.atr[0]*1.5))
           sell_order.addinfo(name = 'MARKET_BUY_STOP')
    
           take_profit = self.sell(size = self.units, exectype = bt.Order.Limit, price = order.executed.price + 0.01000)
           take_profit.addinfo(name = 'Take_Profit')
           
    
    elif order.info['name'] == 'MARKET_SELL' :
          sell_order = self.buy(size = self.units, exectype = bt.Order.Stop, price = (order.executed.price + self.atr[0]*1.5))
          sell_order.addinfo(name = 'MARKET_SELL_STOP')
    
          take_profit = self.sell(size = self.units, exectype = bt.Order.Limit, price = order.executed.price - 0.01000)
          take_profit.addinfo(name = 'Take_Profit')
    
    
    1. What I cant seem to do is execute the take profit along with the stop loss order. I believe I need some way to cancel the orders if one fills to make the logic work. If anyone can provide some insight into this problem of mine, it will be greatly appreciated.
      Thank you in advance :)

  • administrators

    Where is your take profit?



  • Hello @backtrader, the take profit limit order is issued along with the stop-loss order 100 pips above from my executed price. The problem I am facing with the approach is the orders are getting filled simultaneously. I am stuck finding a way to cancel one or the other depending on which one gets hit first.


  • administrators

    Well, you changed the code you pasted ... usually people paste it again corrected or point out that the code has been changed ...

    OCO - Docs - OCO