Series and history

Every value has a past. Read it without looking ahead.

close is not one number. It is a series with a value on every bar. Square brackets read earlier bars: close[1] is the previous bar's close. A value at a bar never depends on a later bar, and the test suite proves it by rewriting the future and checking the past did not move.

AlgoBarsX
prev_close = close[1]
range_high = highest(high, 20)[1]
rising = close > close[1] and close[1] > close[2]
recent = was(rsi(close, 14) < 30, within: 5 bars)
steady = held(close > ema(close, 50), for: 3 bars)
since_cross = bars_since(crosses_above(close, ema(close, 50)))
safe = nz(close[500], close)
  • was is true if a condition was true at some point in recent bars. held is true if it has been true for consecutive bars.
  • crosses_above, crosses_below and crosses detect crossings. starts and ends are true on the bar a condition becomes true or stops being true.
  • bars_since counts bars since a condition was last true.
  • Reading further back than the data goes gives na. nz supplies a fallback.
  • Reading forward is an error, not a bug waiting to happen: “close[-1] would read a future bar. History counts back from the current bar: close[1] is the previous bar.” (AS0203)
  • Indicator calls update on every bar, even when their line sits in a branch that did not run, so a reading is never stale.

Built-in price series: open high low close volume time hl2 hlc3 ohlc4, plus facts about the bar such as bar.index, bar.confirmed and bar.range. See Bar variables.

Try this in the Terminal. AlgoBars is free: $0 a month, no card needed.

Create my free account