Loading…

Stale Price After Sleep Demo

The predefined Bid and Ask variables (and the predefined Time/Open/High/ Low/Close series) are set once when a script's OnStart begins and do not update themselves afterward, no matter how long the script runs or how much the real market moves in the meantime. RefreshRates() is the only thing that pulls that snapshot forward. A script that sleeps for a few seconds - waiting for a condition, throttling a loop, pausing between batch operations - and then reads bare Bid/Ask afterward without calling RefreshRates() first is trading against a price that can already be several ticks old. This script proves it rather than just stating it. It captures the bare Bid/Ask once at the start, then runs a bounded sleep-and-observe loop: each cycle sleeps for a configurable interval and then prints three readings side by side - the untouched bare Bid (still the original snapshot, because nothing has told it to update), MarketInfo(symbol, MODE_BID) (always live regardless of RefreshRates(), which is exactly why the house style calls for it when acting on a symbol other than the chart's own), and Bid again immediately after calling RefreshRates() (live, because it was just explicitly refreshed). Watching the first reading stay frozen while the other two track the real market makes the staleness concrete instead of theoretical. The fix is a single line, RefreshRates(), placed at exactly the point where it matters: immediately before reading Bid/Ask following any Sleep() or other operation that can take real time. The script places that call exactly there, once per cycle, as the concrete pattern to copy. Notes and limitations - This is a read-only observation script - it sends no orders and needs no connected trading account, only a live price feed on the attached symbol. - How much drift appears depends entirely on how much the real market moves during the sleep intervals. On a very quiet symbol or outside trading hours, the bare Bid, MarketInfo() and refreshed readings can legitimately all agree for a while - that is not a failure of the demonstration, it means the price genuinely has not moved yet. Widening InpSleepMs or running the script on a busier symbol makes the effect reliably visible. - Checks IsStopped() before and after every sleep so the script exits immediately and cleanly if removed from the chart mid-run.

Script · MT4 · v1.0 · Free

About Stale Price After Sleep Demo

Stale Price After Sleep Demo is a script for MetaTrader 4. It can alert you with a pop-up, sound, email or push message. It has 2 settings, such as Iterations and Sleep ms. MQL source code included.

Files in the download: StalePriceAfterSleepDemo.mq4, StalePriceAfterSleepDemo.txt.

Settings

SettingDefaultWhat it does
Iterations6Number of sleep/observe cycles to run
Sleep ms2000Milliseconds to sleep between each observation

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
  3. Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
  4. Open the .mq4 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: Alerts · MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria