EA: 双均线交叉智能系统 - 页 2

 

嗯,我在第 281 和 287 行遇到了编译错误 init 失败。因此,我无法运行任何程序=(

 
Александр Князев:

嗯,我在第 281 和 287 行遇到了编译错误 init 失败。因此,我无法运行任何程序=(

请注意下载的内容和地点。目前从 KodoBase 的俄罗斯部分下载了 1.003 版,编译时没有出错。

 

代码

喃喃自语

在您的电脑上输入密码

您有什么问题?

xin cảm ơn.


 
Le Nam :

cảm ơn bạn về 代码。

粵語詞彙

khi nén lại n ó báo lỗi

您是否知道自己在做什么?

xin cảm ơn.


1.003 版可从俄罗斯论坛获取: https: //www.mql5.com/ru/code/16783

 

Hi、

感谢您的 EA,我对它进行了修改,以改变 MA 周期,它的盈利能力很强。

以下是修改后的代码

//+------------------------------------------------------------------+
//| 2MA Bunny Cross Expert(barabashkakvn's edition).mq5 | //| 2MA Bunny Cross Expert(barabashkakvn's edition).mq5 |
//|罗恩-汤普森版权所有
//|http://www.lightpatch.com/forex/ | |
//+------------------------------------------------------------------+
#property copyright "Ron Thompson"
#property link      "http://www.lightpatch.com/forex"
#property version   "1.003"
//---
#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>  
#include <Trade\AccountInfo.mqh>
#include <Expert\Money\MoneyFixedMargin.mqh>
CPositionInfo  m_position;                   // 交易位置对象
CTrade         m_trade;                      // 交易对象
CSymbolInfo    m_symbol;                     // 符号信息对象
CAccountInfo   m_account;                    // 账户信息包装器
CMoneyFixedMargin *m_money;
//--- 输入参数
input double   InpLots     = 0;              // 手数(或 "手数">0,"风险"==0,或 "手数"==0,"风险">0)
input double   Risk        = 5;              // 风险(或 "手数">0,"风险"==0 或 "手数"==0,"风险">0)
input int PeriodMAS=20;                      // Period of the fast MA
input int PeriodMAF=5;                       


input ulong    m_magic=319906800;      // 神奇数字
//---
ulong          m_slippage=10;                // 滑动
int    handle_iMA_5;                         // 用于存储 iMA 指标句柄的变量 
int    handle_iMA_20;                        // 用于存储 iMA 指标句柄的变量 
//+------------------------------------------------------------------+
//| 专家初始化函数|
//+------------------------------------------------------------------+
int OnInit()
  {
   if(!m_symbol.Name(Symbol())) // 设置符号名称
      return(INIT_FAILED);
   RefreshRates();
//---
   m_trade.SetExpertMagicNumber(m_magic);
   m_trade.SetMarginMode();
   m_trade.SetTypeFillingBySymbol(m_symbol.Name());
   m_trade.SetDeviationInPoints(m_slippage);
//--- 调整 3 或 5 位数
   int digits_adjust=1;
   if(m_symbol.Digits()==3 || m_symbol.Digits()==5)
      digits_adjust=10;
//---
   if(!LotsOrRisk(InpLots,Risk,digits_adjust))
      return(INIT_PARAMETERS_INCORRECT);
//---
   if(m_money!=NULL)
      delete m_money;
   m_money=new CMoneyFixedMargin;
   if(m_money!=NULL)
     {
      if(!m_money.Init(GetPointer(m_symbol),Period(),m_symbol.Point()*digits_adjust))
         return(INIT_FAILED);
      m_money.Percent(Risk);
     }
   else
     {
      Print(__FUNCTION__,", ERROR: Object CMoneyFixedMargin is NULL");
      return(INIT_FAILED);
     }
//--- 创建指标 iMA 的句柄
   handle_iMA_5=iMA(m_symbol.Name(),Period(),PeriodMAF,0,MODE_SMA,PRICE_WEIGHTED);
//--- 如果没有创建句柄 
   if(handle_iMA_5==INVALID_HANDLE)
     {
      //--- 说明失败原因并输出错误代码 
      PrintFormat("Failed to create handle of the iMA indicator for the symbol %s/%s, error code %d",
                  m_symbol.Name(),
                  EnumToString(Period()),
                  GetLastError());
      //---指标提前停止 
      return(INIT_FAILED);
     }
//--- 创建指标 iMA 的句柄
   handle_iMA_20=iMA(m_symbol.Name(),Period(),PeriodMAS,3,MODE_SMA,PRICE_WEIGHTED);
//--- 如果没有创建句柄 
   if(handle_iMA_20==INVALID_HANDLE)
     {
      //--- 说明失败原因并输出错误代码 
      PrintFormat("Failed to create handle of the iMA indicator for the symbol %s/%s, error code %d",
                  m_symbol.Name(),
                  EnumToString(Period()),
                  GetLastError());
      //---指标提前停止 
      return(INIT_FAILED);
     }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| 专家去初始化函数|
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   if(m_money!=NULL)
      delete m_money;
  }
//+------------------------------------------------------------------+
//| 专家勾选功能|
//+------------------------------------------------------------------+
void OnTick()
  {
//--- 我们只在新栏诞生时工作
   static datetime PrevBars=0;
   datetime time_0=iTime(m_symbol.Name(),Period(),0);
   if(time_0==PrevBars)
      return;
   PrevBars=time_0;
//---

   double ArrayMA5[];
   ArraySetAsSeries(ArrayMA5,true);
   double ArrayMA20[];
   ArraySetAsSeries(ArrayMA20,true);
   if(!iMAGetArray(handle_iMA_5,0,3,ArrayMA5) || !iMAGetArray(handle_iMA_20,0,3,ArrayMA20) || !RefreshRates())
     {
      PrevBars=0;
      return;
     }
//---
   bool signal_buy=(ArrayMA5[2]<ArrayMA20[2] && ArrayMA5[1]>ArrayMA20[1]);
   bool signal_sell=(ArrayMA5[2]>ArrayMA20[2] && ArrayMA5[1]<ArrayMA20[1]);
//---
   if(signal_buy)
     {
      ClosePositions(POSITION_TYPE_SELL);
      OpenBuy(0.0,0.0);
     }
   if(signal_sell)
     {
      ClosePositions(POSITION_TYPE_BUY);
      OpenSell(0.0,0.0);
     }
//---

  }
//+------------------------------------------------------------------+
//| TradeTransaction 函数|
//+------------------------------------------------------------------+
void OnTradeTransaction(const MqlTradeTransaction &trans,
                        const MqlTradeRequest &request,
                        const MqlTradeResult &result)
  {
//---

  }
//+------------------------------------------------------------------+
//| 获取数组中 iMA 缓冲区的值
//+------------------------------------------------------------------+
bool iMAGetArray(const int handle_iMA,const int start_pos,const int count,double &arr_buffer[])
  {
//---
   bool result=true;
   if(!ArrayIsDynamic(arr_buffer))
     {
      Print("This a no dynamic array!");
      return(false);
     }
   ArrayFree(arr_buffer);
   int       buffer_num=0;          // 指示器缓冲区编号 
//--- 重置错误代码 
   ResetLastError();
//--- 用索引为 0 的指示器缓冲区中的值填充 iMABuffer 数组的一部分 
   int copied=CopyBuffer(handle_iMA,buffer_num,start_pos,count,arr_buffer);
   if(copied<0)
     {
      //--- 如果复制失败,请告知错误代码 
      PrintFormat("Failed to copy data from the iMA indicator, error code %d",GetLastError());
      //----结果为零时退出----这意味着指标被视为未计算 
      return(false);
     }
   else if(copied<count)
     {
      PrintFormat("Moving Average indicator: %d elements from %d were copied",copied,count);
      DebugBreak();
      return(false);
     }
//---
   return(result);
  }
//+------------------------------------------------------------------+
//| 关闭位置|
//+------------------------------------------------------------------+
void ClosePositions(const ENUM_POSITION_TYPE pos_type)
  {
   for(int i=PositionsTotal()-1;i>=0;i--) // 返回当前位置的数量
      if(m_position.SelectByIndex(i))     // 通过索引选择位置,以便进一步访问其属性
         if(m_position.Symbol()==m_symbol.Name() && m_position.Magic()==m_magic)
            if(m_position.PositionType()==pos_type) // 获取位置类型
               m_trade.PositionClose(m_position.Ticket()); // 用指定的符号关闭一个位置
  }
//+------------------------------------------------------------------+
//| 刷新符号报价数据|
//+------------------------------------------------------------------+
bool RefreshRates(void)
  {
//--- 刷新率
   if(!m_symbol.RefreshRates())
     {
      Print("RefreshRates error");
      return(false);
     }
//--- 防止返回值为 "零"
   if(m_symbol.Ask()==0 || m_symbol.Bid()==0)
      return(false);
//---
   return(true);
  }
//+------------------------------------------------------------------+
//| 检查位置音量的正确性
//+------------------------------------------------------------------+
bool CheckVolumeValue(double volume,string &error_description)
  {
//--- 允许的最低贸易量
   double min_volume=m_symbol.LotsMin();
   if(volume<min_volume)
     {
      error_description=StringFormat("Volume is less than the minimal allowed SYMBOL_VOLUME_MIN=%.2f",min_volume);
      return(false);
     }
//-- 允许的最大贸易业务量
   double max_volume=m_symbol.LotsMax();
   if(volume>max_volume)
     {
      error_description=StringFormat("Volume is greater than the maximal allowed SYMBOL_VOLUME_MAX=%.2f",max_volume);
      return(false);
     }
//--- 获取体积变化的最小步长
   double volume_step=m_symbol.LotsStep();
   int ratio=(int)MathRound(volume/volume_step);
   if(MathAbs(ratio*volume_step-volume)>0.0000001)
     {
      error_description=StringFormat("Volume is not a multiple of the minimal step SYMBOL_VOLUME_STEP=%.2f, the closest correct volume is %.2f",
                                     volume_step,ratio*volume_step);
      return(false);
     }
   error_description="Correct volume value";
   return(true);
  }
//+------------------------------------------------------------------+
//| 自由保证金交易的手数或风险(百分比) //| 自由保证金交易的手数或风险(百分比) //| 自由保证金交易的手数或风险(百分比) //...
//+------------------------------------------------------------------+
bool LotsOrRisk(const double lots,const double risk,const int digits_adjust)
  {
   if(lots<0.0 && risk<0.0)
     {
      Print(__FUNCTION__,", ERROR: Parameter (\"lots\" or \"risk\") can't be less than zero");
      return(false);
     }
   if(lots==0.0 && risk==0.0)
     {
      Print(__FUNCTION__,", ERROR: Trade is impossible: You have set \"lots\" == 0.0 and \"risk\" == 0.0");
      return(false);
     }
   if(lots>0.0 && risk>0.0)
     {
      Print(__FUNCTION__,", ERROR: Trade is impossible: You have set \"lots\" > 0.0 and \"risk\" > 0.0");
      return(false);
     }
   if(lots>0.0)
     {
      string err_text="";
      if(!CheckVolumeValue(lots,err_text))
        {
         Print(__FUNCTION__,", ERROR: ",err_text);
         return(false);
        }
     }
   else if(risk>0.0)
     {
      if(m_money!=NULL)
         delete m_money;
      m_money=new CMoneyFixedMargin;
      if(m_money!=NULL)
        {
         if(!m_money.Init(GetPointer(m_symbol),Period(),m_symbol.Point()*digits_adjust))
            return(false);
         m_money.Percent(risk);
        }
      else
        {
         Print(__FUNCTION__,", ERROR: Object CMoneyFixedMargin is NULL");
         return(false);
        }
     }
//---
   return(true);
  }
//+------------------------------------------------------------------+
//| 打开买入仓位|
//+------------------------------------------------------------------+
void OpenBuy(double sl,double tp)
  {
   sl=m_symbol.NormalizePrice(sl);
   tp=m_symbol.NormalizePrice(tp);

   double check_open_long_lot=0.0;
   if(Risk>0.0)
     {
      check_open_long_lot=m_money.CheckOpenLong(m_symbol.Ask(),sl);
      Print("sl=",DoubleToString(sl,m_symbol.Digits()),
            ", CheckOpenLong: ",DoubleToString(check_open_long_lot,2),
            ", Balance: ",    DoubleToString(m_account.Balance(),2),
            ", Equity: ",     DoubleToString(m_account.Equity(),2),
            ", FreeMargin: ", DoubleToString(m_account.FreeMargin(),2));
      if(check_open_long_lot==0.0)
        {
         Print(__FUNCTION__,", ERROR: method CheckOpenLong returned the value of \"0.0\"");
         return;
        }
     }
   else
      check_open_long_lot=InpLots;
//--- 在发送订单前检查交易量,以避免出现 "资金不足 "错误 (CTrade)
   double check_volume_lot=m_trade.CheckVolume(m_symbol.Name(),check_open_long_lot,m_symbol.Ask(),ORDER_TYPE_BUY);

   if(check_volume_lot!=0.0)
     {
      if(check_volume_lot>=check_open_long_lot)
        {
         if(m_trade.Buy(check_open_long_lot,NULL,m_symbol.Ask(),sl,tp))
           {
            if(m_trade.ResultDeal()==0)
              {
               Print("#1 Buy -> false. Result Retcode: ",m_trade.ResultRetcode(),
                     ", description of result: ",m_trade.ResultRetcodeDescription());
               PrintResult(m_trade,m_symbol);
              }
            else
              {
               Print("#2 Buy -> true. Result Retcode: ",m_trade.ResultRetcode(),
                     ", description of result: ",m_trade.ResultRetcodeDescription());
               PrintResult(m_trade,m_symbol);
              }
           }
         else
           {
            Print("#3 Buy -> false. Result Retcode: ",m_trade.ResultRetcode(),
                  ", description of result: ",m_trade.ResultRetcodeDescription());
            PrintResult(m_trade,m_symbol);
           }
        }
      else
        {
         string text="";
         if(Risk>0.0)
            text="< method CheckOpenLong ("+DoubleToString(check_open_long_lot,2)+")";
         else
            text="< Lots ("+DoubleToString(InpLots,2)+")";
         Print(__FUNCTION__,", ERROR: method CheckVolume (",DoubleToString(check_volume_lot,2),") ",
               text);
         return;
        }
     }
   else
     {
      Print(__FUNCTION__,", ERROR: method CheckVolume returned the value of \"0.0\"");
      return;
     }
//---
  }
//+------------------------------------------------------------------+
//| 打开卖出仓位|
//+------------------------------------------------------------------+
void OpenSell(double sl,double tp)
  {
   sl=m_symbol.NormalizePrice(sl);
   tp=m_symbol.NormalizePrice(tp);

   double check_open_short_lot=0.0;
   if(Risk>0.0)
     {
      check_open_short_lot=m_money.CheckOpenShort(m_symbol.Bid(),sl);
      Print("sl=",DoubleToString(sl,m_symbol.Digits()),
            ", CheckOpenLong: ",DoubleToString(check_open_short_lot,2),
            ", Balance: ",    DoubleToString(m_account.Balance(),2),
            ", Equity: ",     DoubleToString(m_account.Equity(),2),
            ", FreeMargin: ", DoubleToString(m_account.FreeMargin(),2));
      if(check_open_short_lot==0.0)
        {
         Print(__FUNCTION__,", ERROR: method CheckOpenShort returned the value of \"0.0\"");
         return;
        }
     }
   else
      check_open_short_lot=InpLots;
//--- 在发送订单前检查交易量,以避免出现 "资金不足 "错误 (CTrade)
   double check_volume_lot=m_trade.CheckVolume(m_symbol.Name(),check_open_short_lot,m_symbol.Bid(),ORDER_TYPE_SELL);

   if(check_volume_lot!=0.0)
     {
      if(check_volume_lot>=check_open_short_lot)
        {
         if(m_trade.Sell(check_open_short_lot,NULL,m_symbol.Bid(),sl,tp))
           {
            if(m_trade.ResultDeal()==0)
              {
               Print("#1 Sell -> false. Result Retcode: ",m_trade.ResultRetcode(),
                     ", description of result: ",m_trade.ResultRetcodeDescription());
               PrintResult(m_trade,m_symbol);
              }
            else
              {
               Print("#2 Sell -> true. Result Retcode: ",m_trade.ResultRetcode(),
                     ", description of result: ",m_trade.ResultRetcodeDescription());
               PrintResult(m_trade,m_symbol);
              }
           }
         else
           {
            Print("#3 Sell -> false. Result Retcode: ",m_trade.ResultRetcode(),
                  ", description of result: ",m_trade.ResultRetcodeDescription());
            PrintResult(m_trade,m_symbol);
           }
        }
      else
        {
         string text="";
         if(Risk>0.0)
            text="< method CheckOpenShort ("+DoubleToString(check_open_short_lot,2)+")";
         else
            text="< Lots ("+DoubleToString(InpLots,2)+")";
         Print(__FUNCTION__,", ERROR: method CheckVolume (",DoubleToString(InpLots,2),") ",
               text);
         return;
        }
     }
   else
     {
      Print(__FUNCTION__,", ERROR: method CheckVolume returned the value of \"0.0\"");
      return;
     }
//---
  }
//+------------------------------------------------------------------+
//| 打印 CTrade 结果|
//+------------------------------------------------------------------+
void PrintResult(CTrade &trade,CSymbolInfo &symbol)
  {
   Print("Code of request result: "+IntegerToString(trade.ResultRetcode()));
   Print("code of request result: "+trade.ResultRetcodeDescription());
   Print("deal ticket: "+IntegerToString(trade.ResultDeal()));
   Print("order ticket: "+IntegerToString(trade.ResultOrder()));
   Print("volume of deal or order: "+DoubleToString(trade.ResultVolume(),2));
   Print("price, confirmed by broker: "+DoubleToString(trade.ResultPrice(),symbol.Digits()));
   Print("current bid price: "+DoubleToString(trade.ResultBid(),symbol.Digits()));
   Print("current ask price: "+DoubleToString(trade.ResultAsk(),symbol.Digits()));
   Print("broker comment: "+trade.ResultComment());
//int d=0;
  }
//+------------------------------------------------------------------+