Neural Network Functions

Parent Previous Next

The neural networks in the toolbox come in two "flavors". 1. Walk-forward version where the neural network trains on a certain number of bars then predicts one bar ahead and then the process starts again on the next bar in a sliding window fashion. Basically, the neural network retrains and predicts on each new bar on a specified number of previous bars. 2. The second type of neural network can be trained on a number of stocks or bars and then saved to a file. This file can then be used to load the neural network and run as an ordinary indicator. Because after each training session it is saved to a file you can easily train the neural network on a number of different stocks or even a whole sector although there a few things that need to be taken into account like data normalization.


Which Neural Network Type to Choose? Walk-forward or Standard.


Walk-forward neural networks have no curve fitting issues unlike the standard training neural networks. This is because they retrain on each new bar and make a prediction before they retrain again on the next bar and make another prediction. However, there is a downside in that they are slower because they are constantly training and predicting on each new bar. Also what they can learn from the data you feed them is limited by the number of periods in the look back. They are particularly useful for predicting rapidly changing markets.


Standard neural networks can train on massive amounts of data and thus learn complex relationships in the data and generalize what they learn. But unlike the walk-forward neural networks they can suffer from curve fitting and need to be trained first before they can be used and, may need retraining once in a while when more data is available. This type of neural network is by far the most common and versatile. If you are trying to create say a data smoother by predicting data that has been smoothed, this type of neural network would be the best choice. Also, if you are creating a system by predicting an optimal trading system based on say zigzag this type of neural network would also be the best choice.


Generally you should be selecting 'Standard Training Neural Network'


Walk-Forward Neural Network Functions


NeuralNetworkIndicator3(input1, input2, output1, UniqueID, LookBack, Prediction) - There are seven versions of this function all with the same interface just a different number of inputs. They are called NeuralNetworkIndicator3...4...5 and so forth. This function is very computationally intensive because it must re-train on every bar for a specified number of LookBack periods. To get over this problem and Amibrokers quirk of recalculating all formulas on every repaint all computations by this function are cached behind the scenes. They are only recalculated when settings are changed. Each result is identified by the UniqueID so it should be different for each stock like the symbol name and quick afl needs to be turned off otherwise the plugin will not know when new data arrives among other things . To counter these problems all formulas that use neural networks need to have SetBarsRequired(99999, 99999) or SetBarsRequired(sbrAll, sbrAll) in the beginning otherwise the computation that can take minutes to calculate will need to be constantly recomputed on every chart scroll or new bar. The Prediction value determines how many bars after training the prediction should happen. For example, if you would like to predict the price 5 days into the future you need to pass ref(C, 5) as the output variable to the function and set Prediction to 5 because the last five days of the output1 are null and cannot be trained on because it is not yet known.


Standard Neural Network Functions


TrainNeuralNetwork3(input1, input2, output1, FileName) - There are seven versions of this function all with the same interface just a different number of inputs. They are called TrainNeuralNetwork3...4...5 and so forth. After training this function will save the trained neural network to a file in the Amibroker directory in WiseTraderToolbox\NeuralNetworks. These files should never be edited under any circumstances as this could lead to unpredictable behavior. This training function will also set the variable NeuralNetworkMSE to the resulting mean squared error at the end of training.


RunNeuralNetwork3(input1, input2, FileName) - Once the neural network has been trained by the above function it can be run using this function by running the saved neural network on the supplied inputs. Like the functions above there are seven versions of this function for the different number of inputs. They are called RunNeuralNetwork3...4...5 and so forth.


AddNeuralNetworkInput(input1, index) - The above training functions have a limited number of inputs. The maximum is 8 but they are included for ease of use. If you would like to train on more inputs you can use this function to add inputs to the plugin's internal memory and then train a neural network on this data using TrainMultiInputNeuralNetwork . This function can add an unlimited amount of inputs but you need to make sure to clear all inputs at the end of the AFL code using ClearNeuralNetworkInputs otherwise the plugin will continue to accumulate data in subsequent runs of the AFL. Index field is used by the Neural Network Wizard for training on multiple stocks. You should always set it to 0.


AddNeuralNetworkOutput(input1, index) - The above training functions have a limited number of outputs. The maximum is 1 but they are included for ease of use. If you would like to have more then one output you can use this function to add ouputs to the plugin's internal memory and then train a neural network on this data using TrainMultiInputNeuralNetwork . This function can add an unlimited amount of outputs but you need to make sure to clear all inputs at the end of the AFL code using ClearNeuralNetworkInputs otherwise the plugin will continue to accumulate data in subsequent runs of the AFL.Index field is used by the Neural Network Wizard for training on multiple stocks. You should always set it to 0.

ClearNeuralNetworkInputs() - Removes all inputs from the Plugins internal memory.


TrainMultiInputNeuralNetwork(FileName) - Trains a neural network on all the inputs stored in the plugins internal memory and saves the resulting neural network to a file called FileName. The saved neural network will be put in WiseTraderToolbox\NeuralNetworks in C:\Program Files\AmiBroker\. This training function will also set the variable NeuralNetworkMSE to the resulting mean squared error at the end of training and the variable TestingDataNeuralnetworkMSE to the mean squared error for the testing data.


RunMultiInputNeuralNetwork (FileName) - Runs a neural network trained using the above method. All output values are ignored and only the inputs are used. The first desired output value is returned from the function and is also put in a variable called "output0". If there are multiple outputs then they will be put in variables called output0...output1...etc. Where ouput0 is the first output and output1 is the second output etc.