scripting
Tick-based scripting
Read individual executions, react with on_trade, run server alerts, and replay archived prints.
Spectra scripts can inspect and react to actual market executions—not synthetic five-second price samples. Tick data is available for crypto and Hyperliquid markets backed by an aggressor feed and trade archive.
Read the retained tape
feed.tap(feed.trades) returns a newest-first, bounded execution window:
let tape = feed.tap(feed.trades)
let biggest = tape.largest()
output biggest.side_known and biggest.usd >= 1000000
Available methods:
| Method | Result |
|---|---|
| len() | Number of retained prints |
| price(i), size(i), usd(i) | Normalized execution fields |
| is_buy(i), side_known(i), time(i) | Aggressor side and Unix-ms timestamp |
| window_ms() | Actual retained time span |
| buy_vol(), sell_vol(), delta() | Known-side volume totals |
| count_buys(), count_sells() | Known-side print counts |
| max_size(), largest() | Largest print information |
| since(ms) | Prints no older than the requested duration |
Older archive frames can have unknown aggressor side. They remain visible, but
side_known(i) is false and buy/sell aggregates treat them as neutral.
React to every print
// @spectra-script 2
let minimum_usd = param.float("Minimum USD", 5000000.0, min = 1.0)
fn on_trade(price, size, is_buy, ts) {
alert.wire(
"whale_buy",
is_buy and price * size >= minimum_usd,
message = "Large aggressive buy"
)
}
output false
Use the callback arguments for the print currently being processed. The trades tap is the host's latest retained snapshot, so during chart micro-batch replay offset zero may already be newer than that callback print. If the script also has bar logic, the execution callback runs first and the bar callback runs second at a boundary. Execution timestamps are non-decreasing; stale input is rejected without changing script state.
Server-side alerts
Choose VPS or Both, then set the alert's target symbol and timeframe. The
worker subscribes to the target's true aggressor stream and schedules prints in
50 ms bounded micro-batches. on_trade still runs once per execution in
timestamp order; batching only reduces scheduler overhead.
Every server script has an independent sandbox and retained state. CPU, memory, loop, and recursion limits contain failures. The worker applies alert cooldowns, records errors, persists fire timestamps, publishes alert events, and forwards configured outbound notifications. Bar-only scripts run only when their saved timeframe closes.
Tick backtests
In the Backtest panel choose Trades and Server archive. Spectra loads at
most the latest 24 hours and newest 10,000 valid executions, sorts them oldest
first, and calls the same sandboxed on_trade runner used live. Tick replay
requires an on_trade function; ordinary bar replay is unchanged.
Production limits
- Live retained tape: 60 seconds and 10,000 prints.
- Archive/tick replay: 24 hours and 10,000 prints.
- Server pending micro-batch queue: 10,000 prints; saturation is measured and logged instead of applying back-pressure to order matching.
- Non-crypto symbols without an aggressor archive receive an empty trades tap and an editor warning, not a runtime crash.
- Spectra Script remains spec version 2; existing bar scripts are unaffected.