Functions

Oscillators

RSI, CCI, and Stochastic — momentum oscillators that flag overbought and oversold conditions.

Overview

Momentum indicators fluctuate between bounded values to identify overbought and oversold conditions. Oscillators help time entries and exits by measuring market momentum and potential reversal points.

TypeDescription
RSI (Relative Strength Index)Measures momentum by comparing recent gains to losses. Values oscillate between 0 and 100. Overbought above 70, oversold below 30.
CCI (Commodity Channel Index)Measures deviation from a statistical mean to identify cyclical trends and extremes. Overbought above +100, oversold below -100.
Stochastic OscillatorCompares the closing price to its recent high/low range. Returns %K and %D values. Overbought above 80, oversold below 20.
FunctionDescription
rsiRelative Strength Index — momentum oscillator
cciCommodity Channel Index — deviation from statistical mean
stochasticStochastic Oscillator — closing price vs recent range

rsi - Relative Strength Index

rsi(source: TimeSeries, period?: number = 14, priceIndex?: number = 1): number — Relative Strength Index oscillator.

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

Returns: number — RSI value from 0 to 100 for the current bar.

//@version=2

define(title="Multi-RSI Strategy", position="onchart", axis=false);

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

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

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

cci - Commodity Channel Index

cci(source: TimeSeries, period?: number = 20, constant?: number = 0.015): number — Commodity Channel Index.

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

Returns: number — CCI value, typically between -100 and +100.

//@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"], fill=false, smooth=true, label=["CCI"], desc=["Commodity Channel Index"]);

stochastic - Stochastic Oscillator

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

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 and 100.

//@version=2

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

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

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

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

Best Practices

Avoid False Signals
Oscillators can remain overbought or 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 or lows but the oscillator does not, a 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.