MQL4如何获取鼠标下图表价格?获取十字光标下时间与价格?

问题
MQL4如何获取鼠标下图表价格?获取十字光标下时间与价格?

解答
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
{
   // 在点击图表时,会出现十字图
   if(id==CHARTEVENT_CLICK)
   {
      // 坐标
      int      x     =(int)lparam;
      int      y     =(int)dparam;
      
      // 时间 价格
      datetime dt     = 0;
      double   price  = 0;
      int      window = 0;
      string   tmp    = "";
     
      // 将当前坐标转成时间与价格
      if(ChartXYToTimePrice(0,x,y,window,dt,price))
      {
         // 显示时间 价格
         if(ChartTimePriceToXY(0,window,dt,price,x,y))
         {
            tmp = StringFormat("时间 = %s  价格 = %G  =>  X = %d  Y = %d",TimeToString(dt),price,x,y);
         }
         else
         {
            tmp = StringFormat("ChartTimePriceToXY 调用错误: #%d",GetLastError());
         }
         Comment(tmp);
         Print(tmp);
            
            
         // 绘制十字线
         ObjectDelete(0,"V Line");
         ObjectDelete(0,"H Line");
         ObjectCreate(0,"H Line",OBJ_HLINE,window,dt,price);
         ObjectCreate(0,"V Line",OBJ_VLINE,window,dt,price);
         ChartRedraw(0);
      }
      else
      {
         PrintFormat("ChartTimePriceToXY 调用错误: #%d",GetLastError());
      }
   }
}