Download MetaTrader 5

Discussion of article "Third Generation Neural Networks: Deep Networks"

To add comments, please log in or register
MetaQuotes Software Corp.
Moderator
189445
MetaQuotes Software Corp.  

New article Third Generation Neural Networks: Deep Networks has been published:

This article is dedicated to a new and perspective direction in machine learning - deep learning or, to be precise, deep neural networks. This is a brief review of second generation neural networks, the architecture of their connections and main types, methods and rules of learning and their main disadvantages followed by the history of the third generation neural network development, their main types, peculiarities and training methods. Conducted are practical experiments on building and training a deep neural network initiated by the weights of a stacked autoencoder with real data. All the stages from selecting input data to metric derivation are discussed in detail. The last part of the article contains a software implementation of a deep neural network in an Expert Advisor with a built-in indicator based on MQL4/R.

This article is going to consider the main ideas of this subject such as Deep Learning and Deep Network without complex computations in layman’s terms.

Experiments with real data confirm (or don't) theoretical advantages of deep neural networks over shallow ones by metric definition and comparison (not sure about metric definition and comparison). The task in hand is classification. We shall create an indicator and an Expert Advisor based on a deep neural network model and working in conjunction according to the client/server scheme and then test them.

The reader is presumed to have a fair idea of the basic concepts used in neural networks.


4. The Implementation (Indicator and Expert Advisor)

Now we are going to write a program for the indicator and Expert Advisor using a deep network for receiving trading signals.

There are two ways of such an implementation:

  • The first one. Training of the neural network is performed in Rstudio manually. After obtaining acceptable results, save the network in the appropriate catalog. Then launch the EA and indicator on the chart. The EA will load the trained network. The indicator prepares a vector of new input data on each new bar and passes it to the EA. The EA presents network data, receives a signal and then acts on it. The EA is carrying on with its usual activities such as opening and closing orders, trailing etc The objective of the indicator is to prepare and pass on to the EA new input data on each new bar and, most importantly, present signals forecast by the network on a chart. Practice shows that visual control is the most efficient way of assessing a neural network.
  • Second way. Launch the EA and the indicator on the chart. At the first launch, the indicator passes on to the EA a prepared large set of input and output data. The EA launches training, testing and selecting the best neural network. After that work goes on like in the first way.

We are going to write the link indicator-EA following the first algorithm. EA with a minimum of bows and frills.

Why is it so difficult? This way of implementation allows to connect several indicators placed on different symbols/timeframes to one EA and work with them consequently. For that, the EA has to go through a little modernization. We are going to talk about it later.

Below is the structure of interaction between the indicator and the EA:

Fig. 31. Structure of interaction between the indicator and the EA

Fig. 31. Structure of interaction between the indicator and the EA

Author: Vladimir Perervenko

Andrew Kreimer
789
Andrew Kreimer  

Great article!

A nice sequence of DM articles lately.

Christian Bannard
97
Christian Bannard  

Thus far have attempted to load everything and no matter what I do, cannot get it to load. All paths are the same as your instructions, I've tried both 3.1.1 which is the same version as you used along with a newer version 3.1.3 and all scripts, DLLs, indicator, headers, etc. are in their correct locations according to your instructions.

Whenever the EA is dropped onto a chart, it comes up with "Rterm has crashed" as an alert window, which on looking through the code says the R isn't loading.

Are there any additional steps required such as a DLL that's required to load R that is missing?

I've also checked all R scripts to ensure correct paths (and case-sensitivity for folder names) and it still doesn't working.

Very impressed by your article and the depth you went to in explanation. I've done a lot of work with Jeff Heaton's Neural Network so wanted to take a look at R as well.

Any advice you can offer would be appreciated.

Vladimir Perervenko
3502
Vladimir Perervenko  
traderd:

Thus far have attempted to load everything and no matter what I do, cannot get it to load. All paths are the same as your instructions, I've tried both 3.1.1 which is the same version as you used along with a newer version 3.1.3 and all scripts, DLLs, indicator, headers, etc. are in their correct locations according to your instructions.

Whenever the EA is dropped onto a chart, it comes up with "Rterm has crashed" as an alert window, which on looking through the code says the R isn't loading.

Are there any additional steps required such as a DLL that's required to load R that is missing?

I've also checked all R scripts to ensure correct paths (and case-sensitivity for folder names) and it still doesn't working.

Very impressed by your article and the depth you went to in explanation. I've done a lot of work with Jeff Heaton's Neural Network so wanted to take a look at R as well.

Any advice you can offer would be appreciated.

Hi/

I am glad that you are interested in article.
I debug cases fall Rterma as follows:
- Comment out everything in the start () except that inspection work Rterma
- Comment out in the init () all but run Rterm ()

- If Rterm is up and running, since Init() uncomment one operator and checked. You specify at what happens operator crash. 

Thereafter easier to determine the cause crash. Usually there are two: the script syntax error or lack of necessary libraries.
I again refer to Appendix to the article.

Ready to help you in the future if you tell which falls Rterm.

Best regards

Andy Reed
59
Andy Reed  
vlad1949:

Hi/

I am glad that you are interested in article.
I debug cases fall Rterma as follows:
- Comment out everything in the start () except that inspection work Rterma
- Comment out in the init () all but run Rterm ()

- If Rterm is up and running, since Init() uncomment one operator and checked. You specify at what happens operator crash. 

Thereafter easier to determine the cause crash. Usually there are two: the script syntax error or lack of necessary libraries.
I again refer to Appendix to the article.

Ready to help you in the future if you tell which falls Rterm.

Best regards

I am having the same issue. I cannot find where you are talking about start() except that inspection work Rterma or the init() all but run Rterm(). I have looked at the R code and in MetaEditor. 
jcl365
20
jcl365  

This is a very interesting and useful article. I got the system to work, however in Zorro, not in MT4. This simplifies the script a lot and I can backtest it from Oct 14, 2014, up to today. 

There is a problem though: It seems you have trained on the ZZ of the same bar, not the ZZ of the next bar. So the system is very good in predicting the bar that just ended. If I trade on the past bar, I get this balance curve:

 Trading with a time machine

A perfect system! But if I use the returned Sig for trading on the next bar, I get this slightly more realistic balance curve:

Real trading

(The red part is the underwater equity).

I've used the October 14, 2014 model. Have you already tried training a model on the ZZ of the next bar? 

Vladimir Perervenko
3502
Vladimir Perervenko  
jcl365:

This is a very interesting and useful article. I got the system to work, however in Zorro, not in MT4. This simplifies the script a lot and I can backtest it from Oct 14, 2014, up to today. 

There is a problem though: It seems you have trained on the ZZ of the same bar, not the ZZ of the next bar. So the system is very good in predicting the bar that just ended. If I trade on the past bar, I get this balance curve:

 

A perfect system! But if I use the returned Sig for trading on the next bar, I get this slightly more realistic balance curve:

(The red part is the underwater equity).

I've used the October 14, 2014 model. Have you already tried training a model on the ZZ of the next bar? 

Hi/

We must bear in mind the following.

1. We get the signal from the zigzag.

sig <- ifelse(diff(zz) > 0, 1, ifelse(diff(zz) < 0, -1, NA)

2. We shift it to another bar in the future.

sig <- Hmisk::Lag(sig, shift=-1)

3. We train the neural network to the signal from the next bar.
The quality of education need to increase the selection of indicators, their parameters, the parameters of the neural network.

The article shows the path and method. The potential of these networks is huge.

Best regards

Vladimir

jcl365
20
jcl365  

I habe now trained a new model with prediction of the next bar, and it seems that it indeed works. The accuracy is still in the 74% range. This is the equity curve now:

:

It behaves just as I would expect: the system is profitable immediately after training, and then slowly deteriorates as the market changes.  

So the next step is a WFO test with regular re-training of the model. For this the training must be integrated in the strategy script.

This is the corrected function for calculating the Sig of the next bar:

Sig <- function(ch = 0.0037, pr = price[ ,'Med']) {
        ZZ <<- ZigZag(pr, change = ch, percent = F, retrace = F, lastExtreme = T)
        for(i in 1:length(ZZ)) { if(is.na(ZZ[i])) ZZ[i] = ZZ[i-1] }
  #shift zz for predicting the next bar
        for(i in 1:length(ZZ)-1) { ZZ[i] = ZZ[i+1] }
        dz <- c(diff(ZZ), NA)
        sig <- ifelse(dz > 0, 0, ifelse(dz < 0, 1, NA))
        return(sig)
}

 The "Compute" function that is executed every 30 mins by the strategy script:

Compute <- function() {
  price <<- pr.OHLC(Open,High,Low,Close)
  X <<- In()
  normalized <- predict(Prepr, tail(X,1))
  pr.sae <- nn.predict(SAE, normalized)
  return(pr.sae[1]);
}

 The strategy script, the "EA":

#include <default.c>
#include <r.h>

string RPath = "F:\\D\\R\\R-3.1.3\\bin\\i386\\Rterm.exe";
int Size = 200;

bool RCheck()
{
   if(!Rr()) {
        quit("R session aborted!");
                return false;           
   }
   return true;
}


function run()
{
        BarPeriod = 30;
        StartDate = 20141014;
        LookBack = Size;
        asset("EUR/USD");
        
        Spread = Slippage = Commission = RollLong = RollShort = 0;
        //Stop = 25*PIP;
        //Trail = 25*PIP;
        
        if(is(INITRUN)) {
           RStart(RPath,1);
           if(!RCheck()) return;
           printf("\n%s running",RPath);
           Rx("rm(list = ls());");
           string Command = strfmt("source('%sStrategy/SAE.r')",
                strrep(ZorroFolder,"\\","/"));
           Rx(Command);
           Command = strfmt("load('%sData/sae.model')",
                strrep(ZorroFolder,"\\","/"));
           Rx(Command);
        }
        
        vars op = rev(series(priceOpen())),
                hi = rev(series(priceHigh())),
                lo = rev(series(priceLow())),
                cl = rev(series(priceClose()));
                
        if(!is(LOOKBACK)) {
                Rv("Open",op,Size);
                Rv("High",hi,Size);
                Rv("Low",lo,Size);
                Rv("Close",cl,Size);
                
                var Predict = Rgd("Compute()");
                if(!RCheck()) return;
                
                if(Predict > 0.6 && !NumOpenShort)
                        enterShort();
                else if(Predict < 0.4 && !NumOpenLong)
                        enterLong();
        }
        
        if(is(EXITRUN)) {
                RStop(); // terminate the session
        }
}
jcl365
20
jcl365  

Another quick test, this time with a 6000 bars training set from September 2014 until Febrary 2015. Out of sample test starts in March:

  

Again we have a profitable phase of about 5 weeks until the model deteriorates.

I believe the splitting into test and training data is unnecessary: we can use all data for training. The accuracy and confusion matrix are misleading because in most cases the ZZ sign is identical to the sign of the previous bar, wrongly suggesting a high accuracy. For the profit, only the sign changes matter. 

Vladimir Perervenko
3502
Vladimir Perervenko  
jcl365:

I habe now trained a new model with prediction of the next bar, and it seems that it indeed works. The accuracy is still in the 74% range. This is the equity curve now:

:

It behaves just as I would expect: the system is profitable immediately after training, and then slowly deteriorates as the market changes.  

So the next step is a WFO test with regular re-training of the model. For this the training must be integrated in the strategy script.

This is the corrected function for calculating the Sig of the next bar:

 The "Compute" function that is executed every 30 mins by the strategy script:

 The strategy script, the "EA":

jcl365:

I habe now trained a new model with prediction of the next bar, and it seems that it indeed works. The accuracy is still in the 74% range. This is the equity curve now:

:

It behaves just as I would expect: the system is profitable immediately after training, and then slowly deteriorates as the market changes.  

So the next step is a WFO test with regular re-training of the model. For this the training must be integrated in the strategy script.

This is the corrected function for calculating the Sig of the next bar:

 The "Compute" function that is executed every 30 mins by the strategy script:

 The strategy script, the "EA":

Hi

You moved the series ZZ one bar in the future.

for(i in 1:length(ZZ)-1) { ZZ[i] = ZZ[i+1] }

You moved the series dz one bar in the future.

 dz <- c(diff(ZZ), NA)

So you moved the target variable in the two bars into the future.

This is equivalent to

dz <- Hmisc::Lag(diff(ZZ), shift=-2)

This option can also be used.


Vladimir Perervenko
3502
Vladimir Perervenko  


Again we have a profitable phase of about 5 weeks until the model deteriorates.

This is normal. The model can and should be periodically re-learn.

I believe the splitting into test and training data is unnecessary: we can use all data for training.

Can. It is important to remember a few important points:
1. training and test sets should not be crossed.
2. The training set should be mixed

3. If the ratio of classes of balance - to make the adjustment.

I am glad that there were colleagues using R.

Best Regards

Vladimir

To add comments, please log in or register