Skip to main content
Reference8 min read
Momentum indicators that fluctuate between bounded values to identify overbought and oversold conditions. Oscillators help time entries and exits by measuring market momentum.

Overview

FunctionRangeOverboughtOversold
rsi()0-100> 70< 30
cci()Unbounded> +100< -100
stochastic()0-100> 80< 20

rsi()

Relative Strength Index - measures momentum by comparing recent gains to losses.

Signature

rsi(source: TimeSeries, period?: number = 14, priceIndex?: number = 1): number

Parameters

ParameterTypeDescription
sourceTimeSeriesSource data for calculation
periodnumberNumber of periods (default: 14)
priceIndexnumberIndex of price data (default: 1)

Returns

number - RSI value between 0-100

Example

//@version=2

define(title="Multi-RSI Strategy", position="offchart", axis=true);

timeseries ohlcvData = ohlcv(symbol=currentSymbol, exchange=currentExchange);

// Calculate multiple RSIs
var rsi14 = rsi(source=ohlcvData.close, period=14);
var rsi50 = rsi(source=ohlcvData.close, period=50);

// Plot with different colors
plotLine(value=rsi14, width=2, colors=["purple"], label=["RSI 14"], desc=["14-period RSI"]);
plotLine(value=rsi50, width=2, colors=["orange"], label=["RSI 50"], desc=["50-period RSI"]);

// Reference lines
hline(value=70, color="red", width=1, style="dashed", label="Overbought");
hline(value=30, color="green", width=1, style="dashed", label="Oversold");

cci()

Commodity Channel Index - measures deviation from statistical mean to identify cyclical trends.

Signature

cci(source: TimeSeries, period?: number = 20, constant?: number = 0.015): number

Parameters

ParameterTypeDescription
sourceTimeSeriesSource data series
periodnumberNumber of periods (default: 20)
constantnumberScaling constant (default: 0.015)

Returns

number - CCI value, typically between -100 and +100

Example

//@version=2

define(title="CCI Strategy", position="offchart", axis=true);

timeseries trade = ohlcv(symbol=currentSymbol, exchange=currentExchange);

// Calculate CCI
var cciData = cci(source=trade, period=20, constant=0.015);

plotLine(value=cciData, width=2, colors=["blue"], label=["CCI"], desc=["Commodity Channel Index"]);

// Reference lines
hline(value=100, color="red", width=1, style="dashed", label="+100");
hline(value=-100, color="green", width=1, style="dashed", label="-100");
hline(value=0, color="gray", width=1, style="solid", label="Zero");

stochastic()

Stochastic Oscillator - compares closing price to price range over time.

Signature

stochastic(source: TimeSeries, kPeriod?: number = 14, kSmoothing?: number = 3, dPeriod?: number = 3): [number, number]

Parameters

ParameterTypeDescription
sourceTimeSeriesSource data series
kPeriodnumber%K periods (default: 14)
kSmoothingnumber%K smoothing (default: 3)
dPeriodnumber%D periods (default: 3)

Returns

[number, number] - [%K, %D] values between 0-100

Example

//@version=2

define(title="Stochastic Strategy", position="offchart", axis=true);

timeseries ohlcvData = ohlcv(symbol=currentSymbol, exchange=currentExchange);

// Calculate Stochastic
var stochData = stochastic(source=ohlcvData, kPeriod=14, kSmoothing=3, dPeriod=3);

plotLine(value=stochData, width=2, colors=["green", "red"], label=["%K", "%D"], desc=["Stochastic %K", "Stochastic %D"]);

// Reference lines
hline(value=80, color="red", width=1, style="dashed", label="Overbought");
hline(value=20, color="green", width=1, style="dashed", label="Oversold");

Tips

Avoid False Signals

Oscillators can remain overbought/oversold for extended periods in strong trends. Always confirm with price action and trend direction.

Divergence Analysis

Look for divergences between price and oscillator. When price makes new highs/lows but oscillator doesn’t, reversal may be coming.

Multiple Timeframes

Use oscillators on multiple timeframes. Higher timeframe signals are generally more reliable than lower timeframe signals.

Market Conditions

Oscillators work best in ranging markets. In strong trends, use them for timing entries rather than counter-trend trading.