Order Rate Limiter
A single logic bug - a condition that never re-evaluates the way it was meant to, a reconnect that replays a signal, a loop missing its exit condition - can turn an EA into something that sends dozens of orders in a few seconds. The damage is not just the trades themselves; brokers ban accounts for exactly this pattern. A rate limiter sitting between "decide to trade" and the actual trade call is cheap insurance against a mistake elsewhere in the code. This header uses a token bucket rather than a fixed counter that resets every minute. A fixed window has a boundary problem: nineteen orders in the last second of one window and nineteen more in the first second of the next both individually look fine against "twenty per minute," but thirty- eight orders landed in two seconds. A token bucket refills continuously instead, so the burst allowance (capacity) and the sustained pace (refill rate) are genuinely separate knobs with no reset-boundary loophole between them. Two buckets are checked on every call: one per symbol, and one shared across the whole account. Both must have room, and a refused attempt spends nothing from either bucket. This means a burst on one symbol cannot silently starve the account-wide ceiling meant to protect everything else, and a quiet account-wide bucket does not let one symbol be hammered without its own limit ever kicking in. TryConsume takes an optional explicit timestamp instead of always reading the current time internally. In production code, leave it at the default and every call uses the real clock. In a test or demo, pass a simulated timestamp instead - this is what lets the accompanying demo show minutes of refill behaviour without the script actually sitting idle for minutes. Notes and limitations - A brand-new symbol starts with a FULL bucket, not an empty one - a symbol that has never traded should not be punished for buckets it never used. - Setting either capacity to 0 blocks that dimension outright; setting a refill rate to 0 makes that bucket a one-time allowance that never replenishes, which may be intentional (a hard cap for the session) or a configuration mistake - check the demo output before shipping either. - This header refuses SENDING an order; it does not know whether an order actually succeeded or failed at the broker. Pair it with your own retry logic for the trade call itself. - Per-symbol lookup is a linear scan over the symbols seen so far - fine for the handful of symbols a typical EA trades, not designed for hundreds. - Not thread-relevant: one CTradeRateLimiter instance is meant to be driven from a single OnTick() context, the same as the rest of an EA's state.
Script · MT5 · v1.0 · Free
About Order Rate Limiter
Order Rate Limiter is a script for MetaTrader 5. It has 4 settings, such as Symbol capacity, Symbol per minute and Global capacity. MQL source code included.
Files in the download: TradeRateLimiter.mq5.mqh, TradeRateLimiter.txt, TradeRateLimiterDemo.ex5, TradeRateLimiterDemo.mq5.
Settings
| Setting | Default | What it does |
|---|---|---|
| Symbol capacity | 5 | Max burst orders allowed on one symbol |
| Symbol per minute | 3 | Sustained per-symbol refill rate (orders/minute) |
| Global capacity | 8 | Max burst orders allowed account-wide |
| Global per minute | 6 | Sustained global refill rate (orders/minute) |
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: Buy/Sell Arrows · MT5 Script · MQL5 Source Code