# Simple Moving Average (SMA)

Simple Moving Average (SMA) is a moving average indicator which measures the trend direction over a period of time. Unlike EMA, this indicator places the same weight on each and every day within the selected period. Because of this, SMA reacts slower to recent price changes.

### **Trading application**

SMA is a line which is shown within the price graph, moving close to the price and eventually intersecting with it. SMA moving above the price indicates a downtrend while SMA moving below the price indicates an uptrend. It can also act as a level of Support/Resistance in uptrends and downtrends, respectively. SMA is one of the better Moving Averages for identifying Support/Resistance.

We can interpret buy/sell signals by using 2 different SMA lines (one with a shorter period than the other). If the shorter SMA (e.g. a 20 day SMA) crosses upwards over the longer SMA (e.g. 50 days), it is a buy signal. The opposite, where the shorter SMA crosses downwards below the longer SMA, indicates a sell signal. Another interpretation arises when intersecting with the price. A shorter SMA crossing the price from the bottom up, while remaining above the longer SMA, is considered a buy signal, in the same way that the shorter SMA crossing the price from above downwards, while the longer SMA is located above the shorter SMA, indicates a sell signal.

### **Calculation**

$$
SMA = X Days Closing Prices Sum / X
$$

### **Parameters**

#### Input Parameters

| Name     | Type | Range of value    | Description                             |
| -------- | ---- | ----------------- | --------------------------------------- |
| `period` | int  | <1, int.MaxValue> | Number of bars used in the calculation. |

#### Output Parameters

Indicator ouptuts a single value.

| Type   | Range of value       |
| ------ | -------------------- |
| double | (0, double.MaxValue) |

### Examples

```csharp
// Prints the current value of a 20 period SMA using
// the daily bars close price value.
sma = indicators.SMA(20).OnSeries(dailyBars.Close);
Log(sma.Value.ToString());

// Prints the previous value of SMA.
sma = indicators.SMA(20).Keep(2).OnSeries(dailyBars.Close);
Log(sma.Values[1].ToString());
```

#### Complete example

```csharp
using Signals.DataSeries.Bars;
using Signals.Framework;
using Signals.Indicators.SMA;

public class MyStrategy : SingleMarketStrategy
{
    private Bars dailyBars;
    private SMA sma;

    public override void Setup(DataMarketplace data, IndicatorsMarketplace indicators)
    {
        dailyBars = data.Bars(BarPeriodType.Day, 1).WithOffset(50);
        sma = indicators.SMA(20).Keep(2).OnSeries(dailyBars.Close);
    }

    public override void RegisterActions()
    {
        OnUpdateOf(dailyBars).Do(() =>
        {
            // Prints the current SMA value
            Log("The current SMA value is " + sma.Value.ToString());

            // Prints the previous SMA value
            Log("The previous SMA value is " + sma.Values[1].ToString());
        });
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://help.signals.network/knowledge-base/indicators/trend-analysis/simple-moving-average-sma.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
