Script Event Limitation Demo
A Script has exactly one entry point, OnStart(). It has no OnInit(), no OnDeinit(), no OnTick(), no OnCalculate() - those simply do not exist for this program type, no matter how the file is written. New MQL4 authors sometimes discover this the hard way: they add an OnChartEvent() handler to a script expecting it to behave like an EA's, and it appears to work while testing, then "stops working" the moment they move on - because it was never going to keep working. The subtlety this script demonstrates is that a Script CAN declare OnTimer() and OnChartEvent(), and they DO fire - but only for as long as OnStart() itself is still executing. The instant OnStart() returns, the script unloads from the chart, and both handlers become permanently unreachable. There is no "pause and resume" for a script; once it finishes, it is gone. This script proves the point rather than just asserting it. It starts a millisecond timer and installs a chart-event handler, then keeps OnStart() alive in a bounded Sleep loop for a configurable number of seconds so you can click the chart and watch OnChartEvent() log each click. When the alive window ends, it prints a summary of everything it captured, and its very last message tells you to try clicking the chart again afterwards - nothing will be logged, because the script no longer exists on the chart at that point. Notes and limitations - This is strictly an educational demo. It does not do anything to a chart's objects, orders, or indicators. - The alive window is a deliberate design choice to make the event-receiving boundary observable, not a general pattern for keeping a script running - a script that needs to run continuously and react to chart events for the life of the chart should be rebuilt as an Expert Advisor, which is exactly the point being taught here. - OnTick() and OnCalculate() are never invoked for a Script under any circumstances; this demo does not attempt to show that negatively (there is nothing to click to disprove), it is stated in the source header and here instead.
Script · MT5 · v1.0 · Free
About Script Event Limitation Demo
Script Event Limitation Demo is a script for MetaTrader 5. It has 2 settings, such as Alive seconds and Timer period ms. MQL source code included.
Files in the download: ScriptEventLimitationDemo.ex5, ScriptEventLimitationDemo.mq5, ScriptEventLimitationDemo.txt.
Settings
| Setting | Default | What it does |
|---|---|---|
| Alive seconds | 15 | How long OnStart() stays alive and listens for events |
| Timer period ms | 1000 | Timer period while alive, in milliseconds |
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: MT5 Script · MQL5 Source Code