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

Bug in bbroder._try_exec_stoplimit()?

  • Hi, guys! I find the following issue when I'm writing blogs(, 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 :)

Log in to reply