Neural Network EA using Neurosolutions - Development

To add comments, please log in or register
summerskill
41
summerskill  

I've tested this in the past and it worked very well but I never had the time to develop the idea further, now I'm hoping with all the intelligent minds at this forum that we can find ways to expand on this innovative strategy.

The idea basically is adding a brain to our expert advisors whereby they can process immense amounts of data and recognize complex patterns in the market that we can use to make extremely accurate trades.

I posted the EA, DLL, and associated files to this post below.

Introduction

Is it possible to make an Expert Advisor connect to a neuronet and trade in the real time?

Yes, it is. Several neuronet programs have the required program interfaces. One of them is called NeuroSolutions. Its latest version is 6, however not everybody has it and the most popular version for now is the 5-th one. That's why this article describes interaction with the 5-th version. You need the full distributive of the program; it includes custom Solution Wizard that we need.

Think of a Strategy

The strategy for our test example will be a simple one. Let's call it WeekPattern. It will predict the close price of a bar at its opening on the D1 timeframe using a neuronet. Depending on obtained information, it will make a Buy or Sell deal and hold it for all day long. The price prediction will be base on OHLC values of 5 previous bars. To increase the accuracy of neuronet operation, we are going to send it only the price changes relatively to the open price of the current (zero) bar, instead of prices themselves.

Preparing Data for Training

Before we start creating a net, let's write a MQL5 script, which will export all the quotes from the client terminal in the required form. This information is required to train the neuronet. The data will be exported to a text file. List field names separated with a comma in the first list of the file. Next lines will be used for comma separated data. Each line is a combination of inputs and outputs of the neuronet. In our case, the script will move back by one bar of price history on each line and write the OHLC values of 6 bars in the line (5 bars from the past are inputs, and one current bar is the output).

The script скрипт WeekPattern-Export.mq5 should be started at a required timeframe of a required symbol (in our example, it is D1 EURUSD). In the settings you should specify a file name and the required number of lines (260 lines fo D1 is about 1 year history). The full code of the script:

#property script_show_inputs

//+------------------------------------------------------------------+

input string Export_FileName = "NeuroSolutions\\data.csv"; // File for exporting (in the folder "MQL5\Files")

input int Export_Bars = 260; // Number of lines to be exported

//+------------------------------------------------------------------+

void OnStart()

{

// Create the file

int file = FileOpen(Export_FileName, FILE_WRITE|FILE_CSV|FILE_ANSI, ',');

if (file != INVALID_HANDLE)

{

// Write the heading of data

string row="";

for (int i=0; i<=5; i++)

{

if (StringLen(row)) row += ",";

row += "Open"+i+",High"+i+",Low"+i+",Close"+i;

}

FileWrite(file, row);

// Copy all required information from the history

MqlRates rates[], rate;

int count = Export_Bars + 5;

if (CopyRates(Symbol(), Period(), 1, count, rates) < count)

{

Print("Error! Not enough history for exporting of data.");

return;

}

ArraySetAsSeries(rates, true);

// Write data

for (int bar=0; bar<Export_Bars; bar++)

{

row="";

double zlevel=0;

for (int i=0; i<=5; i++)

{

if (StringLen(row)) row += ",";

rate = rates[bar+i];

if (i==0) zlevel = rate.open; // level for counting of prices

row += NormalizeDouble(rate.open -zlevel, Digits()) + ","

+ NormalizeDouble(rate.high -zlevel, Digits()) + ","

+ NormalizeDouble(rate.low -zlevel, Digits()) + ","

+ NormalizeDouble(rate.close-zlevel, Digits());

}

FileWrite(file, row);

}

FileClose(file);

Print("Export of data finished successfully.");

}

else Print("Error! Failed to create the file for data export. ", GetLastError());

}

//+------------------------------------------------------------------+

After exporting the data, we obtain the file data.csv; its first lines (for example) look as following:

Open0,High0,Low0,Close0,Open1,High1,Low1,Close1,Op en2,High2,Low2,Close2,Open3,High3,Low3,Close3,Open 4,High4,Low4,Close4,Open5,High5,Low5,Close5

0,0.00463,-0.0041,0.00274,-0.00518,0.00182,-0.00721,-6e-005,0.00561,0.00749,-0.00413,-0.00402,0.02038,0.02242,0.00377,0.00565,0.03642,0. 0379,0.01798,0.02028,0.0405,0.04873,0.03462,0.0364 7

0,0.007,-0.00203,0.00512,0.01079,0.01267,0.00105,0.00116,0. 02556,0.0276,0.00895,0.01083,0.0416,0.04308,0.0231 6,0.02546,0.04568,0.05391,0.0398,0.04165,0.04504,0 .05006,0.03562,0.0456

0,0.00188,-0.00974,-0.00963,0.01477,0.01681,-0.00184,4e-005,0.03081,0.03229,0.01237,0.01467,0.03489,0.0431 2,0.02901,0.03086,0.03425,0.03927,0.02483,0.03481, 0.02883,0.04205,0.02845,0.03809

This is the format, which can be understood by NeuroSolutions. Now we can start creating and training a net.

Creating Neuronet

In NeuroSolutions you can quickly create a neuronet, even if you see this program for the first time and know little about neuronets. To do it, select the wizard for beginners NeuralExpert (Beginner) at the program start:

1. In it you should specify a problem type that should be solved by the neuronet, select function approximation, click next

