Tutorial 2

Parent Previous Next

Tutorial 2 - Trend Prediction


There are many uses for neural networks and it is really up to your imagination on how to use them. In this next section we will create a trend classification neural network using the multiple input neural network functions but you can use the others if you have less then nine inputs and outputs combined.


SetBarsRequired(99999, 99999);
SetSeed(2);
SetMaximumEpochs(10000);
SetNetworkLayer2(30, 10);


//Trend detection (looks into the future)
Out1 = Ref(Zig(C, 5), -1) < Zig(C, 5);

for(i = 5; i < 20; i++)
{
   AddNeuralNetworkInput(VariablePeriodRSI(C, i), 0);
}
AddNeuralNetworkOutput(Out1, 0);


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


EnableProgress();
RestoreDefaults();
ClearNeuralNetworkInputs();


Once the neural network has been trained the following code will run it:


SetBarsRequired(99999, 99999);


for(i = 5; i < 20; i++)
{
   AddNeuralNetworkInput(VariablePeriodRSI(C, i), 0);
}


res = RunMultiInputNeuralNetwork("Trend_Detection");
Plot(res, _DEFAULT_NAME(), colorRed, styleLine);


EnableProgress();
RestoreDefaults();
ClearNeuralNetworkInputs();


This was a very simple example of how neural networks can be applied. Further ideas would be to use the optimizer built in to Amibroker to grow a neural network to find the smallest neural network that can accomplish the task. One can also train a neural network on multiple stocks by resuming an already trained neural network from a file but one needs to make sure that the inputs and outputs have the same scale properties. For example, a moving average will have different scaling properties between different stocks but RSI will be the same between 0 and 100 so the RSI indicator would be okay to use whereas the moving average will not.