Loading…

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 · MT5 · v1.0 · Free

About Event Driven EA Framework

Event Driven EA Framework is an expert advisor (trading robot) for MetaTrader 5. 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.ex5, EventDrivenEaFramework.mq5, EventDrivenEaFramework.txt.

Trading robots can lose money. Check every setting and test on a demo account before using real funds.

Settings

SettingDefaultWhat it does
Trade symbolSymbol to trade, blank = current chart symbol
Level0.0Price level watched for EVT_LEVEL_CROSS_* (0 = disabled)
Lots0.01Lot size for the demo BUY on a level cross
Max drawdown percent20.0Equity drawdown from peak balance that fires EVT_RISK_BREACH
Magic48101Magic number for this EA's own orders
Slippage points30Max slippage, points

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 → Experts.
  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 expert advisor from the Navigator onto a chart, check the settings, allow Algo/Auto Trading and press OK.
  6. Test it first on a demo account or in the Strategy Tester.

Download the PDF guide

Tags: Risk & Lot Size · MT5 Expert Advisor · MQL5 Source Code

Expert advisors (EA) for MT4 & MT5 · PolyMaria