pinescript Copy code
//@version=5
indicator("Custom Deviation Bands", overlay=true)
// === INPUTS ===
maType = input.string(
title="Moving Average Type",
defval="SMA",
options=["SMA", "EMA", "WMA", "VWMA", "SMMA", "RMA", "TEMA", "DEMA"]
)
maLength = input.int(title="Moving Average Length", defval=20, minval=1)
stdDevLength = input.float(
title="Standard Deviation Bands (1-3)",
defval=2.0,
minval=0.1,
maxval=3.0,
step=0.1
)
showMa = input.bool(title="Show Moving Average Line", defval=true)
showBands = input.bool(title="Show Deviation Bands", defval=true)
maColor = input.color(title="MA Line Color", defval=color.blue)
bandColor = input.color(title="Band Color", defval=color.orange)
// === FUNCTIONS ===
getMa(src, length, type) =>
switch type
"SMA" => ta.sma(src, length)
"EMA" => ta.ema(src, length)
"WMA" => ta.wma(src, length)
"VWMA" => ta.vwma(src, length)
"SMMA" => ta.rma(src, length) // Same as SMMA
"RMA" => ta.rma(src, length)
"TEMA" => ta.tema(src, length)
"DEMA" => ta.dema(src, length)
// === CALCULATIONS ===
src = close
ma = getMa(src, maLength, maType)
stdDev = ta.stdev(src, maLength)
upperBand = ma + stdDev * stdDevLength
lowerBand = ma - stdDev * stdDevLength
// === PLOTTING ===
if showMa
plot(ma, title="Moving Average", color=maColor, linewidth=2)
if showBands
plot(upperBand, title="Upper Band", color=bandColor, linewidth=1, style=plot.style_dashed)
plot(lowerBand, title="Lower Band", color=bandColor, linewidth=1, style=plot.style_dashed)
Let me know if you want to add alerts, background shading, or integrate this into a strategy! 🚀