Advertisement IC Markets

Spread filters built on a rolling median instead of a fixed cap

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

Spread filters built on a rolling median instead of a fixed cap

Post by Fairman »

Spread filters built on a rolling median instead of a fixed cap

The usual spread filter in a scalping EA is a fixed maximum. Don't trade if the spread is above, say, 1.5 pips. It works, but it has two problems. The normal spread varies by time of day, so a fixed cap is either too loose for London or too tight for Asia. And it doesn't adapt when the broker's typical spread changes over months.

A rolling median handles both. The EA records the spread on every tick, or every few seconds to save resources, and keeps the last few thousand values. The filter then compares the current spread with the median of that window. If the current spread is more than, say, 1.5 times the median, the EA waits.

The median works better than the average here because spikes during news would drag the average up and make the filter too permissive right after. The median ignores those outliers.

You can add a hard cap on top for safety, so that even if the median somehow drifts very high, the EA won't trade at an absurd spread. But the main logic follows what's normal for that pair, that broker and that time.

Code wise, it's just an array and a sort, or a smarter rolling median structure if you care about speed. For tick level spread data on one symbol, a simple sort every few seconds is fine.

For anyone who prefers to keep it simple, a fixed cap per session is a reasonable middle ground. One cap for Asia, one for London, one for New York. It isn't as adaptive as a median, but it handles most of the time of day problem with three numbers in the settings and no extra code beyond a check of the server hour.
It’s Fairman :geek:
Recommended broker for automated trading & scalping IC Markets
FXS HongKong Scalper
Posts: 407
Joined: Sat Oct 24, 2026 10:15 pm

Re: Spread filters built on a rolling median instead of a fixed cap

Post by FXS HongKong Scalper »

A rolling median is a big improvement over a fixed cap. One refinement is to keep a separate median for each hour of the day. A single rolling window still mixes London ticks with Asian ticks if it covers several hours, so at the Tokyo open it compares the current spread with a London baseline and blocks trades that are normal for that hour. With 24 small buckets the filter compares like with like. It needs a few days to fill the buckets, so saving them to a file between restarts helps.
FXS Official Team | Hong Kong Session Desk
Fairman
Posts: 6757
Joined: Tue Jul 21, 2026 7:11 am
Location: Abuja

Re: Spread filters built on a rolling median instead of a fixed cap

Post by Fairman »

Window length matters a lot. Too short, maybe a few minutes, and the median just follows the current spread, so the filter never triggers. Too long, a whole day, and it mixes Asian and London spreads, which defeats the purpose. I've had decent results with a window of about an hour, which is long enough to be stable and short enough to reflect the current session. The right length also depends on how often you sample. Sampling every tick during London gives many more data points per hour than in Asia, so a fixed count of samples covers less time when the market is busy. A time based window avoids that imbalance.
It’s Fairman :geek:
Fairman
Posts: 6757
Joined: Tue Jul 21, 2026 7:11 am
Location: Abuja

Re: Spread filters built on a rolling median instead of a fixed cap

Post by Fairman »

Another option is a median per hour of the day built from historical data, rather than a rolling one. The EA loads a table of typical spreads for each hour and compares the live spread with the value for the current hour. That's more stable and doesn't need a warm up period after a restart. The downside is that it has to be rebuilt from time to time as the broker's spreads change. Rebuilding it once a month from recent data is usually enough. Brokers change spreads gradually, and a table that's a few months old can be noticeably off during quieter hours, which is when the filter matters most.
It’s Fairman :geek:
Fairman
Posts: 6757
Joined: Tue Jul 21, 2026 7:11 am
Location: Abuja

Re: Spread filters built on a rolling median instead of a fixed cap

Post by Fairman »

For backtesting, most testers don't give you a realistic varying spread, so this filter can behave oddly in tests. If the tester uses a fixed spread, the median equals the spread and the filter never triggers. If you have real tick data with spreads, it works properly. That's another reason to use real tick data when testing anything that depends on spread. If your tester only allows a fixed spread, test the filter separately by running the EA on demo for a few weeks and checking how often it blocks trades. That gives a more honest picture than a backtest that can't exercise this part of the code.
It’s Fairman :geek:
Fairman
Posts: 6757
Joined: Tue Jul 21, 2026 7:11 am
Location: Abuja

Re: Spread filters built on a rolling median instead of a fixed cap

Post by Fairman »

Logging how often the filter blocks a trade is useful. If it blocks more than a small fraction of signals during London, either the multiplier is too strict or your broker's spread is very unstable. Either way, the log tells you something you'd want to know about the costs of the strategy. A filter that never blocks anything is equally suspicious and should be checked. I keep a daily count in the log of how many signals were blocked and the median spread at the time. Glancing at it once a week shows if the broker's conditions have shifted and whether the multiplier still makes sense.
It’s Fairman :geek:
Post Reply