首页 技术指标 帖子

Waddah_Attar_趋势提醒 - MetaTrader 5 指标详解

附件
16832.zip (22.5 KB, 下载 0次)

真正的作者: Eng. Waddah Attar

Waddah_Attar_趋势提醒 指标具有警报、邮件和推送通知功能。

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

  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 &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(ColorArray[index1]!=ColorIndex && 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(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 &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(ColorArray[index1]!=ColorIndex && 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(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("Waddah_Attar_趋势提醒",ColorIndBuffer,0,rates_total,prev_calculated,close,spread);
    SellSignal("Waddah_Attar_趋势提醒",ColorIndBuffer,1,rates_total,prev_calculated,close,spread);
    //---

这里的ColorIndBuffer是用于存储线条颜色索引的颜色索引缓冲区,而0和1则是颜色索引缓冲区中的颜色数量。

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

该指标使用SmoothAlgorithms.mqh库类(请将其复制到<terminal_data_folder>\MQL5\Include)。这些类的使用在文章“不使用额外缓冲区的中间计算平均价格系列”中有详细说明。

图1. Waddah_Attar_趋势提醒指标在图表上

图1. Waddah_Attar_趋势提醒指标在图表上

图2. Waddah_Attar_趋势提醒指标. 生成警报

图2. Waddah_Attar_趋势提醒指标. 生成警报

相关帖子

评论 (0)