Technical indicators are the base of algorithmic trading. So wouldn’t it be nice to know tomorrows indicator value in advance? This article is about how to use a simple neural network to do so. Python and Tradesignal will be used to do the programming.

## A single linear neuron

A single neuron / perceptron is the most simple form of a neural network. It consists of several inputs which are weighted and summed up to give the desired output. The correct weighting of the inputs is done in iterations, stepping through the available test data. In the end the hope is, that a function like a*indicator1 +b*indicator2 +c*indicator3 +…=value of tomorrow. But as the real world is not a simple linear function, the correct weightings a,b,c,.. will always change and so will the accuracy of the prediction of the next day’s value.

To test the behaviour of such a simple one layer neural network, I implemented one in my charting software Tradesignal using the Python programming language.

## Tradesignal Python Neural Network

The first steps when setting up a Python neural net in Tradesignal is to prepare the data for the training and the live prediction of the network.

The input section defines how much data to use and when to start the training and prediction. The price to predict can be any curve you drag and drop the indicator on.

The variables define the placeholders for the live and training data.

Neural Net: prepare data

To predict tomorrows value of a curve, lines 11 to 15 define the needed inputs. Due to the simplicity of the programming the input values have to be between 0 and 1. So has to be the output. With my example network I will use the change of a curve over time as the predictor values for tomorrows value of that curve.

Feel free to put all your creativity in these most essential lines of the network. You have to give the network the right data to do the prediction. I opted for some inputs on the change of the curve and the change over time. So the network knows where the curve to predict might go. Try your own luck by replacing these lines by other or more indicators and try to give the network a better understanding on where the curve is and where it came from.

Given this simplicity tf the single neuron it can only be trained to predict something like an oscillator, but not the market price itself (not running between 0 and 1)

Lines 20 to 24 take the values of the predictor variables s1-s5, delays them by one bar [1] and as st1-st5 are defined as a serialized variables they will always contain the last x values. The amount of data contained is set with the setbackbuffer command. The 1d array “ot1” will contain the learning goal, the prices of the curve itself.

## Defining the Python Neural Net

The next step is to define the needed functions for the neural net. This is strictly Python programming, mostly stolen from kdnuggets.com. Please see the link for more information about this part of the python code.

In Tradesignal the Python code is set between the “once begin…end” command.

Python neural net definition

## Passing Data and Training of Neural Network

As Tradesignal provides live data I decided to automatically re-train the network every month. Line 77 holds the needed command

The python code (line 80..) first converts the data from Tradesignal into the needed Python form. In Lines 83-87 the data defined previously is converted into a numpy array. This way it can be fed to the neural network function. Line 89 arranges the inputs ( historic predictor values), line 90 gets the values for the output (the curve to predict) from Tradesignal.

After doing the training in line 93, the new weights are printed. These weights, multiplied by the input values, will give the prediction for tomorrow.

If these weights change dramatically with each new training at the beginning of the month, the neural network most probably is not able to find a stationary linear function to predict the curve from historic values, but only curve-fits the historic data. In this case do not expect the prediction to be too useful.

Python neural net training

## Plotting the output

The last task after the training is to use the latest value of the predictor variables to calculate the prediction for the next bar.

In line 113 this is done in python, and in line 108/109 this prediction is transferred from Python to Tradesignal and plotted on the chart.

Python neural net output

## Testing the Neural Network

The first test of the neural net is done with a simple sinus curve. As it can be seen the prediction (magenta) is quite close to the actual curve (blue).

This can also nicely been seen on the scatter plot. It shows the correlation of the level of the actual curve vs. the prediction. If this would be a straight line you would have the perfect prediction. The more the prediction deviates from the straight line, the higher the error is.

The network is using the last 25 values of the predictor variables to calculate the prediction for the next bar. For some reason it could not capture the tops and bottoms, but in general it did a good job in learning to predict the sinus curve.

Python neural net sinus test

## Predicting a real world indicator

Every cyclical signal is an easy task for a neural network. But oscillators in technical trading are only semi cyclical. Indicators like RSI or Stochastic oscillate between given values, but the time-distance between their highs and lows is not constant. (like it would be with a sinus signal) So it is questionable if a neural network really can learn to predict tomorrows value of the oscillator. But let’s give it a try.

On the chart below I used a momentum based oscillator as the curve to be predicted. Like demanded by the simple programming it is oscillating between 0 and 1. It shows some cyclical behaviour, so the neural network might be able to guess tomorrows value.

Python neural net momentum test

The network has been trained using the last 500 data points and is re-trained every beginning of a month. The visual examination of the curve and the prediction (blue & magenta) shows a close correlation. This is confirmed by the scatter chart. The shape is somehow cigar like, which is not the perfect fit of the sinus curve, but better than guessing and flipping a coin. So there might be some edge in using such a simple neural network in trading. Give it a try!