请 [注册] 或 [登录]  | 返回主站

量化交易吧 /  量化策略 帖子:3364780 新帖:16

基于大众交易系统和交易机器人优化点金术的专家

螺罗丝发表于:5 月 6 日 18:36回复(0)

简介

此类交易系统在更全局化的时间尺度中完全无法对市场趋势的变化做出反应。这会在市场状况发生变化时导致亏损,因为上述系统无法检测到这些变化。实际上,真实交易中基本不能使用仅以从一个时间范围的图表中获取的数据为基础的系统。正常操作中通常至少要用到两个时间范围。当前趋势通常是在较大时间范围的图表上确定的,而此趋势方向上的入市价位是基于较小时间范围的图表计算得出的。依我看来,之前文章中那些最简单的交易策略示例足以让读者学会如何设计此类系统。那么,现在让我们讨论一下如何根据上述推论改进此类交易系统。



使用两个时间范围的交易系统

从逻辑角度看,无论以之前文章中所述的哪个交易系统为基础,实际上都没有什么区别。我们将构建一个更为复杂的系统。至于其初始本质,每个最简单的交易系统都可用以下形式来表示:

对于多头仓:

对于空头仓:

在使用两个时间范围的交易系统中,这些入市条件将根据在较小时间范围上计算的指标来定义趋势方向将在较大时间范围上确定因此,包含这些条件的算法如下所示。:

对于多头仓:

对于空头仓:

在这种情况下,趋势变量仅定义较大时间范围上的当前趋势方向,而入市的其他条件将EA交易的交易行为限定至该全局趋势的方向上。从程序代码的角度看,无论使用哪种算法,最终也没有任何区别,都将在较大时间范围上检测当前趋势。所以这完全取决于EA编写者决定使用什么算法在较小时间范围上计算入市价位,并在较大时间范围上检测当前趋势。让我们分析早前提过的使用EA Exp_5.mq4呈示的OsMA振荡指标的算法,为了定义当前趋势,我们使用移动J2JMA.mq4。在这种情况下,交易定义条件将会非常简单:

那么现在我们要向现有Exp_5.mq4添加一些代码,以加入上述逻辑。完成的代码如下所示:

