Neural Network Functions
This page covers the functions that actually train and run networks, as opposed to the
Set… configuration functions on the
Settings Reference. There are two ways to work: the
Standard functions (train once, run later) and the
Walk-Forward indicator (train-and-predict in one call). Each comes in
two flavours — a simple fixed-argument family and a flexible multi-input API.
Understanding the numeric suffix
Many of these functions come in numbered variants such as TrainNeuralNetwork3
or NeuralNetworkIndicator5. The number tells the function how many data
arrays you are passing, because AFL functions take a fixed number of arguments. The rule
differs slightly between the families and is spelled out in each section below. If the
fixed variants feel awkward, the multi-input API lets you add
any number of inputs without picking a numbered function.
Standard training: TrainNeuralNetwork3…10
Trains one network on a block of history and saves it to a named file. The suffix is the
total number of data arrays you pass — that is, the inputs plus one output.
So TrainNeuralNetwork3 takes 2 inputs and 1 output;
TrainNeuralNetwork10 takes 9 inputs and 1 output.
TrainNeuralNetwork3(Input1, Input2, Output, "Filename")
| Parameter | Description |
|---|---|
| Input1 … InputN | The input arrays. None may contain empty/null values. |
| Output | The target array the network learns to predict. |
| "Filename" | The name to save the trained network under (last argument). |
SetLearningAlgorithm( 4 );
SetNetworkWithActivationLayer1( 8, 1, 9 );
SetErrorAlgorithm( 0 );
SetPercentTestingData( 25 );
SetMaximumEpochs( 300 );
input1 = PercentDifference( Close, 1 );
input2 = RSI( 14 ) / 100;
target = Ref( PercentDifference( Close, 1 ), 1 ); // tomorrow's change
TrainNeuralNetwork3( input1, input2, target, "MyNetwork" );
RestoreDefaults();
Standard prediction: RunNeuralNetwork3…10
Loads a saved network and applies it to new bars. Use the variant whose number matches
the TrainNeuralNetwork you trained with, but pass only the input arrays —
no output is needed when predicting. (So RunNeuralNetwork3 pairs with
TrainNeuralNetwork3 and takes its 2 input arrays.)
RunNeuralNetwork3(Input1, Input2, "Filename")
The function returns the network's first output as an array, and also publishes each
output into the AFL variables output0, output1, and so on
(useful when a network has more than one output).
input1 = PercentDifference( Close, 1 );
input2 = RSI( 14 ) / 100;
prediction = RunNeuralNetwork3( input1, input2, "MyNetwork" );
Plot( prediction, "NN prediction", colorRed );
The number of inputs and the data length must match what the network was trained with, or the run is rejected and an empty array is returned.
The multi-input API
The numbered functions cap out at a fixed number of inputs and force you to count them. The multi-input API removes both limits: you build the data set up front by adding input, output and (optionally) amplify channels, then train or run. This is also the way to combine several separate data sets — for example several symbols — into one training run.
| Function | Description |
|---|---|
| ClearNeuralNetworkInputs() | Start a fresh data set. Call this first. |
| AddNeuralNetworkInput(Array, Index) | Add one input channel. |
| AddNeuralNetworkOutput(Array, Index) | Add one output (target) channel. |
| AddNeuralNetworkAmplify(Counts, Index) | Add an optional weighting channel that repeats each data point a whole number of times. See Input & Output Scaling. |
| TrainMultiInputNeuralNetwork("Filename") | Train on the assembled data set and save it. |
| RunMultiInputNeuralNetwork("Filename") | Run the saved network on the
assembled inputs; returns the first output and publishes output0…. |
The Index argument
Every Add… function takes a second argument, Index, that
names the data-set group the channel belongs to. For a single data set —
the common case — pass 0. Channels sharing the same index are lined up
bar-for-bar into one aligned data set; different indexes build separate data sets that are
trimmed independently and then merged for training. This lets you train one network across
several symbols: load each symbol's arrays under its own index, and the network learns from
all of them together.
// Single data set (the common case — index 0):
ClearNeuralNetworkInputs();
AddNeuralNetworkInput( PercentDifference( Close, 1 ), 0 );
AddNeuralNetworkInput( RSI( 14 ) / 100, 0 );
AddNeuralNetworkInput( StochD() / 100, 0 );
AddNeuralNetworkOutput( Ref( PercentDifference( Close, 1 ), 1 ), 0 );
TrainMultiInputNeuralNetwork( "MyNetwork" );
RestoreDefaults();
Within one index, every input and output array must be the same length. Bars where any channel is empty/null are skipped automatically, so it's fine to feed indicators that need a warm-up period.
Walk-Forward prediction: NeuralNetworkIndicator3…20
This family does training and prediction in a single call. As it walks along the chart it repeatedly trains a fresh short-lived network on the bars immediately before each bar and predicts a chosen number of bars ahead. Because each prediction only ever sees past data, the output is genuinely out-of-sample at every bar — a realistic picture of live behaviour.
By default it builds an MLP, but it also works with the sequence models (LSTM, GRU, TCN, Transformer) — set the model type before you call it and it trains and predicts that model over each trailing window. The look-back must be at least the model's sequence length / receptive field, otherwise that bar is left empty. It does not support the AFL-export feature.
The suffix here is the number of data arrays: the inputs plus one output.
After the arrays come three more arguments — a data name, the number of training bars to
look back, and how many bars ahead to predict. So NeuralNetworkIndicator3
takes 2 inputs + 1 output, then the three control arguments.
NeuralNetworkIndicator3(Input1, Input2, Output, "DataName", LookBack, DaysAhead)
| Parameter | Description |
|---|---|
| Input1 … InputN | The input arrays. |
| Output | The target array (the value to predict). |
| "DataName" | A name that identifies this data series; used as the prediction cache key. |
| LookBack | How many bars each short-lived network trains on (must be more than 2). For a sequence model it must also be at least the model's sequence length / receptive field, or the bar is left empty. |
| DaysAhead | How many bars ahead to predict (1 or more). |
SetNetworkWithActivationLayer1( 6, 1, 9 );
SetMaximumEpochs( 100 );
input1 = PercentDifference( Close, 1 );
input2 = RSI( 14 ) / 100;
target = PercentDifference( Close, 1 );
pred = NeuralNetworkIndicator3( input1, input2, target, Name(), 200, 1 );
Plot( pred, "Walk-forward NN", colorBlue );
RestoreDefaults();
Cache busting with SetVersion
To stay fast, the Walk-Forward indicator caches each bar's prediction keyed on the data
name and your settings. If you change a setting the cache is invalidated automatically.
But if you change something the cache can't detect — for example the contents of your
input formulas — bump SetVersion(n) to a new number to force every bar to be
recomputed.
Reading the training error: NeuralNetworkMSE & TestingDataNeuralNetworkMSE
After a Standard training call, the toolbox publishes two AFL variables you can read:
NeuralNetworkMSE— the mean squared error on the training data.TestingDataNeuralNetworkMSE— the error on the held-out test data (only produced when you reserve test data withSetPercentTestingData).
Comparing the two is how you detect overfitting — see Accuracy & Avoiding Overfitting.
Exporting a network as AFL: EnableNetworkToAFL
By default a trained network is saved as an internal file you load with
RunNeuralNetwork…. If you call EnableNetworkToAFL() before
training a Standard MLP, the toolbox also writes the trained network out as a standalone
AFL formula — its weights and activations expressed as plain AFL — so you can inspect it
or run the prediction without the multi-input loader. DisableNetworkToAFL()
turns the feature back off.
The export only applies to a plain numeric MLP. It is automatically skipped
(with a trace message) whenever the configuration can't be written as a formula: any
sequence model (LSTM, GRU, TCN, Transformer), LayerNorm
switched on, categorical input embeddings
declared, or an ensemble size greater
than 1. In those cases the network is still saved and runs normally through
RunNeuralNetwork… — only the formula export is skipped.
Every trained network is saved to a single on-disk file with a WTTNN
header that covers all model types — MLP, LSTM, GRU, TCN, and Transformer alike.