MQL4MQL5 インジケーター インジケーター作成
移動平均線を表示するインジケーターを作りたい
結論:バッファを1本用意して線(DRAW_LINE)で表示し、各足の移動平均の値を入れます。
考え方
カスタムインジケーターの基本は「バッファ(値を入れる配列)を用意して、OnCalculate() で各足の値を入れる」ことです。ここでは終値の単純移動平均を自分で計算して線で表示する、いちばん基本的なインジケーターを例にしています。新しい足の分だけ計算し直すので、動作も軽くなります。
- #property でバッファの数・線の種類・色を宣言する
- OnInit() で配列をバッファとして登録する(SetIndexBuffer)
- OnCalculate() で、まだ計算していない足の移動平均を計算してバッファに入れる
そのまま使えるコード
MetaEditor の「新規作成」で「カスタムインディケータ」を選び、できたファイルの中身をすべてこのコードに置き換えてコンパイル(F7)します。ファイルは Indicators フォルダに保存され、ナビゲーターからチャートにドラッグして使います。
MQL4(MT4)
#property strict
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 clrOrange
#property indicator_width1 2
// 単純移動平均線を表示するインジケーター(MT4)
input int InpPeriod = 20; // 期間
double MaBuf[];
int OnInit()
{
SetIndexBuffer(0, MaBuf);
SetIndexStyle(0, DRAW_LINE);
SetIndexLabel(0, "SMA(" + IntegerToString(InpPeriod) + ")");
SetIndexDrawBegin(0, InpPeriod);
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[])
{
if(rates_total <= InpPeriod) return 0;
ArraySetAsSeries(close, true); // MQL4 は 0 = 最新の足
int limit = rates_total - prev_calculated;
if(prev_calculated == 0) limit = rates_total - InpPeriod;
for(int i = limit; i >= 0; i--)
{
if(i + InpPeriod > rates_total) continue;
double sum = 0;
for(int k = 0; k < InpPeriod; k++) sum += close[i + k];
MaBuf[i] = sum / InpPeriod;
}
return rates_total;
}MQL5(MT5)
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots 1
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrOrange
#property indicator_width1 2
// 単純移動平均線を表示するインジケーター(MT5)
input int InpPeriod = 20; // 期間
double MaBuf[];
int OnInit()
{
SetIndexBuffer(0, MaBuf, INDICATOR_DATA);
PlotIndexSetString(0, PLOT_LABEL, "SMA(" + IntegerToString(InpPeriod) + ")");
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, InpPeriod);
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[])
{
if(rates_total <= InpPeriod) return 0;
// MQL5 は 0 = 最も古い足。前回の続きから計算する
int start = (prev_calculated > 0) ? prev_calculated - 1 : InpPeriod - 1;
for(int i = start; i < rates_total; i++)
{
double sum = 0;
for(int k = 0; k < InpPeriod; k++) sum += close[i - k];
MaBuf[i] = sum / InpPeriod;
}
return rates_total;
}MT4 版・MT5 版とも MetaEditor でコンパイル(エラー・警告なし)を確認済みです。実際の口座で使う前に、デモ口座やストラテジーテスターで動作を確かめてください。
カスタマイズのポイント
- 計算式の部分を変えれば、ほかの指標も同じ形で作れます。
- 標準の移動平均を使いたいだけなら、MQL4 は iMA()、MQL5 は iMA() + CopyBuffer() で値を読んでバッファに入れる方法もあります。
注意点
- MQL4 と MQL5 で配列の向き(0 が最新か最古か)が逆なので、ループの向きも逆になります。
このコードで使っている関数
うまく動かないときに出やすいエラー
あわせて読みたいレシピ
「インジケーター作成」のレシピ一覧
- 移動平均線を表示するインジケーターを作りたい
- サブウィンドウにオシレーターを表示したい
- 上昇・下降で色が変わる線を作りたい












