EngineSJS

SJS context and helpers

Find session state, execution APIs, and the helpers supplied to your strategy function.

The app argument has two useful groups: context owns session operations and state, while top-level arguments provide the current bar and injected functions. Destructure only the capabilities your strategy needs.

Session APIs

These signatures summarize the SJS editor contract. Optional methods must be checked before calling them; concrete execution behavior depends on the session family.

APIContract and purpose
context.instrumentThe configured session instrument
context.sjsStateStrategy-owned mutable state; initialize your own namespace
await context.getPositions()Returns position objects
await context.getOrders()Optional method returning order objects
await context.getTrades()Returns trade records
await context.placeOrder(order, options?)Requests an order; return shape varies by runtime
await context.closePositions(position, options?)Requests a close for the supplied position
await context.historicalData(instrument, startDate, endDate, interval?, backfill?)Requests a historical window using Date bounds
context.algos.find(name, id?)Reads a configured algorithm instance, if present
context.algos.byName(name)Reads instances matching a model name
context.plot(name, points, options?)Optional plotting API; points use numeric x and y

Orders and positions are runtime snapshots, not source settings. Use the getter APIs instead of assuming a cached object still describes the account. The quickstart assumes a single-instrument session; if your chosen runtime exposes broader positions, resolve the exact instrument before acting rather than copying its first-position shortcut.

Frame helpers

ArgumentPurpose
barCurrent observation with fields such as date, close, and volume
log(tag, payload?)Session-scoped diagnostic logging
skip(), skipall()Flags for subsequent runner processing
await cal(name, params?)Calls a registered L2 calculator
await pureSides(options?)Returns regime/Pine-based decision data
await rangeIntent(options?)Returns range-intent decision data
positionMemory(options?)Provides position-lifecycle memory helpers
timeFilter(...), getTimeSession(...)Evaluate time windows using the supplied frame/context

Helpers can require configured algorithms or historical data. Their presence does not establish that those dependencies are ready. Read Indicators and algorithms before turning a helper result into an order.

Capabilities SJS does not receive

The ordinary SJS/L2 scope does not include L3's modules.policies, tools, gates, or provider bag. There is no need to add L3 to perform the core strategy loop.

To let L3 oversee requests, keep the SJS entry/exit code here and configure a separate L3 source. See Working with L3.

Keep calls attached to their context

Prefer context.getPositions() over detaching the method into a variable. Context methods may rely on their owning session. Use injected log directly when you need a logging function.

The complete quickstart demonstrates initialization, optional capability checks, awaited reads, and execution calls together.

On this page