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:
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
// 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;
// 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.");
// 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());
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.
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.
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.
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
File Type: mq5 WeekPattern.mq5 (3.8 KB, 46 views)
File Type: mq5 WeekPattern-Export.mq5 (2.0 KB, 40 views)
File Type: zip dll_nsw.zip (383.4 KB, 62 views)