Event Driven EA Framework
Most published EAs grow a single OnTick() that mixes market detection, trading logic, logging and risk control in one long function. Every new feature means finding the right spot to insert another if-block, and after a few features the function is unreadable and every change risks breaking an unrelated one. This template splits that into two halves. A small set of Detector functions run every tick and look for raw conditions changing (a new bar opening, price crossing a watched level, one of this EA's own orders leaving the pool, equity drawdown crossing a threshold) - when a condition fires they call PublishEvent() and stop, doing no further work themselves. A dispatcher then drains the queued events and, for each one, calls every Module_* function subscribed to that EVENT_TYPE. Modules never call each other and never call a detector; they only ever get invoked from the dispatcher. The demonstration wiring is intentionally simple - a Logger module subscribes to every event, a TradingStrategy module opens a market order on a level cross, a TradeJournal module counts order lifecycle events, and a RiskGuard module closes every position this EA owns when a drawdown event fires. Extending the framework means adding a new EVENT_TYPE value and a new Module_* function, not editing OnTick() itself - that is the property worth studying here, not the trading rule. The queue is a small fixed-size ring buffer (32 events) rather than a dynamic array, so a burst of events cannot grow memory unbounded; on overflow the oldest queued event is dropped and a warning is printed, which is the correct behaviour for a monitoring bus that must never block trading logic waiting for queue space. Notes and limitations This is a template/framework submission - the trading rule (buy on upward level cross, close-and-reverse on downward cross) exists only to prove the event wiring actually moves an order end to end; it is not a strategy meant to be profitable as shipped. The event queue lives in memory only and is not persisted, so a terminal restart clears it (the EA rebuilds its detector baselines from current market/account state in OnInit). The bus is single-threaded and cooperative, matching MQL4's own single-threaded event model - there is no concurrent access to guard against.
Expert advisor · MT4 · v1.0 · Free
About Event Driven EA Framework
Event Driven EA Framework is an expert advisor (trading robot) for MetaTrader 4. It can size trades from your balance and risk percent. It has 6 settings, such as Trade symbol, Level and Lots. MQL source code included.
Files in the download: EventDrivenEaFramework.ex4, EventDrivenEaFramework.mq4, EventDrivenEaFramework.txt.
Trading robots can lose money. Check every setting and test on a demo account before using real funds.
Settings
| Setting | Default | What it does |
|---|---|---|
| Trade symbol | Symbol to trade, blank = current chart symbol | |
| Level | 0.0 | Price level watched for EVT_LEVEL_CROSS_* (0 = disabled) |
| Lots | 0.01 | Lot size for the demo BUY on a level cross |
| Max drawdown percent | 20.0 | Equity drawdown from peak balance that fires EVT_RISK_BREACH |
| Magic | 48101 | Magic number for this EA's own orders |
| Slippage points | 30 | Max slippage, points |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Experts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the expert advisor from the Navigator onto a chart, check the settings, allow Algo/Auto Trading and press OK.
- Test it first on a demo account or in the Strategy Tester.
Tags: Risk & Lot Size · MT4 Expert Advisor · MQL4 Source Code