Skip to content

STS Script Runtime

STS Script runtime is responsible for parsing, validating, compiling, and executing user scripts in a controlled environment.

Pipeline

source code
  -> preprocess
  -> parse AST
  -> require script declaration
  -> validate AST
  -> extract input metadata from AST
  -> compile
  -> run in platform runtime
  -> return result/actions/logs

Indicators and robots share the same language syntax, but they run with different capabilities.

The script declaration is the source of truth for runtime kind:

script("Title", kind="indicator")
script("Title", kind="robot")

UI and backend metadata are derived from this declaration.

Indicator Runtime

Indicator scripts calculate series and expose drawing instructions to the chart.

When an indicator script is started, the platform registers it in the chart indicator list under the CUSTOM category. The user can then add or remove it from the chart like any other indicator.

Stopping the script removes it from the custom indicator registry.

Runtime behavior:

  • calculation is performed by the chart renderer;
  • the result is cached by script code and candle signature;
  • indicators cannot call trade.*;
  • indicators can use chart drawing functions such as plot.

Robot Runtime

Robot scripts run inside a worker and interact with the terminal through a controlled bridge.

Robots do not access the platform store, DOM, tokens, or arbitrary network requests directly.

Variables And State

input values are immutable configuration constants.

var declares mutable script variables:

var counter = 0

onTick {
  counter = counter + 1
}

For indicators, var is local to one chart calculation pass.

For robots, top-level var values are stored in the worker runtime state and persist between onBar and onTick events of the same running script. The state is reset by Stop, by a new Run, and by application reload/autostart.

Events

Event When it runs
onBar After historical candles are loaded, and later for bar-based logic
onTick On each live quote event
onInit Reserved lifecycle event
onStop Reserved lifecycle event

Market Data Flow

On Run, the platform loads historical candles through:

GET /quotes/candles

The response rows:

[open, high, low, close, volume, time]

are normalized to candle objects and passed to the script worker.

Live quotes are received through the client stream and delivered to the robot runtime as onTick events. Each quote updates the latest candle or creates a new candle based on timeFrame.

Dry Run

Use dryRun = 1 to test robot logic without sending real trade requests.

input dryRun = 1

In dry-run mode:

  • trade.buy and trade.sell are written to the script log;
  • simulated positions are stored in the current runtime session;
  • trade.positionsCount includes simulated positions;
  • no real order is sent to the trading API.

Use dryRun = 0 for real trading.

Runtime Guards

Robot runtime applies guard settings before sending trade actions to the terminal.

Input Default Description
maxPositions 1 Skips buy/sell when open positions for the symbol already reached the limit
cooldownSeconds 0 Skips buy/sell while cooldown after the previous trade action is active
maxActionsPerEvent 1 Skips actions after the per-event limit is reached

These values are stored as STS Script input declarations so they are persisted with the script source and do not require extra backend fields.

Runtime Notes

  • Historical robot data is loaded from the backend, not from the active chart state.
  • Runtime settings such as symbol, timeFrame, historyCount, dryRun, and guards are extracted from AST inputs.
  • Live robot quotes are delivered through the platform event stream.
  • Run starts/registers a script.
  • Run performs parse -> validate -> extract declaration/inputs before touching runtime.
  • For existing scripts, Run updates the backend first and starts the saved source version.
  • If parser or validator fails on Run, the editor shows a validation error dialog and the script is not started.
  • Parser errors include line:column and are mirrored into Monaco markers.
  • If source is edited while the script is running, the editor marks it as edited while running; the active runtime keeps using the launched source version until the next Run.
  • Stop removes runtime subscriptions and stops the worker.
  • Autostart scripts are started after platform initialization.