SMA

Yaser Rahmati | یاسر رحمتی

What is a Simple Moving Average (SMA)?

The Simple Moving Average (SMA) is a widely used technical analysis tool that helps smooth out price data to create a trend-following indicator. It is calculated by averaging a set number of past data points. The SMA is useful for identifying the direction of the current trend.

How to Calculate SMA

1. Choose the time period:

Decide the number of periods (days, weeks, etc.) over which you want to calculate the average. Common periods are 10, 20, 50, 100, or 200 days.

2. Sum the data points:

Add up the closing prices (or any other chosen data points) for the selected number of periods.

3. Divide by the number of periods:

Divide the total by the number of periods to get the average.

Numerical Example

Let's calculate a 5-day SMA using the following closing prices:

  • Day 1: $10

  • Day 2: $12

  • Day 3: $14

  • Day 4: $13

  • Day 5: $15

1. Sum the closing prices for the last 5 days:

10+12+14+13+15=6410+12+14+13+15=64

2. Divide by the number of days (5):

SMA=645=12.8SMA=\frac{64}{5}=12.8

So, the 5-day SMA is $12.8.

Strategies for Using the 200 SMA

1. Trend Identification:

  • Uptrend: If the price is above the 200 SMA, the market is generally considered to be in an uptrend.

  • Downtrend: If the price is below the 200 SMA, the market is generally considered to be in a downtrend.

2. Support and Resistance:

  • The 200 SMA often acts as a strong support level in an uptrend and a resistance level in a downtrend. Prices may bounce off the 200 SMA multiple times, confirming its importance.

3. Crossovers:

  • Golden Cross: When a shorter-term SMA (like the 50-day SMA) crosses above the 200 SMA, it’s a bullish signal.

  • Death Cross: When a shorter-term SMA crosses below the 200 SMA, it’s a bearish signal.

Pine Script Code For SMA

Here's a basic Pine Script code for a Simple Moving Average (SMA) using Pine Script version 5:

//@version=5
indicator("Simple Moving Average", overlay=true)

// Input for the length of the SMA
smaLength = input.int(14, title="SMA Length", minval=1)

// Calculate the SMA
smaValue = ta.sma(close, smaLength)

// Plot the SMA on the chart
plot(smaValue, title="SMA", color=color.blue)

This script creates a simple moving average (SMA) indicator:

  • Indicator declaration: The script is defined as an indicator with indicator("Simple Moving Average", overlay=true). The overlay=true parameter means the SMA will be plotted on top of the price chart.

  • Input parameter: The length of the SMA is defined as an input parameter with a default value of 14.

  • SMA calculation: The SMA is calculated using the ta.sma function.

  • Plotting: The calculated SMA is plotted on the chart with the plot function, using a blue color for the line.

Pine Script Code For Dead Cross And Golden Cross

Below is a Pine Script version 5 code that detects and highlights Golden Cross and Dead Cross signals using two Simple Moving Averages (SMA):

//@version=5
indicator("Golden Cross and Dead Cross of SMA", overlay=true)

// Input for the short and long SMA lengths
shortSmaLength = input.int(50, title="Short SMA Length", minval=1)
longSmaLength = input.int(200, title="Long SMA Length", minval=1)

// Calculate the short and long SMAs
shortSma = ta.sma(close, shortSmaLength)
longSma = ta.sma(close, longSmaLength)

// Plot the SMAs
plot(shortSma, title="Short SMA", color=color.blue)
plot(longSma, title="Long SMA", color=color.red)

// Detect Golden Cross and Dead Cross
goldenCross = ta.crossover(shortSma, longSma)
deadCross = ta.crossunder(shortSma, longSma)

// Plot signals on the chart
plotshape(series=goldenCross, location=location.belowbar, color=color.green, style=shape.labelup, text="Golden Cross")
plotshape(series=deadCross, location=location.abovebar, color=color.red, style=shape.labeldown, text="Dead Cross")

Explanation

  1. Indicator Declaration: The script is defined as an indicator with indicator("Golden Cross and Dead Cross of SMA", overlay=true), meaning it will be plotted on top of the price chart.

  2. Input Parameters: The lengths of the short and long SMAs are defined as input parameters with default values of 50 and 200, respectively.

  3. SMA Calculation: The short and long SMAs are calculated using the ta.sma function.

  4. Plotting SMAs: The short and long SMAs are plotted on the chart in blue and red colors, respectively.

  5. Cross Detection:

    • A Golden Cross is detected using ta.crossover(shortSma, longSma), which returns true when the short SMA crosses above the long SMA.

    • A Dead Cross is detected using ta.crossunder(shortSma, longSma), which returns true when the short SMA crosses below the long SMA.

  6. Plotting Signals:

    • When a Golden Cross is detected, a green upward-pointing label is plotted below the bar.

    • When a Dead Cross is detected, a red downward-pointing label is plotted above the bar.

Keywords

Simple Moving Average , SMA , Technical Analysis , Stock Trading , Moving Average , Price Trend , Trading Indicator , Chart Analysis , Golden Cross , Dead Cross , Crossover Strategy , Stock Market , Financial Markets , Technical Indicators , Trend Analysis , Trading Signals , Short SMA , Long SMA , Market Analysis , Price Data

Last updated