Loading…

Priority Aging Queue

A plain priority queue has a well-known failure mode: if high- priority work keeps arriving, a low-priority item can wait forever, because its priority never changes relative to newer arrivals. This library fixes that by computing an EFFECTIVE priority for every dequeue decision: base priority plus an aging bonus proportional to how long (in real wall-clock seconds, via GetTickCount()) the entry has been sitting in the queue. Given enough time, any entry's effective priority eventually overtakes any fixed base priority. The demo does not just claim this - it enqueues a high-priority task and a low-priority task, actually Sleep()s for a configurable duration, and then dequeues, showing the low-priority task come out first once its accumulated aging bonus has genuinely pushed its effective priority past the high-priority task's static one. Dequeue is a linear scan for the current maximum effective priority, which is a deliberate choice, not a missing optimisation: a classic binary heap's O(log n) guarantee depends on keys staying fixed between operations, but here every entry's effective priority changes continuously with the clock. A heap would need re-validating (or rebuilding) on every dequeue to stay correct against that moving target, which erases its advantage - the honest O(n) scan used here is simpler and no worse in the worst case. Notes and limitations - The aging clock is GetTickCount() (milliseconds since Windows started), not TimeCurrent() - it measures real wall-clock waiting time, not chart/server time, and keeps working identically in the Strategy Tester's accelerated time as it does live (deliberately - "how long has this genuinely been queued" is a real-time question). - Dequeue/Peek are O(n) per call (a full scan for the current maximum). Fine for queues of up to a few thousand entries; not intended as a drop-in replacement for a static-priority heap on a very large, high-throughput queue. - Unsigned tick-count subtraction wraps correctly across the ~49.7-day GetTickCount() rollover as long as no single entry waits longer than that span - the standard tolerance for tick-count-based timing. - Ties in effective priority keep whichever entry was enqueued earlier among the tied ones.

Script · MT5 · v1.0 · Free

About Priority Aging Queue

Priority Aging Queue is a script for MetaTrader 5. It has 2 settings, such as Aging rate per second and Wait milliseconds. MQL source code included.

Files in the download: PriorityAgingQueue.mq5.mqh, PriorityAgingQueue.txt, PriorityAgingQueueDemo.ex5, PriorityAgingQueueDemo.mq5.

Settings

SettingDefaultWhat it does
Aging rate per second5.0priority points gained per second waited
Wait milliseconds2000how long to let the low-priority task age

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

MQL4 & MQL5 scripts · PolyMaria