Loading…

Tick To Bar Aggregator

Both platforms only offer a short fixed list of chart periods. A strategy that wants 3-minute bars, 90-second bars, or a bar every 25 ticks instead of every fixed clock interval has no native way to ask for it - the usual workaround is downsampling M1 after the fact, which cannot recover intra-minute detail that already averaged away. This header takes the opposite approach: feed it every tick as it happens through OnTickPrice(), and it buckets ticks into whichever bar definition you configured, finalising the previous bar the instant a new bucket begins. Two bucket modes cover the two things "custom timeframe" usually means: TICKBAR_TIME buckets by a fixed number of seconds - any value, not just the ones on the chart period dropdown. TICKBAR_COUNT buckets by a fixed number of ticks per bar, which tracks actual trading activity rather than the clock. On a symbol whose liquidity varies through the session, count-based bars keep roughly the same amount of information in every bar instead of cramming a busy open and a quiet afternoon into equal-sized time buckets. Completed bars are read back through Open/High/Low/Close/Ticks/Time accessors indexed the same way the terminal's own iOpen/iHigh/iLow/iClose are - index 0 is the most recently closed bar - so code that already reads native series can drop in the aggregator's accessors with minimal change. A capped history keeps memory bounded on a strategy that runs for days. The CTickBarAggregator class itself is pure computation - ArrayResize, MathMax and simple arithmetic only, no order, position or price-series API anywhere in the class body - so both platform versions of the header are the identical algorithm. What differs is only what a caller feeds into it; see MQL4 VERSUS MQL5 DIFFERENCES below. Notes and limitations - This is a script demo, so it cannot stay resident between ticks the way an EA can - Part 2 uses a bounded Sleep loop purely to show the aggregator reacting to real ticks within a single run. A real strategy calls OnTickPrice() once per OnTick(), no loop needed. - TICKBAR_TIME buckets align to floor(time / periodSeconds), not to session open - a 180-second bucket does not necessarily start on the minute unless periodSeconds divides evenly into 60. - The M1 replay in Part 1 uses each bar's O/H/L/C as four synthetic ticks, which is a reasonable approximation for a demo but is not the same resolution as genuine tick data - use it to see the mechanism, not as a precise historical reconstruction. - History is capped at maxHistory by dropping the oldest closed bar, an O(n) shift rather than a ring buffer - fine at the size this is meant for (hundreds of bars), not designed for tens of thousands. - Not thread-relevant: one aggregator instance is meant to be driven by one tick stream: call it from OnTick(), not from multiple event handlers. - Identical limitations on both platforms.

Script · MT4 · v1.0 · Free

About Tick To Bar Aggregator

Tick To Bar Aggregator is a script for MetaTrader 4. It has 4 settings, such as Custom period seconds, Ticks per bar and History m1 bars. MQL source code included.

Files in the download: TickBarAggregator.mqh, TickBarAggregator.txt, TickBarAggregatorDemo.mq4.

Settings

SettingDefaultWhat it does
Custom period seconds180Custom bar length in seconds (e.g. 180 = 3 minutes)
Ticks per bar25Alternative: ticks per bar for the count-based demo
History m1 bars60M1 bars replayed to seed Part 1
Live watch seconds8How long Part 2 watches live ticks

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: MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria