WiseTrader Toolbox

Neural Networks for Amibroker (AFL)

Written by Administrator
PDFPrintE-mail

The WiseTrader toolbox adds advanced neural network predictive technology to the Amibroker platform. Coupled with Amibroker's powerful formula language you can now create intelligent trading systems powered by advanced neural networks.

The WiseTrader toolbox adds two different kinds of neural networks to the Amibroker platform: 1. The traditional neural networks that are trained on a fixed number of bars 2. The adaptive walk-forward version which retrains on each new bar. The WiseTrader toolbox also comes with two different learning algorithms: standard momentum back-propagation and a more advanced adaptive quick-propagation learning algorithm with faster convergence. The neural networks are accessible via some simple AFL function calls and come with complete documentation to get you started.

With the WiseTrader toolbox you can easily turn lagging indicators into smooth leading indicators. Download the following example of a leading RSI indicator created with the toolbox and try it out for yourself:

Download 1 or Download 2

Adaptive Walk-Forward Neural Networks

The following AFL demonstrates how the adaptive neural networks can be used to predict the closing price of a stock one bar ahead. Note that this is a simple example meant only to demonstrate how the adaptive neural networks work. A better prediction would use other market indices and maybe economic data to get a more accurate prediction.

//Use all bars
SetBarsRequired(99999, 99999);
//Use the adaptive learning algorithm
SetLearningAlgorithm(1);

//Set our inputs
i1 =
C;
i2 =
Ref(C, -1);
i3 =
Ref(C, -2);
i4 =
Ref(C, -3);
i5 =
Ref(C, -4);
i6 =
Ref(C, -5);
i7 =
Ref(C, -6);
i8 =
Ref(C, -7);
//Set our desired output (One bar into the future)
O1 =
Ref(C, 1);

//Train and compute an adaptive neural network
res =
NeuralNetworkIndicator9(i1, i2, i3, i4, i5, i6, i7, i8, O1, FullName(), 100, 1);
Plot(res, _DEFAULT_NAME(), colorRed, styleLine);

//Calculate accuracy for the recent 100 bars
Title = "Accuracy: " + (Sum(IIf((Ref(C, 1) > C) == (res > Ref(res, -1)), 1, 0), 100));

//Clean up
EnableProgress();
RestoreDefaults();
ClearNeuralNetworkInputs();

When the above formula begins training you should see the following progress dialog box to tell you the progress of the computation:

Neural Network Training

You can stop and resume training at any moment because all adaptive neural network calculations are stored in the plugin's internal memory and only compute as much as is needed so it can be used in your real-time trading as the neural network will only train and predict on the latest bar.

General Neural Networks

The other neural network functions allow you to train a neural network and save it to a file to run later or even generate AFL code directly. The ability to convert a trained neural network to AFL code is the first of its kind not available anywhere else.  The next formula is a simple example of creating a trend detection indicator using neural networks.

//Use all available data for training
SetBarsRequired(99999, 99999);

//Set the seed value of the neural network
//The same seed value will produce the same neural network each time
SetSeed(20);
//Use the adaptive learning algorithm
SetLearningAlgorithm(1);
//Train the neural network for 2000 iterations
SetMaximumEpochs(2000);
//Set the number of hidden layers in the neural network
SetNetworkLayer2(20, 20);

//Trend detection (looks into the future). This is what we want to predict.
//You can set whatever you desire here. Anything you can imagine you can use here
//because the trained neural network won't look into the future.
Out1 =
Ref(Zig(C, 10), -1) < Zig(C, 10);

//Add the inputs for the neural network. Using this method you can add
//as many neural network inputs as you want
for(i = 5; i < 20; i++)
{
AddNeuralNetworkInput(VariablePeriodRSI(C, i));
AddNeuralNetworkInput(VariablePeriodRSI(Ref(C, -1), i));
AddNeuralNetworkInput(VariablePeriodRSI(Ref(C, -2), i));
}
//Last input added is our desired output or the trend
AddNeuralNetworkInput(Out1);

//Delete neural network if there is already another
fdelete("WiseTraderToolbox\\NeuralNetwork\\Trend_Detection");
//Begin training the neural network
TrainMultiInputNeuralNetwork("Trend_Detection");

//Clean up the added inputs and restore default values
EnableProgress();
RestoreDefaults();
ClearNeuralNetworkInputs();

When you run the above formula you should see a training progress dialog box. When the above formula finishes executing you can use the generated AFL version of the indicator to run the neural network or just run the neural network from file. The next example will execute the neural network from file because the generated formula is too big to post here.

//Add the neural network inputs
for(i = 5; i < 20; i++)
{
AddNeuralNetworkInput(VariablePeriodRSI(C, i));
AddNeuralNetworkInput(VariablePeriodRSI(Ref(C, -1), i));
AddNeuralNetworkInput(VariablePeriodRSI(Ref(C, -2), i));
}

//Run the neural network from file
res =
RunMultiInputNeuralNetwork("Trend_Detection");
//Plot the neural network result.
Plot(res, _DEFAULT_NAME(), colorRed, styleLine);

Conclusion

The above are very simple examples of how the neural networks can be used. With the Amibroker platform and WiseTrader toolbox almost everything other neural network platforms can do can be done and more. For example, it is simple to use the optimizer to grow neural networks and find the best performing architecture. You can also try and create zero lag indicators by shifting them forward and trying to predict the result.