Loading…

API Budget Manager

Any EA or indicator that calls out to an external service - a news feed, a config server, a webhook - eventually calls it too often, and the naive guard against that is a single "last call time" timestamp that only enforces a minimum gap between calls. That says nothing about a genuine "N requests per window" quota, the shape almost every real API actually publishes, and it cannot answer "how many calls do I have left this window" for code that wants to budget several different calls against one shared quota. This header registers a named endpoint with a maxRequests/windowSeconds quota and tracks a real fixed-window counter against it. AbmTryConsume() deducts from the budget and returns false - deducting nothing - the instant a call would exceed it, so a refused attempt never partially spends the quota. AbmRemaining(), AbmWindowResetsAt() and AbmMsUntilAvailable() let a caller plan ahead (skip a call, queue it, or back off) rather than only ever finding out after the fact. Calling AbmTryConsume() against an endpoint that was never registered is refused outright, not silently allowed - a typo'd endpoint name fails loudly instead of granting itself an unlimited budget. Notes and limitations - Fixed-window counting, not a sliding/leaky-bucket algorithm - a window boundary can allow up to 2x maxRequests across the boundary itself (e.g. all of window N's quota used at its very end, then all of window N+1's quota used at its very start). Good enough for staying comfortably under a published limit, not a precise token-bucket implementation. - The registry is a fixed array of ABM_MAX_ENDPOINTS (32) slots - registering more distinct endpoint keys than that in one running program fails and logs a message rather than growing unbounded. - This header only tracks and refuses calls locally; it has no idea what the remote service's actual published limit is - the caller must supply the real maxRequests/windowSeconds values themselves, usually read from that service's own documentation. - Purely in-memory - a restart of the EA/script/indicator resets every endpoint's window, it is not persisted across terminal restarts.

Script · MT5 · v1.0 · Free

About API Budget Manager

API Budget Manager is a script for MetaTrader 5. It has 2 settings, such as Max requests and Window seconds. MQL source code included.

Files in the download: ApiBudgetManager.mq5.mqh, ApiBudgetManager.txt, ApiBudgetManagerDemo.ex5, ApiBudgetManagerDemo.mq5.

Settings

SettingDefaultWhat it does
Max requests3requests allowed per window, for the demo endpoint
Window seconds5window length in seconds, for the demo endpoint

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

MQL4 & MQL5 scripts · PolyMaria