Loading…

Fenwick Tree Prefix Sums

A running total over the last N bars - tick volume, cumulative delta, any additive series - is usually computed by walking the array from the current bar back N places every time it is needed. That is fine for a single sum, but a panel that reports session volume, last-20-bar volume and last-50-bar volume together ends up re-walking overlapping stretches of the same array on every tick, and the cost grows with both the lookback length and the number of different lookbacks shown at once. A Fenwick tree (binary indexed tree) keeps a second array of partial sums alongside the data, structured so both a point update - a new bar's value arriving - and a prefix sum up to any position cost O(log n) instead of O(n). Any range sum is then just the difference of two prefix sums. The trade is a small amount of extra bookkeeping on every update in exchange for every query becoming cheap, which is the right trade whenever queries outnumber updates - exactly the pattern for a chart panel re-reading several sums every tick while data only grows one bar at a time. The structure holds partial sums only, not the raw per-position values - FenwickPointValue() recovers a single value on demand as a one-element range sum, which costs the same O(log n) as any other range query. Notes and limitations - Fenwick trees do not support resizing in place; growing the series means rebuilding, which is O(n) - the same cost as the initial build, and still far cheaper than the O(n) queries it replaces once more than a couple of range sums are needed per update. The demo shows this in the point-update section. - All values are doubles, so this suits money, volume and delta sums directly. Nothing here rounds or normalises - callers doing money math should still apply their own broker-reported tick value / lot-size conversion before storing a value (SymbolInfoDouble with SYMBOL_TRADE_TICK_VALUE on MQL5, MarketInfo with MODE_TICKVALUE on MQL4) - this structure only sums whatever it is given. - Not thread-safe and not persisted - the tree lives in memory for the lifetime of the EA or script and is rebuilt from scratch on every restart.

Script · MT5 · v1.0 · Free

About Fenwick Tree Prefix Sums

Fenwick Tree Prefix Sums is a script for MetaTrader 5. It has 1 setting, such as Bars. MQL source code included.

Files in the download: FenwickTree.mq5.mqh, FenwickTree.txt, FenwickTreeDemo.ex5, FenwickTreeDemo.mq5.

Settings

SettingDefaultWhat it does
Bars50How many recent bars' volume to load into the tree

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 · Binary Options · MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria