一个神经网络的EA的示例(含源码)——Combo_Right.mq4 - 小众知识

一个神经网络的EA的示例(含源码)——Combo_Right.mq4

2014-07-11 14:42:40 苏内容
  标签: 神经网络/示例/源码/EA
阅读:6886

去年年底结束的国际大赛的第一名为Better所夺得
他采用的就是神经网络原理的EA
这使得用神经网络方法做EA成为不少人关注的焦点
这里翻译一篇采用神经网络做EA的不错的示例文章
当然附有源码是吸引人的地方
不过也许作者提出了研究神经网络EA的一些思考更为值得注意
作者提出了∶
1。“如果有飞机,为什么还要教人类去飞?”
意思是研究是经网络不必从零起步。MT4里已有了不错的“遗传算法”
文中介绍了如何利用MT4已有的“遗传算法”

2。大家都说做单子最重要的是“顺势而为”,但更需要解决的是∶
“一个基于趋势的交易系统是不能成功交易在盘整(sideways trends),
也不能识别市场的回调(setbacks)和逆转(reversals.,反向走势)!”
这可是抓到不少人心中的“痒处”,有多少人不是到了该逆势时没转向而产生亏损呢?

3。训练神经网络需要用多长的历史数据,提出了并不是用的历史数据越长越好,另外也不是训练的间隔越短越好,文中提出了什么情况下有需再训练它。

等等。。

下面是译文和作者的源码

The problem is stated for this automated trading system (ATS) as follows:
(ATS)自动的(智能的,采用神经网络的)交易系统的问题表述如下

Let's consider we have a basic trading system - BTS. It is necessary to create and teach a neural network in order it to do things that cannot be done with the BTS. This must result in creation of a trading system consisting of two combined and mutually complementary BTS and NN (neural network).
如果我们有一个(BTS, basic trading system),同时需要用创建一个神经网络系统并教会它做BTS所不能做的事,按这个思路就是要创建这样一个交易系统∶它由互相补充(配合)的两部分组成,BTS和NN(神经网络)。

Or, the English of this is: There is no need to discover the continents again, they were all
discovered. Why to teach somebody to run fast, if we have a car, or to fly, if we have a plane?
呃,英语说,我们不需要再去发现“新大陆”,它们是已经存在的东西!进一步说,如果我们已经有了汽车,那为什么还要教人如何跑得快?如果有飞机,为什么还要教人类去飞?

Once we have a trend-following ATS, we just have to teach the neural network in countertrend strategy. This is necessary, because a system intended for trend-based trading cannot trade on sideways trends or recognize market setbacks or reversals. You can, of course, take two ATSes - a trend-following one and a countertrend one - and attach them to the same chart. On the other hand, you can teach a neural network to complement your existing trading system.
一旦有一个趋势交易系统的ATS,我们仅需要教会这个神经网络如何逆势(反趋势)交易的策略。这一点是非常必要的,因为一个基于趋势的交易系统是不能成功交易在盘整(sideways trends),也不能识别市场的回调(setbacks)和逆转(reversals.,反向走势)!当然,你可以采用两个ATS,一个基于“趋势”,一个基于“反趋势”(逆向),然后把它们挂到同一图表上。另一个办法是,你能教会神经网络如何与你现有的系统“互补地”协调工作!

For this purpose, we designed a two-layer neural network consisting of two perceptrons in the lower layer and one perceptron in the upper layer.
为实现这个目标,我们设计了一个两层的神经网络,下层有两个感知机(perceptrons)上层有一个感知机。

The output of the neural network can be in one of these three states:
这个神经网络的能输出下列三种状态之一

Entering the market with a long position
(Entering)市场是处在多向仓

Entering the market with a short position
(Entering)市场是处在空向仓

Indeterminate state
不确定的, (不明确的, 模糊的)状态

Actually, the third state is the state of passing control over to the BTS, whereas in the first two states the trade signals are given by the neural network.
实际上,第三种状态是就把控制权交给BTS,反之前两种状态是交易信号由神经网络给出。

