CODESCRIPT

Logic

15 entries. What each one takes, what it returns, and a working example.

barssince(condition)

The number of bars since the condition was last true (na if it has not happened yet).

condition
bool
The condition searched for (a series).
NOTE

E.g. to measure how many bars have passed since the last breakout.

CODESCRIPT
plot(barssince(crossover(close, sma(close, 20))))

The number of bars since the last cross-above.

change(source, n=1)

The difference between source and its value n bars ago.

source
series
The series whose change is measured.
n
number
How many bars back to compare against (default 1).
NOTE

Measures momentum/difference; a positive value indicates an increase.

CODESCRIPT
plot(change(close, 1))

The price difference between this bar and the previous one.

cross(a, b)

true on the crossing bar.

a
series
First series.
b
series
Second series.
NOTE

crossover or crossunder; use those for direction distinction.

CODESCRIPT
plotshape(cross(close, sma(close, 20)), style="xcross", location="absolute")

A mark on every bar where price crosses SMA20 (regardless of direction).

crossover(a, b)

A boolean series that is true on the bar where a crosses above b, and false on all other bars.

a
series
First series.
b
series
Second series or a constant threshold value.
NOTE

It is true only on the bar where the cross occurs. A constant number can be given instead of b (e.g. a threshold).

CODESCRIPT
plotshape(crossover(close, sma(close, 20)), style="triangleup", location="belowbar", color="#26a69a")

A green triangle below the bar where price crosses above SMA20.

crossunder(a, b)

A boolean series that is true on the bar where a crosses below b, and false on all other bars.

a
series
First series.
b
series
Second series or a constant threshold value.
NOTE

It is true only on the bar where the cross occurs. It is the downward counterpart of crossover.

CODESCRIPT
plotshape(crossunder(close, sma(close, 20)), style="triangledown", location="abovebar", color="#ef5350")

A red triangle above the bar where price crosses below SMA20.

cum(source)

The cumulative sum from the start up to this bar.

source
series
The series to accumulate.
NOTE

E.g. cum(volume) is total volume; the basis of cumulative indicators (like obv).

CODESCRIPT
plot(cum(volume))

The cumulative total volume from the start (an ever-rising line).

falling(source, length)

True if there is an uninterrupted decrease over the last length bars.

source
series
The series being tracked.
length
number
Number of bars checked.
NOTE

length consecutive comparisons are made; if even one does not decrease, the result is false.

CODESCRIPT
plotshape(falling(close, 3), style="circle", location="abovebar", color="#ef5350")

A red circle above bars where the close fell without interruption over the last 3 bars.

highest(source, length)

A series holding the highest value over the last length bars (rolling max).

source
series
The series searched for the highest value (usually high).
length
number
How many bars to look back over.
NOTE

Commonly used in breakout rules: close > highest(high, 20).

CODESCRIPT
plot(highest(high, 20))

The highest peak of the last 20 bars (upper breakout level).

highestbars(source, length)

The distance to the highest value: 0 = current bar, −k = k bars ago.

source
series
The series being tracked.
length
number
Window length.
NOTE

Answers the question of how long ago the peak formed (gives the location of the highest value).

CODESCRIPT
plot(highestbars(high, 10))

How many bars ago the 10-bar peak was (0 = this bar, negative = past).

iff(condition, ifTrue, ifFalse)

For each bar, ifTrue or ifFalse depending on the condition (element-wise selection).

condition
bool
The condition evaluated (may be a series).
ifTrue
series
Value taken when the condition is true (color or number).
ifFalse
series
Value taken when the condition is false.
NOTE

Used in place of a ternary operator; commonly used to produce a color series.

CODESCRIPT
plot(close, color=iff(close > open, "#26a69a", "#ef5350"))

A line colored green on up bars and red on down bars.

lowest(source, length)

A series holding the lowest value over the last length bars (rolling min).

source
series
The series searched for the lowest value (usually low).
length
number
How many bars to look back over.
NOTE

Used for support breaks and tracking lows.

CODESCRIPT
plot(lowest(low, 20))

The lowest trough of the last 20 bars (lower support level).

lowestbars(source, length)

The distance to the lowest value: 0 = current bar, −k = k bars ago.

source
series
The series being tracked.
length
number
Window length.
NOTE

Answers the question of how long ago the bottom formed.

CODESCRIPT
plot(lowestbars(low, 10))

How many bars ago the 10-bar trough was (0 = this bar, negative = past).

prev(source, n=1)

A series holding the value from n bars ago.

source
series
The series whose past value is accessed.
n
number
How many bars back (default 1).
NOTE

For comparing with the previous bar: close > prev(close, 1).

CODESCRIPT
plot(prev(close, 1))

Carries the previous bar's close onto this bar (lag of 1).

rising(source, length)

True if there is an uninterrupted increase over the last length bars.

source
series
The series being tracked.
length
number
Number of bars checked.
NOTE

length consecutive comparisons are made; if even one does not increase, the result is false.

CODESCRIPT
plotshape(rising(close, 3), style="circle", location="belowbar", color="#26a69a")

A green circle below bars where the close rose without interruption over the last 3 bars.

valuewhen(condition, source, occurrence=0)

The source value on the relevant true bar of the condition.

condition
bool
The condition searched for (a series).
source
series
The series whose value is read.
occurrence
number
Which most-recent true occurrence (0 = latest, 1 = the one before).
NOTE

E.g. to remember the price at the last buy signal: valuewhen(buySignal, close, 0).

CODESCRIPT
plot(valuewhen(crossover(close, sma(close, 20)), close, 0))

Carries the close from the most recent cross-above bar as a horizontal level.