
MQL4MQL5 インジケーター作成
SetIndexBuffer()
- できること
- 配列をインジケーターのバッファとして登録する
- 対応
- MQL4・MQL5
SetIndexBuffer() とは
カスタムインジケーターで、double の配列をインジケーターのバッファ(チャートに表示したり、iCustom() で読み出したりできる値)として登録する関数です。OnInit() で呼びます。
SetIndexBuffer() の書式と引数
bool SetIndexBuffer(
int index,
double &buffer[],
ENUM_INDEXBUFFER_TYPE data_type=INDICATOR_DATA
)| 引数 | 型 | 省略時 | 説明 |
|---|---|---|---|
index | int | 必須 | バッファの番号(0 から)。#property indicator_buffers の数より小さい値にします。 |
buffer | double &[] | 必須 | 登録する配列(グローバル変数として宣言した double の動的配列)。 |
data_type | ENUM_INDEXBUFFER_TYPE | INDICATOR_DATA | バッファの種類。INDICATOR_DATA(表示するデータ)、INDICATOR_COLOR_INDEX(色の番号)、INDICATOR_CALCULATIONS(計算用・表示しない)。 |
戻り値
登録できたら true を返します。
SetIndexBuffer() の使い方(コード例)
MQL4
#property strict
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 clrRed
#property indicator_color2 clrBlue
double UpBuf[], DnBuf[];
int OnInit()
{
SetIndexBuffer(0, UpBuf);
SetIndexBuffer(1, DnBuf);
SetIndexStyle(0, DRAW_LINE);
SetIndexStyle(1, DRAW_LINE);
return INIT_SUCCEEDED;
}
// 各バーの高値・安値を線で表示する
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[])
{
int limit = rates_total - prev_calculated;
if(prev_calculated > 0) limit++;
for(int i = 0; i < limit && i < rates_total; i++) // MQL4 は 0 = 最新
{
UpBuf[i] = high[i];
DnBuf[i] = low[i];
}
return rates_total;
}MQL5
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots 2
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrRed
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrBlue
double UpBuf[], DnBuf[];
int OnInit()
{
SetIndexBuffer(0, UpBuf, INDICATOR_DATA);
SetIndexBuffer(1, DnBuf, INDICATOR_DATA);
return INIT_SUCCEEDED;
}
// 各バーの高値・安値を線で表示する
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[])
{
int start = (prev_calculated > 0) ? prev_calculated - 1 : 0;
for(int i = start; i < rates_total; i++) // MQL5 は 0 = 最も古いバー
{
UpBuf[i] = high[i];
DnBuf[i] = low[i];
}
return rates_total;
}MetaEditor でコンパイルできることを確認済みです。
SetIndexBuffer() の注意点
- 登録した配列はターミナルがサイズを管理します。ArrayResize() でサイズを変えてはいけません。
- バッファの数は #property indicator_buffers で宣言します。MQL5 では、表示する線の数を #property indicator_plots でも宣言します。
- MQL4 のバッファは既定で時系列(0 が最新)、MQL5 のバッファは既定で 0 が最も古いバーです。












