Loading…

Gap Fill Policy Engine

Feeding a gapped series straight into a moving average, a regression, or a correlation calculation silently treats the gap as if it never happened - the calculation just steps over the missing timestamps with no record that anything was assumed on the way. This engine forces that choice into the open: the caller picks, up front, what a gap MEANS for their analysis - GAP_FILL_POLICY_HOLD carries the last known value flat across it, GAP_FILL_POLICY_INTERPOLATE linearly interpolates between the values either side, and GAP_FILL_POLICY_MARK_MISSING inserts an explicit sentinel value and flags the point as missing rather than guessing at all - and returns a report of exactly how many points were filled, by which method, so a downstream reader can never mistake a filled value for an observed one. A configurable "normal gap" tolerance keeps ordinary market closes - weekends, holidays - from being mistaken for real data gaps: only a delta larger than maxNormalGapSeconds is treated as a gap to fill at all. The default config sets a generous multiple of the expected interval, but this genuinely needs tuning per instrument and timeframe by the caller, since a "normal" gap on a daily forex chart (a weekend) is very different from a "normal" gap on an equity index chart with scheduled trading-halt periods. Notes and limitations - Input series must be chronological ASCENDING (oldest first) - the natural order for a file import, the opposite of MT4's series-indexed Close[]/Time[] arrays. Feeding a series-indexed (newest-first) array in directly produces nonsense gap detection. - Output arrays are always rebuilt from scratch via ArrayResize - MQL4 cannot assign one dynamic array directly from another, and the engine does not attempt to reuse or extend the caller's existing output buffer in place. - maxNormalGapSeconds needs deliberate tuning per instrument/timeframe. The default in GapFillPolicyEngineDefaultConfig() is a generous, conservative multiple of the expected interval - it is not calibrated to any specific broker's actual trading-session calendar. - The engine has no concept of a trading calendar (holidays, session hours) - it purely compares timestamp deltas against a threshold. A gap caused by a known holiday and a gap caused by a genuine feed dropout of the same duration are treated identically. - GAP_FILL_POLICY_HOLD and GAP_FILL_POLICY_INTERPOLATE both fabricate values with no statistical basis beyond the two real observations either side of the gap - they are conveniences for keeping a calculation running, not a claim that the fabricated points are accurate. - This library performs no trading action and reads no live market data itself - it is pure array-transformation logic over a series the caller already has in hand.

Script · MT4 · v1.0 · Free

About Gap Fill Policy Engine

Gap Fill Policy Engine is a script for MetaTrader 4. It has 1 setting, such as Gap hours. MQL source code included.

Files in the download: GapFillPolicyEngine.mqh, GapFillPolicyEngine.txt, GapFillPolicyEngineDemo.mq4.

Settings

SettingDefaultWhat it does
Gap hours5size, in hours, of the deliberate synthetic gap

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
  3. Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
  4. Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the script onto a chart to run it once.

Download the PDF guide

Tags: Moving Average · MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria