首页 技术指标 帖子

初学者警报 - MetaTrader 5 指标解析

附件
16448.zip (3.75 KB, 下载 0次)

真实作者:

EarnForex

这款指标显示了趋势的极值点(最高点和最低点),可以作为支撑和阻力位,非常适合用来判断当前趋势的通道。它提供了提醒功能,可以通过邮件和推送通知发送到手机。

这是一款简单的指标,通过特定的时间段找到最高点和最低点,并用点标记出来。

为了实现提醒、邮件消息和推送通知,指标代码做了以下修改:

  1. 引入了新的输入参数:
    input uint NumberofBar=1;// 信号的柱数
    input bool SoundON=true; // 启用提醒
    input uint NumberofAlerts=2;// 提醒数量
    input bool EMailON=false; // 启用邮件发送信号
    input bool PushON=false; // 启用发送信号到手机
    
  2. 在指标代码末尾添加了三个新功能:BuySignal()、SellSignal() 和 GetStringTimeframe()
    //+------------------------------------------------------------------+
    //| 买入信号函数                                              |
    //+------------------------------------------------------------------+
    void BuySignal(string SignalSirname,      // 指标名称的文本,用于邮件和推送消息
                   double &BuyArrow[],        // 买入信号的指标缓冲区
                   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(BuyArrow);
       int index;
       if(SeriesTest) index=int(NumberofBar);
       else index=Rates_total-int(NumberofBar)-1;
       if(NormalizeDouble(BuyArrow[index],_Digits) && BuyArrow[index]!=EMPTY_VALUE) 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+=Spread[index];
          string sAsk=DoubleToString(Ask,_Digits);
          string sBid=DoubleToString(Bid,_Digits);
          string sPeriod=GetStringTimeframe(ChartPeriod());
          if(SoundON) Alert("买入信号 
     Ask=",Ask,"
     Bid=",Bid,"
     当前时间=",text,"
     品种=",Symbol()," 周期=",sPeriod);
          if(EMailON) SendMail(SignalSirname+": 买入信号提醒","买入信号在 Ask="+sAsk+", Bid="+sBid+", 日期="+text+" 品种="+Symbol()+" 周期="+sPeriod);
          if(PushON) SendNotification(SignalSirname+": 买入信号在 Ask="+sAsk+", Bid="+sBid+", 日期="+text+" 品种="+Symbol()+" 周期="+sPeriod);
         }
    
    //---
      }
    //+------------------------------------------------------------------+
    //| 卖出信号函数                                             |
    //+------------------------------------------------------------------+
    void SellSignal(string SignalSirname,      // 指标名称的文本,用于邮件和推送消息
                    double &SellArrow[],       // 卖出信号的指标缓冲区
                    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(SellArrow);
       int index;
       if(SeriesTest) index=int(NumberofBar);
       else index=Rates_total-int(NumberofBar)-1;
       if(NormalizeDouble(SellArrow[index],_Digits) && SellArrow[index]!=EMPTY_VALUE) 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+=Spread[index];
          string sAsk=DoubleToString(Ask,_Digits);
          string sBid=DoubleToString(Bid,_Digits);
          string sPeriod=GetStringTimeframe(ChartPeriod());
          if(SoundON) Alert("卖出信号 
     Ask=",Ask,"
     Bid=",Bid,"
     当前时间=",text,"
     品种=",Symbol()," 周期=",sPeriod);
          if(EMailON) SendMail(SignalSirname+": 卖出信号提醒","卖出信号在 Ask="+sAsk+", Bid="+sBid+", 日期="+text+" 品种="+Symbol()+" 周期="+sPeriod);
          if(PushON) SendNotification(SignalSirname+": 卖出信号在 Ask="+sAsk+", Bid="+sBid+", 日期="+text+" 品种="+Symbol()+" 周期="+sPeriod);
         }
    //---
      }
    //+------------------------------------------------------------------+
    //| 获取时间周期字符串                               |
    //+------------------------------------------------------------------+
    string GetStringTimeframe(ENUM_TIMEFRAMES timeframe)
      {
    //----
       return(StringSubstr(EnumToString(timeframe),7,-1));
    //----
      }
    
  3. 在 OnCalculate() 块中的指标计算周期后增加了对 BuySignal() 和 SellSignal() 函数的调用
    //---     
       BuySignal("初学者警报",BuyBuffer,rates_total,prev_calculated,Close,spread);
       SellSignal("初学者警报",SellBuffer,rates_total,prev_calculated,Close,spread);
    //--- 
    

其中 BuyBuffer 和 SellBuffer 是存储买入和卖出信号的指标缓冲区。由于指标缓冲区中的空值要么是零,要么是 EMPTY_VALUE。

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

最初,这个指标是用 MQL4 编写的,并于 2008 年 9 月 3 日首次发布在 代码库

图1. 初学者警报指标在图表上的表现

图1. 初学者警报指标在图表上的表现

图2. 初学者警报指标正在生成提醒。

图2. 初学者警报指标正在生成提醒。

相关帖子

评论 (0)