Alert

在单独窗口中显示信息。

void  Alert(
   argument,     // 第一值
   ...           // 其他值
   );

参量

argument

[in]  任意值,被逗号分开,分开信息输出分类量,可以使用线路供应字符"\n" 或者 "\r\n". 参量的数量不能超过64个

返回值

没有返回值。

注释

数组不能通过Alert() 函数传递,数组应该输出元素,双精度类型数据可以输出小数点后8位以上,浮点类型数据可以输出小数点后5位,不同精度起源不同数字或者系统模式,使用 DoubleToString()

布尔型数据以true或者false字符串输出,日期以 YYYY.MM.DD HH:MI:SS格式,使用 TimeToString()日期另外模式显示日期,颜色类型数据既可以RGB字符串或者颜色名称输出,颜色存在颜色设置中。

Alert() 函数在策略测试中不工作。

示例:

//--- 枚举
enum ENUM_INTERSECT_DIRECTION
  {
   INTERSECT_DIRECTION_NONE0,  // no crossing
   INTERSECT_DIRECTION_UP  = 1,  // upward crossing
   INTERSECT_DIRECTION_DOWN=-1,  // downward crossing
  };
 
//--- 输入参数
input    uint               InpPeriod = 10;            // MA Period
input    int                InpShift  = 0;             // MA Shift
input    ENUM_MA_METHOD     InpMethod = MODE_SMA;      // MA Method
input    ENUM_APPLIED_PRICE InpPrice  = PRICE_CLOSE;   // MA Applied price
 
//--- 全局变量
int      ExtMaHandle;
int      ExtMaPeriod;
double   ExtData[2];
MqlRates ExtRates[2];
 
//+------------------------------------------------------------------+
//| EA交易初始化函数                                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- 如果输入参数中指定为零,则计算移动平均线的周期将等于默认值(10)
   ExtMaPeriod=int(InpPeriod<1 ? 10 : InpPeriod);
//--- 用指定的参数创建移动平均指标的句柄
   ExtMaHandle=iMA(Symbol(),PERIOD_CURRENT,ExtMaPeriod,InpShift,InpMethod,InpPrice);
   ResetLastError();
   if(ExtMaHandle==INVALID_HANDLE)
     {
      PrintFormat("Failed to create iMA() handle. Error code: %d",GetLastError());
      return(INIT_FAILED);
     }
     
//--- 获取最后价更新的时间
   datetime tick_time=TickTime();
//--- 获取最后两个柱形图的移动平均线数据和价格数据
   if(GetData(ExtMaHandle,ExtData,ExtRates) && tick_time!=0)
     {
      //--- 如果价格高于移动平均线(MA)
      if(ExtRates[1].close>ExtData[1])
        {
         //--- 创建消息文本并显示预警(Alert)
         string message=StringFormat("Bar time: %s. The price is above the moving average",TimeToString(ExtRates[1].time));
         Alert(message+" at "+TimeToString(tick_time,TIME_DATE|TIME_MINUTES|TIME_SECONDS));
         /*
        结果:
         AlertBar time2024.02.16 18:00The price is above the moving average at 2024.02.16 18:47:43
         */
        }
      else
        {
         //--- 如果价格低于移动平均线(MA)
         if(ExtRates[1].close<ExtData[1])
           {
            //--- 创建消息文本并显示预警(Alert)
            string message=StringFormat("Bar time: %s. The price is below the moving average.",TimeToString(ExtRates[1].time));
            Alert(message+" at "+TimeToString(tick_time,TIME_DATE|TIME_MINUTES|TIME_SECONDS));
            /*
           结果:
            AlertBar time2024.02.16 19:00The price is below the moving average at 2024.02.16 19:33:14
            */
           }
         else
           {
            //--- 创建消息文本并显示预警(Alert)
            string message=StringFormat("Bar time: %s. The price and moving average are equal.",TimeToString(ExtRates[1].time));
            Alert(message+" at "+TimeToString(tick_time,TIME_DATE|TIME_MINUTES|TIME_SECONDS));
            /*
           结果:
            AlertBar time2024.02.16 20:00The price and moving average are equal at 2024.02.16 20:12:22
            */
           }
        }
     }
     
