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/

    Bug in bbroder._try_exec_stoplimit()?

    General Discussion
    1
    1
    36
    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.
    • 码农甲
      码农甲 last edited by

      Hi, guys! I find the following issue when I'm writing blogs(https://blog.csdn.net/m0_46603114, Chinese only) of backtrader orders:
      When running a stoplimit order, I got the following output:

      2019-05-28, BUY CREATE, exectype StopLimit, close 12.49, price 12.55, pricelimit 12.51, valid: 2019-06-07
      2019-05-29, ORDER SUBMITTED
      2019-05-29, ORDER ACCEPTED
      2019-05-29, Open: 12.36, High: 12.59, Low: 12.26, Close: 12.40
      2019-05-30, Open: 12.32, High: 12.38, Low: 12.11, Close: 12.22
      2019-05-30, BUY EXECUTED, Price: 12.32, Cost: 1232.00.
      

      The stop price is 12.55, and the price limit is 12.51. The stoplimit order was created on 05-28.

      On 05-29, the open(12.36) was less than stop price(12.55), but the high(12.59) was greater than stop price(12.55). So the order was triggered on 05-29.

      Also on 05-29, since the close(12.40) was less price limit(12.51) , and price limit(12.51) was less than stop price(12.55). So the price limit should be reached before the bar on 05-29 was closed and the stoplimit order should be executed on 05-29.

      But the output showed the stoplimit order was executed on 05-30.

      When I dug into the source code of _try_exec_stoplimit() method:

          def _try_exec_stoplimit(self, order,
                                  popen, phigh, plow, pclose,
                                  pcreated, plimit):
              if order.isbuy():
                  if popen >= pcreated:
                      order.triggered = True
                      self._try_exec_limit(order, popen, phigh, plow, plimit)
      
                  elif phigh >= pcreated:
                      # price penetrated upwards during the session
                      order.triggered = True
                      # can calculate execution for a few cases - datetime is fixed
                      if popen > pclose:
                          if plimit >= pcreated:  # limit above stop trigger
                              p = self._slip_up(phigh, pcreated, lim=True)
                              self._execute(order, ago=0, price=p)
                          elif plimit >= pclose:
                              self._execute(order, ago=0, price=plimit)
                      else:  # popen < pclose
                          if plimit >= pcreated:
                              p = self._slip_up(phigh, pcreated, lim=True)
                              self._execute(order, ago=0, price=p)
      

      I found that there were no operations when (phigh >= pcreated) & (popen < pclose) & (plimit < pcreated) happened.

      Is this a bug for stoplimit order? Or other logics were concerned?

      Thanks in advance for your reply :)

      1 Reply Last reply Reply Quote 1
      • 1 / 1
      • First post
        Last post
      Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
      $(document).ready(function () { app.coldLoad(); }); }