The teaching of the neural network is divided into three stages, each stage for teaching one perceptron. At any stage, the optimized BTS must be present for perceptrons to know what it can do.
神经网络的“教育”分成三步骤,每一步骤“教育”一个感知机,在任何一步骤,这个优化了的BTS必须存在为的是“感知机们”知道它自己能做什么。

The separate teaching of perceptrons by a genetic algorithm is determined by the lack of this algorithm, namely: The amount of inputs searched in with the help of such algorithm is limited. However, each teaching stage is coherent and the neural network is not too large, so the whole optimization does not take too much time.
感知机们分别的“教育”由遗传算法来承担,由于这样的算法的缺乏,换句话说,搜索到的这样的算法有限,限制了“输入”(参数变量)的数量(借助这样算法得到的参数变量的值),然而,每一步骤的“教育”是密切配合补充的。(因此效果还是不错),这样这个神经网络不会太大,整个的优化也不会耗费太多的时间。

The very first stage, preceding the teaching of an NN, consists in optimization of the BTS.
在“教育”NN之前的一步是对BTS进行优化。

In order not to lose ourselves, we will record the stage number in the input of the ATS identified as "pass". Identifiers of inputs corresponding with the stage number will and in the number equal to this stage number.
为了不使我们自己也被搞糊涂了,我们将已经测试通过的ATS的输入(参数变量)记录上(”通过”("pass")的步骤号(stage number).,输入s(参数变量)的标识符将和stage number(步骤号)一致(等同)。

Thus, let's start preparations for optimization and teaching the NN. Let's set the initial deposit as $1000000 (in order not to create an artificial margin call during optimization) and the input to be optimized as "Balance" in Expert Advisor properties on the tab of "Testing" in the Strategy Tester, and start genetic algorithm.
这样,我们开始对这个NN进行优化和“教育”的准备。存入初始保证金为$100万(以便于在优化期间不产生人为的补充保证金的通知)。Input(参数变量)是按“余额”进行优化,设置EA的Strategy Tester的测试的属性tab为"Testing" 。开始运行遗传算法。

Let's go to the "Inputs" tab of the EA's properties and specify the volume of positions to be opened by assigning the value 1 to the identifier "lots".
在这个EA的开仓量 "lots".的值设为1 lot。

Optimization will be performed according to the model: "Open prices only (fastest method to analyze the bar just completed, only for EAs that explicitly control bar opening)", since this method is available in the ATS algorithm.
从这个ATS算法明确地有效开始,实施优化,所采用复盘模型是∶“仅用开盘价(以最快速的方法分析刚形成的柱线)”。

Stage 1 of optimization. Optimization of the BTS:
优化步骤1,BTS的优化
Set the value 1 for the input "pass".
设置为 1 为这input(参数变量)“为通过”(the input "pass")。
We will optimize only inputs that correspond with the first stage, i.e., that end in 1. Thus, we check only these inputs for optimization, and uncheck all others.
我们仅仅优化步骤1相关的那些inputs(参数变量),即,尾标为 1 的参数变量,于是,我们仅仅测试优化有关的inputs而不测试其他的变量参数
tp1 - TakeProfit of the BTS. It is optimized with the values within the range of 10 to 100, step 1
tp1,BTS的所取的止盈值(TakeProfit)。在step 1,优化的值的范围在10到100,
sl1 - StopLoss of the BTS. It is optimized with the values within the range of 10 to 100, step 1
sl1,BTS的所取的止损值(StopLoss)。在step 1,优化的值的范围在10到100 。
p1 - period of CCI used in the BTS. It is optimized with the values within the range of 3 to 100, step 1
p1, 用于BTS的CCI的周期值。在step 1 ,优化的值的范围在 3到100
Below are the results of the BTS optimization:
下面是BTS优化的结果


 
Stage 2. Teaching the perceptron responsible for short positions:
步骤 2 ,“教育负责管“开空仓”(short positions)的感知机
Set the value 2 (according to the stage number) for the input "pass".
根据步骤的步骤号,设置(input,参数变量) 的"pass"的值为 2。
Uncheck the inputs checked for optimization in the previous stage. Just in case, save in a file the inputs obtained at the previous stage.
不测试那些已经测试过的优化了的以前步骤的inputs.(变量参数)。以防万一,保存以前步骤获得的inputs(变量参数值)到一个文件中去
Check the inputs for optimization according to our rule: their identifiers must end in 2:
根据我们的规则,必须是测试那些是在尾标为 2的inputs(变量参数)。
x12, x22, x32, x42 - weight numbers of the perceptron that recognizes short positions. It is optimized with the values within the range of 0 to 200, step 1
x12, x22, x32, x42 是识别并开空仓的感知机的权重,它们的值在step 1时被优化在范围0 to 200
tp2 - TakeProfit of positions opened by the perceptron. It is optimized with the values within the range of 10 to 100, step 1
tp2  (TakeProfit) 是感知机所开的仓的止盈值,它们的值在step 1时被优化在范围10 to 100。
sl2 - StopLoss of positions opened by the perceptron. It is optimized with the values within the range of 10 to 100, step 1
sl2 (StopLos) 在 step 1它是感知机所开的仓的止损值,被优化值的范围在 10 to 100
p2 - the period of the values of price difference to be analyzed by the perceptron. It is optimized with the values within the range of 3 to 100, step 1.
p2 感知机所分析的价格差的周期值 (iiCCI()函数的一个参数∶period - Averaging period for calculation),在step 1 它的值所优化的范围在3 to 100
Let's start teaching it using optimization with a genetic algorithm. The obtained results are given below:
现在,开始用遗传算法来优化“教育”NN(让它“学习”市场),获得的结果如下∶
 

 
Stage 3. Teaching the perceptron responsible for long positions:
步骤 3 “教育”负责开多仓的感知机(“学习”市场)。
Set the value 3 (according to the stage number) for the input "pass".
设置值 3 (根据步骤的步骤号)说明这些input(变量参数)已经“通过”(the input "pass")
Uncheck the inputs checked for optimization in the previous stage. Just in case, save in a file the inputs obtained at the previous stage.
同样,不测试,那些已经测试过的优化了的,以前步骤的inputs.(变量参数值),以防万一,保存以前步骤获得的inputs.(变量参数值) 到一个文件中去
Check the inputs for optimization according to our rule: their identifiers must end in 3:
根据我们的规则,优化测试的inputs(变量参数值)必须是尾标为3的那些变量参数。
x13, x23, x33, x43 - weight numbers of the perceptron that recognizes long positions. It is optimized with the values within the range of 0 to 200, step 1.
x13, x23, x33, x43是识别多仓的感知机的权重,它们的值在step 1时被优化时得到的范围在0 to 200
tp3 - TakeProfit of positions opened by the perceptron. It is optimized with the values within the range of 10 to 100, step 1
tp 3  (TakeProfit) 是感知机所开的仓的“止盈值”,它的值在step 1时被优化时的范围是在10 to 100。
sl3 - StopLoss of positions opened by the perceptron. It is optimized with the values within the range of 10 to 100, step 1
sl3 (StopLoss) 是感知机所开的仓的“止盈值”,它们的值在step 1时被优化为范围是10 to 100。
p3 - the period of the values of price difference to be analyzed by the perceptron . It is optimized with the values within the range of 3 to 100, step 1.
p3 --感知机所分析的价差的周期值。它在步骤 1 优化时得到的值的范围是 3 to 100 。
Let's start teaching it using optimization with a genetic algorithm. The obtained results are given below:
启动采用遗传算法的优化来“教育”NN,所获得的结果如下∶

 
Stage 4 (final). Teaching the first layer, i.e., teaching the perceptron that is in the upper layer:
步骤 4 (最终步骤) “教育”第一层,即“教育”在上层的感知机。
Set the value 4 (according to the stage number) for the input "pass".
根据步骤的步骤号,设置值4 为输入通过(for the input "pass")
Uncheck the inputs checked for optimization in the previous stage. Just in case, save in a file the inputs obtained at the previous stage.
不测试那些在之前步骤已经测试过的优化了的“输入” (inputs) (意思是∶已经在之前步骤优化过的变量的参数值就不再优化它们了)。以防万一,将之前步骤获得的这些变量的参数值存到一个文件中去。
Check the inputs for optimization according to our rule: their identifiers must end in 4:
根据我们的规则,只测试优化标识符最后位是4的那些inputs(变量的参数值)
x14, x24, x34, x44 - weight numbers of the perceptron of the first layer. It is optimized with the values within the range of 0 to 200, step 1.
x14, x24, x34, x44 是第一层感知机参数的权重值。在步骤 1 时它们被优化的值的范围在0 to 200 。
p4 - the period of the values of price difference to be analyzed by the perceptron. It is optimized with the values within the range of 3 to 100, step 1.
p4 被感知机分析的价差的值的周期。在步骤 1 它的值的范围被优化在 3 to 100 。
Let's start teaching it using optimization with a genetic algorithm. The obtained results are given below:
采用遗传算法来优化,启动“教育”来教它“学习”。所获得结果如下∶

 
That's all, the neural network has been taught.
这就是全部,神经网络已经被“教育”了。
The ATS has one more non-optimizable input, mn - Magic Number. It is the identifier of positions for a trading system not to mix its orders with the orders opened manually or by other ATSes. The value of the magic number must be unique and not coincide with the magic numbers of positions that have not been opened by this specific Expert Advisor.
这个ATS有一个不能被优化的input(参数) mn-- Magic Number.(魔法号)它是一个交易系统它所开的仓位的识别符,为的是不和手动开仓或其他ATSes开的仓位混淆。这个Magic Number的值必须是唯一的并且和这个特别的ea尚未开仓的magic numbers不一致。
P.S.

The size of the initial deposit is found as the doubled absolute drawdown, i.e., we consider some safety resources for it.
出于保证有一些安全保险的考虑,初始保证金的金额设置是考虑为绝对最大回落的两倍
The EA given in the source codes is not optimized.
这个ea的源代码没有优化。
If you need to replace the built-in BTS with the algorithm of another trading system, you must modify the contents of the function basicTradingSystem().
如果你需要置换嵌入另一个交易系统算法的BTS,你必须修改BTS功能的内部。
In order not to enter the initial and the final values and the values of steps for optimization, you can take the ready file combo.set, place it in the folder \tester MT4, and upload to the EA's properties in Tester.
以便于不输入优化时的初值,终值和步长,你可采用已备好的combo.set文件,把它放置到MT4的 \tester 目录并加载这个ea的属性(properties)到Strategy Tester。
Re-optimization of the EA is to be performed at a weekend, i.e., on Saturday or on Sunday, but only if the results of the preceding week were unprofitable. The presence of losses means that the market has changed, and the re-optimization is necessary. The presence of profits means that the ATS does not need any re-optimization and recognizes market patterns quite well.
这个ea的再优化可在周末进行,即周六和周日,但仅在前面一周的结果是不盈利的。亏损的出现意味着市场已经改变,于是需要重新优化,若是仍然获利意味着这个ATS不需要重新优化,它对市场目前的模型的识别继续有效!
 
附源代码:
 

//+------------------------------------------------------------------+
//|                                                  Combo_Right.mq4 |
//|                               Copyright ?2008, Yury V. Reshetov |
//|                               http://bigforex.biz/load/2-1-0-171 |
//+------------------------------------------------------------------+
#property copyright "Copyright ?2008, Yury V. Reshetov"
#property link      "http://bigforex.biz/load/2-1-0-171"

//---- input parameters
extern double       tp1 = 50;
extern double       sl1 = 50;
extern int          p1 = 10;
extern int          x12 = 100;
extern int          x22 = 100;
extern int          x32 = 100;
extern int          x42 = 100;
extern double       tp2 = 50;
extern double       sl2 = 50;
extern int          p2 = 20;
extern int          x13 = 100;
extern int          x23 = 100;
extern int          x33 = 100;
extern int          x43 = 100;
extern double       tp3 = 50;
extern double       sl3 = 50;
extern int          p3 = 20;
extern int          x14 = 100;
extern int          x24 = 100;
extern int          x34 = 100;
extern int          x44 = 100;
extern int          p4 = 20;
extern int          pass = 1;
extern double       lots = 0.01;
extern int          mn = 888;
static int          prevtime = 0;
static double       sl = 10;
static double       tp = 10;



//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
   if (Time[0] == prevtime) return(0);
   prevtime = Time[0];
  
   if (! IsTradeAllowed()) {
      again();
      return(0);
   }
//----
   int total = OrdersTotal();
   for (int i = 0; i < total; i++) {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == mn) {
         return(0);
      }
   }
  
   sl = sl1;
   tp = tp1;
  
   int ticket = -1;
  
   RefreshRates();
  
   if (Supervisor() > 0) {
      ticket = OrderSend(Symbol(), OP_BUY, lots, Ask, 1, Bid - sl * Point, Bid + tp * Point, WindowExpertName(), mn, 0, Blue);
      if (ticket < 0) {
         again();     
      }
   } else {
      ticket = OrderSend(Symbol(), OP_SELL, lots, Bid, 1, Ask + sl * Point, Ask - tp * Point, WindowExpertName(), mn, 0, Red);
      if (ticket < 0) {
         again();
      }
   }
