首页 技术指标 帖子

ColorXdinMA_Alert:MetaTrader 5中的趋势警报指标

附件
17111.zip (22.91 KB, 下载 0次)

真实作者:dimeon

ColorXdinMA是一款在MetaTrader 5上使用的趋势移动平均线指标,它具备发送警报、电子邮件和移动设备推送通知的功能。

为了实现这些警报功能,指标代码进行了以下更改:

  1. 在输入变量声明之前,全局声明了指标信号生成选项的枚举类型。
    //+----------------------------------------------------+
    //| 信号生成选项枚举   |
    //+----------------------------------------------------+
    enum ENUM_SIGNAL_MODE
      {
       MODE_SIGNAL,  //突破信号
       MODE_TREND    //突破和趋势信号
      };
  2. 新增了输入参数
    //---- 警报的输入变量
    input ENUM_SIGNAL_MODE SignMode=MODE_SIGNAL; //信号生成模式
    input uint NumberofBar=1;                    //生成信号的柱数
    input bool SoundON=true;                     //启用警报
    input uint NumberofAlerts=2;                 //警报数量
    input bool EMailON=false;                    //启用邮件信号
    input bool PushON=false;                     //启用移动设备信号推送
  3. 在指标代码的末尾添加了三个新函数:BuySignal()、SellSignal()和GetStringTimeframe()
    //+------------------------------------------------------------------+
    //| 买入信号函数                                            |
    //+------------------------------------------------------------------+
    void BuySignal(string SignalSirname,// 指标名称文本,用于电子邮件和推送消息
                   double &ColorArray[],// 颜色索引缓冲区
                   int ColorIndex,// 生成信号的颜色索引
                   const int Rates_total,     // 当前柱数
                   const int Prev_calculated, // 上一个刻度的柱数
                   const double &Close[],     // 收盘价
                   const int &Spread[])       // 点差
      {
    //---
       static uint counter=0;
       if(Rates_total!=Prev_calculated) counter=0;

       bool BuySignal=false;
       bool SeriesTest=ArrayGetAsSeries(ColorArray);
       int index,index1;
       if(SeriesTest)
         {
          index=int(NumberofBar);
          index1=index+1;
         }
       else
         {
          index=Rates_total-int(NumberofBar)-1;
          index1=index-1;
         }
       if(SignMode==MODE_SIGNAL)
         {
          if(ColorArray[index1]!=ColorIndex && ColorArray[index]==ColorIndex) BuySignal=true;
         }
       else
         {
          if(ColorArray[index]==ColorIndex) BuySignal=true;
         }
       if(BuySignal && counter<=NumberofAlerts)
         {
          counter++;
          MqlDateTime tm;
          TimeToStruct(TimeCurrent(),tm);
          string text=TimeToString(TimeCurrent(),TIME_DATE)+" "+string(tm.hour)+":"+string(tm.min);
          SeriesTest=ArrayGetAsSeries(Close);
          if(SeriesTest) index=int(NumberofBar);
          else index=Rates_total-int(NumberofBar)-1;
          double Ask=Close[index];
          double Bid=Close[index];
          SeriesTest=ArrayGetAsSeries(Spread);
          if(SeriesTest) index=int(NumberofBar);
          else index=Rates_total-int(NumberofBar)-1;
          Bid+=_Point*Spread[index];
          string sAsk=DoubleToString(Ask,_Digits);
          string sBid=DoubleToString(Bid,_Digits);
          string sPeriod=GetStringTimeframe(ChartPeriod());
          if(SignMode==MODE_SIGNAL || (SignMode==MODE_TREND && ColorArray[index1]!=ColorIndex))
            {
             if(SoundON) Alert("BUY signal \n Ask=",Ask,"\n Bid=",Bid,"\n currtime=",text,"\n Symbol=",Symbol()," Period=",sPeriod);
             if(EMailON) SendMail(SignalSirname+": BUY signal alert","BUY signal at Ask="+sAsk+", Bid="+sBid+", Date="+text+" Symbol="+Symbol()+" Period="+sPeriod);
             if(PushON) SendNotification(SignalSirname+": BUY signal at Ask="+sAsk+", Bid="+sBid+", Date="+text+" Symbol="+Symbol()+" Period="+sPeriod);
            }
          else
            {
             if(SoundON) Alert("Up Trend signal \n Ask=",Ask,"\n Bid=",Bid,"\n currtime=",text,"\n Symbol=",Symbol()," Period=",sPeriod);
             if(EMailON) SendMail(SignalSirname+": Up Trend signal alert","BUY signal at Ask="+sAsk+", Bid="+sBid+", Date="+text+" Symbol="+Symbol()+" Period="+sPeriod);
             if(PushON) SendNotification(SignalSirname+": Up Trend signal at Ask="+sAsk+", Bid="+sBid+", Date="+text+" Symbol="+Symbol()+" Period="+sPeriod);
            }
         }

    //---
      }
    //+------------------------------------------------------------------+
    //| 卖出信号函数                                             |
    //+------------------------------------------------------------------+
    void SellSignal(string SignalSirname,// 指标名称文本,用于电子邮件和推送消息
                    double &ColorArray[],       // 颜色索引缓冲区
                    int ColorIndex,             // 生成信号的颜色索引
                    const int Rates_total,     // 当前柱数
                    const int Prev_calculated, // 上一个刻度的柱数
                    const double &Close[],     // 收盘价
                    const int &Spread[])       // 点差
      {
    //---
       static uint counter=0;
       if(Rates_total!=Prev_calculated) counter=0;

       bool SellSignal=false;
       bool SeriesTest=ArrayGetAsSeries(ColorArray);
       int index,index1;
       if(SeriesTest)
         {
          index=int(NumberofBar);
          index1=index+1;
         }
       else
         {
          index=Rates_total-int(NumberofBar)-1;
          index1=index-1;
         }

       if(SignMode==MODE_SIGNAL)
         {
          if(ColorArray[index1]!=ColorIndex && ColorArray[index]==ColorIndex) SellSignal=true;
         }
       else
         {
          if(ColorArray[index]==ColorIndex) SellSignal=true;
         }
       if(SellSignal && counter<=NumberofAlerts)
         {
          counter++;
          MqlDateTime tm;
          TimeToStruct(TimeCurrent(),tm);
          string text=TimeToString(TimeCurrent(),TIME_DATE)+" "+string(tm.hour)+":"+string(tm.min);
          SeriesTest=ArrayGetAsSeries(Close);
          if(SeriesTest) index=int(NumberofBar);
          else index=Rates_total-int(NumberofBar)-1;
          double Ask=Close[index];
          double Bid=Close[index];
          SeriesTest=ArrayGetAsSeries(Spread);
          if(SeriesTest) index=int(NumberofBar);
          else index=Rates_total-int(NumberofBar)-1;
          Bid+=_Point*Spread[index];
          string sAsk=DoubleToString(Ask,_Digits);
          string sBid=DoubleToString(Bid,_Digits);
          string sPeriod=GetStringTimeframe(ChartPeriod());
          if(SignMode==MODE_SIGNAL || (SignMode==MODE_TREND && ColorArray[index1]!=ColorIndex))
            {
             if(SoundON) Alert("SELL signal \n Ask=",Ask,"\n Bid=",Bid,"\n currtime=",text,"\n Symbol=",Symbol()," Period=",sPeriod);
             if(EMailON) SendMail(SignalSirname+": SELL signal alert","SELL signal at Ask="+sAsk+", Bid="+sBid+", Date="+text+" Symbol="+Symbol()+" Period="+sPeriod);
             if(PushON) SendNotification(SignalSirname+": SELL signal at Ask="+sAsk+", Bid="+sBid+", Date="+text+" Symbol="+Symbol()+" Period="+sPeriod);
            }
          else
            {
             if(SoundON) Alert("Down trend signal \n Ask=",Ask,"\n Bid=",Bid,"\n currtime=",text,"\n Symbol=",Symbol()," Period=",sPeriod);
             if(EMailON) SendMail(SignalSirname+": Down trend signal alert","SELL signal at Ask="+sAsk+", Bid="+sBid+", Date="+text+" Symbol="+Symbol()+" Period="+sPeriod);
             if(PushON) SendNotification(SignalSirname+": Down trend signal at Ask="+sAsk+", Bid="+sBid+", Date="+text+" Symbol="+Symbol()+" Period="+sPeriod);
            }
         }
    //---
      }
    //+------------------------------------------------------------------+
    //| 获取时间框架的字符串 |
    //+------------------------------------------------------------------+
    string GetStringTimeframe(ENUM_TIMEFRAMES timeframe)
      {
    //----
       return(StringSubstr(EnumToString(timeframe),7,-1));
    //----
      }
  4. 在OnCalculate()代码块的指标计算周期后添加了BuySignal()和SellSignal()的调用。
    //---    
       BuySignal("ColorXdinMA_Alert",ColorXdinMA,1,rates_total,prev_calculated,close,spread);
       SellSignal("ColorXdinMA_Alert",ColorXdinMA,2,rates_total,prev_calculated,close,spread);
    //--- 

其中,ColorXdinMA是用于存储指标颜色的颜色索引缓冲区的名称。值1和2分别是移动平均线上升或下降时该缓冲区中的颜色编号。

假设在指标代码的OnCalculate()块中只会调用一次BuySignal()和SellSignal()函数。

该指标使用SmoothAlgorithms.mqh库的类(将其复制到<terminal_data_folder>\MQL5\Include)。关于类的使用,已在文章《不使用额外缓冲区进行中间计算的价格序列平均化》中进行了详细描述。

图1. ColorXdinMA_Alert指标在图表上的显示

图1. ColorXdinMA_Alert指标在图表上的显示

图2. ColorXdinMA_Alert指标。生成突破信号的警报

图2. ColorXdinMA_Alert指标。生成突破信号的警报

图3. ColorXdinMA_Alert指标。生成趋势信号的警报

图3. ColorXdinMA_Alert指标。生成趋势信号的警报

相关帖子

评论 (0)