Simple Moving Average (SMA)
Simple Moving Average is the average price over a lookback window. An extended SMA analysis option includes mean absolute deviation (MAD), mean square error (MSE), and mean absolute percentage error (MAPE). [Discuss] 💬
// C# usage syntax (with Close price)
IReadOnlyList<SmaResult> results =
bars.ToSma(lookbackPeriods);Parameters
| param | type | description |
|---|---|---|
lookbackPeriods | int | Number of periods (N) in the lookback window. Must be greater than 0. |
Historical price bars requirements
You must have at least N periods of bars to cover the warmup periods.
bars is a collection of generic TBar historical price bars. It should have a consistent frequency (day, hour, minute, etc). See the Guide for more information.
Response
IReadOnlyList<SmaResult>- This method returns a time series of all available indicator values for the
barsprovided. - It always returns the same number of elements as there are in the historical price bars.
- It does not return a single incremental indicator value.
- The first
N-1periods will havenullvalues since there's not enough data to calculate.
SmaResult
| property | type | description |
|---|---|---|
Timestamp | DateTime | Date from evaluated TBar |
Sma | double | Simple moving average |
Utilities
See Utilities and helpers for more information.
Analysis
This indicator has an extended version with more analysis. See SMA with extended analysis for the full documentation including streaming support.
// C# usage syntax
IReadOnlyList<SmaAnalysisResult> analysis =
bars.ToSmaAnalysis(lookbackPeriods);SmaAnalysisResult
| property | type | description |
|---|---|---|
Timestamp | DateTime | Date from evaluated TBar |
Sma | double | Simple moving average |
Mad | double | Mean absolute deviation |
Mse | double | Mean square error |
Mape | double | Mean absolute percentage error |
Chaining
This indicator may be generated from any chain-enabled indicator or method.
// example
var results = bars
.Use(CandlePart.Volume)
.ToSma(..);Results can be further processed on Sma with additional chain-enabled indicators.
// example
var results = bars
.ToSma(..)
.ToRsi(..);See Chaining indicators for more.
Streaming
Use the buffer-style List<T> when you need incremental calculations without a hub:
SmaList smaList = new(lookbackPeriods);
foreach (IBar bar in bars) // simulating stream
{
smaList.Add(bar);
}
// based on `ICollection<SmaResult>`
IReadOnlyList<SmaResult> results = smaList;Subscribe to a BarHub for advanced streaming scenarios:
BarHub barHub = new();
SmaHub observer = barHub.ToSmaHub(lookbackPeriods);
foreach (IBar bar in bars) // simulating stream
{
barHub.Add(bar);
}
IReadOnlyList<SmaResult> results = observer.Results;See Buffer lists and Stream hubs for full usage guides.