Skip to main content
Reference6 min read
Helper functions for common trading calculations and signal detection.

highest()

Find the highest value over a specified period.

Signature

highest(source: TimeSeries, period: number): number

Parameters

ParameterTypeDescription
sourceTimeSeriesSource data series
periodnumberLookback period

Example

timeseries ohlcvData = ohlcv(symbol=currentSymbol, exchange=currentExchange);
var highestHigh = highest(source=ohlcvData.high, period=20);

lowest()

Find the lowest value over a specified period.

Signature

lowest(source: TimeSeries, period: number): number

Parameters

ParameterTypeDescription
sourceTimeSeriesSource data series
periodnumberLookback period

Example

timeseries ohlcvData = ohlcv(symbol=currentSymbol, exchange=currentExchange);
var lowestLow = lowest(source=ohlcvData.low, period=20);

crossover()

Detect when one series crosses above another.

Signature

crossover(series1: number, series2: number): boolean

Parameters

ParameterTypeDescription
series1numberFirst series (the one crossing over)
series2numberSecond series (being crossed)

Example

timeseries ohlcvData = ohlcv(symbol=currentSymbol, exchange=currentExchange);
var fastMA = ema(source=ohlcvData.close, period=10);
var slowMA = ema(source=ohlcvData.close, period=20);

var goldenCross = crossover(fastMA, slowMA);  // Fast crosses above slow

crossunder()

Detect when one series crosses below another.

Signature

crossunder(series1: number, series2: number): boolean

Parameters

ParameterTypeDescription
series1numberFirst series (the one crossing under)
series2numberSecond series (being crossed)

Example

timeseries ohlcvData = ohlcv(symbol=currentSymbol, exchange=currentExchange);
var fastMA = ema(source=ohlcvData.close, period=10);
var slowMA = ema(source=ohlcvData.close, period=20);

var deathCross = crossunder(fastMA, slowMA);  // Fast crosses below slow

change()

Calculate the change from a previous value.

Signature

change(source: number, period?: number): number

Parameters

ParameterTypeDescription
sourcenumberCurrent value
periodnumberLookback period (default: 1)

Example

timeseries ohlcvData = ohlcv(symbol=currentSymbol, exchange=currentExchange);
var priceChange = change(source=ohlcvData.close, period=1);
var weeklyChange = change(source=ohlcvData.close, period=7);

valuewhen()

Get a value when a condition was true.

Signature

valuewhen(condition: boolean, source: number, occurrence?: number): number

Parameters

ParameterTypeDescription
conditionbooleanCondition to check
sourcenumberValue to retrieve
occurrencenumberWhich occurrence (0=most recent)

Example

timeseries ohlcvData = ohlcv(symbol=currentSymbol, exchange=currentExchange);
var rsiValue = rsi(source=ohlcvData.close, period=14);

// Get the price when RSI last crossed above 70
var overboughtPrice = valuewhen(crossover(rsiValue, 70), ohlcvData.close, 0);

barssince()

Count bars since a condition was true.

Signature

barssince(condition: boolean): number

Parameters

ParameterTypeDescription
conditionbooleanCondition to check

Example

timeseries ohlcvData = ohlcv(symbol=currentSymbol, exchange=currentExchange);
var rsiValue = rsi(source=ohlcvData.close, period=14);

// How many bars since RSI was oversold
var barsSinceOversold = barssince(rsiValue < 30);

print()

Output debug information to the console.

Signature

print(value: any): void

Example

var rsiValue = rsi(source=ohlcvData.close, period=14);
print(rsiValue);  // Outputs RSI value to console for debugging

Practical Examples

Donchian Channel

//@version=2

define(title="Donchian Channel", position="onchart", axis=false);

var period = input(name="period", type="number", defaultValue=20, label="Period");

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

var upper = highest(source=ohlcvData.high, period=period);
var lower = lowest(source=ohlcvData.low, period=period);
var middle = (upper + lower) / 2;

plotLine(value=upper, width=1, colors=["red"], label=["Upper"], desc=["Upper Channel"]);
plotLine(value=middle, width=1, colors=["gray"], label=["Middle"], desc=["Middle Line"]);
plotLine(value=lower, width=1, colors=["green"], label=["Lower"], desc=["Lower Channel"]);

MA Crossover Strategy

//@version=2

define(title="MA Crossover Signals", position="onchart", axis=false);

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

var fastMA = ema(source=ohlcvData.close, period=10);
var slowMA = ema(source=ohlcvData.close, period=20);

var buySignal = crossover(fastMA, slowMA);
var sellSignal = crossunder(fastMA, slowMA);

plotLine(value=fastMA, width=2, colors=["green"], label=["Fast MA"], desc=["Fast EMA"]);
plotLine(value=slowMA, width=2, colors=["red"], label=["Slow MA"], desc=["Slow EMA"]);

// Plot signals
var buyPrice = buySignal ? ohlcvData.low : na;
var sellPrice = sellSignal ? ohlcvData.high : na;

plotShape(value=buyPrice, shape="triangle", width=2, colors=["green"], label=["Buy"], desc=["Buy Signal"]);
plotShape(value=sellPrice, shape="triangle", width=2, colors=["red"], label=["Sell"], desc=["Sell Signal"]);

Price Range Analysis

//@version=2

define(title="Price Range", position="offchart", axis=true);

var period = input(name="period", type="number", defaultValue=20, label="Period");

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

var high20 = highest(source=ohlcvData.high, period=period);
var low20 = lowest(source=ohlcvData.low, period=period);
var range = high20 - low20;

// Position within range (0-100%)
var position = ((ohlcvData.close - low20) / range) * 100;

plotLine(value=position, width=2, colors=["blue"], label=["Position %"], desc=["Position in Range"]);
hline(value=80, color="red", width=1, style="dashed", label="High");
hline(value=20, color="green", width=1, style="dashed", label="Low");

Tips

Crossover Detection

Crossover functions check if the crossing happened on the current bar. Use them in conditional logic:
if (crossover(fastMA, slowMA)) {
  // Handle buy signal
}

Period Selection

For highest() and lowest(), common periods are:
  • 20 bars for short-term range
  • 52 bars for annual high/low (weekly charts)

Debugging

Use print() liberally during development to verify calculations.