MT4のチャートにはもともと注文、決済などを行えるパネルがありますが
MQL4でもボタンを作成することが可能です
簡単なButtonを作成
最初は簡単なボタンを表示させて、
クリックイベントを取得できるようにしてみたいと思います
Buttonを表示
最初に簡単なボタンを表示させてみましょう
ObjectCreate() を使ってオブジェクトタイプを
OBJ_BUTTONとします
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
string button_01="Button_01"; int OnInit(){ // Button_01 create ObjectCreate( 0, // long chart_id button_01, // string object_name OBJ_BUTTON, // ENUM_OBJECT object_typ 0, // int sub_window 0, // datetime time1 0 // double price1 ); return(INIT_SUCCEEDED); } void OnDeinit(const int reason){ ObjectDelete(0, button_01); } void OnTick(){ } |
コンパイル、MT4チャートにボタンが表示されました
Buttonの名前、配色、位置
ボタンの名前、配色、位置などを設定します
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
// ... int OnInit(){ // Button_01 create ObjectCreate( 0, // long chart_id button_01, // string object_name OBJ_BUTTON, // ENUM_OBJECT object_typ 0, // int sub_window 0, // datetime time1 0 // double price1 ); // Button Set up ObjectSetInteger(0, button_01, OBJPROP_COLOR, clrBlue); // Text color ObjectSetString(0, button_01, OBJPROP_TEXT, "Button"); // Text ObjectSetString(0, button_01, OBJPROP_FONT, "Arial"); // Font ObjectSetInteger(0, button_01, OBJPROP_FONTSIZE, 12); // Font Size ObjectSetInteger(0, button_01, OBJPROP_CORNER, CORNER_LEFT_UPPER); // Anchor Set ObjectSetInteger(0, button_01, OBJPROP_XDISTANCE, 200); // X position ObjectSetInteger(0, button_01, OBJPROP_YDISTANCE, 100); // Y position ObjectSetInteger(0, button_01, OBJPROP_XSIZE, 100); // button size ObjectSetInteger(0, button_01, OBJPROP_YSIZE, 32); // button height ObjectSetInteger(0, button_01, OBJPROP_BGCOLOR, clrAqua); // button color ObjectSetInteger(0, button_01, OBJPROP_BORDER_COLOR, clrBlue); // button border color ObjectSetInteger(0, button_01, OBJPROP_STATE, false); // button click ObjectSetInteger(0, button_01, OBJPROP_BACK, false); // button backgroud ObjectSetInteger(0, button_01, OBJPROP_SELECTABLE, false); // button selectable ObjectSetInteger(0, button_01, OBJPROP_SELECTED, false); // button status ObjectSetInteger(0, button_01, OBJPROP_HIDDEN, true); // button display or hide ObjectSetInteger(0, button_01, OBJPROP_ZORDER, 0); // button click priority return(INIT_SUCCEEDED); } // ... |
ボタンの色や位置などを細かく設定できました
Buttonクリック
Buttonクリックつまりイベント処理です
OnChartEvent() を使ってボタンがクリックされたのを受けとり何らかの処理をします
1 2 3 4 5 |
void OnChartEvent(const int id, // Event ID const long& lparam, // Parameter of type long event const double& dparam, // Parameter of type double event const string& sparam // Parameter of type string events ); |
id からCHARTEVENT_OBJECT_CLICKを判定して
それが該当のボタンIDかどうかif文で選別します
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// Get the object event void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam){ if(id == CHARTEVENT_OBJECT_CLICK){ // Button click if(sparam == button_01){ // Button Status bool btStatus = ObjectGetInteger(0, button_01, OBJPROP_STATE); Print("Button Pressed = ", btStatus); } } } |
ボタンをクリックする毎にステータスが変化し
ボタンクリックが取得されています
まとめたコードになります
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
string button_01="Button_01"; int OnInit(){ // Button_01 create ObjectCreate( 0, // long chart_id button_01, // string object_name OBJ_BUTTON, // ENUM_OBJECT object_typ 0, // int sub_window 0, // datetime time1 0 // double price1 ); // Button Set up ObjectSetInteger(0, button_01, OBJPROP_COLOR, clrBlue); // Text color ObjectSetString(0, button_01, OBJPROP_TEXT, "Button"); // Text ObjectSetString(0, button_01, OBJPROP_FONT, "Arial"); // Font ObjectSetInteger(0, button_01, OBJPROP_FONTSIZE, 12); // Font Size ObjectSetInteger(0, button_01, OBJPROP_CORNER, CORNER_LEFT_UPPER); // Anchor Set ObjectSetInteger(0, button_01, OBJPROP_XDISTANCE, 200); // X position ObjectSetInteger(0, button_01, OBJPROP_YDISTANCE, 100); // Y position ObjectSetInteger(0, button_01, OBJPROP_XSIZE, 100); // button size ObjectSetInteger(0, button_01, OBJPROP_YSIZE, 32); // button height ObjectSetInteger(0, button_01, OBJPROP_BGCOLOR, clrAqua); // button color ObjectSetInteger(0, button_01, OBJPROP_BORDER_COLOR, clrBlue); // button border color ObjectSetInteger(0, button_01, OBJPROP_STATE, false); // button click ObjectSetInteger(0, button_01, OBJPROP_BACK, false); // button backgroud ObjectSetInteger(0, button_01, OBJPROP_SELECTABLE, false); // button selectable ObjectSetInteger(0, button_01, OBJPROP_SELECTED, false); // button status ObjectSetInteger(0, button_01, OBJPROP_HIDDEN, true); // button display or hide ObjectSetInteger(0, button_01, OBJPROP_ZORDER, 0); // button click priority return(INIT_SUCCEEDED); } void OnDeinit(const int reason){ ObjectDelete(0, button_01); } void OnTick(){ } // Get the object event void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam){ if(id == CHARTEVENT_OBJECT_CLICK){ // Button click if(sparam == button_01){ // Button Status bool btStatus = ObjectGetInteger(0, button_01, OBJPROP_STATE); Print("Button Pressed = ", btStatus); } } } |
References:
MQL4 ObjectCreate
MQL4 OBJ_BUTTON
MQL4 ObjectSetInteger
MQL4 OnChartEvent