I notice this request is similar to the previous one, but to ensure clarity, I'll interpret it as a request to create an indicator that adds up to 3 standard deviation bands around an existing moving average indicator (rather than building a new moving average from scratch). This means the script will reference the output of another moving average indicator already on your chart, calculate the standard deviation bands, and plot them.
Since Pine Script cannot directly reference another indicator's output programmatically, you will need to manually ensure the moving average indicator is on your chart and then use this script to calculate and plot the standard deviation bands based on the same data (e.g., close price) and parameters (e.g., MA type and length) as the existing moving average indicator.
Below is a Pine Script indicator that allows you to match the settings of an existing moving average indicator and add up to 3 standard deviation bands:
pinescript
//@version=5
indicator("StdDev Bands for Existing MA", overlay=true)
// User inputs to match the existing MA indicator
ma_type = input.string(defval="SMA", title="Moving Average Type", options=["SMA", "EMA", "WMA", "VWMA"])
length = input.int(20, minval=1, title="MA Length")
src = input.source(close, title="Source")
stddev_mult = input.int(3, minval=1, maxval=3, title="Number of StdDev Bands")
// Calculate the moving average (to match the existing MA indicator)
ma = switch ma_type
"SMA" => ta.sma(src, length)
"EMA" => ta.ema(src, length)
"WMA" => ta.wma(src, length)
"VWMA" => ta.vwma(src, length)
=> na
// Calculate standard deviation of the source price
stddev = ta.stdev(src, length)
// Plot the MA (optional, uncomment if you want to see it overlaid)
plot(ma, color=color.blue, title=ma_type + " " + str.tostring(length))
// Plot standard deviation bands
plot(stddev_mult >= 1 ? ma + stddev : na, color=color.red, title="Upper 1 StdDev", linewidth=1)
plot(stddev_mult >= 1 ? ma - stddev : na, color=color.red, title="Lower 1 StdDev", linewidth=1)
plot(stddev_mult >= 2 ? ma + 2 * stddev : na, color=color.orange, title="Upper 2 StdDev", linewidth=1)
plot(stddev_mult >= 2 ? ma - 2 * stddev : na, color=color.orange, title="Lower 2 StdDev", linewidth=1)
plot(stddev_mult >= 3 ? ma + 3 * stddev : na, color=color.green, title="Upper 3 StdDev", linewidth=1)
plot(stddev_mult >= 3 ? ma - 3 * stddev : na, color=color.green, title="Lower 3 StdDev", linewidth=1)
How to Use This Indicator:
Add Your Moving Average Indicator to the Chart:
Add the desired moving average indicator (e.g., SMA, EMA, WMA, or VWMA) to your TradingView chart.
Note its settings (e.g., type, length, and source).
Add This Indicator to the Chart:
Add the above script as a new indicator on your chart.
In the settings of this indicator, match the Moving Average Type, MA Length, and Source to the settings of the existing moving average indicator on your chart.
Customize the Standard Deviation Bands:
Use the Number of StdDev Bands input to choose how many standard deviation bands to display (1, 2, or 3).
Notes:
Source Input: The Source input allows you to match the data source used by the existing moving average indicator (e.g., close, open, hl2, etc.).
Overlaying the MA: The script includes an optional plot of the moving average (currently enabled). If you already have the moving average indicator on your chart, you can comment out the plot(ma, ...) line to avoid redundancy.
Limitations: Pine Script cannot directly reference the output of another indicator. Therefore, this script recalculates the moving average internally to match the existing one. Ensure the settings match exactly for the bands to align correctly.
If you need a different approach or further customization, please let me know!