//-- Exit --
   return(0);
}
//+--------------------------- getLots ----------------------------------+


double Supervisor() {
   if (pass == 4) {
      if (perceptron3() > 0) {
         if (perceptron2() > 0) {
            sl = sl3;
            tp = tp3;
            return(1);
         }
       } else {
         if (perceptron1() < 0) {
            sl = sl2;
            tp = tp2;
            return(-1);
         }
      }
      return(basicTradingSystem());
   }

   if (pass == 3) {
      if (perceptron2() > 0) {
         sl = sl3;
         tp = tp3;
         return(1);
       } else {
         return(basicTradingSystem());
       }
   }

   if (pass == 2) {
      if (perceptron1() < 0) {
         sl = sl2;
         tp = tp2;
         return(-1);
       } else {
         return(basicTradingSystem());
       }

   }
   return(basicTradingSystem());
}

double perceptron1()   {
   double       w1 = x12 - 100;
   double       w2 = x22 - 100;
   double       w3 = x32 - 100;
   double       w4 = x42 - 100;
   double a1 = Close[0] - Open[p2];
   double a2 = Open[p2] - Open[p2 * 2];
   double a3 = Open[p2 * 2] - Open[p2 * 3];
   double a4 = Open[p2 * 3] - Open[p2 * 4];
   return(w1 * a1 + w2 * a2 + w3 * a3 + w4 * a4);
}

