TradingView indicator calculations

Rainbow Oscillator: smoothing layers and normalization

The Rainbow Oscillator compares the current close with the mean of successively smoothed price layers, then divides that distance by a recent closing-price range. This page uses ten cascaded two-bar SMAs and an 11-close range. The exact construction matters: other scripts called Rainbow use different smoothing, denominators or even RSI and stochastic calculations.

See the decision guide

Separate the algorithm from the Rainbow name

The reference is Mel Widner's 1997 article, "Rainbow Charts." We implement its ten two-point smoothing passes and its inclusive current-through-ten-bars-ago closing range. Identify a TradingView script by author and source before assuming its values match this construction.

  • GMMA compares groups of moving averages with different lengths. Here every stage has length two and receives the previous stage, so changing the name or colors cannot make the constructions equivalent.
Version or familyCalculation to identifyComparison rule
This page: ten-layer SMA oscillatorRepeated two-bar SMA; close minus layer mean; 11-close rangeUse the formulas and fixture below.
A modified Rainbow OscillatorAny changed source, SMA length, layer count or normalization windowRecord every choice. A ten-bar range is not the eleven-close range used here.
Rainbow Adaptive RSI by LuxAlgoAn adaptive moving average feeds an RSI oscillator, with a trigger lineIts RSI values and crosses are a separate calculation.
Stochastic Rainbow by shayankmEight stochastic oscillators with different speedsIts colored lines do not represent these ten price averages.

Define layers, range and output separately

Set A0[t] = close[t]. For k = 1 through 10, Ak[t] = (A(k−1)[t] + A(k−1)[t−1]) / 2. Let M be the mean of A1 through A10. Let D be the highest close minus the lowest close across t through t−10, inclusive. RO = 100 × (close − M) / D; bandwidth RB = 100 × (max(A1…A10) − min(A1…A10)) / D.

  • Use closing prices for D, not candle highs and lows. The distinction changes the units of comparison even if the smoothing layers match.
  • With consecutive available closes, the tenth layer first exists on the eleventh observation. This example does not fill missing startup layers with zero.
  • When D = 0, return unavailable for RO and RB. A perfectly flat window has no defined normalized ratio; zero would be an added display convention.

Reproduce every layer on an eleven-close ramp

Assume eleven hypothetical closes in chronological order: 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110. Each row below is the final value after applying the two-bar average again. These invented prices are a calculation fixture, not market observations.

  • M = (109.5 + 109 + … + 105) / 10 = 107.25. D = 110 − 100 = 10. RO = 100 × 2.75 / 10 = 27.5.
  • RB = 100 × (109.5 − 105) / 10 = 45. The displayed bandwidth lines are +45 and −45.
  • Using only the last ten closes gives D = 110 − 101 = 9, RO = 30.5556 and RB = 50. A one-bar window difference can explain a numerical mismatch.
LayerFinal valueDelay on this one-unit-per-bar ramp
A1109.50.5 bar
A21091 bar
A3108.51.5 bars
A41082 bars
A5107.52.5 bars
A61073 bars
A7106.53.5 bars
A81064 bars
A9105.54.5 bars
A101055 bars

Smoother layers do not imply a quieter oscillator

The layer average on the ramp trails price by 2.75 bars, while A10 trails by five bars. Those are exact ramp relationships, not promises about signal delay on arbitrary prices. RO still subtracts the smoothed reference from the unsmoothed current close, so a sudden close can move RO sharply.

  • In the jump example, Ak = 100 + 10/2^k. The sum of the ten increments is 10 × (1 − 1/1024), which gives M = 100.9990234375. This is an independent check of the table.
  • For the stated 11-close denominator, each layer and its mean lie inside that same close range. Therefore |RO| ≤ 100 and 0 ≤ RB ≤ 100 when D > 0. This does not imply |RO| ≤ RB; the jump is a counterexample.
  • Increasing smoothing delays the reference. Adding an extra smoother to RO changes the oscillator and may delay crosses. Compare timing and false signals on a specified dataset instead of treating visual smoothness as evidence of trading quality.
Hypothetical close sequenceFinal layer behaviorRO / RBWhat it demonstrates
Ten closes at 100, then 110A1 = 105; A10 = 100.009765625; M = 100.999023437590.0098 / 49.9023RO can exceed the positive bandwidth line.
Alternating 100 and 101, with enough history; latest close 101All ten layers equal 100.5+50 / 0Layer convergence can coexist with a large oscillator value.
Same alternating sequence; latest close 100All ten layers still equal 100.5−50 / 0Zero crossings can alternate every bar in a sideways range.
Eleven closes all at 100All layers equal 100; D = 0Unavailable / unavailableFlat prices require an explicit denominator guard.

Define a sideways zone before using it as a filter

