millionchart
فارسی

Series and bars

Prices are series, one value for every bar, and `close[1]` is the previous bar's close. These examples build indicators from the definitions.

Prices are series, one value for every bar. Reading close[1] gives the close of the previous bar, and a function such as tech.ema keeps its own memory from bar to bar.

EMA pair with cross marks#

Two exponential moving averages and a mark where they cross.

Cuprel
// Two exponential moving averages and a mark where the fast one crosses the slow one.
// EMA(t) = alpha * price + (1 - alpha) * EMA(t - 1), with alpha = 2 / (length + 1).
indicator "EMA pair"
    edition = 1
    display = price

input fast_length: int = 12, name = "Fast length", min = 1
input slow_length: int = 26, name = "Slow length", min = 2
let fast = tech.ema(close, fast_length)
let slow = tech.ema(close, slow_length)
plot(fast, name = "Fast", color = color.green)
plot(slow, name = "Slow", color = color.red)
mark.shape(
    tech.crosses_above(fast, slow),
    name = "Up",
    shape = triangle_up,
    place = below,
    color = color.green,
)
mark.shape(
    tech.crosses_below(fast, slow),
    name = "Down",
    shape = triangle_down,
    place = above,
    color = color.red,
)

RSI written out#

Wilder RSI built from gains, losses and the running mean, with levels.

Cuprel
// Wilder's Relative Strength Index written out: RS = average gain / average loss and RSI = 100 - 100 / (1 + RS).
// The averages are Wilder's running means (tech.rma), the same smoothing the built-in tech.rsi uses.
indicator "RSI by hand"
    edition = 1
    display = pane

input length: int = 14, name = "Length", min = 2
let change = close - close[1]
let gain = math.max(change, 0)
let loss = math.max(-change, 0)
let average_gain = tech.rma(gain, length)
let average_loss = tech.rma(loss, length)
let strength = average_gain / average_loss
let value = 100 - 100 / (1 + strength)
plot(value, name = "RSI", color = color.purple)
level(70, name = "Overbought", color = color.red, style = dashed)
level(30, name = "Oversold", color = color.green, style = dashed)

Inside bars#

A candlestick pattern from its definition, using the value of earlier bars.

Cuprel
// A candlestick pattern from its definition: an inside bar has a high below and a low above the bar before it.
// A close above the mother bar's high after an inside bar is marked as a break.
indicator "Inside bars"
    edition = 1
    display = price

let inside = high < high[1] and low > low[1]
let broke_up = inside[1] and close > high[2]
let broke_down = inside[1] and close < low[2]
mark.char(inside, name = "Inside bar", char = "i", place = above, color = color.gray)
mark.shape(broke_up, name = "Break up", shape = triangle_up, place = below, color = color.green)
mark.shape(broke_down, name = "Break down", shape = triangle_down, place = above, color = color.red)

Bars since a high#

A state kept between bars and changed with mut-style assignment.

Cuprel
// A value the script keeps between bars: `state` starts once, `mut` changes it.
// This counts the bars since the close last made a 20-bar high, and resets it on every new high.
indicator "Bars since a high"
    edition = 1
    display = pane

input length: int = 20, name = "Length", min = 2
state count = 0
let new_high = close >= tech.highest(close, length)
if new_high
    count = 0
else
    count += 1
plot(count, name = "Bars since", style = histogram, color = color.teal)