📌 MT4 オブジェクト関数 & OBJPROP 完全チートシート(MQL4)
1. オブジェクトを作る
bool ObjectCreate(
long chart_id, // チャートID(通常 0)
string name, // オブジェクト名(重複不可)
int type, // OBJ_ 定数(例: OBJ_HLINE, OBJ_TEXT)
int sub_window, // サブウィンドウ番号(0=メインチャート)
datetime time1, // 第1ポイントの時間
double price1, // 第1ポイントの価格
... // 第2, 第3ポイント(種類によって必要)
);
2. オブジェクトを削除
bool ObjectDelete(long chart_id, string name); // 指定オブジェクトを削除
bool ObjectsDeleteAll(long chart_id, int sub_window = -1, int type = -1);
// すべて削除(ウィンドウやタイプで絞り込み可能)
3. オブジェクトのプロパティ設定
数値プロパティ
ObjectSetInteger(chart_id, name, property_id, value);
例:
ObjectSetInteger(0, "line1", OBJPROP_COLOR, clrRed); // 色
ObjectSetInteger(0, "line1", OBJPROP_STYLE, STYLE_DOT); // 線種
ObjectSetInteger(0, "line1", OBJPROP_WIDTH, 2); // 線の太さ
実数プロパティ
ObjectSetDouble(chart_id, name, property_id, value);
例:
ObjectSetDouble(0, "line1", OBJPROP_PRICE, 1.2345); // 水平線の価格
文字列プロパティ
ObjectSetString(chart_id, name, property_id, value);
例:
ObjectSetString(0, "label1", OBJPROP_TEXT, "Hello MT4!");
4. オブジェクトのプロパティ取得
数値取得
long val = ObjectGetInteger(chart_id, name, property_id);
実数取得
double val = ObjectGetDouble(chart_id, name, property_id);
文字列取得
string val = ObjectGetString(chart_id, name, property_id);
5. オブジェクトの数・名前取得
int total = ObjectsTotal(chart_id, sub_window = -1, type = -1);
string name = ObjectName(chart_id, index);
6. 主なオブジェクトタイプ(OBJ_ 定数)
定数 | 説明 |
---|---|
OBJ_HLINE | 水平線 |
OBJ_TREND | トレンドライン |
OBJ_VLINE | 垂直線 |
OBJ_RECTANGLE | 四角形 |
OBJ_TEXT | テキスト |
OBJ_LABEL | ラベル(スクリーン座標表示) |
OBJ_ARROW | 矢印 |
OBJ_FIBO | フィボナッチ |
OBJ_CHANNEL | チャネルライン |
OBJ_ELLIPSE | 楕円 |
7. 主なプロパティ(OBJPROP_ 定数)
定数 | 型 | 説明 |
---|---|---|
OBJPROP_COLOR | int | 色(clrRed など) |
OBJPROP_STYLE | int | 線種(STYLE_SOLID , STYLE_DOT , STYLE_DASH ) |
OBJPROP_WIDTH | int | 線の太さ |
OBJPROP_TIME1 | datetime | 第1ポイントの時間 |
OBJPROP_PRICE1 | double | 第1ポイントの価格 |
OBJPROP_TIME2 | datetime | 第2ポイントの時間 |
OBJPROP_PRICE2 | double | 第2ポイントの価格 |
OBJPROP_TEXT | string | 表示テキスト(テキスト・ラベル用) |
OBJPROP_FONTSIZE | int | フォントサイズ |
OBJPROP_CORNER | int | 表示位置(ラベル用:CORNER_LEFT_UPPER など) |
OBJPROP_XDISTANCE | int | X座標の距離(ピクセル) |
OBJPROP_YDISTANCE | int | Y座標の距離(ピクセル) |
OBJPROP_BACK | bool | 背景に表示するか |
OBJPROP_HIDDEN | bool | オブジェクトリストに表示しない |
8. 実用サンプル
チャート左上に背景付きテキストを表示
void OnInit()
{
string name = "MyLabel";
ObjectCreate(0, name, OBJ_LABEL, 0, 0, 0);
ObjectSetInteger(0, name, OBJPROP_CORNER, CORNER_LEFT_UPPER);
ObjectSetInteger(0, name, OBJPROP_XDISTANCE, 10);
ObjectSetInteger(0, name, OBJPROP_YDISTANCE, 20);
ObjectSetInteger(0, name, OBJPROP_FONTSIZE, 12);
ObjectSetInteger(0, name, OBJPROP_COLOR, clrWhite);
ObjectSetInteger(0, name, OBJPROP_BGCOLOR, clrBlue); // 背景色(MQL4ビルド600以降)
ObjectSetInteger(0, name, OBJPROP_BACK, false);
ObjectSetString(0, name, OBJPROP_TEXT, "MT4 Object Example");
}
void OnDeinit(const int reason)
{
ObjectDelete(0, "MyLabel");
}
これがあれば、
水平線も、日足ローソクの重ね描きも、トレード履歴の表示も、背景つき統計表示も全部 作れる状態です。
コメント