stratester.com

Indicators · condition block

SMA cross (simple moving average)

The SMA cross block fires on the candle where the close crosses its simple moving average: the plain average of the last N closes. It is the most common trend filter in technical analysis.

How the engine evaluates it

The SMA at a candle is the average of the N closes ending with that candle, the current close included.

crosses above fires when the previous close was at or below the previous SMA and the current close is above the current SMA. crosses below is the reverse. It is an event: true on the crossing candle only.

The block is false until N closes exist. Before your start date the engine loads extra history (at least 50 candles, up to 5,000, scaled to the longest indicator in the bot) so the indicator has settled by the first candle of the run.

Formula
SMA(N) = (close[t] + close[t−1] + … + close[t−N+1]) / N
crosses above: close[t−1] ≤ SMA[t−1]  and  close[t] > SMA[t]
crosses below: close[t−1] ≥ SMA[t−1]  and  close[t] < SMA[t]

Botscript syntax

In the Lab you pick this block from the Indicators group and fill in its fields. In the code view the same condition is written like this:

price crosses above sma(50)
price crosses below sma(200)

Settings

SettingValuesDefaultNotes
Directioncrosses above, crosses belowcrosses aboveBullish or bearish cross.
Length1 – 5,000 candles50How many closes the average covers.
Timeframethe run's candles, or 15m, 30m, 1h, 2h, 4h, 6h, 12h, 1d, 1wthe run's candlesOptional. At least the run's candle size and a whole multiple of it.

On a higher timeframe

Add a timeframe at the end to read SMA cross from bigger candles than the run uses, for example price crosses above sma(50) on 1d. The engine builds those candles from the run's own candles (UTC, weeks start on Monday) and evaluates the block only on candles that have closed, so there is no look-ahead. Lengths and lookbacks count the bigger candles.

SMA cross is an event on a higher timeframe: it fires once, on the run candle that closes the bigger candle where it happened, and is false on the other run candles.

The timeframe must be at least the run's candle size and a whole multiple of it; otherwise the run stops with an error and is not charged. Multi-timeframe conditions covers the details, including warm-up.

How traders use it

  • Trend following: buy when price crosses above the 50 or 200-candle SMA, sell when it crosses back below.
  • Filters: put the SMA cross in an entry rule with an oscillator so you only buy oversold dips in the right direction.
  • Exits: price crosses below sma(20) as a trailing exit that adapts to the trend.

Example bot

botscript
bot "SMA 50 Trend"

when price crosses above sma(50)
then buy 100%

when price crosses below sma(50)
then sell 100%
  • Enters when the close crosses above the 50-candle average.
  • Exits on the opposite cross.

Pitfalls

  • In sideways markets price chops back and forth across the average, producing many small losing trades (whipsaws).
  • An SMA lags by about half its length. Longer averages give fewer, later signals.

SMA cross: frequently asked questions

Is the current close included in the average?

Yes. The SMA at a candle includes that candle's close, the same way most charting tools draw it.

Does the block fire while price stays above the SMA?

No. It is a crossing event, true only on the candle where the close moves from one side to the other.

SMA or EMA?

An EMA reacts faster because it weights recent closes more. An SMA is smoother and slower. Test both on your candle size; neither is better in general.

Why is there no signal at the very start of my data?

The average needs N closes before it exists. The engine loads warm-up history before your start date, so this only matters at the beginning of all data in 2017.

Can I compare two averages instead of price?

Yes, with the golden cross block: sma(20) crosses above sma(100).

Simulated results are hypothetical. They include only the fee and slippage you set on the bot, not every real-world cost or market condition, and past performance does not guarantee future results. Nothing here is financial advice.