Can a neural network trade the markets effectively?

Hoggums

Senior member
Messages
2,176
Likes
878
I thought I'd share with you a little experiment I'm performing to see if a neural network can be trained to successfully trade an index and/or forex.

Generally speaking, neural networks are trained by comparing the output it produces with an expected result and feeding the error back into the network so it can correct itself slightly. The next time the neural net is tested the output is slightly closer to the expect result.

But herein lies the problem with teaching a neural network how to trade - who's to say the way you are teaching it is either the best or correct way. You are teaching it with your own knowledge and therefore own misconceptions and mistakes and the neural net will repeat those mistakes after training. I've seen neural nets use moving averages, entry/exit rules and other such inputs to make trading decisions. I want to get away from this.

Wouldn't it be better to let the neural network figure out how to trade for itself?

So the aim of the experiment is to start off with a completely random network and let it trade and record the results. I am not going to tell it what results I am expecting, but just reward the best results. I am going to use Darwins theory of natural selection to let the network "evolve" itself into a better trader. The network will spawn off several copies of itself with small random variations, let them all trade and take the best few networks and discard the rest. Then repeat the process until consistent and reliable results are achieved and no more improvements can be found.

I also want to see what the optimum amount of data is necessary to produce the best network - e.g. for trading on daily candles do you need only 1 week of data or several months to make an accurate prediction.
That's the theory anyway. Currently I have no idea if it will work and it may take several neural net configurations before I have some consistent data. What will be interesting is to see what kind of trading approach evolves, I'm going to start trading with daily candlesticks and it'll be interesting to see whether it decides buy and hold is the best or whether it tries to trade short term swings.

Simple steps to start with, I am going to build a direction predictor. The output of the network will be a single neuron with 2 states - either BUY or SELL. I will input a range of candlestick data into the net and count how many pips are won/lost by the time the next candlestick is completed, then the net will evaluate BUY/SELL again.

For the first experiments, networks will be kept/disgarded purely on the basis of the total number of points they achieve over the historical data. However I will weigh the points according to the index level. Gaining 1 point when the S&P was at 100 is a lot more significant than gaining one point when it was at 1200.

I'm going to train the network on daily data from the S&P from 3-Jan-1962 to 31-Dec-2006. The S&P is relatively friendly to indicators given it's large number of constituents. I am keeping data from 1-Jan-2007 aside for forward testing, I think it will be interesting to see how any evolved network will cope with the credit crunch. I'm hoping by training on such a large range of data from '62 - '07 that the net will not become too optimised, however I can't guarantee this and I'm deliberately leaving out a pretty volatile period in history to see if the network can cope with this unseen data.

Afterwards I will increase the complexity of the outputs - stating trade size, stop size and limits. I will also see if the evolved network is capable of trading other more tricky indices like the Dow Jones Industral and the Nikkei without ever having seen the data before.

If any of this is successful I will move onto shorter timeframes in forex to see if a similar network can be built.


keep tuned more will follow in the coming days..
 
You are in for a LOT of work. At one stage I spent quite a lot of time fiddling around with TradingSolutions. It's hard to build decent ANN models. What TS does by default is construct an "optimal analog trading signal" using future knowledge according to certain parameters (eg profit 5% per trade) and trains the NN to predict this signal. It also runs the predicted signal thru a genetic optimizer to adjust he zero offset. Even with all the bells and whistles of a reasonable software package such as TS it's still hard to make any real progress.

Even if you get a reasonable correlation between the optimal signal and predicted signal, the thing still does not necessarily trade profitably. In other words even with well chosen inputs, a suitable network and demonstrably some predictive ability you can still lose money. It really is difficult stuff.

Good luck with training over 40 years of S&P data, I found that over those sorts of periods the training just doesn't converge. You can have too much training data as well as too little. Probably because of the changing nature of the market.

One other possible application for ANN is not prediction, but classification and pattern recognition. One thing I have been thinking about is pattern recognition in order book behavior in futures markets. It's hard stuff, but probably no more difficult than time series forecasting.

Anyway, good luck. Will be very interested to hear if you have any success.
 
