This page is a copy of the ipynb file being hosted on Github. Full research paper can be found here
Predicting the forex market can be quite hard for a nerual network to understand due to the properties of the market. To get an accurate prediction, a neural network needs a very high number of parameters, a lot of data, and thousands of iterations of training.
This paper tries to explore the question on whether a neural network is able to accurately predict the foreign exchange market given a limited set of parameters.
The program works by using a neural networking algorithm known as "NEAT". NEAT stands for "Neural Networks through Augmented Topologies". The neat algorithm works by using the concept of life, where each generation starts to learn more based on the knowledge of the previous generation; The best scoring individual from this generation gets to reproduce and go onto the next generation.
A proper explanation on how the algorithm works can be found here
To start, we'll retrieve the data using a public module which imports data from yahoo.finance and automatically converts it into a csv file. This piece of code saves the data as a .csv file.
To use NEAT, we need to convert the ticker data and optimize the parameters we pass to it.
We need to show the algorithm some trends in the data so it is able to learn patterns, so we pass the algorithm:
RSI: Relative strength index
EMA: Exponential moving averages
Close Ratios: The ratio of the current close by the rolling averages of the past closes
Trend Horizons: Current trend of profits / losses
After filling the dataset with all these values, the program minmaxes all the values to be between 0 and 1, generally considered good practise for inputs in machine learning.
Plot the important inputs that are passed to the neural network. The following function plots:
Adj Close
EMA: Exponential Moving Average
RSI: Relative Strength Index
The input data that are passed to the neural network should help it approximate whether the next time period's closing value will be higher / lower compared to the current close.
To plot the statistics of the neural network, matplotlib is used while the neat module gives us most of the info needed to create the charts.
This plot displasy the fitness of each each generation. The fitness attribute is given to a genome when it starts making profits by predicting the outcome of the future closes.
The model is trained using the intial data that was was declared as a dataframe.
The training works by essentially telling the giving the genome today's close and rewarding it fitness based on the prediction it makes.
The genome is passed all the values in the scaled_data dataframe with the state that it is currently in. The genome gains a very small amount of fitness for holding onto a trade while the value of that trade increases.
There are 3 outputs that the genome gives:
Index 0: represents a hold state
Index 1: represents a buy state
Index 2: represents a sell state
The genome is not allowed to gain negative profits and is removed from the population if it starts gaining negative profits.
As the data shows, during learning, the genome's profits stay erratic between each generation and do not increase at a linear rate. To create a more efficient algorithm, the data that is passed to the neat algorithm would need to be more correlational for the algorithm to start to see patterns in data. More computing power and more iterations of generations are needed to see progress in profits.
During testing, the best genome in all 200 generations is not able to create a profit at all with a new and different dataset. As displayed by the bar graph shown above, the neural network decides to mostly make hold decisions because it thinks that the longest way to survive is by holding. The neural network also decides to buy approximately 2 times but never decides to close the trade which leaves it at always a negative profit. One reason that the neural network decides to always hold the trade is possibly because it gains some fitness when the value of the currency goes up and it sees selling as too much of a risky decision.
All in all, the current neat algorithm, although efficient, is not smart enough to predict Forex Charts based on pure trends in the data and only 200 iterations of generations.