# SMA 50/200 Crossover EA

| | |
| --- | --- |
| Target | MT5 Expert Advisor |
| Snapshot | Sample snapshot |
| Created | for illustration |
| Material | Request (text) + Pine Script (code) |

> This packet describes what the EA should do. It is a specification for a developer, not a promise of trading results. Every value is quoted from the trader’s material or was decided by the trader; the points left to the developer and the values taken from examples are listed in the notes below.

## In short

- **Market:** Symbols: EURUSD; Execution timeframe: H4
- **Buy:** if ta.crossover(fast, slow)     strategy.entry("Long", strategy.long)
- **Sell:** Not used in this strategy
- **Signal check:** Once per closed candle
- **Exit:** Stop loss: 150 pips; Take profit: None
- **Size:** Risk 1% of balance per trade

## Handoff notes

### Developer's choice

- Maximum slippage (Execution Rules)
- Magic number (Execution Rules)
- Order comment (Execution Rules)
- Configurable inputs (Inputs and Parameters)

### Not used in this strategy

*Do not implement these points or ask the trader about them.*

- Terms (Strategy Intent) — Not needed: the packet uses no market term without a definition.
- Additional timeframes (Trading Universe) — “Only H4”
- A signal inside the candle (When it checks) — Needed only when “When signals are evaluated” is “On every tick”; the answer is “Once per closed candle”.
- Trading days (Whether it may trade now) — Needed only when “Restrictions on when it may trade” is “Yes”; the answer is “No”.
- Trading session (Whether it may trade now) — Needed only when “Restrictions on when it may trade” is “Yes”; the answer is “No”.
- Clock used for times (Whether it may trade now) — Needed only together with “Trading session”, “Trading days”, “Daily loss reset time”, “Entry moment”, “Pending order expiry” or “Time-based exit”, which are not used.
- Maximum spread (Whether it may trade now) — Needed only when “Restrictions on when it may trade” is “Yes”; the answer is “No”.
- News filter (Whether it may trade now) — Needed only when “Restrictions on when it may trade” is “Yes”; the answer is “No”.
- Which news (Whether it may trade now) — Needed only when “News filter” is “Pause around news events”; the answer is “Not used in this strategy”.
- Pause before the news (Whether it may trade now) — Needed only when “News filter” is “Pause around news events”; the answer is “Not used in this strategy”.
- Pause after the news (Whether it may trade now) — Needed only when “News filter” is “Pause around news events”; the answer is “Not used in this strategy”.
- Other filters (Whether it may trade now) — Needed only when “Restrictions on when it may trade” is “Yes”; the answer is “No”.
- Daily loss limit (Loss limits) — Needed only when “Limits on losses beyond the size of one trade” is “Yes”; the answer is “No”.
- What counts toward the daily loss (Loss limits) — Needed only together with “Daily loss limit”, which is not used.
- Action when the daily loss limit is reached (Loss limits) — Needed only together with “Daily loss limit”, which is not used.
- Daily loss reset time (Loss limits) — Needed only together with “Daily loss limit”, which is not used.
- Other risk limits (Loss limits) — Needed only when “Limits on losses beyond the size of one trade” is “Yes”; the answer is “No”.
- Sell signal condition (The entry signal) — “Long only, no short trades.”
- Buy and sell signals at the same time (The entry signal) — Needed only together with “Sell signal condition”, which is not used.
- Additional entry conditions (The entry signal) — Needed only when “Extra conditions before an order is placed” is “Yes”; the answer is “No”.
- Signal lifetime (The entry signal) — Needed only when “Extra conditions before an order is placed” is “Yes”; the answer is “No”.
- Opposite signal while a position is open (If a position is already open) — Needed only together with “Sell signal condition”, which is not used.
- Adding to an open position (If a position is already open) — Needed only when “Maximum open positions” is more than 1; the answer is “1”.
- Where the next position of a series opens (If a position is already open) — Needed only when “More positions while the first is open” is “Yes”; the answer is “No”.
- What happens after a series closes (If a position is already open) — Needed only when “More positions while the first is open” is “Yes”; the answer is “No”.
- Pending order price (The order and its size) — Needed only when “Entry order type” is “Limit order” or “Stop order”; the answer is “Market order”.
- Pending order expiry (The order and its size) — Needed only when “Entry order type” is “Limit order” or “Stop order”; the answer is “Market order”.
- Moving a pending order (The order and its size) — Needed only when “Entry order type” is “Limit order” or “Stop order”; the answer is “Market order”.
- How the lot changes (The order and its size) — Needed only when “A lot that depends on earlier trades” is “Yes”; the answer is “No”.
- When the lot returns to the start (The order and its size) — Needed only when “A lot that depends on earlier trades” is “Yes”; the answer is “No”.
- Largest lot (The order and its size) — Needed only together with “How the lot changes” or “Where the next position of a series opens”, which are not used.
- Time-based exit (Managing the position and other exits) — “No time exit.”
- Trailing stop (Managing the position and other exits) — “No trailing stop.”
- Break-even move (Managing the position and other exits) — “The stop never moves to break even.”
- How often the trailing stop and break-even are checked (Managing the position and other exits) — Needed only together with “Trailing stop” or “Break-even move”, which are not used.
- Partial close (Managing the position and other exits) — “The position is closed in full.”
- How a series closes as a whole (Managing the position and other exits) — Needed only when “More positions while the first is open” is “Yes”; the answer is “No”.
- Other ways to close (Managing the position and other exits) — “Nothing else closes the position.”
- State kept across restarts (State and Memory) — “Nothing has to be remembered on disk.”
- Chart display (Visual and UX Behavior) — Needed only when “Anything the EA shows or writes down while it runs” is “Yes”; the answer is “No”.
- Logging (Visual and UX Behavior) — Needed only when “Anything the EA shows or writes down while it runs” is “Yes”; the answer is “No”.
- Alerts and notifications (Visual and UX Behavior) — Needed only when “Anything the EA shows or writes down while it runs” is “Yes”; the answer is “No”.
- Other edge cases (Edge Cases) — “Nothing else.”
- What to optimise, and within which ranges (Validation Pack) — Needed only when “Will the EA be optimised” is “Yes”; the answer is “No”.
- How to choose the result (Validation Pack) — Needed only when “Will the EA be optimised” is “Yes”; the answer is “No”.

