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

Simulating Limit Orders Clarifications

  • Hello All,

    I am wondering what is a correct sumulation when there is a gap between 2 consecutive bars.

       datetime    open    high     low   close 
     2016-12-12  785.04  791.25  784.35  789.27 
     2016-12-13   793.9  804.38  793.34   796.1 

    Let's execute a long limit order on 2016-12-12 with price equal to the high of current bar (791.25)

    ipdb> pprint(vars(order.created))
    {'comm': 0.0,
     'dt': 736310.9999999999,
     'exbits': deque([]),
     'margin': None,
     'p1': 0,
     'p2': 0,
     'pclose': 789.27,
     'pnl': 0.0,
     'pprice': 0,
     'price': 791.25,
     'pricelimit': 791.25,
     'psize': 0,
     'remsize': 0,
     'size': 6,
     'trailamount': None,
     'trailpercent': None,
     'value': 0.0}

    BT will skip that order execution due to the following logic in ./backtrader/brokers/

        718             if plimit >= popen:
        719                 # open smaller/equal than requested - buy cheaper
        720                 pmax = min(phigh, plimit)
        721                 p = self._slip_up(pmax, popen, doslip=self.p.slip_open,
        722                                   lim=True)
        723                 self._execute(order, ago=0, price=p)
        724             elif plimit >= plow:
        725                 # day low below req price ... match limit price
        726                 self._execute(order, ago=0, price=plimit)

    Technically our limit order price is between high of previous bar and low of current bar, so during live trading that order will be executed I think.

    I am wondering what are your thoughs on this scenario.

  • administrators

    If you issue an order on 2016-12-12 (the timeframe seems daily) it won't be considered for execution until 2016-12-13.

    The bar on 2016-12-12 is already closed when you see it.

  • Thanks for clarification.

    I had missunderstanding of Stop vs Limit Order.

Log in to reply