double perceptron2()   {
   double       w1 = x13 - 100;
   double       w2 = x23 - 100;
   double       w3 = x33 - 100;
   double       w4 = x43 - 100;
   double a1 = Close[0] - Open[p3];
   double a2 = Open[p3] - Open[p3 * 2];
   double a3 = Open[p3 * 2] - Open[p3 * 3];
   double a4 = Open[p3 * 3] - Open[p3 * 4];
   return(w1 * a1 + w2 * a2 + w3 * a3 + w4 * a4);
}

double perceptron3()   {
   double       w1 = x14 - 100;
   double       w2 = x24 - 100;
   double       w3 = x34 - 100;
   double       w4 = x44 - 100;
   double a1 = Close[0] - Open[p4];
   double a2 = Open[p4] - Open[p4 * 2];
   double a3 = Open[p4 * 2] - Open[p4 * 3];
   double a4 = Open[p4 * 3] - Open[p4 * 4];
   return(w1 * a1 + w2 * a2 + w3 * a3 + w4 * a4);
}

double basicTradingSystem() {
   return(iCCI(Symbol(), 0, p1, PRICE_OPEN, 0));
}

void again() {
   prevtime = Time[1];
   Sleep(30000);
}
扩展阅读
相关阅读
© CopyRight 2010-2021, PREDREAM.ORG, Inc.All Rights Reserved. 京ICP备13045924号-1