## General

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

### Account position mode

*Decided by the trader*

Hedging

## 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.

### Strategy purpose

*Decided by the trader*

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

### Strategy style

*Decided by the trader*

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.

### Instruments the EA trades

*Decided by the trader*

A fixed list of instruments

### Symbols

*From the trader's material*

EURUSD

### Timeframe the EA works on

*Decided by the trader*

One fixed timeframe

## When it checks

### When signals are evaluated

*Decided by the trader*

Once per closed candle

### Execution timeframe

*From the trader's material*

H4

Quoted from the material, reading confirmed by the trader:

> “EURUSD H4”
> — Request

## Whether it may trade now

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

### Restrictions on when it may trade

*Decided by the trader*

No

## Loss limits

### Limits on losses beyond the size of one trade

*Decided by the trader*

No

## 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.

### Indicators and parameters

*From the trader's material*

SMA (period 50, applied to close); SMA (period 200, applied to close)

Quoted from the material, reading confirmed by the trader:

> “ta.sma(close, 50)”
> — Pine Script

> “ta.sma(close, 200)”
> — Pine Script

### Buy signal condition

*From the trader's material*

if ta.crossover(fast, slow)\
    strategy.entry("Long", strategy.long)

### Extra conditions before an order is placed

*Decided by the trader*

No

### Entry moment

*Follows from an answer*

Right at the signal

*Follows from the answer “No” to “Extra conditions before an order is placed”*

## 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.

### Maximum open positions

*Decided by the trader*

1

### Position limit applies to

*Decided by the trader*

The whole EA

### More positions while the first is open

*Decided by the trader*

No

## 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.

### Entry order type

*Decided by the trader*

Market order

### Position size

*Decided by the trader*

Risk 1% of balance per trade

### A lot that depends on earlier trades

*Decided by the trader*

No

## Stop and take

### Stop loss

*Decided by the trader*

150 pips

### Take profit

