Conditional functions

ifelse (bool c, var x, var y): var

ifelse (bool c, int x, int y): int

ifelse (bool c, string x, string y): string

Returns x when the expression or condition c is true or nonzero, otherwise y.

Parameters:

c - any boolean expression.
x,y - variable, constant, or string, both of the same type.

Returns:

x when c is true, otherwise y.
 

once (bool c): int

Helper function; returns nonzero when its c parameter became true or nonzero the first time in a session or backtest. Afterwards it returns 0 regardless of Condition.

Parameters:

c - any integer or boolean expression.

Returns:

1 when called the first time with a nonzero c, otherwise 0.
 

barssince (bool c): int

Returns the number of bars or time frames since the expression or condition c was true or nonzero the last time, or -1 when it was never true. This function internally creates series and must be called in a fixed order in the script. Source code in indicators.c

Parameters:

c - any boolean expression.

Returns:

Number of bars, 0 when true on the current bar, -1 when never true.
  

valuewhen (bool c, vars Data, int n): var

Returns the Data value at which the expression or condition c was true or nonzero on the n-th most recent occurrence. This function internally creates series and must be called in a fixed order in the script. Source code in indicators.c.

Parameters:

c - any boolean expression.
Data - data series of LookBack elements.
n - number of occurrences, 1 = most recent, 2 = second most recent etc.

Returns:

Data[i] when c was true the n-th time i bars ago, otherwise 0
 

Remarks:

Examples:

var MaxOfXY = ifelse(X > Y,X,Y);         
var CloseAtLastCross = valuewhen(crossOver(Data1,Data2),seriesC(),1); 
if(once(!is(LOOKBACK))) printf("\nEnd of lookback reached!");

See also:

abs, min, max, between, sign, clamp, if

► latest version online