Lesson 5 of 15

SMA Crossover Signal

SMA Crossover Signal

The SMA crossover is one of the simplest and most popular trend-following signals. When a fast-moving average crosses above a slow-moving average, it signals an uptrend (buy). When it crosses below, it signals a downtrend (sell).

Rules

Compare the fast and slow SMA at consecutive time steps:

  • Signal = 1 (bullish crossover): fast_sma[t-1] <= slow_sma[t-1] and fast_sma[t] > slow_sma[t]
  • Signal = -1 (bearish crossover): fast_sma[t-1] >= slow_sma[t-1] and fast_sma[t] < slow_sma[t]
  • Signal = 0: otherwise (including positions where either SMA is None)

The output list has the same length as prices.

Task

Implement crossover_signal(prices, fast, slow) that returns a list of signals (1, -1, or 0) for each time step.

Python runtime loading...
Loading...
Click "Run" to execute your code.