//--- 成功
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   ResetLastError();
//--- 获取最后两个柱形图的移动平均线数据和价格数据
   if(!GetData(ExtMaHandle,ExtData,ExtRates))
      return;
//--- 在当前柱形图获取价格穿过移动平均线的方向
   ENUM_INTERSECT_DIRECTION intersect=GetIntersectDirection(ExtData,ExtRates);
 
//--- 用于保存前一条消息的变量
   static string message_prev="";
 
//--- 如果价格在当前柱形图向上穿过移动平均线
   if(intersect==INTERSECT_DIRECTION_UP)
     {
      //--- 获取穿过时的报价时间
      datetime tick_time=TickTime();
      if(tick_time==0)
         return;
      //--- 创建消息文本
      string message=StringFormat("Bar time: %s. The price crossed the MA from bottom to top",TimeToString(ExtRates[1].time));
      //--- 如果前一条消息与当前消息不一致,则显示预警(Alert),包含消息和报价时间
      if(message!=message_prev)
        {
         Alert(message+" at "+TimeToString(tick_time,TIME_DATE|TIME_MINUTES|TIME_SECONDS));
         message_prev=message;
         /*
         Result:\
         AlertBar time2024.02.16 09:00The price crossed the MA from bottom to top at 2024.02.16 09:20:35
         */
        }
     }
 
//--- 如果价格在当前柱形图向下穿过移动平均线
   if(intersect==INTERSECT_DIRECTION_DOWN)
     {
      //--- 获取穿过时的报价时间
      datetime tick_time=TickTime();
      if(tick_time==0)
         return;
      //--- 创建消息文本
      string message=StringFormat("Bar time: %s. The price crossed the MA from top to bottom",TimeToString(ExtRates[1].time));
      //--- 如果前一条消息与当前消息不一致,则显示预警(Alert),包含消息和报价时间
      if(message!=message_prev)
        {
         Alert(message+" at "+TimeToString(tick_time,TIME_DATE|TIME_MINUTES|TIME_SECONDS));
         message_prev=message;
         /*
         Result:\
         AlertBar time2024.02.16 10:00The price crossed the MA from top to bottom at 2024.02.16 10:42:15
         */
        }
     }
  }
//+------------------------------------------------------------------+
//| 将价格和移动平均线数据放入数组                                       |
//+------------------------------------------------------------------+
bool GetData(int handle,double &ma_data[],MqlRates &price_data[])
  {
   ResetLastError();
//--- 获取最后两个柱形图的移动平均线数据
   if(CopyBuffer(handle,0,0,2,ma_data)!=2)
     {
      PrintFormat("CopyBuffer() failed. Error code: %d",GetLastError());
      return(false);
     }
//--- 获取最后两个柱形图的价格数据
   if(CopyRates(Symbol(),PERIOD_CURRENT,0,2,price_data)!=2)
     {
      PrintFormat("CopyRates() failed. Error code: %d",GetLastError());
      return(false);
     }
 
   return(true);
  }
//+------------------------------------------------------------------+
//| 返回价格穿过移动平均线的方向                                         |
//+------------------------------------------------------------------+
ENUM_INTERSECT_DIRECTION GetIntersectDirection(double &ma_data[],MqlRates &price_data[])
  {
   double ma0=ma_data[1];
   double ma1=ma_data[0];
   double close0=price_data[1].close;
   double close1=price_data[0].close;
 
   if(close1<=ma1 && close0>ma0)
      return(INTERSECT_DIRECTION_UP);
   else
     {
      if(close1>=ma1 && close0<ma0)
         return(INTERSECT_DIRECTION_DOWN);
      else
         return(INTERSECT_DIRECTION_NONE);
     }
  }
//+------------------------------------------------------------------+
//| 返回以秒为单位的报价时间                                            |
//+------------------------------------------------------------------+
datetime TickTime()
  {
   MqlTick tick={};
 
   ResetLastError();
   if(!SymbolInfoTick(Symbol(),tick))
     {
      PrintFormat("SymbolInfoTick() failed. Error code: %d",GetLastError());
      return(0);
     }
 
   return(tick.time);
  }