Loading…

IsStopped Handling Demonstrator

MQL4 does not forcibly kill a running script the instant the user removes it from the chart or shuts down the terminal. It sets IsStopped() to true and waits for the script to notice and return on its own; if the script never checks, MetaTrader eventually force-terminates it anyway, but only after whatever the script was doing at that moment - writing a file, sending an order, drawing an object - has already been interrupted mid- operation. A batch job that writes results to a CSV file without checking IsStopped() can be forcibly cut off with the file handle still open and the last record half-written, corrupting the file for anything else that tries to read it. This script runs a genuine, if simulated, long batch job: it processes a configurable number of "items", writing one CSV record per item to a file in the sandbox with a short delay between each, specifically so the run takes long enough to interrupt on purpose. IsStopped() is checked at the top of every iteration, before that iteration's work begins, and again immediately after each delay - the delay is exactly where a stop request is most likely to arrive, so checking only at the top of the next loop would add up to one full delay of avoidable extra work before exiting. The important part is not the check itself but what happens next. Every exit path - normal completion or an interruption partway through - funnels through one finalising function that closes the file handle and writes an honest summary record stating how many items were actually completed. There is no path through this script that can return without that function running, which is the real fix: not merely detecting the stop request, but guaranteeing the same clean shutdown happens regardless of when it arrives. A chart label tracks progress live, so the interruption point is visible without needing the log window open. Notes and limitations - The "work" performed per item is a simple deterministic calculation (a sine wave value) written to a CSV row - this demonstrates the interruption and cleanup pattern honestly without needing any real trading activity or market dependency to reproduce. - File output is sandboxed to MQL4\Files\ (or MQL5\Files\ on the native MQL5 build) as with every MQL4 file operation - this script makes no attempt to write anywhere else. - If the terminal is closed so abruptly that MetaTrader has no chance to let OnStart run at all (a hard crash, not a normal stop/removal), no script-level check can protect against that - this pattern covers the normal "user removed the script" and "AutoTrading/terminal requested a stop" cases, which is what IsStopped() exists for. - The on-chart progress label is left in place after the script finishes as a visible summary, in the same spirit as the chart-objects note elsewhere in this catalogue - it is a single object named "isstop_progress" and is simply overwritten on the next run.

Script · MT4 · v1.0 · Free

About IsStopped Handling Demonstrator

IsStopped Handling Demonstrator is a script for MetaTrader 4. It can alert you with a pop-up, sound, email or push message. It has 3 settings, such as Item count, Delay ms per item and Output file name. MQL source code included.

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

Settings

SettingDefaultWhat it does
Item count300Simulated work items to process
Delay ms per item60Milliseconds slept per item, to make the job long enough to interrupt
Output file nameIsStoppedHandlingDemo_output.csvFile written in MQL4\Files\

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