3

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?)
Cleared
  • 133
  • 5

3 Answers3

2

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.

misantroop
  • 604
  • 4
  • 9
1

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).

Stian
  • 1,104
  • 9
  • 9
0

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

Philip
  • 5,808
  • 20
  • 20