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

Switch off limit price matching rule on open



  • Consider the following lines in the BT doc

    *Price Matching:

    backtrader tries to provide most realistic execution price for Limit orders.
    
    Using the 4 price spots (Open/High/Low/Close) it can be partially inferred if the requested price can be improved.
    
    For Buy Orders
    
            Case 1:
    
            If the open price of the bar is below the limit price the order executes immediately with the open price. The order has been swept during the opening phase of the session*
    

    Is there anyway to switch this behavior off? Say for cases when the OHLC bars are constructed from intraday data?

    Basically to have the open price be treated just as the other ones (for a limit buy, if open price < smaller than limit price, execute the limit at the limit price).

    Alternatively, would it be possible to have BT ignore the open price altogether? I mean is it safe to feed BT an open-less file or do I risk running into undefined behavior?


  • administrators

    @kav said in Switch off limit price matching rule on open:

    Is there anyway to switch this behavior off?

    No.

    @kav said in Switch off limit price matching rule on open:

    Say for cases when the OHLC bars are constructed from intraday data?

    There will still be an open price for the bar, regardless of how it is constructed

    @kav said in Switch off limit price matching rule on open:

    Alternatively, would it be possible to have BT ignore the open price altogether?

    Yes. Write a broker and plug it in.



  • @BT: thanks for the quick answer. Just to clarify, when I wrote

    Say for cases when the OHLC bars are constructed from intraday data?

    I meant from say when using 1Min OHLC bars (so the bars are not daily frequencies).

    When you write:

    Yes. Write a broker and plug it in.

    can you kindly give me the name of the function to change, so I can find it in the source and do that?

    Thanks in advance,


    P.S. I understand the behavior I'am trying to mute is this one:

            if order.isbuy():
                if plimit >= popen:
                    # open smaller/equal than requested - buy cheaper
                    pmax = min(phigh, plimit)
                    p = self._slip_up(pmax, popen, doslip=self.p.slip_open,
                                      lim=True)
    self._execute(order, ago=0, price=p)
    

    and

                if plimit <= popen:
                    # open greater/equal than requested - sell more expensive
                    pmin = max(plow, plimit)
                    p = self._slip_down(plimit, popen, doslip=self.p.slip_open,
                                        lim=True)
    self._execute(order, ago=0, price=p)
    

    Could you kindly flesh out how to go about overloading the broker class so as to mute these conditions? I could not find a documentation about this,



  • So in the file lib/python3.6/site-packages/backtrader/brokers/, I copied bbroker.py to mybbroker.py which is the same as the original except for the function _try_exec_limit which I have replaced by:

        def _try_exec_limit(self, order, popen, phigh, plow, plimit):
            if order.isbuy():
                if 1==0:
                    #plimit >= popen:
                    # open smaller/equal than requested - buy cheaper
                    pmax = min(phigh, plimit)
                    p = self._slip_up(pmax, popen, doslip=self.p.slip_open,
                                      lim=True)
                    self._execute(order, ago=0, price=p)
                elif plimit >= plow:
                    # day low below req price ... match limit price
                    self._execute(order, ago=0, price=plimit)
    
            else:  # Sell
                if 1==0:
                    #plimit <= popen:
                    # open greater/equal than requested - sell more expensive
                    pmin = max(plow, plimit)
                    p = self._slip_down(plimit, popen, doslip=self.p.slip_open,
                                        lim=True)
                    self._execute(order, ago=0, price=p)
                elif plimit <= phigh:
                    # day high above req price ... match limit price
                    self._execute(order, ago=0, price=plimit)
    

    Now my questions is how can I tell BT to use mybbroker.py instead of bbroker.py? (I understand I can simply over-write bbroker.py but I'm sure there is a more elegant way to switch these things. I just don't know how)

    Thanks in advance,


Log in to reply
 

});