---
url: /indicators/sma.md
description: >-
  Simple moving average.  Extended to include mean absolute deviation, mean
  square error, and mean absolute percentage error
package: FacioQuo.Stock.Indicators
docs_version: v3
canonical: https://dotnet.stockindicators.dev/indicators/sma
generated: 2026-09-23T06:23:37.276Z
commit: 1c2a6283c73a1ab8cc3b9330d93527c3d74e63c7
---

# Simple Moving Average (SMA)

[Simple Moving Average](https://en.wikipedia.org/wiki/Moving_average#Simple_moving_average) is the average price over a lookback window.  An [extended SMA analysis](/indicators/sma-analysis.md) option includes mean absolute deviation (MAD), mean square error (MSE), and mean absolute percentage error (MAPE).
[\[Discuss\] 💬](https://github.com/facioquo/stock-indicators-dotnet/discussions/240 "Community discussion about this indicator")

```csharp
// 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](/guide/getting-started.md#historical-bars) for more information.

## Response

```csharp
IReadOnlyList<SmaResult>
```

* This method returns a time series of all available indicator values for the `bars` provided.
* 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-1` periods will have `null` values since there's not enough data to calculate.

### `SmaResult`

| property | type | description |
| -------- | ---- | ----------- |
| `Timestamp` | *`DateTime`* | Date from evaluated `TBar` |
| `Sma` | *`double`* | Simple moving average |

### Utilities

* [.Condense()](/utilities/results.md#condense)
* [.Find(lookupDate)](/utilities/results.md#find-by-date)
* [.RemoveWarmupPeriods()](/utilities/results.md#remove-warmup-periods)
* [.RemoveWarmupPeriods(removePeriods)](/utilities/results.md#remove-warmup-periods)

See [Utilities and helpers](/utilities.md) for more information.

## Analysis

This indicator has an extended version with more analysis.  See [SMA with extended analysis](/indicators/sma-analysis.md) for the full documentation including streaming support.

```csharp
// 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.

```csharp
// example
var results = bars
    .Use(CandlePart.Volume)
    .ToSma(..);
```

Results can be further processed on `Sma` with additional chain-enabled indicators.

```csharp
// example
var results = bars
    .ToSma(..)
    .ToRsi(..);
```

See [Chaining indicators](/guide/chaining.md) for more.

## Streaming

Use the buffer-style `List<T>` when you need incremental calculations without a hub:

```csharp
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:

```csharp
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](/guide/styles/buffer.md) and [Stream hubs](/guide/styles/stream.md) for full usage guides.
