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

How to handle a wide bid-ask spread?



  • I'm a beginner of backtrader. I read all the document and found that the excute price only depend on the OHLC (open-high-low-close) price. What if the underlying asset has a wide bid-ask spread? Can backtrader handle this kind thing?


  • administrators

    backtrader was never meant for bid-ask simulation and it for a very simple reason.

    Let's imagine you have an OHLC and you have a limit order with a price that happens to sit in the middle of the HL range (i.e.: limit price = (H + L) / 2)

    If the timeframe of the bar is 1-day and we look at recent ES-Mini bars the range can be 120 points between 2891 and 2771 with our limit price right at 2831.

    We don't really know when the 2831 price was hit during the day. The question here: Which bid-ask prices have to be considered for matching the desired limit price?.

    When we see the bar the only valid bid-ask prices would be that corresponding to the close which in this case was 2772, far far away from our price. And even if we had information for the bid-ask prices for the area 2831, this price could have been crossed several times during the day, so at which time do we execute the trade?

    Because backtrader has to work with any timeframe, the bid-ask component as shown cannot be really considered.

    That's why people use slippage: Docs - Slippage which simply worsens your matching price.

    In any case and for a Limit order, one shouldn't apply slippage because the Limit order will be matched at the requested or at a better price. Use it for Market orders.


Log in to reply
 

});