"I thought I'd share with you a little experiment I'm performing..............." - not so little, and very interesting methinks!

I'd also like to know the physical details of how you are putting this into practice eg kit & software assistance etc. I suspect this thread will be very illuminating if you have the time (and the brain pain hasn't been too serious) to explain your progress. I wonder if at the end of it we will be reminded of how difficult and primitive AI (artificial intelligence) still is - compared to the mk1 human brain?

Good stuff - looking forward to more, and thanks for sharing.
 
My university 3rd year project was on pattern recognition in neural networks so I have some experience in this. It's an idea that's been sitting in the back of my mind for a while to try out. I've already written the program to do the testing which I'm running final tests and checks on at the moment to make sure it produces sensible results.

It's my own bespoke c++ program and I'm using high/low/close data from Yahoo to start with. I know there are doubts about the accuracy of the data from yahoo but this is more of a proof of concept than an attempt to write a tradable EA at this stage.

dcraig1 - one of the purposes of this test is not to impose specific trading conditions on the neural nets such as 5% profit and optimal signals - there are no optimal paths I am training this net to. Only nets which are more profitable than it's ancestor over the training period will be allowed to spawn, I am not influencing it's style of trading.

However when I start looking at position sizing and stop losses I will impose drawdown conditions so that any net that has too large total drawdown from peak to trough will not be allowed to spawn, but I will not be influencing the "correct" way to trade on any network.

I also have deliberately chosen a large range of trading data so that the network does not become too optimised on a specific period, the theory being that if it can cope with 40+ years of trading it should have found common patterns to trade against for all of those periods and therefore be more likely to be profitable in the future.

I agree - I don't think it's going to be easy - but part of what I'm doing is also to dispell the myth that neural networks and artificial intelligence are somehow super advanced technologies that will solve all the worlds problems. Neural networks are designed to mimic brain function - so why should they be any better than a human brain. However neural networks are not affected by mood swings and emotions.

Training a large and complex network takes a long time - especially as I don't have a server farm to play with. I'll be starting with smallish networks to begin with for that reason.
 
First experiment

The first experiment I'm doing is more of a simple test and proof of concept and will serve as a reference point for future tests. I'm going to start with a small network that is quick to train. I will only read in the last 2 days of Hi/Lo data plus the current market close and make a prediction of next days direction.

Attached shows a picture of the net - just 5 input neurons. I've added 3 hidden layers for no other reason than to be sure there are enough connections to ensure the net is capable of sufficient complexity. There is no real science behind the design.

In the uploaded spreadsheet I have uploaded test runs from 3 nets - the first is the net without any training, the 2nd is after 2 training runs and the third is the fully trained net.

The column accum Percent is the sum of all of the percent gains/losses from each daily trade. It serves as a better gauge than using pips/points only because percentages are scaled to the level the market is at. It is this result that the nets are trained against. If the net was trained against pips only it would have a bias towards recent years when the index and pip gains were larger.

Initially the net seems to sell everything - but after 2 generations it has already decided that buy and hold (with a few exceptions) is the best policy. After 7 generations the network cannot be improved upon which you can see produces only marginally better results than from generation 2. Bear in mind the net can only see the last 2 days of market data.

This net will be retained for forward and cross index testing later.

Coming next is training a net with last five days of data to see if using this extra data it can improve the results.
 

Attachments

  • Net1.jpg
    Net1.jpg
    32.7 KB · Views: 5,678
  • Results.xls
    3.9 MB · Views: 1,519
5 days of input data

