首页 > 返佣资讯 > EA测试 > 为您的交易机器人设置虚假触发保护...

为您的交易机器人设置虚假触发保护(三)

发布时间:

上文我们提到了在错误触发的例子中,对于整体算法进行修整可能是最简单也最有效的方式,而时间因素是最可已确定的标准因素,并提出,需要记录正确的开单时间和算法参数,且需要禁止后续的未经计算而触发的条目。

解决方法其实很简单。首先,我将写下一段代码,而后再解释更多细节。这是一段需要替代EA算法的辅助性代码。

//+------------------------------------------------------------------+
//|                                                  MACD Sample.mq4 |
//|                   Copyright 2005-2014, MetaQuotes Software Corp. |
//|                                              https://www.mql4.com |
//+------------------------------------------------------------------+

#property copyright   "2005-2014, MetaQuotes Software Corp."

#property link        "https://www.mql4.com"

input double TakeProfit    =50;
input double Lots          =0.1;
input double TrailingStop  =30;
input double MACDOpenLevel =3;
input double MACDCloseLevel=2;
input int    MATrendPeriod =26;
//--- enter new variable (value in seconds for 1 bar of this TF, for М15 equals 60 с х 15 = 900 с)
datetime Time_open=900;
//--- enter new variable (time of opening the bar with 1st entry)
datetime Time_bar = 0;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick(void)
  {
   double MacdCurrent,MacdPrevious;
   double SignalCurrent,SignalPrevious;
   double MaCurrent,MaPrevious;
   int    cnt,ticket,total;
//---
// initial data checks
// it is important to make sure that the expert works with a normal
// chart and the user did not make any mistakes setting external
// variables (Lots, StopLoss, TakeProfit,
// TrailingStop) in our case, we check TakeProfit
// on a chart of less than 100 bars
//---
   if(Bars<100)
     {
      Print("bars less than 100");
      return;
     }
   if(TakeProfit<10)
     {
      Print("TakeProfit less than 10");
      return;
     }
//--- to simplify the coding and speed up access data are put into internal variables
   MacdCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0);
   MacdPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);
   SignalCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0);
   SignalPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1);
   MaCurrent=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,0);
   MaPrevious=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,1);

   total=OrdersTotal();
   if(total<1)      {       //--- no opened orders identified       if(AccountFreeMargin()<(1000Lots))         {          Print("We have no money. Free Margin = ",AccountFreeMargin());          return;         }       //--- check for long position (BUY) possibility              //--- enter new string (removes ban on repeated entry if a new bar is opened)       if( (TimeCurrent() - Time_bar) > 900 ) Time_open = 900;              if(MacdCurrent<0 && MacdCurrent>SignalCurrent && MacdPrevious(MACDOpenLevelPoint) && MaCurrent>MaPrevious &&
         (TimeCurrent()-Time[0])0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
             {
              Print("BUY order opened : ",OrderOpenPrice());
              Time_open = TimeCurrent()-Time[0]; //enter new string (store interval from the bar opening time with the entry until the exit moment)
              Time_bar = Time[0]; //enter new string (remember opening time of the bar that had 1st entry)
             }
           }
         else
            Print("Error opening BUY order : ",GetLastError());
         return;
        }

更多说明:

用来替代我们所关联的绝对时间——从打开正确的蜡烛图到进入的时间间隙这个值是需要被具体比较后设定的,可能是相当大的值,将会允许第一次进入时就被触发。在开单的位置我们改变了开单时间的值,通过写入这里的时间间隙值。从进入任何项目开始的时间,值是(TimeCurrent)- Time[0]将会超过我们写下的进入点的值,然后(TimeCurrent() - Time[0]) < Time_open这一向将会提示为不可能的,因此取消该错误触发。

以这种方式,不任何进入单的数量和价格变化幅度的分析,我们就能够解决虚假触发的问题。

下一次我们将会提供一个具体的示例性EA代码,为大家展示是如何插入代码和对代码运行的结果起到了怎样的调整作用的。

柚子返佣网提醒您,投资金融产品会有承担损失的风险,请理性投资。关注柚子返佣网,为您炒货币对、炒期货带来更多相关金融资讯、更高返佣比例、更简单的线上开户模式!

柚子返佣交易流程

  • 免费注册
  • 下载软件
  • 在线开户
  • 注入资金
  • 立即返佣