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/

    Cancel order example?

    General Code/Help
    2
    2
    156
    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.
    • F
      Firebee last edited by

      Hi all,
      what I'm trying to do in english is this:

      If price closes under the lowest of last 50 candles:

      • create 2 layers of limit buy order at 0.9lowest and 0.8lowest.

      • if one layer is filled, limit sell at buyprice+5%

      • if both layers are filled, cancel the sell order above and set a new limit sell order at averagebuyprice+5%

      #------------------------------------------

      The canceling part is needed because the next round my signal enters, the unfilled sell limit will be there waiting for me.
      I haven't found any working examples of cancelling a specific order, and can't make it just by reading the docs.

      Strat is working as intended, but I can't get to cancel the sell order.

      What I'm trying rn is:

      when one layer is filled:
      self.order = self.sell(exectype=Order.Limit, price=self.buy1 * 1.05, tradeid=self.something)

      if the second layer gets filled:
      self.cancel(ref=self.something)
      self.order = self.sell(exectype=Order.Limit, price=self.averagebuy* 1.05)

      any help or live example is appreciated. I know I could probably ease my life using oco or brackets but I really would like to understand how to cancel orders just to have it in my toolbox.

      1 Reply Last reply Reply Quote 0
      • run-out
        run-out last edited by

        @firebee You are going to have to track your orders. You can do this either with a list or a dictionary. If just trading one symbol, a list will suffice,otherwise a dict of lists with symbols as keys.

        So, based upon what you said above, when you place your orders, you will have

        # two limit buy orders. 
        self.ords = [o1, o2]
        

        When these orders fill, you will get a notification in notify order where you can filter for completed trades. As your trades complete, you can take action.

        def notify_order(self, order):
            # Check if an order has been completed
            if order.status in [order.Completed]:
                   ## Here you have a just completed order. Compare this to your self.ords list and take whatever actions you need. Cancel/create, whatever works. 
                
        

        RunBacktest.com

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