Friday, January 20, 2012

Meta-Trader - Neural Nets - Part 1

Welcome back Meta-Traders.

Over the holiday season I spent some quality time getting started with neural networks inside Meta-Trader. During that time, I did a review of Sunqu, the first neural net based system on Asirikuy. You can read that review here.

At the time I was thinking there are some things that I would do differently if I had the chance. Well I had the chance and here's what I came up with. Keep in mind this is still a work in progress. It is a functioning Expert Advisor, but needs a lot of work before its ready for demo testing. Let's go through some of the design points here.

While setting up some experts for Asirikuy systems, I noticed a daily pattern in the ATR where the 14-bar ATR seemed to bottom out at about 8:00 and peak at 20:00 Alpari server time. Those times are shown in the chart above with the red line at 8AM and the blue line at 20:00. That means that the bulk of the day's movement occurs between the the red line and the blue line with red signaling "wake up" and blue signaling "go to sleep".

So it occurred to me that the expert advisor should activate at 8:00 and make its prediction for the day based on the state of the indicators at that time. The system should predict the change between the day's open at 8:00 and the day's close at 22:00. At 22:00, the expert should activate and train the network to associate the inputs presented at 8:00 with the actual output experienced at 22:00.

What exactly are we trying to predict? From my experience with neural networks, each neuron's output is essentially binary in nature. Put another way, each neuron partitions the input space such that if the inputs fall on on half of the "decision boundary" neuron responds one way, otherwise the neuron responds the opposite way. In other works, and individual neuron can only be expected to answer a question in a Yes or No fashion. You can still train networks to output arbitrary values like 65.7, but you should structure the problem such that values we are trying to predict fall between +1 and -1.

So based on that, I decided to train the network to predict the daily change between 8:00 and 22:00 as a multiple of the ATR. If the price rose by +1 ATR - train the network to produce +1. If prices declined by one half an ATR, I train the network to produce -0.5. Sure price movements can easily exceed +1 or -1 ATR, but the concept seemed solid. After all, markets can only do 2 things, go up or down and by some magnitude, right?

What about input values? Output of course is the easy part since we only need one daily value generally between -1 and +1. The inputs is where the challenge is and any decent technician can up with almost an unlimited number of indicator values. So what did I come up with here?

I decided to stick with the same scheme on inputs as outputs to consider inputs as real numbers in the range of -1 to +1 measured as a percent change in the ATR. Over what period though? Multiple time periods of course. Here was my initial try:

dH1 = (Close[2]-Close[1]) / dATR;
dH2 = (Close[3]-Close[2]) / dATR;
dH3 = (Close[4]-Close[3]) / dATR;
dH4 = (Close[5]-Close[4]) / dATR;
dH5 = (Close[6]-Close[5]) / dATR;
dD1 = (iClose(Symbol(), PERIOD_D1, 1)-iClose(Symbol(), PERIOD_D1, 2)) / dATR;
dD2 = (iClose(Symbol(), PERIOD_D1, 2)-iClose(Symbol(), PERIOD_D1, 3)) / dATR;
dD3 = (iClose(Symbol(), PERIOD_D1, 3)-iClose(Symbol(), PERIOD_D1, 4)) / dATR;
dW1 = (iClose(Symbol(), PERIOD_W1, 1)-iClose(Symbol(), PERIOD_W1, 2)) / dATR;

To summarize, take as input the ATR changes in the last 5 hourly bars plus the last 3 daily bars plus the last 1 weekly bars. If there is a pattern to be detected based on changes in ATR values, that would surely capture it, right?

Here is the network topology on my first try:

9 Input neurons represented by the real number values above
13 hidden neurons based on 1.5 times the number of input neurons
1 output neuron trying to predict the expect output

Seems pretty easy right? Before we get to the expert coding, we have to deal with some implementation challenges, and we will get to those in part 2.

Enjoy your Saturday.


3 comments:

  1. Hi Chris,

    Thank you for this interesting post :o) I see a problem with your inputs which is that price inputs cannot be normalized in this way because they can never see into the future. So if you have normalized the close using the ATR then your problem will be when you have candles which exceed the ATR of the previous period. You might create artifacts in the network because - depending on the functions you use - training between +1 and -1 might not be an option and these values exceeding one will create problems.

    I look forward to learning more about this effort and your results in back-tests :o) Definitely it is good to see you working on things like these. Keep up the great work!

    Best Regards,

    Daniel

    PS: Be careful when you create your training loops because the ArrayCopyRates function and iClose, iHigh and iLow functions can see into the future (if you ever call iClose(Symbol(), PERIOD_D1, 0)- or any of these functions with shift 0 - you will peek into the future).

    ReplyDelete
  2. Hi Daniel-

    Thanks for reading and for the comments.

    Agreed that daily moves can exceed 1 ATR. I decided later on to take trading signals only if the prediction exceeds some threshold. So beyond the threshold, the value of the prediction won't have any impact on the results.

    I know about the problems with using bar 0, and don't think i'm using it in any of my calculations.

    Thanks for the encouragement, I appreciate it!

    Chris

    ReplyDelete
  3. Hi Chris,

    Very interesting concept. Looking forward to reading the rest of the posts!

    Kind regards,

    Edward

    ReplyDelete