Simple Moving Average(SMA)
The unweighted mean of the last N closes.
sma(prices, period: int)The simple moving average is the arithmetic mean of a fixed window of closes, recomputed each bar. Every observation in the window carries identical weight, and observations outside it carry none — which is the whole of its behaviour, and the source of both its usefulness and its defects.
Its main virtue is that it is legible. When an SMA crosses a price or another SMA, you can say exactly what changed and why, which matters more than it sounds: a strategy you cannot explain is a strategy you cannot debug when it starts losing. Its main defect is the equal weighting — a large move drops out of the window abruptly N bars later and jolts the average, an artefact of the window rather than anything the market did.
There is no default period, deliberately. Any number here is a claim about the horizon you are trading, and the SDK will not make that claim on your behalf.
Where it misleads
- The drop-out effect: a large move leaving the window shifts the average with no corresponding price action, which reads as a signal and is not one.
- In a sideways market, price crosses a moving average constantly. A crossover rule with no trend filter will trade every one of them.
- Round-number periods (50, 100, 200) are conventions. They are worth testing precisely because so many people watch them, but not worth assuming.
In code
from quantide import fetch_data, sma, crossover
data = fetch_data("AAPL", "2018-01-01", "2025-01-01")
fast = sma(data["Close"], period=20)
slow = sma(data["Close"], period=50)
entries = crossover(fast, slow)