Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.darvas.app/llms.txt

Use this file to discover all available pages before exploring further.

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.
onBar(() => {
  const close  = ctx.close();    // current bar close
  const prev   = ctx.close(1);   // previous bar close
  const change = close - prev;
});

Bar metadata

ctx.length
number
Total number of bars in the current window.
ctx.bar
string
Current timeframe string, e.g. "1H", "4H", "1D".
ctx.instId
string
Current instrument, e.g. "BTC-USDT-SWAP".
ctx.i()
number
Current bar index. 0 = oldest bar in window, ctx.length - 1 = newest.
ctx.barIndex()
number
Alias for ctx.i().
ctx.isLast()
boolean
true when processing the most recent (realtime) bar.

OHLCV accessors

All accessors accept an optional offset - bars back from the current bar. offset = 0 (default) is the current bar; offset = 1 is the previous bar.
ctx.open(offset?)
number | null
Open price.
ctx.high(offset?)
number | null
High price.
ctx.low(offset?)
number | null
Low price.
ctx.close(offset?)
number | null
Close price.
ctx.volume(offset?)
number | null
Volume.
ctx.time(offset?)
number | null
Bar open time as unix seconds (UTC).

Composite price accessors

ctx.hl2(offset?)
number | null
(high + low) / 2
ctx.hlc3(offset?)
number | null
(high + low + close) / 3 - typical price, commonly used with ta.cci.
ctx.ohlc4(offset?)
number | null
(open + high + low + close) / 4

Passing accessors as sources

The OHLCV accessors are function references - they match the (offset?) => number | null signature that ta.* functions expect as their source argument:
// Correct: pass ctx.close as a function reference
const rsi = ta.rsi(ctx.close, 14);

// Correct: input.source returns the same shape
const src = input.source("Source", "close");
const rsi2 = ta.rsi(src, 14);

// Wrong: calling ctx.close() gives you a scalar, not a source
// const rsi3 = ta.rsi(ctx.close(), 14); // TypeError

Example: multi-bar lookback

onBar(() => {
  const c0 = ctx.close();   // current close
  const c3 = ctx.close(3);  // close 3 bars ago

  // Detect 3-bar momentum
  const momentum = c0 - c3;
  plot("3-bar Momentum", momentum);

  // Log bar info on last bar
  if (ctx.isLast()) {
    console.log(ctx.instId, ctx.bar, "index:", ctx.i());
  }
});

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.