2. Then specify the file with training information, which we've created above, simply click browse and find it on your hard drive

3. As the inputs of the net, select all the fields of the file except the fields of the zero bar, click next

4. Since we don't have text fields, don't select anything in the categorical data window, click next

5. Click 'use input file as desired file' to Specify our collected data csv, click next

6. Select only one output of our net (close0), click next

7. The wizard suggests creating the simplest net on default, select low, click next

8. The wizard has finished its work creating a neuronet for us (not a trained net, just a simple structure):

Now we can work with it. We can train it, test and use for data analysis. If you click the Test button, you'll be able to see how the untrained net will solve our problem. Answer the questions of the testing wizard:

Perform the test on the basis of information from the same file so select the same csv from before as input and desired. After clicking next make sure 'Display in a window' and 'Include the desired data' are selected, then click next and finish to test.

The test is over. In the window "Output vs. Desired Plot" you can see the chart that shows the values obtained from the net (the red color) on our history and the real values (the blue color). You can see that they pretty different:

Now let's train the net. In order to do it, click the green button Start on the toolbar below the menu. The training will be finished after a few second and the chart will change:

Now in the chart you can see that the net shows the results that seem to be true. Therefore, you can use it for trading. Save the net under the name WeekPattern.

The process continues in the next post, we've now got our structured, trained and tested network, so we need a way to get it into Metatrader. This tutorial is for MT5 and I'm hoping with our development of this idea we can get it into MT4.

Export the neuronet in a DLL

Without exiting NeuroSolutions, click the CSW button that starts the Custom Solution Wizard. We need to generate a DLL from the current neuronet.

The wizard can generate DLLs for different programs. As far as I understood, for compilation of the DLL you need Visual C++ of one of the following versions: 5.0/6.0/7.0 (.NET 2002)/7.1 (.NET 2003)/8.0 (.NET 2005). For some reason, it doesn't use the Express version (I've checked it).

There is no MetaTrader in the list of target applications. That's why select Visual C++.

Path to save the result:

If everything has passed successfully, the wizard tells says "DLL creation successfully created".

A lot of files will appear in the folder specified in the wizard. The ones we need most are: WeekPattern.dll, it contains our neuronet with the program interface to it; and the file WeekPattern.nsw that contains the balance settings of the neuronet after its training. Among the other files you can find the one with an example of working with this DLL-neuronet. In this case it is the Visual C++ 6 project.

Connecting DLL-Neuronet to MetaTrader

Created in the previous chapter DLL-neuronet is intended for using in Visual C++ projects. It operates with the objects of a complex structure that would be hard to describe on MQL5 or even impossible. That is why we are not going to connect this DLL to MetaTrader directly. Instead of it we are going to create a small DLL adapter. This adapter will contain one simple function for working with the neuronet. It will create the network, pass it the input information and return the output data.

This adapter will be easily called from MetaTrader 5. And the adapter will connect to the DLL-neuronet created in NeuroSolutions. Since the adapter will be written in Visual C++, it won't have any problems with objects of this DLL.

Using Neuronet in Expert Advisor

Well, we have already created several files. Let me list the files, which are necessary for the Expert Advisor to work, and the folders where you should put them. All those files are attached to the article.

File Description File Location

'WeekPattern.dll' our DLL-neuronet created in NeuroSolutions MQL5\Files\NeuroSolutions\

'WeekPattern.nsw' balance settings of our neuronet MQL5\Files\NeuroSolutions\

'NeuroSolutionsAdapter.dll' universal DLL-adapter for any DLL-neuronet MQL5\Libraries\

A good way to check, whether we have connected the neuronet correctly, is to run the Expert Advisor in the strategy tester on the same time period as the one used for training the neuronet.

Well, as experienced traders say, the neuronet is "adapter" for that period. So it is trained to recognize and inform about a profit signal for those exact data patterns, which dominate in this specific period. A profitability graph of an Expert Advisor drawn for such a period should be ascending.

Let's check it. In our case it will be the following beautiful chart:

Just in case, let me give explanations for novice developers of trade strategies and neuronets.

The profitability of an Expert Advisor on a period, which was used for its optimization (training of its neuronet), doesn't tell about the total profitability of the EA. In other words, it doesn't guarantee its profitability on the other period. There can be other dominating patterns.

Creation of trade strategies that keep their profitability behind the training period is a complex and complicated task. You shouldn't count on the NeuroSolutions or any other neuronet application to solve this problem for you. It only creates a neuronet for you data.

Those are the reasons why I didn't give here the result of forward testing of the obtained Expert Advisor. Creation of a profitable trade strategy is not the aim of this article. The aim is to tell how to connect a neuronet to an Expert Advisor.

Conclusion

Now traders have another powerful and easy tool for automatic trading analysis and trading. Using it together with a deep understanding of principles and possibilities of neuronets as well as the rules of training them will allow you following the road of creation of profitable Expert Advisors.

Necessary Files:

You'll need these two programs to create and train the neural net and create the DLL - they are easy to install:

here are the necessary files so you can test for yourself:

Neurosolutions can be found here: DepositFiles

Visual Studio 6 - DepositFiles

VS 6 is needed to create the DLLs from neurosolutions

Lukasz Starczewski
150
Lukasz Starczewski  

I'm getting access violation read on CalcNeuralNet part in mt4. How can I resolve this issue?

To add comments, please log in or register