LabelCreate関数を使い数字をカウントダウンさせる方法
数字をカウントダウンさせる方法
「MQLプログラミング言語のOnTimer関数を利用するための準備」では、Comment関数を使って簡易時計を作成し、Labelオブジェクトを使ってローソク足が確定するまでの残り時間を表示しました。
参考記事:MQLプログラミング言語のOnTimer関数を利用するための準備
しかし、数字はカウントダウンの動きをしていませんでした。これはなぜかというと、LabelCreate関数の中で「一度作成が成功した場合に、次から実行されない指定」になっているからです。そこで、一度作成が成功した場合でも、数字を反映するように修正する必要があります。
MQL4リファレンスからコピーして貼り付けたLabelCreate関数内のcreate a text labelにあるif文の中にObjectSetString(chart_ID,name,OBJPROP_TEXT,text);」という一文を加えましょう。
//--- create a text label
if(!ObjectCreate(chart_ID,name,OBJ_LABEL,sub_window,0,0))
{
ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);
Print(__FUNCTION__,
": failed to create text label! Error code = ",GetLastError());
return(false);
}
これでコンパイルすると、文字列情報が更新されるようになるため、数字がカウントダウンを始めます。
カウントダウンの終了を「0」にする
これは好みにもよるのですが、今の状態では1までカウントダウンが進んだ後の次の数字が「0」ではなく「60」に戻るようになっています。そこでOnTimer関数内に「if(countDown == PeriodSeconds()) countDown = 0;」というif文を追加し、カウントダウンが「0」で終了した後、数字が「59」に戻るように改修してみましょう。
void OnTimer()
{
//---
Comment(TimeLocal(),"¥n",TimeCurrent(),"¥n",int(TimeLocal() - TimeCurrent()) / 3600);
int countDown = PeriodSeconds() - (int)TimeLocal() % PeriodSeconds();
if(countDown == PeriodSeconds()) countDown = 0;
LabelCreate(0, "Clock", 0, 100, 100, CORNER_LEFT_UPPER, (string)countDown, "Arial Bold", 30, clrWhite);
}
このif文は、「カウントダウンが足の秒数と一致しときに0にする」という条件です。これを追加することで、カウントダウン終了時に「0」と表示されるようになります。
最後に、チャートからインジケーターを消去したときに文字やオブジェクトが消えるようにOnDeinit関数のところに「Comment(“”);」を加えておくと良いでしょう。
void OnDeinit(const int reason)
{
EventKillTimer();
ObjectDelete("Clock");
Comment("");
}
ソースコード
今回、作成したソースコードは下記の通りです。
//+------------------------------------------------------------------+
//| TimerTest.mq4 |
//| Copyright 2021, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
#property indicator_chart_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping
EventSetMillisecondTimer(100);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
EventKillTimer();
ObjectDelete("Clock");
Comment("");
}
//+------------------------------------------------------------------+
//| 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[])
{
//---
//--- return value of prev_calculated for next call
return(rates_total);
}
//+------------------------------------------------------------------+
//| Timer function |
//+------------------------------------------------------------------+
void OnTimer()
{
//---
Comment(TimeLocal(),"¥n",TimeCurrent(),"¥n",int(TimeLocal() - TimeCurrent()) / 3600);
int countDown = PeriodSeconds() - (int)TimeLocal() % PeriodSeconds();
if(countDown == PeriodSeconds()) countDown = 0;
LabelCreate(0, "Clock", 0, 100, 100, CORNER_LEFT_UPPER, (string)countDown, "Arial Bold", 30, clrWhite);
}
//+------------------------------------------------------------------+
//| Create a text label |
//+------------------------------------------------------------------+
bool LabelCreate(const long chart_ID=0, // chart's ID
const string name="Label", // label name
const int sub_window=0, // subwindow index
const int x=0, // X coordinate
const int y=0, // Y coordinate
const ENUM_BASE_CORNER corner=CORNER_LEFT_UPPER, // chart corner for anchoring
const string text="Label", // text
const string font="Arial", // font
const int font_size=10, // font size
const color clr=clrRed, // color
const double angle=0.0, // text slope
const ENUM_ANCHOR_POINT anchor=ANCHOR_LEFT_UPPER, // anchor type
const bool back=false, // in the background
const bool selection=false, // highlight to move
const bool hidden=true, // hidden in the object list
const long z_order=0) // priority for mouse click
{
//--- reset the error value
ResetLastError();
//--- create a text label
if(!ObjectCreate(chart_ID,name,OBJ_LABEL,sub_window,0,0))
{
ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);
Print(__FUNCTION__,
": failed to create text label! Error code = ",GetLastError());
return(false);
}
//--- set label coordinates
ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x);
ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y);
//--- set the chart's corner, relative to which point coordinates are defined
ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner);
//--- set the text
ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);
//--- set text font
ObjectSetString(chart_ID,name,OBJPROP_FONT,font);
//--- set font size
ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,font_size);
//--- set the slope angle of the text
ObjectSetDouble(chart_ID,name,OBJPROP_ANGLE,angle);
//--- set anchor type
ObjectSetInteger(chart_ID,name,OBJPROP_ANCHOR,anchor);
//--- set color
ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
//--- display in the foreground (false) or background (true)
ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
//--- enable (true) or disable (false) the mode of moving the label by mouse
ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);
ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);
//--- hide (true) or display (false) graphical object name in the object list
ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);
//--- set the priority for receiving the event of a mouse click in the chart
ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);
//--- successful execution
return(true);
}
//+------------------------------------------------------------------+
本記事の監修者・HT FX
2013年にFXを開始し、その後専業トレーダーへ。2014年からMT4/MT5のカスタムインジケーターの開発に取り組む。ブログでは100本を超えるインジケーターを無料公開。投資スタイルは自作の秒足インジケーターを利用したスキャルピング。
本ホームページに掲載されている事項は、投資判断の参考となる情報の提供を目的としたものであり、投資の勧誘を目的としたものではありません。投資方針、投資タイミング等は、ご自身の責任において判断してください。本サービスの情報に基づいて行った取引のいかなる損失についても、当社は一切の責を負いかねますのでご了承ください。また、当社は、当該情報の正確性および完全性を保証または約束するものでなく、今後、予告なしに内容を変更または廃止する場合があります。なお、当該情報の欠落・誤謬等につきましてもその責を負いかねますのでご了承ください。