Skip to content

StScript ML Classifier

The ml namespace provides a bounded, client-worker nearest-neighbor classifier. It is an independent causal implementation, not an identical port of the Pine ANN heuristic or the closed Premium indicator. The numeric vote is not a calibrated probability of a profitable trade.

script("Lorentzian example", kind="indicator")
rsi = ta.rsi(close, 14) / 100
cci = ta.normalize(ta.cci(close, 20))
ml.feature("direction", "rsi", rsi)
ml.feature("direction", "cci", cci)
prediction = ml.predict("direction", close, 8, 1500, 4, 8)
count = ml.samples("direction")
plotText(low, when=isClosed and prediction > 0, text="Bullish", color="#76F7B8", position="below")

Calls

All arguments are positional.

API Result
ml.feature(model, name, value) Registers a scalar feature for the current bar; returns value
ml.predict(model, source, neighbors, lookback, horizon, stride) Integer vote in -k..k, or null until k valid neighbors exist
ml.samples(model) Valid neighbor count of the latest prediction, at most k

Register every feature on every bar, including during warmup, then call predict exactly once per bar starting at bar zero. Keep the feature schema and model options fixed during a replay. Missing/non-finite features are missing observations, never zero-imputed. Strings are not feature values. Names match [A-Za-z][A-Za-z0-9_]{0,63}. Up to two models and 15 features per model are supported. Calls are not allowed inside loops or robot events; robot scripts may calculate ML series at top level and read them from events.

Training labels and distance

A candidate at index j receives label sign(source[j+horizon] - source[j]) only once j+horizon <= current bar. The current bar is never used as a candidate with an unknown future outcome. Candidates with missing features or source/outcome are ignored.

Distance is the sum of log(1 + abs(currentFeature - candidateFeature)). Candidates with the smallest distance vote -1, 0 or +1. Equal distances prefer more recent observations.

Parameters are fixed integers: neighbors 1..64, lookback 1..2000, horizon 1..100, stride 1..2000. Search starts at current-horizon, then goes backwards to max(0,current-lookback). Effective stride is max(stride, ceil(lookback/256)), limiting each prediction to at most 256 candidates. A large neighbor count with too few candidates continues to return null.

Replay and resources

Each model uses a dedicated 2101-row ring store, separate from array.* limits. Native distance work has a shared budget of 50 million feature-distance components per evaluation. Existing DSL iteration limits and the 5-second worker deadline remain in force.

An indicator runner caches searches over unchanged history. OHLCV/time/bid/ask corrections invalidate the changed bar and all later results. Appending/truncating history also invalidates the old forming bar because isClosed changes. Source changes create a new runner. Other DSL expressions and technical features still replay sequentially; this is not arbitrary incremental execution of the entire language. Cache state is local to a worker and is not saved or shared across charts/accounts.

See TA features, example and chart colors.