Examples
Run small calculation-only scripts and inspect their output before adding execution.
These examples observe calculations or configuration. They do not submit orders or disable other execution layers. Use an experiment without other order-producing code when inspecting them.
Compare two time-window averages
This complete SJS script compares registered SMA calculations over three and ten minutes. It logs only when the ABOVE, BELOW, or EQUAL relationship changes. Missing or invalid values clear the previous relationship, so recovery is visible as a new observation.
export async function app({ context, bar, cal, log }) {
if (!bar?.instrument || bar.date == null || typeof cal !== "function") {
return "waiting: inputs unavailable";
}
const timestamp = new Date(bar.date).getTime();
if (!Number.isFinite(timestamp)) return "waiting: invalid timestamp";
const state = (context.sjsState ??= {}).averageComparison ??= {};
const fast = await cal("sma", { minutes: 3 });
const slow = await cal("sma", { minutes: 10 });
if (![fast, slow].every(value => typeof value === "number" && Number.isFinite(value))) {
state.previous = undefined;
return "waiting: averages unavailable";
}
const relationship = fast > slow ? "ABOVE" : fast < slow ? "BELOW" : "EQUAL";
if (relationship !== state.previous) {
log?.("average-comparison", { timestamp, fast, slow, relationship });
}
state.previous = relationship;
return relationship;
}For pairs (fast, slow) of (102, 100), (103, 100), and (99, 100), the script returns ABOVE, ABOVE, BELOW and logs the first and third relationships. (0, 0) returns EQUAL; neither zero is treated as missing.
The script does not detect completed bars, enforce monotonic timestamps, or prove historical coverage. It has no asynchronous overlap guard. Use it as a small serialized observation walkthrough, not a ready-made order strategy. A changed relationship after a missing-value interval is not evidence of a crossover through that interval.
Inspect configured instance selection
After loading the configuration example, this complete SJS script checks for the exact configured instance. It does not claim its calculations are ready:
export async function app({ context, log }) {
const model = context.algos?.find?.("sessionRegime", "fast");
if (!model) return "waiting: sessionRegime fast is not configured";
log?.("configured-model", { name: "sessionRegime", id: model.id });
return "configured";
}Run it over a short period because it logs on each invocation. Finding an instance establishes configuration presence only. Read the model-specific state contract before turning that result into a decision.
For a complete strategy with timestamp handling, a local rolling window, order guards, and entry/exit requests, use your first SJS strategy.
Continue with debugging.