Skip to content
The Chat Archive
o3-2025-04-16· OpenAI LMArena Human Preference 140K

create a tradingview pinescript indicator that adds up to 3 standard deviation bands on…

  1. User

    create a tradingview pinescript indicator that adds up to 3 standard deviation bands on a different moving average indicator.

  2. Assistant

    Here’s a ready-to-paste TradingView Pine Script (v5) that plots up to three standard-deviation “envelopes” around ANY moving-average you like (SMA, EMA, WMA, VWMA, RMA, HMA).
    You can also feed it the output of another indicator via the “Source” drop-down, so the bands don’t have to be built on price alone.

    //@version=5
    indicator(title = "MA ± 3-Sigma Bands", overlay = true, max_lines_count = 50, max_labels_count = 50)
    
    // ──────────────────────────────────────────────
    // 1. USER INPUTS
    // ──────────────────────────────────────────────
    maType          = input.string("SMA", title="Moving-Average Type",
                        options = ["SMA", "EMA", "WMA", "VWMA", "RMA", "HMA"])
    maLen           = input.int(50,  title="MA Length",          minval = 1)
    src             = input.source(close, title="Source (price or other indicator)")
    stdevLen        = input.int(maLen, title="StDev Look-back",  minval = 1)
    
    show1           = input.bool(true,  title="Show ±1 σ")
    show2           = input.bool(true,  title="Show ±2 σ")
    show3           = input.bool(false, title="Show ±3 σ")  // default off to keep chart tidy
    fillBands       = input.bool(true,  title="Fill Band Interiors?")
    colorMA         = input.color(color.yellow, "MA Color")
    
    // Optional multipliers (in case you want 0.5, 1.8, etc.)
    mult1 = input.float(1.0, "σ-multiplier #1")
    mult2 = input.float(2.0, "σ-multiplier #2")
    mult3 = input.float(3.0, "σ-multiplier #3")
    
    // ──────────────────────────────────────────────
    // 2. HELPER: CHOOSE THE MA
    // ──────────────────────────────────────────────
    ma = switch maType
        "SMA"  => ta.sma(src, maLen)
        "EMA"  => ta.ema(src, maLen)
        "WMA"  => ta.wma(src, maLen)
        "VWMA" => ta.vwma(src, maLen)                 // needs volume, auto-handled by Pine
        "RMA"  => ta.rma(src, maLen)                  // aka Wilder’s Smoothing
        "HMA"  => ta.hma(src, maLen)
        => na
    
    // ──────────────────────────────────────────────
    // 3. STANDARD-DEVIATION CALC
    //    • Using same src but you can alter stdevLen above
    // ──────────────────────────────────────────────
    σ = ta.stdev(src, stdevLen)
    
    // Pre-compute bands
    upper1 = ma + mult1 * σ
    lower1 = ma - mult1 * σ
    upper2 = ma + mult2 * σ
    lower2 = ma - mult2 * σ
    upper3 = ma + mult3 * σ
    lower3 = ma - mult3 * σ
    
    // ──────────────────────────────────────────────
    // 4. PLOTS
    // ──────────────────────────────────────────────
    // 4.1 Base MA
    plot(ma, title="Base MA", color=colorMA, linewidth=2)
    
    // 4.2 ±1 σ
    pU1 = plot(show1 ? upper1 : na, title="+1σ", color=color.new(#26a69a, 0),  linewidth=1)
    pL1 = plot(show1 ? lower1 : na, title="-1σ", color=color.new(#26a69a, 0),  linewidth=1)
    
    // 4.3 ±2 σ
    pU2 = plot(show2 ? upper2 : na, title="+2σ", color=color.new(#ef6c00, 0),  linewidth=1)
    pL2 = plot(show2 ? lower2 : na, title="-2σ", color=color.new(#ef6c00, 0),  linewidth=1)
    
    // 4.4 ±3 σ
    pU3 = plot(show3 ? upper3 : na, title="+3σ", color=color.new(#d32f2f, 0),  linewidth=1)
    pL3 = plot(show3 ? lower3 : na, title="-3σ", color=color.new(#d32f2f, 0),  linewidth=1)
    
    // 4.5 OPTIONAL FILLS
    if fillBands
        // 1-σ fill
        fill(pU1, pL1, color = color.new(#26a69a, 85))
        // 2-σ fill
        fill(pU2, pL2, color = color.new(#ef6c00, 90))
        // 3-σ fill
        fill(pU3, pL3, color = color.new(#d32f2f, 95))
    

    How to use:

    1. Paste into a new TradingView Pine Script, click “Add to Chart.”
    2. In the settings panel:
      • Pick the MA type and length.
      • Decide which σ bands to display (±1, ±2, ±3).
      • Change the “Source” to an external indicator’s plot if desired (the drop-down will list any compatible plot series from indicators already on your chart).
    3. Turn the “Fill Band Interiors” toggle on/off for cleaner visuals.

    Quick tweaks you might like: • Use request.security() if you want the bands from a higher timeframe.
    • Swap src in the stdev section with ma if you prefer “classic” Bollinger style (deviation of the MA rather than raw price).
    • Add alerts: alertcondition(close > upper2, "Price above +2σ"), etc.

    Happy coding & tight spreads!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.