Advertisement IC Markets

Giving a backtest a different spread for each hour of the day

Share, develop, and backtest custom MQL4/MQL5 Expert Advisors, Python data-scraping scripts, trading bots, and automated market alert systems.
Post Reply
Fairman
Posts: 6757
Joined: Tue Jul 21, 2026 7:11 am
Location: Abuja

Giving a backtest a different spread for each hour of the day

Post by Fairman »

Giving a backtest a different spread for each hour of the day

Most backtests use a single fixed spread or the spread recorded in the historical data. Both can be misleading for a scalping strategy.

A fixed spread assumes the cost of trading at 03:00 is the same as at 09:00. It isn't. On EURUSD the spread during London can be a fraction of what it is just after the New York close. A strategy that trades at all hours will look better in the test than it would live, because the expensive hours are priced as if they were cheap.

Historical spread data in tick files is better, but it comes from a particular broker at a particular time. Your broker's spreads may differ, especially in the quiet hours where differences between brokers are biggest.

A practical middle ground is to build an hourly spread profile from your own broker. Record the spread on each pair every minute for a few weeks, then calculate the median and a high percentile for each hour of the day. In the backtest, apply the spread for the hour the trade happens in, not a single value.

If your tester doesn't support this directly, you can apply it in your own result processing. Take the trade list, look up the hour, subtract the extra cost. It's not perfect, but it's much closer to reality than one number for the whole day.

Using the high percentile instead of the median gives a more conservative test. If the strategy survives that, it has a better chance live.
It’s Fairman :geek:
Recommended broker for automated trading & scalping IC Markets
Fairman
Posts: 6757
Joined: Tue Jul 21, 2026 7:11 am
Location: Abuja

Re: Giving a backtest a different spread for each hour of the day

Post by Fairman »

Day of week matters too, though less than hour. Monday morning in Asia and Friday afternoon in New York often have wider spreads than the same hour midweek. If you have enough data, splitting the profile by weekday as well as hour gives a finer picture. With a few weeks of data, though, each cell in that grid will only have a handful of samples, so it may be better to just add a separate adjustment for the Monday open and Friday close rather than building a full table. Keep it simple until you have enough data to support something more detailed.
It’s Fairman :geek:
Fairman
Posts: 6757
Joined: Tue Jul 21, 2026 7:11 am
Location: Abuja

Re: Giving a backtest a different spread for each hour of the day

Post by Fairman »

News times are the other big source of spread variation. A spread profile built from regular minutes will understate costs around scheduled releases. One approach is to remove the minutes around major news from the profile, then handle those minutes separately. Either exclude trades during news windows in the backtest altogether, or apply a much wider spread for that period. Mixing news minutes into an hourly average hides them, because one wide minute out of sixty barely moves the median, but it can easily wipe out a scalp that happened to land in that minute. An extra column in the trade list marking whether each trade was within a set number of minutes of a release makes this easy to apply after the test runs.
It’s Fairman :geek:
Fairman
Posts: 6757
Joined: Tue Jul 21, 2026 7:11 am
Location: Abuja

Re: Giving a backtest a different spread for each hour of the day

Post by Fairman »

Slippage can be modelled in a similar way. If you have live trade history, you can calculate the average difference between the requested and filled price by hour. Adding that to the hourly spread gives a total cost per hour, which is closer to what the strategy actually pays. Most of the time slippage is small compared with spread, but in quiet hours with thin liquidity it can add up, and on stop orders it's often bigger than people expect, especially for strategies that trade breakouts right as the level is taken out. Even a rough estimate of slippage by hour is better than assuming zero, which is what most testers do by default.
It’s Fairman :geek:
FXS London Scalper
Posts: 806
Joined: Sun Oct 18, 2026 12:25 pm

Re: Giving a backtest a different spread for each hour of the day

Post by FXS London Scalper »

An hourly spread profile makes a backtest far more realistic. A useful source is your own broker's tick data, recorded for a few weeks and averaged by hour. Using the maximum spread per hour as well as the average is worth considering, since scalping entries tend to happen in fast moments. If a strategy only stays profitable with the average spread, it is likely fragile at the London open, where spreads can briefly jump before settling.
FXS Official Team | London Session Desk
Fairman
Posts: 6757
Joined: Tue Jul 21, 2026 7:11 am
Location: Abuja

Re: Giving a backtest a different spread for each hour of the day

Post by Fairman »

Put the results from the fixed spread test and the hourly spread test side by side. If the strategy's results change a lot, the hours it trades in are doing a lot of the work, and that's useful to know. Some strategies turn out to only work in the cheapest hours, which isn't a problem in itself, as long as the live version is restricted to those hours too. The test should make that dependency visible instead of hiding it behind an average spread that doesn't exist at any real time of day. A table with both sets of results, broken down by hour, usually makes the dependency obvious in a few seconds.
It’s Fairman :geek:
Post Reply