Loading…

Range And Constant Volume Bars

A one-minute bar during a dead London lunch hour and a one-minute bar during an NFP release contain wildly different amounts of actual trading activity, yet every wall-clock timeframe treats them as identical containers. Range bars and volume bars fix that by defining a bar's boundary in terms of what actually happened - a fixed amount of price movement, or a fixed amount of trading activity - rather than a fixed amount of time. RB_OnPrice feeds one price at a time into a forming range bar and returns true the instant (high - low) reaches the configured range, at which point it hands back the just-CONFIRMED bar and immediately opens the next one at the triggering price. The forming bar itself is never exposed through the streaming API - only a fully confirmed bar is ever returned, so calling code can never accidentally read (and repaint on) a bar that has not actually closed yet. VB_OnPrice does the same for volume: it accumulates tick count (or real volume, wherever the broker feeds it) until the configured target is reached, then closes the bar and starts the next one from zero. Because volume bars close on activity rather than price movement, a quiet stretch produces a long, thin bar and a burst of activity produces several bars in quick succession - visible proof the clock is tracking activity, not time. Both builders also ship a batch function (RB_BuildRangeBars, RB_BuildVolumeBars) that converts an entire existing price series into confirmed bars in one call, useful for backtesting a range-bar or volume-bar strategy against ordinary chart history, or for feeding another indicator's input series. Notes and limitations - 'range' is a raw price distance, already converted from pips if needed - this header never reads MODE_DIGITS or Point itself, since it has no symbol context of its own. Convert pips to price units at the call site (see the demo's InpRangePoints for a worked example). - The batch functions discard any trailing partial bar, matching the streaming API's rule that only a fully confirmed bar is ever returned - the tail of a finite series is very often mid-formation, and returning it as if it were closed would silently misrepresent the data. - Range bars re-open the next bar at the exact price that confirmed the previous one (no gap). Some published range-bar generators instead skip a fixed distance past the trigger; if a strategy was tuned against that convention, expect slightly different bar boundaries here. - Neither builder guesses at tick volume when it is not supplied - pass 1 per tick if the broker's real volume feed is not being used, which is what the demo does outside its synthetic "burst" window.

Script · MT5 · v1.0 · Free

About Range And Constant Volume Bars

Range And Constant Volume Bars is a script for MetaTrader 5. It has 3 settings, such as Tick count, Range points and Volume target. MQL source code included.

Files in the download: RangeBarBuilder.mq5.mqh, RangeBarBuilder.txt, RangeBarBuilderDemo.ex5, RangeBarBuilderDemo.mq5.

Settings

SettingDefaultWhat it does
Tick count200Number of synthetic ticks to generate
Range points0.0030Range bar size, in raw price units
Volume target15Ticks per volume bar

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
  3. Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
  4. Open the .mq5 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 · Trading Sessions · MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria