# A Neural Network based trading strategy

I always dreamed about the machine which tells me to enter long right before the market starts to go up. Might a neural network be this machine? Using Tradesignal and the free Python Neural Net library Pyrenn it is easy to find out…

### Part one: Classification of data

The first step in the process is to tell the Neural Network when it should give me a go. Therefore I designed me small indicator which returns 1 whenever the market has been rising for a given number of bars without falling back in between. This should have been an easy environment to make money and so I want the neural network to analyse the bars before this signal and see, if it can detect a pattern.

classification

### Part 2: Feeding data to neural network

After the classification indicator is working, the script which will do the trading has to take this information and prepare the inputs for the neural net.

data inputs to nn

I opted for a neural network with 5 inputs. These inputs will describe the market behaviour before the signal occurred by giving the %change of the market prior to the signal over 1,3,5,10&15 days. Be creative, the way you describe the market will massively influence the ability of the neural network to learn something from the past.

Beside the preparation of the training data in lines 12 to 19 the script also prepares the input data for the live trading in lines 23 to 27. These inputs, when applied on the trained neural net, will then hopefully give me a signal before the market has some good days.

### Part 3: Create and train the neural network

The codes above run on every bar of the chart and thus build a history of signals. On a specific date on the chart I want to use this data and train the network. Using the Pyrenn neural network module and the data prepared before, this is done with the following lines of code.

As defined in line 37 this is a network with 5 inputs, one output and 2 hidden layers with 3 neurons each.

Lines 44 to 49 transfer the data collected in Tradesignal to the Python environment.

Finally in line 49 the training is done. The network is trained until the max number of iterations or the minimum error has been reached.

Neural Net training

### Part 4: Testing the signals with a trading strategy

nn test

After the training has been done the neural net is fed with live data (lines 59 to 62) to calculate the prognosis for tomorrow. If the bars before today hint that I should buy, the neural net should return 1, otherwise 0.

The most simple test for the quality of the output is a simple trading strategy. It buys if the neural net signals a buy (1) and closes the position after the number of expected positive days (as demanded by classification script) have passed.

### Part 5: Results for daily S&P500 Index data

To obtain the results shown below the NN was trained with 2000 bars of daily data, prior to 2018. The out of sample trading simulation starts in 2018. About 68% of the trades have been positive, leading to a profit factor of 2.64. Not too bad for such a simple approach.

nn results

The strategy shows a low trading frequency, and as the drawdowns show, a more sophisticated exit strategy should have advantages and bring down the magnitude of the losing trades.