//对于EA操作,Metatrader \ EXPERTS \ indicators文件夹必须//包含指标5c_OsMA.mq4和J2JMA.mq4 // + ======================= =========================================== + // | Exp_11.mq4 | // | 版权所有©2008,Nikolay Kositsin | // | 哈巴罗夫斯克,farria @ mail.redcom.ru | // + =============================================== =================== + #properties copyright“Copyright©2008,Nikolay Kositsin”#property link“farria@mail.redcom.ru”// + ++ // EA输入参数用于购买TRADESextern bool Test_Up = true; //过滤交易计算directionextern double Money_Management_Up = 0.1; //输入自定义指标的参数J2JMA.mq4extern int TimeframeX_Up = 240; extern int Length1X_Up = 4; //第一个smoothingextern的深度int Phase1X_Up = 100; //第一次平滑的参数//在-100 ... +100范围内变化,影响*删除瞬态过程的质量; extern int Length2X_Up = 4; //第二个smoothingextern的深度int Phase2X_Up = 100; //第二次平滑的参数,//在-100 ... +100范围内变化,影响*删除瞬态过程的质量; extern int IPCX_Up = 0; / *选择指标所在的价格 //第二次平滑的参数,//在-100 ... +100范围内变化,影响*删除瞬态过程的质量; extern int IPCX_Up = 0; / *选择指标所在的价格 //第二次平滑的参数,//在-100 ... +100范围内变化,影响*删除瞬态过程的质量; extern int IPCX_Up = 0; / *选择指标所在的价格 计算(0-CLOSE,1-OPEN,2-HIGH,3-LOW,4-MEDIAN,5-TYPICAL, 6-WEIGHTED,7-Heiken Ashi Close,8-SIMPL,9-TRENDFOLLOW,10-0.5 * TRENDFOLLOW, 11-Heiken Ashi Low,12-Heiken Ashi High,13-Heiken Ashi Open, 14-Heiken Ashi Close。)* ///自定义指标的输入参数5c_OsMA.mq4extern int Timeframe_Up = 60; extern double IndLevel_Up = 0; // indicatorextern的突破级别为FastEMA_Up = 12; //快速EMA periodextern int SlowEMA_Up = 26; //慢EMA periodextern int SignalSMA_Up = 9; //信号SMA periodextern int STOPLOSS_Up = 50; //停止lossextern int TAKEPROFIT_Up = 100; //取profitextern int TRAILINGSTOP_Up = 0; // trailing stopextern int PriceLevel_Up = 40; //当前价格与//待定订单价格之间的差异triggeringextern bool ClosePos_Up = true; //允许强制仓位关闭// + ++ // EA INPUT参数卖出交易项目bool Test_Dn = true; //交易计算过滤器directionextern double Money_Management_Dn = 0.1; //自定义指标的输入参数J2JMA.mq4extern int TimeframeX_Dn = 240; extern int Length1X_Dn = 4; //平滑深度extern int Phase1X_Dn = 100; //第一次平滑的参数//在-100 ... +100范围内变化,影响*删除瞬态过程的质量; extern int Length2X_Dn = 4; // smoothing depthextern int Phase2X_Dn = 100; //第二次平滑的参数//在-100 ... +100范围内变化,影响*删除瞬态过程的质量; extern int IPCX_Dn = 0; / *选择指标所用的价格 //第一次平滑的参数//在-100 ... +100范围内变化,影响*删除瞬态过程的质量; extern int Length2X_Dn = 4; // smoothing depthextern int Phase2X_Dn = 100; //第二次平滑的参数//在-100 ... +100范围内变化,影响*删除瞬态过程的质量; extern int IPCX_Dn = 0; / *选择指标所用的价格 //第一次平滑的参数//在-100 ... +100范围内变化,影响*删除瞬态过程的质量; extern int Length2X_Dn = 4; // smoothing depthextern int Phase2X_Dn = 100; //第二次平滑的参数//在-100 ... +100范围内变化,影响*删除瞬态过程的质量; extern int IPCX_Dn = 0; / *选择指标所用的价格 计算(0-CLOSE,1-OPEN,2-HIGH,3-LOW,4-MEDIAN,5-TYPICAL, 6-WEIGHTED,7-Heiken Ashi Close,8-SIMPL,9-TRENDFOLLOW,10-0.5 * TRENDFOLLOW, 11-Heiken Ashi Low,12-Heiken Ashi High,13-Heiken Ashi Open, 14-Heiken Ashi Close。)* ///自定义指标的输入参数5c_OsMA.mq4extern int Timeframe_Dn = 60; extern double IndLevel_Dn = 0; //指示文件的突破级别为FastEMA_Dn = 12; //快速EMA periodextern int SlowEMA_Dn = 26; //慢EMA periodextern int SignalSMA_Dn = 9; //信号SMA periodextern int STOPLOSS_Dn = 50; //停止lossextern int TAKEPROFIT_Dn = 100; //取profitextern int TRAILINGSTOP_Dn = 0; // trailing stopextern int PriceLevel_Dn = 40; //当前价格与//挂单的价格之间的差异triggeringextern bool ClosePos_Dn = true; //允许强制位置关闭// + ++ //计算最小值的整数变量barsint MinBarX_Up,MinBar_Up,MinBarX_Dn,MinBar_Dn; // + =============================================== =================== + // | TimeframeCheck()函数| // + ========================================== ======================== + void TimeframeCheck(string Name,int Timeframe)  {// + //检查Timeframe变量值的正确性if(Timeframe!= 1)    if(Timeframe!= 5)     if(Timeframe!= 15)      if(Timeframe!= 30)       if(Timeframe!= 60)        if(Timeframe!= 240)         if(Timeframe!= 1440)           打印(StringConcatenate(“TimeframeCheck:Parameter”,Name,                     “不能”,“等于”,Timeframe,“!!!”)); // +} // + =========================================== ======================= + // | 自定义专家功能| // + =========================================== ======================= + #include <Lite_EXPERT1.mqh> // + ================ ================================================== + // | 自定义专家初始化函数| // + ========================================== ======================== + int init()  {//检查Timeframe_Up变量值TimeframeCheck(“Timeframe_Up”,Timeframe_Up)的正确性;    //检查TimeframeX_Up变量值TimeframeCheck(“TimeframeX_Up”,TimeframeX_Up)的正确性; //检查Timeframe_Dn变量值TimeframeCheck(“Timeframe_Dn”,Timeframe_Dn)的正确性;   //检查Timeframe_Dn变量值TimeframeCheck(“TimeframeX_Dn”,TimeframeX_Dn)的正确性; //变量的初始化MinBar_Up = 3 + MathMax(FastEMA_Up,SlowEMA_Up)+ SignalSMA_Up;   MinBarX_Up = 3 + 30 + 30;   MinBar_Dn = 3 + MathMax(FastEMA_Dn,SlowEMA_Dn)+ SignalSMA_Dn;   MinBarX_Dn = 3 + 30 + 30; //初始化结束返回(0);  } // + ============================================== ==================== + // | 专家取消初始化功能| // + =========================================== ======================= + int deinit()  {// +       // EA deinitialization返回结束(0); // +} // + =============================== =================================== + // | 自定义专家迭代功能| // + ========================================== ======================== + int start()  {   // +声明局部变量double J2JMA1,J2JMA2,Osc1,Osc2;   // +声明静态变量// + +  -  +静态双TrendX_Up,TrendX_Dn;   static datetime StopTime_Up,StopTime_Dn;    static int LastBars_Up,LastBarsX_Up,LastBarsX_Dn,LastBars_Dn;   static bool BUY_Sign,BUY_Stop,SELL_Sign,SELL_Stop;   // + +  -  + // ++代码以获得更长的位置| // + +  -  + if(Test_Up)     {      int IBARS_Up = iBars(NULL,Timeframe_Up);      int IBARSX_Up = iBars(NULL,TimeframeX_Up);            if(IBARS_Up> = MinBar_Up && IBARSX_Up> = MinBarX_Up)       {         // + ++ // +定义趋势| // + ++ if(LastBarsX_Up!= IBARSX_Up)          {           // +变量的初始化LastBarsX_Up = IBARSX_Up;           BUY_Stop = false;                      // +计算J2JMA的指标值J2JMA1 = iCustom(NULL,TimeframeX_Up,                                 “J2JMA”,长度1X_Up,长度2X_Up,                                             Phase1X_Up,Phase2X_Up,                                                       0,IPCX_Up,0,1);           //  -  J2JMA2 = iCustom(NULL,TimeframeX_Up,                                 “J2JMA”,长度1X_Up,长度2X_Up,                                             Phase1X_Up,Phase2X_Up,                                                       0,IPCX_Up,0,2);                      // +定义趋势TrendX_Up = J2JMA1  -  J2JMA2;           // +定义关闭交易的信号if(TrendX_Up <0)                      BUY_Stop = true;                                                }                   // + ++ // +为市场进入定义信号| // + ++ if(LastBars_Up!= IBARS_Up)          {           // +变量的初始化BUY_Sign = false;           LastBars_Up = IBARS_Up;           StopTime_Up = iTime(NULL,Timeframe_Up,0)                                          + 60 * Timeframe_Up;           // +计算指标值Osc1 = iCustom(NULL,Timeframe_Up,                          “5c_OsMA”,FastEMA_Up,SlowEMA_Up,                                               SignalSMA_Up,5,1);           //  -  Osc2 = iCustom(NULL,Timeframe_Up,                          “5c_OsMA”,FastEMA_Up,SlowEMA_Up,                                               SignalSMA_Up,5,2);                      // +定义交易信号if(TrendX_Up> 0)                                                       if(Osc2 <IndLevel_Up)             if(Osc1> IndLevel_Up)                        BUY_Sign = true;                                         }                   // + +  -  + // +交易的执行| // + +  -  + if(!OpenBuyLimitOrder1(BUY_Sign,1,               Money_Management_Up,STOPLOSS_Up,TAKEPROFIT_Up,                                            PriceLevel_Up,StopTime_Up))                                                                 返回(-1);         if(ClosePos_Up)                if(!CloseOrder1(BUY_Stop,1))                                        返回(-1);                                                 if(!Make_TreilingStop(1,TRAILINGSTOP_Up))                                              返回(-1);        }     }   // + +  -  + // ++代码用于短位置| // + +  -  + if(Test_Dn)     {      int IBARS_Dn = iBars(NULL,Timeframe_Dn);      int IBARSX_Dn = iBars(NULL,TimeframeX_Dn);            if(IBARS_Dn> = MinBar_Dn && IBARSX_Dn> = MinBarX_Dn)       {         // + ++ // +定义趋势| // + ++ if(LastBarsX_Dn!= IBARSX_Dn)          {           //  - 变量的初始化LastBarsX_Dn = IBARSX_Dn;           SELL_Stop = false;                      // +计算J2JMA的指标值J2JMA1 = iCustom(NULL,TimeframeX_Dn,                                 “J2JMA”,长度1X_Dn,长度2X_Dn,                                             Phase1X_Dn,Phase2X_Dn,                                                       0,IPCX_Dn,0,1);           //  -  J2JMA2 = iCustom(NULL,TimeframeX_Dn,                                 “J2JMA”,长度1X_Dn,长度2X_Dn,                                             Phase1X_Dn,Phase2X_Dn,                                                       0,IPCX_Dn,0,2);                      // +定义趋势TrendX_Dn = J2JMA1  -  J2JMA2;           // +定义关闭交易的信号if(TrendX_Dn> 0)                      SELL_Stop = true;                                                }                  // + ++ // +为市场进入定义信号| // + ++ if(LastBars_Dn!= IBARS_Dn)          {           // +变量的初始化SELL_Sign = false;           LastBars_Dn = IBARS_Dn;           StopTime_Dn = iTime(NULL,Timeframe_Dn,0)                                          + 60 * Timeframe_Dn;           // +计算指标值Osc1 = iCustom(NULL,Timeframe_Dn,                          “5c_OsMA”,FastEMA_Dn,SlowEMA_Dn,                                               SignalSMA_Dn,5,1);           //  -  Osc2 = iCustom(NULL,Timeframe_Dn,                          “5c_OsMA”,FastEMA_Dn,SlowEMA_Dn,                                               SignalSMA_Dn,5,2);                      // +定义交易信号if(TrendX_Dn <0)                                                       if(Osc2> IndLevel_Dn)             if(Osc1 <IndLevel_Dn)                        SELL_Sign = true;                                         }                   // + +  -  + // +交易的执行| // + +  -  + if(!OpenSellLimitOrder1(SELL_Sign,2,               Money_Management_Dn,STOPLOSS_Dn,TAKEPROFIT_Dn,                                            PriceLevel_Dn,StopTime_Dn))                                                                 返回(-1);          if(ClosePos_Dn)                if(!CloseOrder1(SELL_Stop,2))                                        返回(-1);                                                  if(!Make_TreilingStop(2,TRAILINGSTOP_Dn))                                                  返回(-1);        }     }   // + +  -  + // +         返回(0);  } // ++


