Overview
ctx is a global object available everywhere in your script. Inside onBar it reflects the current bar being processed. All OHLCV accessors are functions that accept an optional offset argument - the number of bars to look back from the current bar.
Bar metadata
number
Total number of bars in the current window.
string
Current timeframe string, e.g.
"1H", "4H", "1D".string
Current instrument, e.g.
"BTC-USDT-SWAP".number
Current bar index. 0 = oldest bar in window,
ctx.length - 1 = newest.number
Alias for
ctx.i().boolean
true when processing the most recent (realtime) bar.OHLCV accessors
All accessors accept an optionaloffset - bars back from the current bar. offset = 0 (default) is the current bar; offset = 1 is the previous bar.
number | null
Open price.
number | null
High price.
number | null
Low price.
number | null
Close price.
number | null
Volume.
number | null
Bar open time as unix seconds (UTC).
Composite price accessors
number | null
(high + low) / 2number | null
(high + low + close) / 3 - typical price, commonly used with ta.cci.number | null
(open + high + low + close) / 4Passing accessors as sources
The OHLCV accessors are function references - they match the(offset?) => number | null signature that ta.* functions expect as their source argument:
Example: multi-bar lookback
Related pages
Inputs
input.source returns the same function shape.
Moving averages
ta.* functions that accept a source parameter.
NaN handling
What to do when ctx.close returns null.