Spread Filter
A spread filter is one of the simplest guards an EA can have and one of the easiest to get quietly wrong. Two mistakes account for nearly every broken version of this idea: measuring in points instead of pips, which silently tenths or tens the threshold on a 3 or 5 digit broker, and reading Bid/Ask from a stale quote instead of the live one - on MQL4 that means a missing RefreshRates() call before the read; on MQL5, SymbolInfoDouble() always reads live server state, so there is no equivalent step to forget. This header fixes both. SpfSpreadPips always converts through the same 3/5-digit-aware factor used across this catalogue, and the platform-specific mechanics of reading a genuinely current Bid/Ask are handled internally on each platform - the MQL4 version calls RefreshRates() first, on the basis that a spread filter checking a stale number has already failed at its one job; the MQL5 version needs no such call. An unresolvable symbol - misspelled, or a Point of zero - is treated as a fail, not a pass. A filter that cannot measure the spread must refuse to trade rather than wave a broken lookup through as if the spread were acceptable; the alternative, treating "unknown" the same as "0.0 spread", is how a filter ends up being trusted for a symbol it was never actually checking. Notes and limitations - Measures the instantaneous spread only. It has no memory of recent history, so it cannot answer "has the spread been wide for the last five minutes" - call it repeatedly from OnTick if that pattern is needed. - On MQL4, RefreshRates() is called on every read, which is correct for a filter meant to gate live trading decisions but does mean the header is not meant to be called hundreds of times per tick in a tight loop without caching the result once per tick first. The MQL5 version has no equivalent cost to this pattern since SymbolInfoDouble() has no refresh step, but the same "cache once per tick if calling heavily" advice still applies for simple efficiency. - Spread here means Ask minus Bid on the traded symbol directly; it does not account for any additional commission a broker charges separately from the spread.
Script · MT5 · v1.0 · Free
About Spread Filter
Spread Filter is a script for MetaTrader 5. It has 1 setting, such as Max spread pips. MQL source code included.
Files in the download: SpreadFilter.mq5.mqh, SpreadFilter.txt, SpreadFilterDemo.ex5, SpreadFilterDemo.mq5.
Settings
| Setting | Default | What it does |
|---|---|---|
| Max spread pips | 2.0 | Threshold used in the pass/fail table below |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
- Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
- Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: MT5 Script · MQL5 Source Code