尽管方法似乎并不那么复杂,但实际代码看起来比初始Exp_5mq4代码大两倍!现在我们来讨论这个结果。我还是只分析EA的多头仓部分,空头仓的部分是类似的。用于获取J2JMA指标的必要值的额外源代码如下:

// +计算J2JMA的指标值J2JMA1 = iCustom(NULL,TimeframeX_Up,“J2JMA”,Length1X_Up,Phase1X_Up,Length2X_Up,Phase2X_Up,0,IPCX_Up,0,1);           //  -  J2JMA2 = iCustom(NULL,TimeframeX_Up,“J2JMA”,Length1X_Up,Phase1X_Up,Length2X_Up,Phase2X_Up,0,IPCX_Up,0,2); // +定义趋势Trend_Up = J2JMA1  -  J2JMA2;


因此,EA开头部分现在包含六个新外部变量的声明,对应于J2JMA指标调用:

extern int TimeframeX_Up = 240; extern int Length1X_Up = 4; //第一个smoothingextern的深度int Phase1X_Up = 100; //第一次平滑的参数//在-100 ... +100范围内变化,影响*删除瞬态过程的质量; extern int Length2X_Up = 4; //第二个smoothingextern的深度int Phase2X_Up = 100; //第二次平滑的参数,//在-100 ... +100范围内变化,影响*删除瞬态过程的质量; extern int IPCX_Up = 0; / *选择指标所在的价格 计算(0-CLOSE,1-OPEN,2-HIGH,3-LOW,4-MEDIAN,5-TYPICAL, 6-WEIGHTED,7-Heiken Ashi Close,8-SIMPL,9-TRENDFOLLOW,10-0.5 * TRENDFOLLOW, 11-Heiken Ashi Low,12-Heiken Ashi High,13-Heiken Ashi Open, 14-Heiken Ashi Close。)* /


