チャート上の現在時刻を表示するインジケータ

どんなとき、使うのか

バックテストをしているとき、シミュレートしている時間、つまり、 シミュレート中の最も右のバーの時刻を表示したいときに使います。

FX業者のサーバの時刻との差で、MT4に表示されている時刻が違うので、 サブウィンドウに日本時間を表示するインジケータは見つかりましたが、 バックテストで大量のバーを表示していると計算、描画に時間がかかるので、 現在時刻(最右のバーの時刻)を表示するインジケータを作りました。

ポイント

チャート上の最右のバーの時刻は下記で取れます。

datetime dt = time[0];

あとはCommentは字が小さいので、Textを作っています。

通常のTimeToStrでは曜日がわからないので、日付と時刻の間に曜日を出すようにしました。

ソース

//+------------------------------------------------------------------+
//|                                                     ShowTime.mq4 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window

extern string LabelName = "ShowTime";
extern int OffsetH = 6;
extern int Corner = 0;
extern double LocationX = 0;
extern double LocationY = 20;
extern int FontSize = 18;
extern string FontName = "メイリオ";
extern color FontColor = Yellow;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
   datetime dt_org = time[0];
   datetime dt_show = dt_org + 60*60*OffsetH;
   int dayOfWeek = TimeDayOfWeek(dt_show);
   
   string dayStr = "";
   switch(dayOfWeek) {
      case 0: dayStr = "(日)"; break;
      case 1: dayStr = "(月)"; break;
      case 2: dayStr = "(火)"; break;
      case 3: dayStr = "(水)"; break;
      case 4: dayStr = "(木)"; break;
      case 5: dayStr = "(金)"; break;
      case 6: dayStr = "(土)"; break;
      default: dayStr = "(?)";
   }
   
   string timeStr = TimeToStr(dt_show, TIME_DATE) + " " + dayStr + " " + TimeToStr(dt_show, TIME_MINUTES);
   
   ObjectCreate(LabelName, OBJ_LABEL, 0, 0, 0);
   ObjectSetText(LabelName, timeStr, FontSize, FontName, FontColor);
   ObjectSet(LabelName, OBJPROP_XDISTANCE, LocationX);
   ObjectSet(LabelName, OBJPROP_YDISTANCE, LocationY);
   ObjectSet(LabelName, OBJPROP_CORNER, Corner);

   return(rates_total);
  }
//+------------------------------------------------------------------+