Documentation
Still have questions?
Contact support
Still have questions?
Contact support
You are looking at an outdated 7.11.0 version of this document. Switch to the 7.12.0 version to see the up to date information.

# Simple Moving Average

## Overview

A simple moving average (SMA) is the unweighted mean of the previous n data points. In technical analysis there are various popular values for n, like 10 days, 40 days, or 200 days. The period selected depends on the kind of movement one is concentrating on, such as short, intermediate, or long term. In any case moving average levels are interpreted as support in a rising market, or resistance in a falling market.

AnyChart Stock allows you to add SMA with desired period to any of your charts.

Mathematical description of the indicator: Simple moving average (SMA) Mathematical Description.

SMA indicator is added using .sma() method, it requires a mapping with value field in it:

``````// create data table on loaded data
var dataTable = anychart.data.table();

// add data to a table

var mapping = dataTable.mapAs({'value': 4});

// create stock chart
chart = anychart.stock();

// create plot on the chart
var plot = chart.plot(0);

// create SMA indicators with period 20
var sma20 = plot.sma(mapping, 20).series();
sma20.stroke('#bf360c');
``````

Here is a live sample:

## Indicator parameters

SMA indicator need three parameters: mapping with value field in it, period and a type of series to be displayed as:

``````var sma10 = plot.sma(mapping, 10, "column");
``````

## Visualization

Vizualization of an indicator depends on the type of a series you display it with. Here is a sample where SMA with different parameters and settings is added to different plots:

``````// create SMA indicator with period 20 and show as line on the first plot
var sma20 = plot_0.sma(mapping, 20).series();
sma20.stroke('#bf360c');

// create SMA indicator with period 50 and show as column on the second plot
var sma50 = plot_1.sma(mapping, 50, "column").series();
sma50.fill('#ff6d00');
``````

Live sample: