State Machine EA Template
A published EA usually grows its control flow one feature at a time: a boolean for "am I in a trade", another for "am I waiting on a pending order", a third for "did I already see today's signal". Each is individually reasonable. Together they form a set of flags that has to stay in lock-step by hand, and it rarely does forever - a missed fill event after a reconnect, an order closed by the broker's own stop rather than the EA's own logic, or a restart mid-sequence, and the flags describe a situation that no longer matches reality. The usual symptom is a second position opened on top of one the EA had already forgotten about, or an EA that simply stops doing anything and nobody notices why. This template replaces the flags with one ENUM_EA_STATE that can only ever hold a single value: SCANNING, ENTERING, IN_TRADE or COOLDOWN. Every transition goes through one function that logs the from and to state, so the Experts log alone tells you exactly what the EA believed it was doing at any point in its history - not an inference from which flags happened to be true. STATE_IN_TRADE re-derives whether its tracked ticket is still open from OrderSelect() every tick rather than trusting a flag, so a broker-side stop-out is noticed and handled the same way a normal exit is. The moving-average cross wired into STATE_SCANNING exists only to make the template runnable end to end out of the box. It is a placeholder, not the point of the submission - replace CheckForEntrySignal() with real logic and the state machine around it needs no changes at all. Notes and limitations - This is a framework/template submission. Traded live unmodified, it runs a simple MA cross strategy - reasonable for seeing the state machine work, not a strategy claim on its own. - Only one position is tracked at a time (single ticket in g_ticket). A strategy that needs to pyramid or manage several simultaneous tickets needs an array of tracked states, one per ticket, following the same pattern. - STATE_ENTERING is resolved synchronously inside the same tick that raised it; the switch case for it exists as a fail-safe back to SCANNING in case that ever changes, and is not expected to run in normal operation. - Cooldown counts closed bars on the chart's own timeframe, not wall-clock seconds, so behaviour is consistent regardless of which timeframe the EA is attached to.
Expert advisor · MT4 · v1.0 · Free
About State Machine EA Template
State Machine EA Template is an expert advisor (trading robot) for MetaTrader 4 based on Moving Average. It can size trades from your balance and risk percent and has a news filter. It has 13 settings, such as Fast ma period, Slow ma period and Ma method. MQL source code included.
Files in the download: StateMachineEa.ex4, StateMachineEa.mq4, StateMachineEa.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 |
|---|---|---|
| Fast ma period | 10 | Fast MA period (placeholder signal) |
| Slow ma period | 30 | Slow MA period (placeholder signal) |
| Ma method | MODE_EMA | MA method for both lines |
| Size mode | SIZE_FIXED_LOTS | How the traded lot size is chosen |
| Fixed lots | 0.10 | Lots used when InpSizeMode is SIZE_FIXED_LOTS |
| Risk percent | 1.0 | Percent of equity risked when InpSizeMode is SIZE_RISK_PERCENT |
| Stop loss pips | 20 | Stop loss distance, pips |
| Take profit pips | 40 | Take profit distance, pips |
| Cooldown bars | 3 | Bars to wait after an exit before scanning again |
| Magic | 20261213 | Magic number for every order this EA sends |
| Slippage pips | 3 | Slippage tolerance, in pips |
| Max retries | 5 | Attempts per order before giving up this tick |
| Retry wait ms | 500 | Pause between attempts, milliseconds |
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: Buy/Sell Arrows · Pending Orders · Risk & Lot Size · News Trading · Moving Average · MT4 Expert Advisor · MQL4 Source Code