Spread filters built on a rolling median instead of a fixed cap
Posted: Thu Oct 22, 2026 6:22 am
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.
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.