Creating a Trading System Using Neural Networks

Creating, Trading System, Using, Neural, Networks, Trading Systems, fx trader, forex neural_network

Machine learning has become incredibly popular during the past decade with the advent of better algorithms and enough computational power to tackle even the most demanding problems. Today machine learning algorithms solve problems in many areas where complex relationships between variables are presents and this makes machine learning a potentially viable tool for the creation of trading strategies. But how can we create a trading system using this type of technology? On this article we are going to learn how to use a basic machine learning algorithm – called a neural network – to create a simple trading system on the EUR/USD.

All coding fragments are samples taken from our F4 programming framework, available at Asirikuy.com. The open source Shark library is used for the creation and training of the machine learning algorithms. However the general ideas and algorithmic notions put forward within this article can be translated to other libraries and programming languages.

What is a Neural Network?

A neural network is a type of machine learning algorithm. The simplest classic neural network is composed of an input layer, a hidden layer and an output layer, where each layer contains a given number of “neurons”.   Each neuron in the input layer gets a value, processes it using a function and passes it to one or several neurons in the hidden layer with a given set of weights, the neurons then repeat the process and pass the values to one or several output neurons. In essence the neural network takes some input values and delivers some output values by processing the inputs through its functional structure. Neurons are nothing but functional processing units that pass values multiplied by certain weights to other units.

RegressionDataset regression_i_simpleReturn_o_simpleReturn(){

       Data<RealVector> inputs(84,RealVector(2));

       Data<RealVector> labels(84,RealVector(1));


       int  i = 0;


                           inputs.element(i)[0] = (cOpen(1+i)-cOpen(2+i))/cOpen(2+i);

                           inputs.element(i)[1] = (cOpen(2+i)-cOpen(3+i))/cOpen(3+i);

                           labels.element(i)[0] = (cOpen(i)-cOpen(i+1))/cOpen(i+1);


       RegressionDataset dataset(inputs,labels);

       return dataset;


Code Fragment 1. Function in C++ that creates 84 examples using 2 returns as inputs and the next bar's return as output

However a neural network does not know how to process inputs from the start since it does not know the weights that are given to each neural network connection. This is why we need to “train” a neural network using a given set of inputs and output values so that the weights that define the connections between neurons can be properly defined. We then use a trained neural network to predict the outcomes on unknown data, which is where we can obtain a benefit by predicting some outcome related with price data.