Five days of inputs and the results are vastly better. Attached is the spreadsheet showing the untrained net (#1), the net after 4 generations (#2) and the fully trained network (#3). The network took 22 generations this time round before it was fully trained.

You can see that the network no longer decides buy and hold is the best policy and percentage gains are pretty smooth throughout the time period. one notable point is that using the extra information it turns the losses from the previous 2day input network in the 2000-2001 period into substantial gains.

22 day (roughly 1 calendar month) input network to follow tomorrow. It should be interesting to see if this extra information produces better results.

 

Attachments

  • Results.xls
    3.9 MB · Views: 1,631
Issues...

The larger net size has uncovered a flaw in my training program which is very good and quick to train relatively small networks, but because it takes shortcuts to train the net it can (& does) miss out profitable permutations for larger networks. So the result I'm seeing is that larger networks are not coming in as profitable as my 5 day input network.

The simple solution to this is to increase the number of decendants per generation - but all of those take processing time, and the larger the network the more permutations there are to cover. So I have reached a compromise - I am increasing the decendants to a certain degree and will take the result from these knowing that I probably haven't trained the network to the best it can be.

As a result my 22day input network is still in training, I have added results from a 10 day input network which falls just a tad short of the results from the 5 day input network. I think it is significant that despite multiple attempts I cannot train a network to give better results than the 5 day input network. This does suggest that perhaps the previous weeks market data is largely irrelevant when trading on a 1 day timeframe looking to make a profit the following day.

Bearing this is mind, if the results are poorer for the 22 day input network I will discard this and concentrate on shorter input periods.

tomorrow I will look at forward testing these networks on S&P500 data from 2007-present and testing them on indices they have not been trained on to see how they perform.

Results file contains test results from an untrained network (#1), after 5 generations (#2) and the fully trained network generation 33 (#3)
 

Attachments

  • Results.xls
    3.9 MB · Views: 1,236
Very interesting Hoggums. I do always wonder though... if you're looking at applying neural networks to the markets, why not just use the one between your ears. It is likely to be far more advanced, learn faster and have more intuitive leaps. Which also answers your question of whether a neural net can trade effectively.

Also, when you perturb the strategies slightly and record the results, how long do you plan to judge this on. Evolution is a great concept, but you have to leave it for quite a long time to get results, right?

Wish you good luck with this, always interested in a different approach.
 
The 22 day input network has been discarded as it cannot produce better results. So onto forward testing...

First - to test the 3 networks on the SnP from 2007-Present. - In particular seeing how well they perform through the credit crunch. And the results are less than spectacular - in fact the only network to finish pip positive is 5 day input network. All of them fall foul of the September 08 crash. While they are not complete disasters it does not bode well.

Results attached in the first image - Network 1 is the 2 day input network, Net 2 is the 5 day input and net three is the 10 day input. Results do not simulate real trading conditions such as buy/sell spreads.

Next I tested them on the FTSE and Dow Jones indices. Image results attached. You can see that all the networks make a profit, however they all fall foul of the 1987 crash.

Finally I tested the network on the Nikkei 225 which has meandered along all of it's own the past 2 decades. And all of the networks seem unable to cope with this period. Network 1 which has a propensity to buy and hold is no suprise when it does particularly badly.

Conclusion?

The fact that all of the networks didn't do well in the SnP from 2007-2010 shows that the market behaviour changed radically in this period and the network was unable to cope with these large moves. However it didn't disgrace itself with huge runaway losses but there's nothing there that I would be confident in basing a trading system from.

All of the networks seem to do badly from 1999 onwards - the majority of the training for the nets was from 1962-1999 and there was obviously a shift in market behaviour, probably with the advent of ever increasing electronic trading. I am tempted to retrain the networks exclusively for the period from 1990 onwards to see how they would cope with 2007-2010 unseen market data based on this training.

Despite the fact that all networks made a profit on unseen index data (except Nikkei) I am not drawing too many conclusions as yet because these markets were closely correlated with the SnP. I think testing on a new unseen period in time is more valuable than testing on different index data. Except perhaps for the Nikkei data because it seemed to be in a world of it's own throughout the 90s.

Regardless of all of this - predicting the direction of the market does not a trading system make! So it's time to add in position sizing into the equation. To size up a position you need to keep a track of the account level - and to monitor drawdown. There's no point designing a system with 10000%+ a year return if at some point there's a 99% drawdown. So I'm going to restrict my trading systems to a max 50% drawdown and see how much money they make!

There will be a pause in proceedings while I adapt my code to this. I will conduct some experiments on training the nets on shorter timeframes in the meantime.
 

Attachments

  • SnP2007-2010.JPG
    SnP2007-2010.JPG
    79.4 KB · Views: 1,189
  • FTSE.JPG
    FTSE.JPG
    82.2 KB · Views: 1,478
  • DJI.JPG
    DJI.JPG
    83.2 KB · Views: 1,335
  • nikkei.JPG
    nikkei.JPG
    85.4 KB · Views: 1,193
Maybe you could share your code so us fellow programmers can, if possible, help a bit?

Cheers!
 
Underneath it all when you set up an ann, you are effectively setting up an equation (a polynomial)
The same question is : can I write a formula that trades markets effectively.

I think you can. Not easy though.

I think it has got to the point where a normal person (of the sort who read this) has more than enough access to data and computational power to work this sort of thing out if the interest takes them.

You need to be careful with the training set - I do think markets behave differently given the same circumstances compared to 20 (or 10) years ago.
If you make the net complicated enough, it will very closely match the training set supplied

Do not forget to back-test against reality that was not in the training data.

There are two other (less obvious) points
- If you are rolling your own ann, you might want to think about some method of adjusting for current experience (though what weight this is given is interesting)
- You might not need to ask the system complicated questions (what will the close tomorrow be),
simpler questions may give better results (will close tomorrow be higher/lower than close today)
 
ANNs can be used effectively if you choose the right "feature space" as it is called. So instead of using OHLC bars you augment with other indicators. ANNs are a form of regression. If you are providing OHLC bars then basically your ANN represents a multi-dimensional surface representing the regression (curve/surface fitting) of past OHLC bars.

So you need to look farther afield with your inputs. Look at additional data, such as co-movement of other instruments, derivative measures on the instruments, etc.

The other issue with ANNs is that it is much too easy to overfit. Getting a balance between in-sample fit and out-of-sample seems to be more of an art form than a science.

Given that this is essentially a classification problem { buy, sell, out-of-market }, many have moved to variants of K-nearest neighbor, SVM, or other classifiers in preference to ANNs.

There are a whole range of classifiers now that use "kernel methods" to remap raw data into a new space that is easier to classify. A kernel transform can also be helpful with your ANN as well.

Needless to say, all of this involves a huge amount of effort and experimentation.

--
Jonathan Shore
http://tr8dr.wordpress.com/
 
Neural Networks are fantastic and interesting. Perhaps not at trading yet, but I enjoyed this thread all the same. Thanks Hoggums
 
Disclaimer : I am an amateur.

For my part, I started using (simple) ANNs but made the mistake (i think) of over-training.
Returns were v.v. variable. Ended up with small profit.

I am currently experimenting using something v. close to what jshore called K-nearest neighbor, except I did not realise that that was what I was doing. This seems a little more 'sensible' to me. Currently re-working it but currently looking at a small loss.

The problem is that (this is subjective opinion, not a tested assertion) markets behave in a certain manner for a while and then at unpredictable times, change their behavior. If you are using anything other than some basic indicator to provide anything more than a rough guide, you will start to lose.
Finding a way of detecting the point at which you need to re-train or re-parameterise is something I think about whilst I watch TV with the OH.
 
Has this experiment come to a conclusion?

Peter

No I just got bored with it. I wasn't getting any better results with more complex nets. I may return to it later when I have time and look at training nets to specific conditions rather than let the network figure out for itself how to trade. I'll probably need a bit more time into looking at more effective training of the network.

Results are remarkably similar to systems that use moving averages and/or other indicators.
 
Hi -

For what it's worth, I've done quite a bit of modeling with neural nets (typically recurrent backprops). I generally find the best performance with only one or two neurons in a single hidden layer with no more than four well chosen inputs. For most market timeseries I can build models which produce an average 20-30% correlation on out-of-sample data.

GRNN/PNN type nets that try to memorise patterns seem even less effective.

Given market data features so much noise, generally nets with many neurons/parameters just model noise - which is a thought that upsets a lot of people ;)

Neural nets definitely are a good tool - especially for creating leading indicators etc but keep 'em simple ;)

Windsurfing Stew
 
question about implementation: how are you using your c++ code? is the code embedded into excel or some trading platform?

many thanks.
 
Top