Loading…

Slippage And Requote Handler

OrderSend's slippage parameter is in points, but a trader thinks in pips, and the pip-to-point factor is 10 on a 3 or 5 digit broker and 1 otherwise - mixing that up silently doubles or tenths the tolerance a trader actually intended. ShPipsToPoints and its inverse handle that conversion the same way the house style's own example does, kept as a local copy here because this library has to stand alone. Not every OrderSend failure is worth retrying, and not every retryable failure needs a re-priced order. ShIsRetryableError classifies the errors that indicate a transient server or pricing condition rather than a genuine rejection - the same table this catalogue's house style documents, so a wrapper built on top of this header does not need to redefine it and risk the two lists drifting apart. ShIsRequoteError narrows that further to the specific case where the price itself moved, because that is the case where re-pricing before the retry actually matters; a busy-server error does not call for a new price, only a wait. ShFreshPrice supplies that re-priced level. It calls RefreshRates() before reading Ask or Bid, because the terminal's cached quote is very often exactly the stale value that caused the requote in the first place - retrying with an unrefreshed price just reproduces the same rejection. A broker's own slippage parameter only bounds what the server will reject; it says nothing about what a filled order actually cost. ShActualSlippagePips and ShSlippageExceeded measure that after the fact, so a caller can flag a fill for review even when the broker accepted it without complaint. ShRetryDelayMs returns a linear backoff capped at a fixed ceiling. A fixed delay under-reacts when the server is genuinely overloaded; an uncapped exponential delay leaves a trading program visibly stuck for a long time on a bad run. The cap is a deliberate trade of a little raw efficiency for predictable, bounded retry behaviour. Notes and limitations - This header does not call OrderSend, OrderModify or Sleep itself - it only supplies the decisions a wrapper around those calls needs to make. Building the actual retry loop is left to the caller (or to a dedicated OrderSend wrapper library) so this header stays a pure, easily-tested set of functions with no trading side effects. - ShFreshPrice covers only OP_BUY and OP_SELL. Pending order types have no single "current price" in the same sense and are out of scope here. - The retry backoff schedule is a fixed policy (linear, 250ms step, 2000ms cap). It is not configurable per call - copy the function and adjust the constants if a program needs a different schedule. - ShIsRetryableError and ShIsRequoteError are pure classification; they do not know how many times an order has already been retried, so an unbounded loop using only these two functions could retry forever on a persistently busy server. Always cap the attempt count in the caller.

Script · MT5 · v1.0 · Free

About Slippage And Requote Handler

Slippage And Requote Handler is a script for MetaTrader 5. It has 1 setting, such as Max slippage pips. MQL source code included.

Files in the download: SlippageHandler.mq5.mqh, SlippageHandler.txt, SlippageHandlerDemo.ex5, SlippageHandlerDemo.mq5.

Settings

SettingDefaultWhat it does
Max slippage pips3.0Tolerance used in the slippage-exceeded example

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