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.

The missing-value problem

OHLCV accessors return null when data is unavailable (e.g. before the lookback window is filled). Technical indicator functions also return NaN during their warmup period. Both values need to be handled before use in downstream math.

Function reference

na()
boolean
Zero-argument form: returns true if no value has been set yet in the current context. Rarely used directly.
na(x)
boolean
One-argument form: returns true if x is null or NaN. Use this to guard before arithmetic.
const c = ctx.close();
if (na(c)) return; // skip bars with no data
nz(x, fallback?)
number
Returns fallback (default 0) if x is null or NaN; otherwise returns x.
const rsi = ta.rsi(ctx.close, 14);
const safe = nz(rsi, 50); // use 50 as neutral during warmup

Two modes of na

// Mode 1 - zero args: check for unset state (rare)
if (na()) {
  console.log("Nothing set yet");
}

// Mode 2 - one arg: check a specific value
const v = ctx.close();
if (na(v)) {
  console.log("close is null or NaN");
}

Why NaN propagates

JavaScript NaN is infectious - any arithmetic involving NaN produces NaN:
NaN + 1     // NaN
NaN * 0     // NaN
Math.max(1, NaN)  // NaN
NaN + 1 === NaN. If you pass a NaN into your calculation chain, every downstream result will also be NaN. Use na(x) or nz(x) to break the chain early.

Practical patterns

onBar(() => {
  const rsi = ta.rsi(ctx.close, 14);

  // Pattern 1: skip the bar entirely if indicator is not ready
  if (na(rsi)) return;

  // Pattern 2: substitute a neutral default
  const safeRsi = nz(rsi, 50);

  // Pattern 3: conditional plot (NaN plots as a gap in line charts)
  plot("RSI", na(rsi) ? NaN : rsi);
});

NaN coordinates render nothing

For drawing entities, assigning NaN or null to any coordinate (x1, y1, etc.) causes the entity to render as invisible. No error is raised - it simply disappears silently.
onBar(() => {
  const ph = ta.pivothigh(ctx.high, 5, 5);
  if (na(ph)) return; // no pivot this bar - skip Line creation

  Line(`ph_${ctx.i()}`, {
    x1: ctx.i() - 5, y1: ph,
    x2: ctx.i() + 5, y2: ph,
    color: "#22c55eFF",
  });
});

Bar context

OHLCV accessors that return null during warmup.

Series

Series.get() during warmup also returns NaN.

Gotchas

Silent NaN propagation and invisible entities.