# Build a moving-average crossover

> A step-by-step build of a complete strategy graph, from the Data node to the Trade entry node.

Source: https://quantraider.com/learn/strategy-builder/first-strategy

Course: Building strategies as node graphs (lesson 2 of 5) · Level: Intermediate · 5 min

This lesson builds a complete long-only moving-average crossover from the `Data` node to the `Trade entry` node, with a stop from ATR and a target at twice the risk. The aim is to learn the pattern, not to suggest the strategy is profitable.

## The idea in words

Enter a long trade on the candle where a fast moving average crosses above a slow one. Place the stop 1.5 ATR below the entry and the target at twice the distance to the stop. The numbers used below, 20 and 50, are examples, not recommendations. Nodes can be placed anywhere in the editor, so build it from left to right.

*Figure: The fast average crossing above the slow one: the single candle where the entry trigger should be true.*

## Step 1: the averages and the state

1. Add a `Data` node. Add two `SMA` nodes and set `Period (N)` to 20 on one (the fast average) and 50 on the other (the slow average).
2. Connect `Data.Close` to the `Data` input of both `SMA` nodes.
3. Add a `GreaterThan` node. Connect the fast `SMA` output to `A` and the slow `SMA` output to `B`. Its `Result` is true on every candle where the fast average is above the slow one.

## Step 2: only on the crossing candle

The state alone is not enough. If the `Trade entry` trigger were connected here, it would be true on every candle while the fast average stays above the slow one, which can be dozens of candles in a row. You want to enter once, on the candle where the state changes from false to true.

A crossing means that the fast average is above the slow one now and was not above it one candle ago. `PreviousValue` takes a number and returns the value from one candle earlier, so apply it to each average, not to the boolean.

1. Add two `PreviousValue` nodes. Connect the fast `SMA` to one and the slow `SMA` to the other. Their `Previous` outputs are last candle's averages.
2. Add a second `GreaterThan` node with the previous fast value on `A` and the previous slow value on `B`. Its `Result` says whether the fast average was above the slow one on the previous candle.
3. Add a `Not` node and connect that second `Result` to its input `A`. It is true when the fast average was not above the slow one one candle ago.
4. Add an `And` node. Connect the first `GreaterThan` `Result` (the state now) to `A` and the `Not` `Result` to `B`.

**Worked example: Why the trigger fires once**

1. Suppose the fast-above-slow state over seven candles is: false, false, true, true, true, false, true.
2. The previous-candle state is the same list shifted by one: (none), false, false, true, true, true, false.
3. The trigger is the state now `And` `Not` previous state, which is true only where the state is true and the previous state was false.
4. That is candle 3 (true after false) and candle 7 (true after false). Candles 4 and 5 are true but follow a true, so `Not` makes them false.

Without the previous-candle check the trigger would be true on 4 candles (3, 4, 5 and 7); with it, only on 2 (3 and 7), one per crossing.

## Step 3: the entry and a stop from ATR

1. Add a `Trade entry` node, set the direction to buy and the entry price type to `Market`. Connect the `And` `Result` to `Trigger`.
2. Add an `ATR` node (for example a period of 14) and connect `Data.High`, `Data.Low` and `Data.Close` to its `High`, `Low` and `Close` inputs.
3. Add a `Multiply` node with the `ATR` output on `A` and a number constant of 1.5 on `B`. That is the stop distance.
4. Add a `Subtract` node with `Data.Close` on `A` and the `Multiply` `Result` on `B`, then connect its `Result` to `SL1`. The stop is the close minus 1.5 ATR.

## Step 4: a target at 2R

A target at **2R** is twice as far from the entry as the stop is. The graph needs the risk, which is the entry minus the stop, then twice that, added to the entry. Here `Data.Close` stands in for the entry price.

```
Risk = Entry − Stop
Target = Entry + 2 × Risk
```

Built from `Subtract`, `Multiply` (by 2) and `Add`, ending in `TP1`.

**Worked example: The numbers on the crossing candle**

1. On the crossing candle the close is 100.30 and the ATR is 2.00.
2. Stop distance: 1.5 × 2.00 = 3.00, so the stop is 100.30 − 3.00 = 97.30.
3. Risk: 100.30 − 97.30 = 3.00. Twice the risk is 2 × 3.00 = 6.00.
4. Target: 100.30 + 6.00 = 106.30. Reward to risk is 6.00 / 3.00 = 2.

Entry 100.30, stop 97.30, target 106.30: risking 3.00 per unit to aim for 6.00 per unit.

Connect the final `Add` `Result` to `TP1`. Connect `Data.Close` to `Entry Price` too, so the graph and your calculation use the same reference. With the `Market` type the actual fill can differ from the close, so compare the entry prices in the replay with what you expected.

> **Check before trusting it**
>
> A crossover with fixed multiples is a template, not a tested idea. Run it over different markets and periods, include costs, and look at the trades one by one before drawing conclusions.

> **Build it yourself**: Recreate the graph above node by node, then run a backtest on a strategy that uses it. [Open the graph editor](https://quantraider.com/nodesystem)

## Key takeaways

- A comparison of two averages is a state; adding a `PreviousValue` check with `Not` and `And` turns it into a one-candle crossing event.
- `PreviousValue` takes numbers, so apply it to each `SMA` output and compare the earlier values in a second `GreaterThan`.
- An ATR stop is `Subtract` of close and ATR × 1.5, fed into `SL1`.
- A 2R target is entry + 2 × (entry − stop), built from `Subtract`, `Multiply` and `Add`, fed into `TP1`.

> Educational content, not financial advice. Trading involves risk of loss.