一个新的类似变量MinBarX_Up被添加到全局变量的声明行,以获取计算柱的最小值,它跟在EA外部变量的后面:

//计算最小值的整数变量barsint MinBarX_Up,MinBar_Up,MinBarX_Dn,MinBar_Dn;


在EA初始化块中,额外检查新外部变量TimeframeХ_Up的正确性:

//检查TimeframeX_Up变量值TimeframeCheck(“TimeframeХ_Up”,TimeframeX_Up)的正确性;


MinBarX_Up = 3 + 30 + 30;


在EA的start()函数块中执行进一步的代码修改。在本地变量声明行中添加两个新变量:J2JMA1和J2JMA2:

// +声明局部变量double J2JMA1,J2JMA2,Osc1,Osc2;


Trend_Up变量被声明为一个静态变量,因为它仅在柱变更时被初始化一次,它的值用于start()函数的进一步价格变动:

static double TrendX_Up,TrendX_Dn;


照更类推,变量LastBarsX_Up被声明为静态变量:

static int LastBars_Up,LastBarsX_Up,LastBarsX_Dn,LastBars_Dn;


在多头仓的代码中,检查计算的充分性变得更加复杂:

if(Test_Up)     {      int IBARS_Up = iBars(NULL,Timeframe_Up);      int IBARSX_Up = iBars(NULL,TimeframeX_Up);            if(IBARS_Up> = MinBar_Up && IBARSX_Up> = MinBarX_Up)       {         //长期代码}     }


并且添加了一个新的块:

         // + ++ // +定义趋势| // + ++ if(LastBarsX_Up!= IBARSX_Up)          {           // +变量的初始化LastBarsX_Up = IBARSX_Up;           BUY_Stop = false;                      // +计算J2JMA的指标值J2JMA1 = iCustom(NULL,TimeframeX_Up,                                 “J2JMA”,Length1X_Up,Phase1X_Up,                                            长度2X_Up,Phase2X_Up,                                                       0,IPCX_Up,0,1);           //  -  J2JMA2 = iCustom(NULL,TimeframeX_Up,                                 “J2JMA”,Length1X_Up,Phase1X_Up,                                            长度2X_Up,Phase2X_Up,                                                       0,IPCX_Up,0,2);                      // +定义趋势TrendX_Up = J2JMA1  -  J2JMA2;           // +定义关闭交易的信号if(Trend_Up <0)                      BUY_Stop = true;                                                }


在这个必要的块中,对变量Trend_Up初始化,此外还在此处定义强制平仓信号(BUY_Stop变量的初始化)。一般来说,在初始Exp_5.mq4中,最后一个变量在“定义市场进入的信号“程序块中初始化,但在新EA中,更合理的做法是将此初始化放在”DEFINING TREND“块中,并更改其初始化算法。

最重要的是在块“定义市场输入的信号”中对信号定义算法做一个小调整:

           // +定义交易信号if(TrendX_Up> 0)                                                       if(Osc2 <IndLevel_Up)             if(Osc1> IndLevel_Up)                        BUY_Sign = true;


完成所有修改后,此算法使用变量Trend_Up将当前趋势方向考虑在内。

现在来说一些关于EA优化的细节.EA一般都应仅针对多头仓或空头仓进行单独的优化,但即便在这种情况下,要优化的外部变量也非常多。也许同时优化所有这些变量并不合理。尤其是,优化的遗传算法不会优化八个以上的变量!这种情况下,最合适的解决方案是固定某些变量的值,仅优化其余未固定值的变量,即最紧急的变量,优化之后,选择最合适的方法,试着优化其余参数。

例如,对于多头仓,方法如下:

包含测试程序的这些设置的文件Exp_11.ini在TESTER.zip存档中。这里我们无需优化Money_Management_Up和TimeframeX_Up。至于TimeframeX_Up变量,要注意的是,它的值必须大于变量Timeframe_Up的值.Length1X_Up的值的可变量围相当大,而Phase1X_Up的值的范围为-100至100.长好2X_Up,Phase2X_Up和IPCX_Up的值,我在上一篇文章中介绍Exp_5.mq4时提到的IndLevel_Up参数也是如此。对于FastEMA_Up和SlowEMA_Up参数,参数变更的下限值不应太小。当然,它们可以表现出令人惊异的结果,但这种结果有任何意义吗?优化之后还应检查使用追踪止损位是否合理。但在趋势变化时,应始终通过逻辑变量ClosePos_Up强制平仓。最好将其值固定为“true”。

优化期间,策略测试程序中的图表周期应等于变量Timeframe_Up或Timeframe_Dn的值(取决于优化期间的交易方向),在某个帐户上进行最终测试或操作时,图表周期应设为等于这些值的最小此专家顾问使用至少两个时间范围,因此在帐户上下载用于优化,测试和操作的历史数据之时要留心这一点,尤其是你使用在不同经销商开。值。还有个重要的细节。立的多个帐户时更要注意。

第四篇文章中,我介绍了如何导出Microsoft Excel中进一步统计分析的优化结果。在我看来,这篇文章中提供的EA最适合此类程序。如果有人要试用这个EA,我用帐号修改了此EA代码,作为本文的推荐EA(Exp_11_2.mq4)。此代码附于本文之后。



针对不同时间范围的两个图表的计算数据的另一个EA使用示例

我想,仅仅一个基于此理念的EA示例还不足以供本文使用,所以我会再加一个根据此原理构建的EA交易。我将以我第一篇文章中的第一个EA Exp_1.mq4为基础。用于定义入市条件和管理仓位的代码部分已准备就绪。现在我们需要定义较大时间范围的活跃市场趋势。在此EA交易中,我使用指标MAMA_NK.mq4:

在这种情况下,定义趋势方向的条件是第一个柱上两个移动之间的差值:

让我们用类似方式编写一段代码,以Exp_11.mq4的代码为模板:

// + =============================================== =================== + // | Exp_12.mq4 | // | 版权所有©2008,Nikolay Kositsin | // | 哈巴罗夫斯克,farria @ mail.redcom.ru | // + =============================================== =================== + #properties copyright“Copyright©2008,Nikolay Kositsin”#property link“farria@mail.redcom.ru”// + +  -  + / / EA INPUT PARAMETERS for BUY TRADESExtern bool Test_Up = true; //过滤交易计算directionextern double Money_Management_Up = 0.1; // extern int TimeframeX_Up = 240; extern double FastLimitX_Up = 0.5; extern double SlowLimitX_Up = 0.05; extern int IPCX_Up = 9; / *选择价格,计算指标(0-CLOSE,1-OPEN,2-HIGH,3-LOW,4-MEDIAN,5-TYPICAL,6-WEIGHTED, 7-Heiken Ashi Close,8-SIMPL,9-TRENDFOLLOW,10-0.5 * TRENDFOLLOW, 11-Heiken Ashi Low,12-Heiken Ashi High,13-Heiken Ashi Open, 14-Heiken Ashi Close,15-Heiken Ashi Open0。)* /// +  -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -  + extern int Timeframe_Up = 60; extern int Length_Up = 4; // smoothing depthextern int Phase_Up = 100; //参数在//  -  100 ... +100范围内变化,影响瞬态过程的质量; extern int IPC_Up = 0; / *选择价格,指标将在此基础上 计算(0-CLOSE,1-OPEN,2-HIGH,3-LOW,4-MEDIAN,5-TYPICAL, 6-WEIGHTED,7-Heiken Ashi Close,8-SIMPL,9-TRENDFOLLOW,10-0.5 * TRENDFOLLOW, 11-Heiken Ashi Low,12-Heiken Ashi High,13-Heiken Ashi Open, 14-Heiken Ashi Close。)* / extern int STOPLOSS_Up = 50; //停止lossextern int TAKEPROFIT_Up = 100; //采用profitextern bool ClosePos_Up = true; //允许强制仓位关闭// + +  -  + //卖出交易的EA输入参数extern bool Test_Dn = true; //交易计算过滤器directionextern double Money_Management_Dn = 0.1; // extern int TimeframeX_Dn = 60; extern double FastLimitX_Dn = 0.5; extern double SlowLimitX_Dn = 0.05; extern int IPCX_Dn = 9; / *选择价格,计算指标 (0-CLOSE,1-OPEN,2-HIGH,3-LOW,4-MEDIAN,5-TYPICAL,6-WEIGHTED, 7-Heiken Ashi Close,8-SIMPL,9-TRENDFOLLOW,10-0.5 * TRENDFOLLOW, 11-Heiken Ashi Low,12-Heiken Ashi High,13-Heiken Ashi Open, 14-Heiken Ashi Close,15-Heiken Ashi Open0。)* /// +  -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -  + extern int Timeframe_Dn = 60; extern int Length_Dn = 4; // smoothing depthextern int Phase_Dn = 100; //参数在// -100 ... +100范围内变化,会影响瞬态过程的质量; extern int IPC_Dn = 0; / *选择指标所依据的价格 计算(0-CLOSE,1-OPEN,2-HIGH,3-LOW,4-MEDIAN,5-TYPICAL, 6-WEIGHTED,7-Heiken Ashi Close,8-SIMPL,9-TRENDFOLLOW,10-0.5 * TRENDFOLLOW, 11-Heiken Ashi Low,12-Heiken Ashi High,13-Heiken Ashi Open, 14-Heiken Ashi Close。)* / extern int STOPLOSS_Dn = 50; //停止lossextern int TAKEPROFIT_Dn = 100; //取profitextern bool ClosePos_Dn = true; //允许强制关闭位置// + +  -  + //最小计数barsint的整数变量MinBar_Up,MinBar_Dn,MinBarX_Up,MinBarX_Dn; // + ================ ================================================== + // | 自定义专家功能| // + =========================================== ======================= + #include <Lite_EXPERT1.mqh> // + ================ ================================================== + // | TimeframeCheck()函数| // + ========================================== ======================== + void TimeframeCheck(string Name,int Timeframe)  {// + //检查Timeframe变量值的正确性if(Timeframe!= 1)    if(Timeframe!= 5)     if(Timeframe!= 15)      if(Timeframe!= 30)       if(Timeframe!= 60)        if(Timeframe!= 240)         if(Timeframe!= 1440)           打印(StringConcatenate(“参数”,名称,                     “不能”,“等于”,Timeframe,“!!!”)); // +} // + =========================================== ======================= + // | 自定义专家初始化函数| // + ========================================== ======================== + int init()  {//检查Timeframe_Up变量值的正确性TimeframeCheck(“TimeframeX_Up”,TimeframeX_Up); //检查Timeframe_Up变量值的正确性TimeframeCheck(“Timeframe_Up”,Timeframe_Up); //检查Timeframe_Dn变量值的正确性TimeframeCheck(“TimeframeX_Dn” “,TimeframeX_Dn); //检查Timeframe_Dn变量值的正确性TimeframeCheck(”Timeframe_Dn“,Timeframe_Dn); //变量的初始化MinBarX_Up = 1 + 7;   MinBar_Up = 4 + 39 + 30;   MinBarX_Dn = 1 + 7;   MinBar_Dn = 4 + 39 + 30;                                       //初始化结束返回(0);  } // + ============================================== ==================== + // | 专家取消初始化功能| // + =========================================== ======================= + int deinit()  {// +       // EA deinitialization返回结束(0); // +} // + =============================== =================================== + // | 自定义专家迭代功能| // + ========================================== ======================== + int start()  {   // +局部变量声明int bar;   double Mov [3],dMov12,dMov23,Mama1,Fama1;   // +静态变量的声明static double TrendX_Up,TrendX_Dn;   static int LastBars_Up,LastBars_Dn,LastBarsX_Up,LastBarsX_Dn;   static bool BUY_Sign,BUY_Stop,SELL_Sign,SELL_Stop;      // + +  -  + // ++代码以获得更长的位置| // + +  -  + if(Test_Up)     {      int IBARS_Up = iBars(NULL,Timeframe_Up);      int IBARSX_Up = iBars(NULL,TimeframeX_Up);            if(IBARS_Up> = MinBar_Up && IBARSX_Up> = MinBarX_Up)       {                // + ++ // +定义趋势| // + ++ if(LastBarsX_Up!= IBARSX_Up)          {           // +变量的初始化LastBarsX_Up = IBARSX_Up;           BUY_Stop = false;                      // +计算指标值Fama1 = iCustom(NULL,TimeframeX_Up,                     “MAMA_NK”,FastLimitX_Up,SlowLimitX_Up,IPCX_Up,0,1);           //  -  Mama1 = iCustom(NULL,TimeframeX_Up,                     “MAMA_NK”,FastLimitX_Up,SlowLimitX_Up,IPCX_Up,1,1);           // +定义趋势TrendX_Up = Mama1  -  Fama1;           // +定义交易结束的信号if(TrendX_Up <0)                      BUY_Stop = true;                                                }                  // + ++ // +为市场进入定义信号| // + ++ if(LastBars_Up!= IBARS_Up)          {           // +变量的初始化BUY_Sign = false;           LastBars_Up = IBARS_Up;                       // +计算指标值并上传i * buffer for(bar = 1; bar <= 3; bar ++)                     Mov [bar  -  1] =                                           iCustom(NULL,Timeframe_Up,                                 “JFatl”,Length_Up,Phase_Up,                                                    0,IPC_Up,0,bar);                      // +定义交易信号dMov12 = Mov [0]  -  Mov [1];           dMov23 = Mov [1]  -  Mov [2];                        if(TrendX_Up> 0)                                   if(dMov23 <0)                if(dMov12> 0)                        BUY_Sign = true;                                              }                  // + +  -  + // +交易的执行| // + +  -  + if(!OpenBuyOrder1(BUY_Sign,1,Money_Management_Up,                                           STOPLOSS_Up,TAKEPROFIT_Up))                                                                 返回(-1);          if(ClosePos_Up)                if(!CloseOrder1(BUY_Stop,1))                                        返回(-1);        }     }        // + +  -  + // ++代码用于短位置| // + +  -  + if(Test_Dn)     {      int IBARS_Dn = iBars(NULL,Timeframe_Dn);      int IBARSX_Dn = iBars(NULL,TimeframeX_Dn);            if(IBARS_Dn> = MinBar_Dn && IBARSX_Dn> = MinBarX_Dn)       {         // + ++ // +定义趋势| // + ++ if(LastBarsX_Dn!= IBARSX_Dn)          {           // +变量的初始化LastBarsX_Dn = IBARSX_Dn;           SELL_Stop = false;                      // +计算指标值Fama1 = iCustom(NULL,TimeframeX_Dn,                     “MAMA_NK”,FastLimitX_Dn,SlowLimitX_Dn,IPCX_Dn,0,1);           //  -  Mama1 = iCustom(NULL,TimeframeX_Dn,                     “MAMA_NK”,FastLimitX_Dn,SlowLimitX_Dn,IPCX_Dn,1,1);           // +定义趋势TrendX_Dn = Mama1  -  Fama1;           // +定义交易结束的信号if(TrendX_Dn> 0)                      SELL_Stop = true;                                                }                  // + ++ // +为市场进入定义信号| // + ++ if(LastBars_Dn!= IBARS_Dn)          {           // +变量的初始化SELL_Sign = false;                      LastBars_Dn = IBARS_Dn;                       // +计算指标值并上传i * buffer for(bar = 1; bar <= 3; bar ++)                     Mov [bar  -  1] =                                           iCustom(NULL,Timeframe_Dn,                                 “JFatl”,Length_Dn,Phase_Dn,                                                    0,IPC_Dn,0,bar);                      // +定义交易信号dMov12 = Mov [0]  -  Mov [1];           dMov23 = Mov [1]  -  Mov [2];                        if(TrendX_Dn <0)                                               if(dMov23> 0)                if(dMov12 <0)                       SELL_Sign = true;                                                   }                   // + +  -  + // +交易的执行| // + +  -  + if(!OpenSellOrder1(SELL_Sign,2,Money_Management_Dn,                                             STOPLOSS_Dn,TAKEPROFIT_Dn))                                                                   返回(-1);          if(ClosePos_Dn)                if(!CloseOrder1(SELL_Stop,2))                                        返回(-1);        }     } // +         返回(0);  } // ++


尽管此EA的基本算法不同于前一个EA,但这种情况下,使用两个图表的基本理念看上去仍是绝对可行的。



总结

我认为本文所述的自动交易系统构建方法将帮助已有一些EA编写经验的读者用最少的精力构建类似的Expert Advisor。这里还要说明一点,即此类Expert Advisor的实用价值很大程度上取决于其正确的优化。


全部回复

0/140

量化课程

    移动端课程