WeightOscillator_Alert:MetaTrader 5的趋势警报指标

Mike 2017.01.26 18:26 44 0 0
附件

WeightOscillator 趋势指标新增了警报功能,支持发送邮件和推送通知到移动设备,让你不错过任何交易机会。

为了实现警报、邮件和推送通知功能,指标代码进行了以下修改:

  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, // 上一个tick的柱数
                   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, // 上一个tick的柱数
                    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("WeightOscillator_Alert",ColorBuffer,0,rates_total,prev_calculated,close,spread);
       SellSignal("WeightOscillator_Alert",ColorBuffer,4,rates_total,prev_calculated,close,spread);
    //---  

其中 ColorBuffer 为存储指标颜色的颜色索引缓冲区。值 0 和 4 代表该振荡器处于超买和超卖区域。

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

该指标使用了 SmoothAlgorithms.mqh 库的类(请将其复制到 <terminal_data_folder>\MQL5\Include)。有关类的使用已在文章中详细说明,文章链接为"Averaging Price Series for Intermediate Calculations Without Using Additional Buffers"

图1. WeightOscillator_Alert 指标在图表上的表现

图1. WeightOscillator_Alert 指标在图表上的表现

图2. WeightOscillator_Alert 指标生成趋势信号警报

图2. WeightOscillator_Alert 指标生成趋势信号警报

图3. WeightOscillator_Alert 指标生成趋势信号警报

图3. WeightOscillator_Alert 指标生成趋势信号警报

列表
评论 0