Stack And Queue
A stack and a queue are both usually reinvented on the spot in MQL4 as a plain array plus manual index bookkeeping - workable, but easy to get wrong around the empty case or an off-by-one on the wraparound. CStack and CQueue package the two in a small, self-contained header with the same discipline as the rest of this catalogue's containers: no allocation on every push/enqueue, and a found/not-found return value on every remove rather than a sentinel that could collide with a genuinely stored value. Both grow their backing array by doubling instead of resizing by one element per call, which keeps repeated pushes and enqueues amortised O(1) rather than O(n) each. CQueue is a circular buffer internally: the front index wraps rather than shifting every remaining element down on each Dequeue(), and a Grow() relinearises the wrapped contents into a fresh, larger buffer only when the queue is actually full. CQueue is deliberately a different tool from this catalogue's CRingBuffer, not a duplicate of it. A ring buffer has a fixed capacity and overwrites the oldest element once full - correct for a rolling window of the last N ticks, where dropping old data on purpose is the entire point. CQueue instead grows when full and never drops data, which is what a queue of pending actions needs: losing an entry silently there would be a bug, not a feature. Reach for CRingBuffer when the container should have a ceiling and discard the oldest entry past it; reach for CQueue when every entry must eventually be processed. Both containers store doubles rather than a generic type - neither MQL4 nor MQL5 has templates, and double covers prices, volumes, tickets cast to double, and running totals, which is the overwhelming majority of trading use for either structure. Notes and limitations - Values are plain doubles; there is no generic or templated version - neither MQL4 nor MQL5 has templates for this kind of container. - Neither container ever shrinks its backing array. Draining every element frees logical slots for reuse but does not release the underlying memory - build a fresh instance to reclaim capacity after a large one-off burst. - Not thread-safe, which matches both platforms' single-threaded-per-chart execution model; nothing here changes that.
Script · MT5 · v1.0 · Free
About Stack And Queue
Stack And Queue is a script for MetaTrader 5. It has 1 setting, such as Item count. MQL source code included.
Files in the download: StackQueue.mq5.mqh, StackQueue.txt, StackQueueDemo.ex5, StackQueueDemo.mq5.
Settings
| Setting | Default | What it does |
|---|---|---|
| Item count | 9 | Number of items pushed/enqueued, chosen above the initial capacity of 4 |
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: MT5 Script · MQL5 Source Code