NeuraLib
A tensor-based, auto-differentiating machine-learning runtime built natively for Pine Script v6, complete with neural networks, backpropagation, optimizers, datasets, and production-minded guardrails.
What is NeuraLib?
NeuraLib brings the mechanics behind modern deep-learning systems directly onto a TradingView chart. Instead of relying only on fixed formulas or static regressions, you define an architecture, feed it market features, train it on resolved outcomes, and run inference inside Pine Script.
Its core math has been parity-tested against established runtimes including Keras, TensorFlow, and PyTorch. Forward passes, gradients, losses, and optimizer behavior follow the same underlying logic used in professional machine-learning environments, adapted for Pine’s time-series execution model and resource limits.
A native ML runtime
Imports
Import the main library at the top of a Pine Script v6 indicator or strategy:
import Alien_Algorithms/NeuraLib/1 as nlFor advanced architecture blocks, import NeuraLib_Models after the main library. Import order matters because the companion library extends NeuraLib’s exported types with fluent methods.
import Alien_Algorithms/NeuraLib/1 as nl
import Alien_Algorithms/NeuraLib_Models/1 as models.lstm() and .transformerEncoder(), so the alias does not need to appear in the chain.Your first model
This complete indicator constructs a four-feature network with dropout, a hidden ReLU layer, Huber loss, and AdamW. It runs an untrained forward pass so you can verify architecture and tensor flow before adding a dataset and training loop.
//@version=6
indicator("NeuraLib Basic Model", overlay = false, calc_bars_count = 600)
import Alien_Algorithms/NeuraLib/1 as nl
var nl.Sequential model = nl.sequential("basic_model")
var float modelOutput = na
if barstate.isfirst
nl.CompileConfig cfg = nl.compileConfig()
cfg := cfg
.optimizer(nl.adamW(0.001))
.loss(nl.LossKind.huber)
.metric(nl.MetricKind.mae)
.withTrainingGate(true)
model := model
.input(array.from(4), "features")
.dropout(0.15)
.dense(8, nl.ActivationKind.relu, "hidden")
.dense(1, nl.ActivationKind.linear, "output")
.compile(cfg)
float rsiValue = ta.rsi(close, 14)
float emaValue = ta.ema(close, 21)
float atrValue = ta.atr(14)
float atrPct = close == 0.0 ? 0.0 : atrValue / close
float momentum = na(close[1]) or close[1] == 0.0 ? 0.0 : close / close[1] - 1.0
bool ready = not na(rsiValue) and not na(emaValue) and not na(atrPct) and not na(momentum)
if ready
float priceVsEma = emaValue == 0.0 ? 0.0 : close / emaValue - 1.0
nl.Tensor inputTensor = nl.vector(array.from(rsiValue, priceVsEma, atrPct, momentum), "features")
nl.Tensor outputTensor = model.predict(inputTensor)
modelOutput := outputTensor.get1d(0)
plot(modelOutput, "Untrained model output", color = color.aqua, linewidth = 2)
hline(0.0, "Zero", color = color.new(color.gray, 70))The output is intentionally untrained. The next guides show how to build leakage-safe examples, scale them, train on batches, and compare training with validation behavior.
The NeuraLib workflow
- Build a feature row from information available at that bar.
- Resolve a target later after the future outcome is known.
- Push the pair into a dataset with shape and
nachecks. - Extract a training batch while preserving a chronological validation holdout.
- Train and evaluate using the same compile configuration.
- Scale live features and predict with the trained model.
Where to go next
NeuraLib is created by Alien_Algorithms.