I am currently developing a daytrading application which based on rules and algorithms buys or sells stocks, which right now might buy/sell a stock in the magnitude of 100 per day.

I am trying to backtest my strategy but i do not know how to handle the spread between bid and ask. If i for example in my backtesting assume that i can buy for the current price, i might get a biased result since buying at the latest price assumes that someone is still willing to sell at that price, which might not be the case.

Are there any best practises here? One of my big problems is that i do not have access to historical order depth for each data-point, so i do not know the ask and bid. Here are a couple of options i have considered (for buying a stock):

  • Use the latest price (In the real world, i might not be able to buy at this price)
  • Calculate the average movement between to trades in the stock, and add this to the latest price and use that.

And if i would have access to the order depth:

  • Use the current bid (What if the volume of that bid is much smaller than my volume?)
  • Use the average of ask and bid (Should give a good value, assuming that half of the time the next price will be the current ask, and half the time it will be the bid?)
  • What order types would you be using in real life and in the back-testing? It looks from your scenario market orders. Have you considered using stop buy and stop sell orders?
    – Victor
    May 31, 2017 at 7:16
  • Yes, my plan is to use market orders. But even if i use stop orders, wont I face the same problem? Lets say a stock reaches (from a higher price) $5.00 is my trigger, and then i want to buy at $5.00, but the current ask is $5.01 so i will not be able to buy at $5.00.
    – Cleared
    May 31, 2017 at 7:34
  • 1
    Stop orders ARE market orders. A regular stop order just means if price reaches point X, the order is sent at market. With a stop-limit, the limit order is sent instead but the default is market for "stop orders".
    – misantroop
    May 31, 2017 at 9:19
  • 1
    You cannot perfectly backtest a strategy without historical data. And even then, it won't be dead on accurate because you're not going to have tick by tick data indicating volume size available at any given moment in time. Sep 11, 2021 at 13:18

3 Answers 3


Without using depth data, the best approximate would be a function of volatility and liquidity. The weights would depend on your size. Without going this far, the simplest method would be to wait for the price to trade through your price above x percent. This assumes your position sizes are adjusted for liquidity for the instrument beforehand.

  • This was my original idea, to some how calculate an "average spread" for a given stock, as a function of volatility and liquidity. I will give this a try to see if i can find a good calculation for "average spread"
    – Cleared
    May 31, 2017 at 12:46
  • The classic problem with using something like average spread is when prices are at their most volatile and impact the results the most, generally the spread is also at its widest and most illiquid, which creates infrequent but huge bias, as all factors are often all simultaneously correlated.
    – Philip
    May 31, 2017 at 13:59
  • That's very true. I suppose comparing the very short time volatility to "average" longer term volatility could help slightly but it's no substitute for real depth. It will come down to whether mediocre changes in slippage are determining the strategy viability or not.
    – misantroop
    May 31, 2017 at 14:14

I would advice forward testing.

Let's assume that your purchases and sales are not affecting the market. That is an error, but a concious one. It'll stay small for as long as you stay small.

What you can do then is to simulate actually running the strategy. I'd be careful to not only randomly select days or periods to run a simulation, but to define market conditions you want represented. Bull / bear - disruptions, quarterlies, annuals, etc.

If your method(s) and hypotheses survives forward testing, that is a much stronger proof than backtesting. No matter how careful backtesting is done, you always end up (in some way) validating your hypotheses on the same data that was used to create it - which is the most common and dangerous scientific malpractice (IMHO).


As you rightly observe, you really need access to order depth data to avoid a variety of biases related to spread/liquidity. It would be strongly recommended to try and get that data as soon as possible, as it is exceptionally hard to accurately backtest any trading strategy without it. If/when you have order depth data you need to use the most pessimistic and close to real world assumptions when you do run your model:

  • You should always only allow your strategy to purchase off the current bid
  • Only allow it to purchase/sell the liquidity that was available
  • If your strategy suggested prices deeper into the order book were also buys/sells you should also buy them at the relevant liquidities
  • You need to model your bankroll fluctuations via Kelly or whatever money management system you are using to make sure you stay solvent over the time frame using said strategy

Two further quotes always worth remembering when backtesting any strategy:

"I’ve never seen a bad backtest”

-- Dimitris Melas

"The first principle is that you must not fool yourself – and you are the easiest person to fool"

-- Richard Feynman

  • Thanks, I will try to see if I can get my hands on the depth data as well, but i think it will be hard.
    – Cleared
    May 31, 2017 at 12:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .