Loading…

Multi Timeframe Data Accessor

iOpen, iHigh, iLow and iClose all fail the same quiet way on both platforms: on a symbol or timeframe the terminal has not finished downloading, they return 0.0 with no exception raised. Nothing distinguishes that from a genuine zero price, so code that reads a higher timeframe straight off these functions can run for weeks against a fully synchronised broker and then feed a silent zero into a stop distance or a lot calculation the first time it is tried on a fresh terminal or an unfamiliar symbol. Every getter in this header confirms the bar's open time is non-zero before it will hand back a price, and returns a bool so the caller is forced to handle the missing case rather than trust whatever came back. The second problem is the one that survives code review. Shift 0 on any timeframe - not only a higher one read from a lower-timeframe chart - is the bar currently forming. Its open, high, low and close change on every tick until the period ends. Treating it as a closed, final bar is a repaint bug: the EA back-tests perfectly because the strategy tester only ever sees closed bars, then behaves differently live because shift 0 keeps moving under it. MtfLastClosedShift() returns the plain number 1, but calling it instead of writing the bare digit turns every call site into documentation of the decision, and MtfIsBarClosed() lets a caller check the assumption explicitly when it matters. The header does not cache or copy series data - it is a thin, defensive layer directly over iOpen/iHigh/iLow/iClose/iBarShift (and iVolume on MQL4, iTickVolume on MQL5), so it costs nothing beyond what those functions already cost and never goes stale. iTime/iOpen/iHigh/iLow/iClose/iBarShift are direct series-access calls on MQL5 too - unlike iMA, they were never made handle-based, so this port stayed close to the MQL4 source. Three genuine differences did apply: every timeframe parameter changed from MQL4's plain int to MQL5's ENUM_TIMEFRAMES (there is no implicit int-to-enum conversion, so a caller passing a bare 0 now needs PERIOD_CURRENT); iVolume() is deprecated on MQL5 and raises a compiler warning, so MtfVolume() calls iTickVolume() instead; and the seconds- per-bar calculation now defers to the built-in PeriodSeconds(tf) rather than hand-multiplying tf*60 - MQL4 timeframes are plain minute counts so that multiplication worked, but MQL5's ENUM_TIMEFRAMES uses a flag+multiplier encoding where tf*60 is simply the wrong number for anything above M59. Notes and limitations - This is a read layer, not a cache. It calls the underlying iOpen/iHigh/ iLow/iClose/iVolume-or-iTickVolume/iBarShift every time, so it is only as fast as those already are. - MtfHistoryReady confirms bars exist now; it does not download missing history itself. On a symbol the terminal has never charted, the first calls may still fail until the terminal finishes fetching data in the background. - MN1 seconds are nominal (43200 minutes = 30 days on MQL4; MQL5's PeriodSeconds(PERIOD_MN1) carries the same nominal figure). Do not use MtfPeriodSeconds(PERIOD_MN1) for exact calendar-month arithmetic on either platform. - MtfIsBarClosed returning false does not distinguish "still forming" from "does not exist" - both are equally unsafe to trade on, which is why both return the same answer.

Script · MT4 · v1.0 · Free

About Multi Timeframe Data Accessor

Multi Timeframe Data Accessor is a script for MetaTrader 4. It has 1 setting, such as Higher timeframe. MQL source code included.

Files in the download: MtfData.mqh, MtfData.txt, MtfDataDemo.mq4.

Settings

SettingDefaultWhat it does
Higher timeframePERIOD_D1Higher timeframe to sample alongside the chart's own

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: Volume · Multi-Timeframe (MTF) · Buy/Sell Arrows · MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria