Spread History Store
The naive way to answer "what is the typical spread at 14:00 server time" keeps a growing array of every observed sample and sorts it on every query - workable for a few hundred points, unworkable after weeks of tick-by-tick recording, both in memory and in the cost of sorting. This library trades unbounded sample precision for a bounded, 1-point- resolution histogram: 24 hour-of-day buckets and 4 session buckets, each holding a 300-slot count array (0 to 298 points 1:1, slot 299 catching "299 points or more"). Recording a sample is a single array increment; answering any percentile query is a single walk over 300 cumulative counts, regardless of whether the store has recorded a hundred samples or ten million. Sessions are classified from configurable server-time hour boundaries for London and New York; anything outside both windows falls through to Asian, so the overnight wrap needs no special-case handling. The London/ New York overlap is computed as the genuine intersection of the two configured windows, not a separately hand-tuned range, so changing either session's boundaries automatically keeps the overlap consistent with them. Notes and limitations - The histogram resolution is fixed at 1 point per bucket up to 298 points; a 299th "overflow" bucket absorbs anything at or above that, reported as exactly 299 rather than the true (higher) value. This is more than enough range for ordinary forex spreads but will compress genuinely extreme spread spikes into that one bucket. - Session boundaries are server-time hours the caller supplies (or accepts as the shipped defaults). MQL4 has no built-in way to know a broker's server-time offset from GMT/UTC, so the same input hour can mean a different real-world time on different brokers - the shipped defaults are a reasonable approximation, not a guaranteed-correct session split for every broker. - SpreadHistoryStorePercentileFromBuckets() returns -1 (not 0) when a bucket set has no recorded samples yet, so a caller can distinguish "no data" from a genuine zero-point spread. - SpreadHistoryStore itself uses only fixed-size struct array members (both dimensions of every histogram are compile-time constants), so it copies and assigns safely as an ordinary value type with no manual deep-copy step needed. - This library performs no trading action; SpreadHistoryStoreRecordNow() is the only function that reads live market data, and only the current spread and current time - it opens no chart objects, orders, or files.
Script · MT4 · v1.0 · Free
About Spread History Store
Spread History Store is a script for MetaTrader 4. It has 1 setting, such as Samples per hour. MQL source code included.
Files in the download: SpreadHistoryStore.mqh, SpreadHistoryStore.txt, SpreadHistoryStoreDemo.mq4.
Settings
| Setting | Default | What it does |
|---|---|---|
| Samples per hour | 50 | synthetic samples generated per hour bucket |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: Trading Sessions · MT4 Script · MQL4 Source Code