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

Broker with real order book:bid/bid size/ask/asksize

  • Hello,

    I would like to back test a strategy using a real order book, is there any way to build an order book with bid and ask, I have both bid and ask second series and I would like that if I buy with market order, the broker use ask prices and if you sell , the broker use bid prices. Also if you use limit orders the broker should use the correct time series

    Is there any way to cover this funcionality with backtrader?



  • administrators

    No without developing a broker which understands that the prices carry and extra payload (bid and ask) and uses that payload for the order execution.

    Furthermore, there are several things to take into account for such bid/ask scenarios:

    • Your working timeframe is for example 1-minute. That means that for a complete set of ticks that make up the 1-minute bar there are two final prices representing bid and ask

    • If your signal says ... buy, the prices to consider are those of the next bar. If the buy is of type Limit, the execution price may fall in the middle of the next bar ... but the only available bid / ask prices are those around the close, which may be actually far away from the middle: the bid / ask information needed for the Limit execution is lost in the process of making up the 1-minute bar.

    One really needs to know/define what the expected behavior of using bid / ask

  • The problem, when you consider the mid price as close price, is that threre is a big difference between bid-mid and ask-mid and your error when the asset is active (EUR/USD), is very big. I have reviewd github repository and these projects could solve my problem:

  • Another choice is to consider the bid offer spread as a measure of transactions costs:

    what is your opinion?

  • administrators

    Those projects and the blog post are describing the behavior of Limit Order Books. Of course when you have real-time you can monitor each and every change of those and see how things are executed (and even estimate how things are going to be executed)

    Not bearing any relationship to timeframes (minutes, hours, ...), because you see every change (ticks) is the problem.

    Let's imagine the scenario:

    • You pass 1-minute data to backtrader
    • A set of indicators gives you the signal to enter the market long (buy)
    • You fire the order in backtrader

    And this is what happens:

    • backtrader gets the next 1-minute bar
    • And it tries to see, depending on the execution type, if the order can be matched against the range of prices from that bar

    With, for example, Limit orders:

    • The matching price can be at many points in the bar

    Now the question:

    • Are you going to provide a Limit Order Book for each an every possible price in the 1-minute bar? (Luckily it's not an infinite number, but depending on the range it can be a huge amount of entries in the Limit Order Book)

    From a practical point of view the cost of the transaction and simulating the ask-bid spread effect can be achieved by including slippage (which is already supported by the broker)

Log in to reply