RO near zero means the close is near M relative to D. Low RB means the smoothed layers converge. Neither condition alone identifies every sideways market, as the alternating-price fixture shows. A threshold is a chosen decision rule, not part of the oscillator formula.

  • For a reproducible research rule, define a neutral state as |RO| ≤ 5 and RB ≤ 10 for three consecutive completed bars. These thresholds and duration are illustrative, not recommended or optimized.
  • That rule rejects the alternating 100/101 fixture despite its narrow trading range, because |RO| = 50. If the task is to find a small price range, inspect D in price units or D/close as a separate measure.
  • An old extreme leaving the 11-close window changes D and can change normalized readings without a new breakout. Compare the numerator and denominator separately.
  • A closed-bar cross can still reverse on the next bar. Entry, exit, position size and transaction costs require separate rules before evaluating trades.

Inspect the calculation in Pine Script v6

This indicator plots RO and ±RB in a separate pane. Its Data Window exposes all ten layers, M and D. A pale band marks RO from −5 to +5; a darker background marks completed bars meeting the three-bar neutral rule above. The optional alert detects a completed-chart-bar cross above zero, independently of that neutral rule. It places no orders.

  • Paste the source into Pine Editor, add it to a standard-candle chart, and compare completed timestamps with your reference implementation. Keep symbol, session and interval identical.
  • The live plot can change until close. The alert uses barstate.isconfirmed; select Once Per Bar Close when creating it. No future offset or higher-timeframe request is used.
  • Check a ramp, jump, alternating sequence and flat window in a separate numerical fixture. A chart with unrelated prices will not reproduce the invented table.
  • A syntax check verifies compilation diagnostics, not agreement with another script or profitable behavior. Compare the ten intermediate values before adding trading logic.
Fixed ten-layer, two-bar SMA implementation with an 11-close denominator. Values remain unavailable until the layers exist and the denominator is positive.pine
//@version=6
indicator("Rainbow oscillator: ten SMA layers", overlay = false)
a1 = ta.sma(close, 2)
a2 = ta.sma(a1, 2)
a3 = ta.sma(a2, 2)
a4 = ta.sma(a3, 2)
a5 = ta.sma(a4, 2)
a6 = ta.sma(a5, 2)
a7 = ta.sma(a6, 2)
a8 = ta.sma(a7, 2)
a9 = ta.sma(a8, 2)
a10 = ta.sma(a9, 2)
meanLayer = (a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 + a10) / 10
layerHigh = math.max(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
layerLow = math.min(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
closeRange = ta.highest(close, 11) - ta.lowest(close, 11)
ready = not na(a10) and closeRange > 0
ro = ready ? 100 * (close - meanLayer) / closeRange : na
rb = ready ? 100 * (layerHigh - layerLow) / closeRange : na
plot(ro, "RO", color = color.blue, linewidth = 2)
plot(rb, "Positive bandwidth", color = color.gray)
plot(-rb, "Negative bandwidth", color = color.gray)
hline(0, "Zero")
upperNeutral = hline(5, "Illustrative neutral upper", color = color.new(color.gray, 60))
lowerNeutral = hline(-5, "Illustrative neutral lower", color = color.new(color.gray, 60))
fill(upperNeutral, lowerNeutral, color = color.new(color.gray, 95))
neutralNow = ready and math.abs(ro) <= 5 and rb <= 10
neutralThree = neutralNow and neutralNow[1] and neutralNow[2]
bgcolor(barstate.isconfirmed and neutralThree ? color.new(color.gray, 85) : na, title = "Three-bar neutral condition")
plot(a1, "Layer 1", display = display.data_window)
plot(a2, "Layer 2", display = display.data_window)
plot(a3, "Layer 3", display = display.data_window)
plot(a4, "Layer 4", display = display.data_window)
plot(a5, "Layer 5", display = display.data_window)
plot(a6, "Layer 6", display = display.data_window)
plot(a7, "Layer 7", display = display.data_window)
plot(a8, "Layer 8", display = display.data_window)
plot(a9, "Layer 9", display = display.data_window)
plot(a10, "Layer 10", display = display.data_window)
plot(meanLayer, "Layer mean", display = display.data_window)
plot(closeRange, "Close range", display = display.data_window)
crossUp = ta.crossover(ro, 0)
alertcondition(barstate.isconfirmed and crossUp, "RO close above zero", "Rainbow oscillator crossed above zero on a completed chart bar.")

Give Pineify a calculation contract

The Pine Script AI Coding Agent can help draft and revise a script from explicit requirements. Supply the recurrence, eleven-close denominator, unavailable-value policy and closed-bar rule, then ask it to preserve the diagnostic plots while making your requested changes.

  • Use the ramp result of RO = 27.5 and RB = 45 as expected values. Require the flat-window guard and the alternating-price counterexample to remain understood after edits.
  • Compare the result with TradingView values before adding alerts or strategy orders. Request an implementation of the public formula rather than a copy of inaccessible proprietary source.
Where Pineify fits

Pine Script AI Coding Agent

Draft or revise the stated Rainbow Oscillator calculation, inspect its intermediate values, and define when an alert becomes eligible.

Draft a Rainbow audit script

Frequently asked questions

Educational calculations, not investment advice. All numerical examples are hypothetical. Smoothing, a threshold crossing or syntax validation does not establish future returns.

Sources and verification

Published by Pineify. Pineify is an independent product and is not affiliated with TradingView. Sources support the technical explanations; examples do not establish trading performance.