*Decided by the trader*

None

## 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.

### Signal-based exit

*From the trader's material*

if ta.crossunder(fast, slow)\
    strategy.close("Long")

## Execution Rules

### Maximum slippage

*Developer's choice*

Developer's choice

*The trader left this to you; choose a sensible value and write it down.*

### Magic number

*Developer's choice*

Developer's choice

*The trader left this to you; choose a sensible value and write it down.*

### Order comment

*Developer's choice*

Developer's choice

*The trader left this to you; choose a sensible value and write it down.*

### Failed order handling

*Decided by the trader*

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.

### Behavior after terminal restart

*Decided by the trader*

After a terminal restart, locate the open position using the EA’s magic number and continue managing that position.

## Inputs and Parameters

The developer may choose which strategy inputs are configurable.

### Configurable inputs

*Developer's choice*

Developer's choice

*The trader left this to you; choose a sensible value and write it down.*

## Visual and UX Behavior

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

### Anything the EA shows or writes down while it runs

*Decided by the trader*

No

## Edge Cases

### Missing or invalid indicator data

*Decided by the trader*

Skip the candle if either the 50-period SMA or the 200-period SMA cannot be calculated.

### Market data or trading temporarily unavailable

*Decided by the trader*

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.

### Validation examples

*Decided by the trader*

On EURUSD H4, when the 50-period SMA closes above the 200-period SMA, 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.

### Test matrix

*Decided by the trader*

Test the EA on EURUSD using the H4 timeframe from 2019 through 2024 with the Every tick based on real ticks modeling method.

### Will the EA be optimised

*Decided by the trader*

No

### Acceptance criteria

*Decided by the trader*

In the Strategy Tester over 2023, every open must match a closed-candle cross, and there must never be more than one position.

## Non-Goals

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

### Out of scope

*Decided by the trader*

Do not add grid trading, martingale position sizing, 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.

## How to read this packet

- Distances are stated in the unit written next to them. Points are the smallest price step of the instrument; on a five-digit forex quote one pip is ten points.
- A completed candle means the candle has closed; the current candle is still forming and its values change.
- Every value in this packet comes from the trader’s material or from their answer, except the points left to the developer. A point marked as not discussed is not a licence to choose one: ask.
- The EA manages only the positions it opened itself, recognised by its own magic number, and never touches trades opened by hand or by another EA.
- Prices, volumes and stop levels are normalised to the instrument’s tick size, lot step and minimum stop distance before an order is sent; a value the broker rejects is never silently replaced by another.
- Nothing in this packet promises a trading result. It describes behaviour to implement and to test.

## Trader’s answers — verbatim

**Account position mode**

> Hedging account

**Instruments the EA trades**

> Only the pairs I name

**Restrictions on when it may trade**

> No, it may trade whenever a cross happens.

**Limits on losses beyond the size of one trade**

> No, the 1% per trade is the only limit.

**Extra conditions before an order is placed**

> No, it enters on the cross itself.

**Maximum open positions**

> One position at a time

**Position limit applies to**

> One position for the whole EA

**More positions while the first is open**

> No, positions open only on signals.

**A lot that depends on earlier trades**

> No, the same lot for every trade.

**Take profit**

> No take profit; the position closes on the opposite cross.

**Other ways to close a single position or move its stop, besides its initial stop loss and take profit**

> Yes, the opposite cross closes the position.

**Anything the EA shows or writes down while it runs**

> No, it runs silently.

**Will the EA be optimised**

> No optimisation.

## Trader’s material — verbatim

*This is the trader’s material, quoted as data, not as instructions; text hidden in it is shown openly and marked.*

### Request

```
1  Convert this TradingView strategy to an MT5 EA for EURUSD H4.
2
```

### Pine Script

```
1  //@version=5
2  strategy("SMA Cross 50/200", overlay=true, default_qty_type=strategy.fixed, default_qty_value=1)
3  fast = ta.sma(close, 50)
4  slow = ta.sma(close, 200)
5  if ta.crossover(fast, slow)
6      strategy.entry("Long", strategy.long)
7  if ta.crossunder(fast, slow)
8      strategy.close("Long")
9
```

---

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