# SMA 50/200 Crossover EA

MT5 Expert Advisor · Sample snapshot · for illustration

## General

The EA is named SMA 50/200 Crossover EA and runs on a hedging account.

## Strategy Intent

When the 50-period SMA crosses above the 200-period SMA, the EA buys. While the long position is open, the EA holds it until the 50-period SMA crosses back below the 200-period SMA, or the stop loss closes the position. If the stop loss closes the position, then the EA waits for the next cross of the 50-period SMA above the 200-period SMA before it buys again. The strategy style is trend following.

## Trading Universe

The EA trades only the named instruments, which in this strategy is EURUSD. It uses one fixed timeframe only: H4; no additional timeframes are used.

## Whether it may trade now

The EA may trade whenever a crossover signal occurs, with no additional restrictions on when trading is permitted.

## The entry signal

Calculate a 50-period SMA and a 200-period SMA, both applied to closing prices. Evaluate signals once per closed candle and open a long position when the 50-period SMA crosses above the 200-period SMA. This strategy is long only and does not open short trades.

When a bullish crossover of the 50-period SMA above the 200-period SMA occurs, place a market order to open a long position. No additional conditions are required before placing the order.

## If a position is already open

The EA allows one position for the whole EA at a time. When an exit is triggered, the position is closed in full; partial closes are not used.

## The order and its size

Risk 1% of account balance per trade. Do not apply any additional limits on losses beyond the 1% risk assigned to each trade.

## Managing the position and other exits

Set a 150-pip stop loss and no take-profit order. Close the long position when the fast SMA crosses under the slow SMA. Do not use a time-based exit, trailing stop, or break-even stop move.

## Execution Rules

- Failed order handling: If an order is rejected, then the EA retries it once after 5 seconds. If the retry is rejected, then the EA skips the signal.

## State and Memory

The strategy does not persist any state to disk. After a terminal restart, it finds the open position by its magic number and continues managing it.

## Inputs and Parameters

The developer may choose which strategy inputs are configurable.

## Visual and UX Behavior

The EA runs silently and does not show or write anything while it runs.

## Edge Cases

- Missing or invalid indicator data: Skip the candle if either the 50-period SMA or the 200-period SMA cannot be calculated.
- Market data or trading temporarily unavailable: While market data or trading is temporarily unavailable, the EA does not trade until prices update again.

## Validation Pack

When the 50-period SMA closes above the 200-period SMA on EURUSD H4, the EA opens one buy position on the next tick. When the 50-period SMA closes back below the 200-period SMA, the EA closes that position. If the stop loss closes the position while the 50-period SMA is still above the 200-period SMA, then the EA opens no new buy until the 50-period SMA crosses above the 200-period SMA again. The test matrix is EURUSD H4, 2019 to 2024, every tick based on real ticks. No optimisation is used. In the Strategy Tester over 2023, every open must match a closed-candle cross. In the Strategy Tester over 2023, there must never be more than one position.

## Non-Goals

Do not add grid trading, martingale behavior, or news-based trading.

## Parameters implied by these values

These are the settled values as EA inputs. The defaults are the trader's own values; a point
they did not settle has no row here.

| Input | Type | Default | Stated as | Comes from |
| --- | --- | --- | --- | --- |
| `InpSymbols` | string | `"EURUSD"` | EURUSD | Symbols (Trading Universe) |
| `InpExecutionTimeframe` | ENUM | `PERIOD_H4` | H4 | Execution timeframe (When it checks) |
| `InpSMAPeriod` | int | `50` | SMA (period 50, applied to close) | Indicators and parameters (The entry signal) |
| `InpSMAPeriod2` | int | `200` | SMA (period 200, applied to close) | Indicators and parameters (The entry signal) |
| `InpStopLossPips` | int | `150` | 150 pips | Stop loss (Stop and take) |
| `InpMaximumOpenPositions` | int | `1` | 1 | Maximum open positions (If a position is already open) |
| `InpRiskPercent` | double | `1.00` | Risk 1% of balance per trade | Position size (The order and its size) |

