Ring Buffer
A rolling window is one of the most common pieces of state in an EA: the last 20 spreads for a filter, the last 50 tick prices for a volatility estimate, the last N execution latencies for a health check. The naive implementation uses a plain array, resizes it to grow, and shifts every element down by one whenever the window is full and a new value arrives - an O(n) cost paid on every single tick, for a window that is conceptually O(1) to maintain. CRingBuffer stores a fixed-capacity array once and moves a head pointer instead of moving data. Pushing a value when the buffer is full overwrites the oldest element and advances the head - no resize, no shift, no allocation after construction. The behaviour at capacity is a deliberate design choice, not an oversight: Push() always succeeds and always overwrites the oldest value once full. It never rejects a new value and never grows the buffer past its capacity. A rolling window is defined by its capacity - "the last 50 ticks" stops meaning the last 50 ticks the instant it is allowed to hold 51 - so overwrite-oldest is the only behaviour consistent with what a ring buffer is for. Code that needs to know whether a push discarded data should check IsFull() before calling Push(), not after. Built as a class so the buffer, its head pointer and its count travel together as one object - useful when a program needs several independent rolling windows (one per symbol, for instance) rather than a single global array. Notes and limitations - Values only, no timestamps. If you need to know when each sample was pushed, maintain a parallel CRingBuffer of times and push to both together. - Min()/Max()/Sum() are O(n) over the held elements on every call; they are not cached. Fine for windows in the tens or low hundreds of elements updated per tick, not intended as a running statistic over thousands. - Capacity is fixed for the life of the object except through Init(), which also discards whatever data was held - there is no resize-and-keep-data operation. - Not thread-safe in any sense that matters here - both platforms are single-threaded per chart - but two programs on different charts sharing one instance through a global pointer would need their own synchronisation regardless, on either platform.
Script · MT5 · v1.0 · Free
About Ring Buffer
Ring Buffer is a script for MetaTrader 5. It has 1 setting, such as Capacity. MQL source code included.
Files in the download: RingBuffer.mq5.mqh, RingBuffer.txt, RingBufferDemo.ex5, RingBufferDemo.mq5.
Settings
| Setting | Default | What it does |
|---|---|---|
| Capacity | 5 | Capacity used for the walkthrough |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
- Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
- Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: Arbitrage · MT5 Script · MQL5 Source Code