DocsType Reference

Context Objects

Each lifecycle function receives a different context object. The context provides access to the APIs available at that stage.

DefineContext

Passed to define(ctx). Only has the param() method.

interface DefineContext { param(name: string, options: ParamDefinition): void; }

InitContext

Passed to init(ctx). Provides parameter values, indicator declaration, and state.

interface InitContext { readonly p: Record<string, number | string | boolean>; indicator(name: string, type: IndicatorType, options: IndicatorOptions): void; state: Record<string, unknown>; }
PropertyDescription
ctx.pRead-only parameter values (set before init runs)
ctx.indicator()Declare a host-computed indicator
ctx.stateMutable object for tracking state between bars

OnBarContext

Passed to onBar(ctx, i). Full access to indicators, price data, orders, and state.

interface OnBarContext { readonly p: Record<string, number | string | boolean>; readonly ind: Record<string, number[] | MACDResult | BBandsResult | DonchianResult | StochResult>; readonly series: Series; readonly order: OrderAPI; readonly barIndex: number; position(symbol: string): Position; state: Record<string, unknown>; }
PropertyDescription
ctx.pParameter values
ctx.indComputed indicator data arrays
ctx.seriesOHLCV price data
ctx.orderOrder execution API
ctx.barIndexCurrent bar index (same as i)
ctx.position()Current position for a symbol
ctx.stateMutable state object
contextDefineContextInitContextOnBarContextctxtypes