## Acceptance checklist

One check per settled point, in the trader's own accepted words. A point without a scenario is
still part of the specification: check the value as the packet states it.

### Opening a position

1. **Buy signal condition** — if ta.crossover(fast, slow)     strategy.entry("Long", strategy.long)
   With the EA running on EURUSD H4, no open position, and the 50-period SMA of close below the 200-period SMA of close on the last closed candle, when the 50-period SMA crosses above the 200-period SMA on a closed candle, the EA places one market buy order with the ID "Long". On a closed candle where the 50-period SMA does not cross above the 200-period SMA, the buy signal condition is not met and the EA places no buy order.
2. **When signals are evaluated** — Once per closed candle
   Signals are checked once, when a candle has closed (as the next one opens), so in the “Open prices only” mode the EA opens trades on the same candles as in the “Every tick based on real ticks” mode. Exits that trigger inside a candle may differ between these modes: the result is judged in the “Every tick based on real ticks” mode, the closest to real trading.
3. **Entry order type** — Market order
   On EURUSD H4, after a valid closed-candle buy signal and with trading available, the EA opens the position. The position must be opened with a market order.

### Closing a position

4. **Stop loss** — 150 pips
   With one buy position open on EURUSD H4, place its stop loss 150 pips below the entry price. The position must have a stop loss set at that 150-pip level and must close when the market reaches it.
5. **Take profit** — None
   With one long EURUSD position opened after a closed-candle crossover on H4 and a 150-pip stop loss, the 50-period SMA closes below the 200-period SMA. No take-profit level is set, and the position closes on the opposite cross.
6. **Signal-based exit** — if ta.crossunder(fast, slow)     strategy.close("Long")
   Set up the EA on EURUSD H4 with one long position open under the label Long, opened by a buy signal, and with no stop loss or take profit hit. When the 50-period SMA crosses below the 200-period SMA on a closed candle, the EA sends a closing order for the position labelled Long, and that closing order is the only trade it sends on that candle. The check passes only if the Long position is closed on that candle and no other position is opened or closed.

### Size and limits

7. **Maximum open positions** — 1
   With the EA running on EURUSD H4 in hedging mode and no position open, a buy signal opens a market position. The EA must then allow only one position at a time for the whole EA and must not place another entry while that position remains open.
8. **Position size** — Risk 1% of balance per trade
   On EURUSD H4, when the 50-period SMA closes above the 200-period SMA and a buy market order is opened with a 150 pip stop loss, the EA calculates the position size from the account balance. The order volume must be set so that the risk at the 150 pip stop loss is 1% of balance per trade.

### Orders, data and the unexpected

9. **Failed order handling** — Retry a rejected order once after 5 seconds, then skip the signal.
   A buy signal is active and the EA’s market order is rejected. When the EA receives the rejection, it retries that order once after 5 seconds. If the retry is rejected, the EA makes no further order attempt for that signal.
10. **Missing or invalid indicator data** — Skip the candle when either moving average cannot be calculated.
   Set up EURUSD on the H4 execution timeframe with the 50-period or 200-period SMA unable to be calculated for a closed candle. When that candle is evaluated, skip the candle when either moving average cannot be calculated. No order is placed for that candle.
11. **Market data or trading temporarily unavailable** — Do not trade until prices update again.
   With a buy signal available on a closed candle, the market data or trading is temporarily unavailable. When prices have not updated again, the EA does not place a trade. After prices update again, the EA may trade on a valid signal.

### Restarts and candle timing

12. **Behavior after terminal restart** — After a restart, find the open position by its magic number and keep managing it.
   An open position exists on EURUSD when the MT5 terminal is restarted, and the position is identified by the EA's magic number. After the restart, the EA finds that open position by its magic number and keeps managing it.

## Not settled in the spec

- **Left to the developer:** Maximum slippage; Magic number; Order comment; Configurable inputs

*This is the short view: each section’s accepted text, the inputs and the acceptance checklist. The quotes and the trader’s answers are in the full version.*

---

Prepared with [ToSpeca](https://tospeca.com/en)
