Getting Started

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.

Updated Publisher Alien_AlgorithmsPine Script v6

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.

Research runtime, not a signal generator
NeuraLib supplies model mechanics. Your features, targets, validation design, risk assumptions, and interpretation determine what the model actually learns.

A native ML runtime

Tensor engine
Scalars, vectors, matrices, reshaping, slicing, broadcasting-style operations, and differentiable graph execution.
Training stack
Backpropagation, losses, metrics, gradient clipping, optimizers, schedules, early stopping, and weight transfer.
Dataset pipeline
Flat and rolling datasets, chronological holdouts, feature builders, scaling, and train/validation extraction.
Advanced models
Optional Conv1D, LSTM, GRU, attention, Transformer, residual, Q-head, and replay components through NeuraLib_Models.

Imports

Import the main library at the top of a Pine Script v6 indicator or strategy:

Core importpine
import Alien_Algorithms/NeuraLib/1 as nl

For 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.

Core + model expansionpine
import Alien_Algorithms/NeuraLib/1 as nl
import Alien_Algorithms/NeuraLib_Models/1 as models
Why the models alias may look unused
Pine can attach exported methods from one imported library to a type exported by another. The second import registers methods such as .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.

NeuraLib Basic Modelpine
//@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 na checks.
  • 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.

NeuraLib is a machine-learning research runtime for Pine Script. Model output is not financial advice and does not guarantee future performance. © Alien_Algorithms.