You can select the type of moving average (e.g., SMA, EMA, WMA, etc.) and set its length. The script also allows you to choose how many standard deviation bands to show (1, 2, or 3).
pinescript Copy code
//@version=5
indicator("Custom MA with 1-3 SD Bands", overlay=true)
// === Input Settings ===
maType = input.string(title="Moving Average Type", defval="SMA", options=[
"SMA", "EMA", "WMA", "VWMA", "SMMA", "RMA", "TEMA", "DEMA"])
maLength = input.int(20, title="MA Length", minval=1)
numSD = input.int(2, title="Number of Standard Deviations", minval=1, maxval=3)
showBands = input.bool(true, title="Show SD Bands")
// === Moving Average Calculation ===
ma(src, len, type) =>
ta.ma(type, src, len)
baseMA = ma(close, maLength, maType)
// === Standard Deviation Calculation ===
stdDev = ta.stdev(close, maLength)
// === Band Calculations ===
upper1 = baseMA + stdDev * 1
lower1 = baseMA - stdDev * 1
upper2 = baseMA + stdDev * 2
lower2 = baseMA - stdDev * 2
upper3 = baseMA + stdDev * 3
lower3 = baseMA - stdDev * 3
// === Plotting ===
plot(baseMA, title="Base MA", color=color.orange, linewidth=2)
plotCond(cond) => cond and showBands
plot(plotCond(numSD >= 1) ? upper1 : na, title="Upper 1 SD", color=color.blue, linestyle=plot.style_dotted)
plot(plotCond(numSD >= 1) ? lower1 : na, title="Lower 1 SD", color=color.blue, linestyle=plot.style_dotted)
plot(plotCond(numSD >= 2) ? upper2 : na, title="Upper 2 SD", color=color.purple, linestyle=plot.style_dotted)
plot(plotCond(numSD >= 2) ? lower2 : na, title="Lower 2 SD", color=color.purple, linestyle=plot.style_dotted)
plot(plotCond(numSD >= 3) ? upper3 : na, title="Upper 3 SD", color=color.red, linestyle=plot.style_dotted)
plot(plotCond(numSD >= 3) ? lower3 : na, title="Lower 3 SD", color=color.red, linestyle=plot.style_dotted)
Let me know if you'd like this applied to another source (like volume), or want alerts when price crosses a band!