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 problems

    Indicators/Strategies/Analyzers
    3
    5
    748
    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.
    • da zhang
      da zhang last edited by

      I'm learning the broker.cancel function and did the following small test. I find the market order and stop trail order cannot be canceled after the order is accepted.

                      self.cancel(o)
                      self.cancel(st)
      

      From my point of view, since the market buy order and stoptrail order will be completed on the next bar, why not cancel on present bar in backtesting? Hope to get help.

      from __future__ import (absolute_import, division, print_function,
                              unicode_literals)
      
      import argparse
      import datetime
      
      import backtrader as bt
      
      class St(bt.Strategy):
          params = dict(
              ma=bt.ind.SMA,
              p1=10,
              p2=30,
              stoptype=bt.Order.StopTrail,
              trailamount=0.0,
              trailpercent=0.0,
          )
      
          def __init__(self):
              ma1, ma2 = self.p.ma(period=self.p.p1), self.p.ma(period=self.p.p2)
              self.crup = bt.ind.CrossUp(ma1, ma2)
              # self.order = None
      
          def next(self):
              if not self.position:
                  if self.crup:
                      o = self.buy()
                      st= self.sell(exectype=self.p.stoptype,
                             # trailamount=self.p.trailamount,
                             trailpercent=self.p.trailpercent)
                      self.cancel(o)
                      self.cancel(st)
                      # self.order = None
                      
                      print('*' * 50)
      
      B 1 Reply Last reply Reply Quote 0
      • A
        ab_trader last edited by

        I am not an author and it might be a bug, but it seems right: order should be submitted and accepted by broker, only after it can be cancelled. Notifications about order changes happen after the current bar close. So therefore bt can not see the order on the bar when it was issued. Also Market order can'not be cancelled after it is submitted, broker accepts and executes it momentarily if there are enough funds.

        • If my answer helped, hit reputation up arrow at lower right corner of the post.
        • Python Debugging With Pdb
        • New to python and bt - check this out
        1 Reply Last reply Reply Quote 1
        • B
          backtrader administrators @da zhang last edited by backtrader

          @da-zhang said in Cancel order problems:

          From my point of view,

          Interesting point of view. Go to any exchange, send a Market order and try to cancel it.

          Come then back and let us know how things worked out.

          @da-zhang said in Cancel order problems:

          why not cancel on present bar in backtesting?

          Because "present" in "present bar" is a fallacy. That bar is already the PAST, because if you see that it has ALREADY happened.

          If you go to the market thinking in terms of bars your money will be gone very soon. The market is made of TICKS which is a single price point representing an execution (in FOREX not even that, as most data providers provide the Bid price even if no execution has happened).

          Your Market order may actually be the one that generates the next tick.

          Using bars is simply a representation of the market.

          NOTE: The StopTrail order as sent to the broker is also immediately executed because it will be immediately triggered with the given parameters.

          da zhang 1 Reply Last reply Reply Quote 2
          • da zhang
            da zhang last edited by

            thanks for your reply, I understand it now. From the log information, I see the buy order is submitted, accepted and completed on the next bar, So it cannot be cancelled. The system is designed wisely.

            1 Reply Last reply Reply Quote 0
            • da zhang
              da zhang @backtrader last edited by

              @backtrader @ab_trader
              thanks for your reply, I understand it now. From the log information, I see the buy order is submitted, accepted and completed on the next bar, So it cannot be cancelled. The system is designed wisely.

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