使用Neurosolutions开发神经网络EA - MetaTrader 4外汇交易 - 小众知识

使用Neurosolutions开发神经网络EA - MetaTrader 4外汇交易

2014-03-08 18:45:30 苏内容
  标签:
阅读:4982
我已经在过去的测试了一下,效果非常好,但我一直没有时间去进一步开发它,现在我希望在论坛大牛的帮助下,我们可以拓展这个策略方法。
 
这个想法的本质是为我们的专家顾问增加一个大脑,因此他们可以处理大量数据和识别市场的复杂模式,我们依据这个进行极为精确的交易。
 
我将EA,DLL和相关文件发布在这个帖子的下面。
 
介绍
是否有可能使专家顾问连接到neuronet并实时交易?
 
是的,可以。几个neuronet程序具备必要的程序接口。其中之一是NeuroSolutions。它的最新版本是6,但不是每个人都有,最流行的版本现在是5。这就是为什么这篇文章使用版本5描述了。你需要完整的程序,它包括我们需要的自定义解决方案向导。
 
 
思考一个策略
我们的测试示例是一个简单的策略。我们把它叫做WeekPattern。它会在D1时间框架根据柱线的开盘价预测它的收盘价。根据获得的信息,它会做出买入或卖出交易,并持有一整天。价格预测基于之前5根柱线的OHLC值。为了提高neuronet操作的准确性,我们将只把价格变动发送到目前(零)柱线,而不是价格本身。
 
 
准备训练数据
在我们开始创建一个网络之前,让我们写一个MQL5脚本 ,这将从客户端输出所需格式的所有报价。此信息是训练neuronet必需的。这些数据将被导出到一个文本文件中。列表字段名用逗号分隔。下一行是用逗号分隔的数据。每一行是neuronet的输入和输出的组合。在我们的例子中,脚本将在每一行回迁一根柱线,并在这一行写入6根柱线的OHLC值(过去的5根是输入,最近的一根是输出)。
 
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.
 
 
创建Neuronet
NeuroSolutions可以快速的创建一个neuronet,即使你第一次使用并对neuronets知之甚少。要做到这一点,在程序启动后选择初学者向导NeuralExpert(Beginner):
 
 
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
Attached Files
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)
 
扩展阅读
相关阅读
© CopyRight 2010-2021, PREDREAM.ORG, Inc.All Rights Reserved. 京ICP备13045924号-1