/** * Driving the engine from a chart's recompute. * * The descriptor's two calculation hooks are the engine's two entry points * almost exactly. A full recompute is a fresh engine over every bar. A tail * change is `update()` on the bar that moved and `append()` for anything after * it, which is what the engine's rollback exists for: re-executing a moving bar * gives the same numbers as executing it once, so a live chart and a backtest of * the same data agree. Nothing here recomputes anything the engine computes. * * **The tail path is refused rather than trusted.** A held engine is only usable * while it was loaded from the same settings and has executed exactly the bars * before the tail, so the settings are compared by their own spelling and the * first and last bar times are compared against what was run. Anything that does * not line up returns nothing and the chart falls back to a full recompute, * because splicing a tail onto a history that has changed underneath it draws a * wrong study that looks entirely plausible. * * **The engine never throws and a chart's calculation has to.** A failure the * engine returns becomes an error carrying that diagnostic whole, which is the * one place in this adapter where the two error models meet. */ import type { Clock, EffectRoute, EngineLimits, Grid, Instrument, Drawing, Value } from '../../core/engine/index.js'; import type { CompiledProgram } from '../../core/emit/index.js'; import type { SourceFile } from '../../core/index.js'; import type { ChartBar, ChartCalcContext, ChartSettings, ChartStore } from './contract.js'; /** What a host tells the adapter that neither the chart nor the program says. */ export interface ChartAdapterOptions { /** * The registry id, and the name a saved layout stores. * * It defaults to the program's own source hash, so the same script restores * to the same study and an edited script does not silently inherit the * settings of the one it replaced. */ readonly id?: string; /** The category a picker groups the study under, when `meta.group` is empty. */ readonly category?: string; /** * The chart library's own version: the `VERSION` string it exports, which a * host that imports the chart already holds, passed on as * `descriptorFor(program, { chartVersion: VERSION })`. * * It says which of the descriptor's hooks the chart this descriptor is * registered with will read. Two arrived after the oldest chart the peer * range accepts: a band's per-bar colour and the list of grids. From 2.5.4 * on, a band whose colour the script computes is drawn bar by bar and every * declared grid is drawn. Before it, or when nothing is stated, the adapter * cannot tell whether the chart will read either hook, and a chart that * ignores one draws a band in its own default colours or the first grid * alone, with nothing said. So such a program is refused before any bar runs * with OS6024, which is what a host that states nothing got before this * option existed. * * The version rather than a switch per hook, because it is one fact the * host already holds, it moves with the chart the host actually installed, * and a hook the adapter learns to read later needs nothing new from any * host. `capabilities.ts` has the rule, prereleases and unreadable strings * included. */ readonly chartVersion?: string; /** * What the host has stored for this study's inputs, for the declared shape. * * A declaration option may be written as an `input()`, and `docs/inputs.md` * teaches that as how a reader changes something the declaration decides: * `study("S", precision = input(2, "Places"))` puts the pane's precision on * the settings dialog. The engine resolves such a field against the settings * it was loaded with. The descriptor's declared shape is fixed before bar 0 * and is a value rather than a call, so the only settings it can be resolved * against are the ones a host states here; built without them, every one of * those fields reads the declared default and a stored value reaches none of * them. * * A host that keeps one descriptor per study instance passes that instance's * stored settings and builds again when a user changes one. A host that * registers one descriptor for a script and runs several instances against it * gets the declared shape of whatever it built with, and the parts that are * asked for again per call follow each instance: which those are is recorded * in `spec/chart-narrowings.json`, and `scripts/check-chart-surface.mjs` * measures both halves rather than leaving the sentence to be believed. * * These are the chart's own settings object, in the chart's own spelling, and * the same conversions apply as on the calculation path: a colour arrives as * a CSS string and a choice as the text of an option. */ readonly settings?: ChartSettings; /** * The plate colour a marker takes when the script named none. * * `signal`'s colour argument defaults to absence, which `stdlib.md` 14.3 * reads as the host's own default for a marker, and a chart needs a colour * for every marker it draws. A host with a marker colour in its theme states * it here; without one every unnamed marker is drawn in a neutral grey. */ readonly markerColor?: string; /** The source, so a diagnostic can carry an offset as well as a line. */ readonly source?: SourceFile; /** * Instrument facts a chart does not hold: the exchange, the lot size, and * the trading session. * * The session is where every per-bar session fact comes from * (`host-interface.md` 4.3), so a host that knows its calendar states the * hours here and the engine derives the rest. A chart holds an interval and a * timezone and no exchange calendar, so a host that states none gets a study * whose session never begins, which is the honest answer rather than a * guessed one. */ readonly instrument?: Instrument; /** * Where an applied order goes, and where its frames come back from. * * Without one the engine declares no `orders` capability and refuses a * strategy at load with OS6006, naming it. That is deliberate: a chart that * quietly swallowed a strategy's orders while drawing its plots would be a * strategy the user believes is running. * * There is no position option beside it. A strategy's position is folded from * the orders it sent and the frames this route's owner reports back * (`stdlib.md` 17.1), never handed over by the chart. */ readonly orders?: EffectRoute; /** * Run a strategy against a simulated destination where the host gives none. * * **Off by default, because the refusal it replaces is useful.** A strategy * with nowhere to send an order is refused at load with OS6006 naming the * capability, which is exactly what a host that meant to wire a destination * and forgot needs to be told. A chart that quietly filled one in for them * would draw a convincing strategy that routed nothing. * * **On, it is the venue a backtest uses.** A chart drawing a strategy then * fills the same orders at the same prices as the report of the same script, * so the marks on the price and the trades in the report are one answer * rather than two. This is what a host turns on to draw a strategy the way it * draws a study: the plots, the legend row and the settings all follow from * the program running at all. * * It does not place an order anywhere. A host that wants that supplies * `orders`, which wins over this. */ readonly simulateOrders?: boolean; readonly limits?: Partial; readonly clock?: Clock; /** * A wall clock string in the chart's zone, as UTC seconds. * * A `"time"` input is stored as a wall clock string so that a saved layout * restores to the same reading in another zone, and turning one into an * instant needs the zone's calendar, which the engine does not have. A host * passes the chart's own conversion; without one the string is read as UTC. */ readonly resolveTime?: (text: string, timezone: string) => number; } /** Every channel's whole column, for the bars that were run. */ export type Columns = readonly (readonly Value[])[]; /** * What one run produced beyond the columns. * * The grids and the drawing objects are here because neither is a column of * numbers: a grid is the buffer the last executed bar left behind, and an object * is a shape the script created and has been mutating since. They are read off * the engine while it is in hand rather than left for a caller to go back for, * because after a tail run the engine is the held one and nobody else has it. */ export interface RunOutput { readonly columns: Columns; readonly tables: readonly Grid[]; readonly drawings: readonly Drawing[]; } export declare function fullRun(program: CompiledProgram, bars: readonly ChartBar[], settings: ChartSettings, store: ChartStore, ctx: ChartCalcContext | undefined, options: ChartAdapterOptions): RunOutput; /** * The bars from `from` onwards, or nothing when the held engine cannot serve * them. * * `from` is the previously last bar, which may have been replaced rather than * followed, so it is re-executed rather than appended: that is step 1 of the bar * cycle and the reason a moving bar draws the same study however many ticks it * took. */ export declare function tailRun(program: CompiledProgram, bars: readonly ChartBar[], from: number, settings: ChartSettings, store: ChartStore, ctx: ChartCalcContext | undefined, _options: ChartAdapterOptions): RunOutput | null; /** * A held engine is dropped when the descriptor's instance goes away. * * The station goes with it, because a fetch still in flight would otherwise ask * a chart that has removed this study to recompute it. */ export declare function release(store: ChartStore): void; //# sourceMappingURL=run.d.ts.map