Use Code: SPRING25
25% OFF Evaluations
00 D
00 H
00 M
00 S

How to Build a Simple Trend-Following EA From Scratch

Before any code gets written, the strategy needs three pieces: a trend filter, an entry signal, and an exit mechanism. Learning how to build a simple trend-following ea from scratch starts there, not in the editor. For this build, the trend filter is a 200-period SMA, the entry signal is a 20-period SMA crossing it, and the exit is a fixed stop and target sized off ATR. Simple, auditable, and explainable if the account gets reviewed.

The Three-Piece Logic Before Any Code

The 200 SMA defines the dominant direction. Price and the 20 SMA above it means long bias, below means short bias. The entry signal is the 20 SMA crossing the 200 SMA in that direction, confirmed on the previous completed bar, never the current forming one. The exit is 1.5 times ATR for the stop and twice that distance for the target, a 2:1 reward to risk ratio. Three moving parts, no discretionary calls buried in the logic.

The MQL5 Structure Everything Sits Inside

OnInit runs once at EA start. It creates the indicator handles, the 20 SMA, 200 SMA, and ATR, and initializes input variables. Nothing trade-related belongs here. OnTick runs on every price update and holds the actual trading logic, crossover check, daily loss check, entry, lot size calculation. OnDeinit runs when the EA is removed and releases the indicator handles. Keeping trading logic out of OnInit and cleanup out of OnTick isn’t a style preference, it’s what prevents an EA from misfiring on chart reload or terminal restart.

The Prop Firm Parameters That Can’t Be Added Later

Four inputs need to exist from the first draft, not bolted on before going live. A minimum hold time input, set to 120 seconds to satisfy TTT Markets’ two minute minimum hold rule, checked before any early exit fires. A maximum daily loss input the EA checks before opening any new position, halting new trades for the day if breached. A trade stacking check confirming no position already exists on the symbol before a new one opens. And a lot size function calculating size from account balance, risk percentage, current ATR, and stop distance, never a fixed lot input.

Entry Logic, Plain Language Then Pseudocode

In plain language: confirm no position is already open on the symbol, confirm the 20 SMA crossed the 200 SMA on the last completed bar in the trade direction, calculate the stop at 1.5x ATR and the target at 2x the stop distance, confirm the daily loss limit hasn’t been hit, then open the trade at the calculated lot size.

Conclusion – How to Build a Simple Trend-Following EA From Scratch

Before this touches a funded account, run it in MT5 Strategy Tester using Every Tick Based on Real Ticks modeling across at least two years covering two distinct market regimes. Confirm the backtest’s max drawdown stays within the firm’s limit at the intended position size. Then run it on a demo account for thirty days, logging every trade’s actual hold time to confirm the minimum hold filter is functioning, not just present in the code. That sequence is what how to build a simple trend-following ea from scratch actually means, code plus verification. Only after that does an evaluation make sense.

FAQ – How to Build a Simple Trend-Following EA From Scratch

1. Why use a 20 and 200 SMA crossover instead of something complex?
Because simple is auditable. If the account gets reviewed, a two-moving-average crossover with an ATR-based exit is easy to explain and trust. Complexity doesn’t add edge by default, it usually just adds curve fitting risk.

2. Why calculate lot size instead of just using a fixed lot input?
Because a fixed lot ignores account balance and current volatility entirely. Risk changes with ATR even if the lot size doesn’t, meaning a fixed lot takes inconsistent risk from trade to trade without anyone noticing.

3. Do I really need thirty days on demo before an evaluation?
Yes. Backtests don’t catch execution issues like slippage, requotes, or a hold time filter that isn’t actually blocking early exits. Thirty days of logged demo trades is cheap insurance against finding those problems on a funded account instead.

We have helped thousands of traders reach funding at TTT Markets from account sizes of $5k upwards to $500k. Check out our programs. 

Additional resources:

Building Strategies with EA: A Beginner’s Guide – FinanceWorld – Trading Signals and Asset Management 

Do Trend-Following Forex EAs Work? Verified by Backtesting – Forex Robot Lab 

How to Build a Simple Trend-Following EA From Scratch

Suggested Article

The content provided on this website is for educational and informational purposes only and does not constitute financial advice. Trading involves risk and may not be suitable for all investors. Past performance is not indicative of future results. Always do your own research before making financial decisions.

Discover more from TTT Markets

Subscribe now to keep reading and get access to the full archive.

Continue reading