Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    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

    General Code/Help
    execution backtrader
    2
    4
    496
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Robin Dhillon
      Robin Dhillon last edited by Robin Dhillon

      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 :)
      1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators last edited by

        Where is your take profit?

        1 Reply Last reply Reply Quote 0
        • Robin Dhillon
          Robin Dhillon last edited by

          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.

          1 Reply Last reply Reply Quote 0
          • B
            backtrader administrators last edited by

            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

            1 Reply Last reply Reply Quote 0
            • 1 / 1
            • First post
              Last post
            Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors