[PyneTS runtime]

Runtime.run envelope: na is null, series lookback, call-site TA, request.security without data → na, stream and providers.

PyneTS runtime

Abstract

Runtime is the TypeScript counterpart of pynescript.runtime.Runtime. It is not a port of runtime/host.py line-by-line; it matches the public envelope: run(source, ohlcv) → plots / series / events / drawings. Default mode is interpret (AST walk). compile / auto, stream, runProvider, and registerLibrarySource are on @hoox-sh/pynets 0.2.0, including this repo’s pynets/ pin. Python Runtime remains the oracle.

There is no Runtime.evaluate.

Conceptual model

Interface surface

new Runtime(symbol = "AAPL", options?: RuntimeOptions)

runtime.run(source, ohlcv, extra?: RuntimeOptions | InputOverrides): RuntimeResult
runtime.stream(source): RuntimeStream
runtime.runProvider(source, provider, extra?): Promise<RuntimeResult>
runtime.registerLibrarySource(namespace, name, version, source): void

RuntimeOptions

FieldMeaning
inputsinput.* overrides (Record<string, number | string | boolean>)
broker{ commission?, slippage?, pyramiding? }
timeframeChart TF string, or null
librariesLibraryRegistry instance
modeinterpret (default) | compile | auto

Per-call extra merges over the constructor. A plain object of input values (no mode / broker / …) is treated as InputOverrides.

OHLCVBar

All fields optional: open, high, low, close, volume, time. Missing numeric fields become na at use.

RuntimeResult

FieldMeaning
seriesTitled plot series → Array<number | null>
plotsFirst / default plot column
plot_meta{ title }[]
countBar count consumed
script_name / script_typeFrom indicator / strategy
mode"interpret" or "compile" (what actually ran)
auto_backendWhen mode: "auto"
compile_fallback_reasonWhy auto fell back
eventsStrategyEvent[]
fillsBroker fills
drawingsline / label / box / …
logslog.* records
strategyBook scalars when the script is a strategy
error / error_kindSoft failure (parse | runtime | …)

Parse and runtime failures return an error envelope; they do not throw from run (except unexpected host bugs). interpret(source, bars) (helper) does throw if out.error.

Stream

const s = new Runtime("AAPL").stream(src);
s.on("bar", (out) => { /* full re-eval on bars so far */ });
s.on("error", (err) => {});
s.push({ close: 10 });
s.push({ close: 11 });
s.close();

Each push re-runs the script on all bars so far (Python-shaped, not incremental-only).

Providers

import { Runtime, MemoryProvider } from "@hoox-sh/pynets";

const provider = new MemoryProvider({
  AAPL: [{ close: 1 }, { close: 2 }, { close: 3 }],
});
const out = await new Runtime("AAPL").runProvider(src, provider, { limit: 3 });

Also: StaticMapProvider, JsonBarProvider. request.security without a matching feed stays na.

Internals

PathRole
src/runtime/interpret.tsHost, Runtime, interpretTree
src/runtime/series.tsNA, PineSeries
src/runtime/ta.tsIncremental TaEngine
src/runtime/strategy.tsBroker, fills, risk, OCA
src/runtime/request.tsrequest.security policy
src/runtime/library.tsIn-process import ns/Name/ver
src/runtime/provider.tsBar providers
src/runtime/drawings.tsDrawing book + GC

Invariants & edge cases

  1. na is null. A non-finite number in or out becomes na.
  2. na == na is true. Any other comparison involving na is false.
  3. Lookback: close[1] is the previous bar; OOB → na.
  4. Call-site TA. Two ta.sma(close, 14) at different AST nodes do not share state.
  5. Foreign / HTF request.security without data → na. Same-symbol simple OHLCV may passthrough; the chart is never invented as another ticker.
  6. v3/v4 bare aliases (sma, ema, rsi, …) resolve when Python does.
  7. Libraries: registerLibrarySource then import namespace/Name/version. Unresolved aliases stub to na (soft), they do not crash the run.

Worked examples

na and lookback

const out = new Runtime("TEST").run(
  `indicator("t")
plot(close[1])
plot(na == na ? 1 : 0)`,
  [{ close: 10 }, { close: 20 }],
);
// last bar: plots[0] === 10, plots[1] === 1

Inputs and strategy

const out = new Runtime("AAPL", {
  inputs: { len: 5 },
  broker: { commission: 0.001, pyramiding: 1 },
}).run(src, bars);

if (out.error) throw new Error(out.error);
console.log(out.events, out.fills, out.strategy);

Failure modes

SymptomCauseFix
error_kind: "parse"Grammar failureparse(source) / pynets check
All request.security values nullNo foreign feedExpected. Pass a provider or accept na
Plots off-by-one vs TVLookback / na policyCompare to Python, not to TradingView
Stream looks "slow"Full re-eval each pushExpected host model
mode in the result is interpret after autoCompile ineligible / errorRead compile_fallback_reason

See also