Below is an example of a TradingView Pine Script (v5) indicator that computes a moving average based on a selectable MA type (SMA, EMA, WMA, or HMA) and then plots bands at ±1, ±2, and ±3 standard deviations (computed on the same source and length). You can adjust the settings via the inputs.
pinescript
//@version=5
indicator("MA with Std Dev Bands", shorttitle="MA ± StdDev Bands", overlay=true)
// ===== INPUTS =====
src = input(close, title="Source")
length = input.int(20, minval=1, title="Length")
maType = input.string("SMA", title="MA Type", options=["SMA", "EMA", "WMA", "HMA"])
// ===== SELECT MOVING AVERAGE TYPE =====
ma = switch maType
"SMA" => ta.sma(src, length)
"EMA" => ta.ema(src, length)
"WMA" => ta.wma(src, length)
"HMA" => ta.hma(src, length)
// ===== STANDARD DEVIATION =====
// Here we compute the standard deviation of the source over the same period
stdDev = ta.stdev(src, length)
// ===== CALCULATE DEVIATION BANDS =====
// Bands around the moving average
band1_up = ma + stdDev
band1_down = ma - stdDev
band2_up = ma + 2 * stdDev
band2_down = ma - 2 * stdDev
band3_up = ma + 3 * stdDev
band3_down = ma - 3 * stdDev
// ===== PLOT THE MOVING AVERAGE =====
plot(ma, title="Moving Average", color=color.blue, linewidth=2)
// ===== PLOT THE DEVIATION BANDS =====
plot(band1_up, title="1 StdDev Upper", color=color.green)
plot(band1_down, title="1 StdDev Lower", color=color.green)
plot(band2_up, title="2 StdDev Upper", color=color.orange)
plot(band2_down, title="2 StdDev Lower", color=color.orange)
plot(band3_up, title="3 StdDev Upper", color=color.red)
plot(band3_down, title="3 StdDev Lower", color=color.red)
// ===== OPTIONAL: FILL BETWEEN BANDS =====
fill(plot1=plot(band1_up, display=display.none), plot2=plot(band1_down, display=display.none), color=color.new(color.green, 90), title="1 StdDev Fill")
fill(plot1=plot(band2_up, display=display.none), plot2=plot(band2_down, display=display.none), color=color.new(color.orange, 90), title="2 StdDev Fill")
fill(plot1=plot(band3_up, display=display.none), plot2=plot(band3_down, display=display.none), color=color.new(color.red, 90), title="3 StdDev Fill")
Explanation
Inputs and MA selection:
The script lets you choose a source (default is close), a length (default is 20), and a moving average type from a list (SMA, EMA, WMA, HMA).
Based on your selection, it calculates the corresponding moving average.
Standard Deviation Calculation:
It computes the standard deviation of the same source over the specified length.
Bands Calculation:
It creates bands by adding and subtracting 1, 2, and 3 times the standard deviation from the moving average.
Plotting:
The moving average and each of the standard deviation bands are plotted.
Additionally, the script fills the area between the upper and lower bands for 1, 2, and 3 standard deviations (with a high transparency setting).
You can copy and paste this code into the TradingView Pine Script editor and add it to your chart. Feel free to adjust the settings or color schemes according to your preferences.