MQL4MQL5 インジケーター インジケーター作成
上昇・下降で色が変わる線を作りたい
結論:MQL5 は DRAW_COLOR_LINE と色番号のバッファを使います。MQL4 は色ごとに線のバッファを分け、つなぎ目の足に両方の値を入れます。
考え方
移動平均線を「上向きなら青、下向きなら赤」のように色分けすると、トレンドが一目で分かります。MQL5 には色分け専用の描画(DRAW_COLOR_LINE)があり、値のバッファと色番号のバッファを組み合わせます。MQL4 にはないため、上昇用・下降用の2本の線を用意して描き分けます。
- 移動平均の値を計算する
- 1本前より上がっていれば上昇、下がっていれば下降と判定する
- MQL5 は色番号のバッファに 0(上昇色)か 1(下降色)を入れる。MQL4 は上昇用・下降用のバッファの片方に値を入れる
そのまま使えるコード
MetaEditor の「新規作成」で「カスタムインディケータ」を選び、できたファイルの中身をすべてこのコードに置き換えてコンパイル(F7)します。ファイルは Indicators フォルダに保存され、ナビゲーターからチャートにドラッグして使います。
MQL4(MT4)
#property strict
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 clrDodgerBlue
#property indicator_color2 clrRed
#property indicator_width1 2
#property indicator_width2 2
// 上向きは青、下向きは赤で表示する移動平均線(MT4)
input int InpPeriod = 20; // 期間
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[])
{
if(rates_total <= InpPeriod + 2) return 0;
int limit = rates_total - prev_calculated + 1;
if(prev_calculated == 0) limit = rates_total - InpPeriod - 2;
for(int i = limit; i >= 0; i--) // MQL4 は 0 = 最新の足
{
double ma0 = iMA(_Symbol, 0, InpPeriod, 0, MODE_SMA, PRICE_CLOSE, i);
double ma1 = iMA(_Symbol, 0, InpPeriod, 0, MODE_SMA, PRICE_CLOSE, i + 1);
UpBuf[i] = EMPTY_VALUE;
DnBuf[i] = EMPTY_VALUE;
if(ma0 >= ma1)
{
UpBuf[i] = ma0;
if(DnBuf[i + 1] != EMPTY_VALUE) UpBuf[i + 1] = ma1; // 色の切り替わりで線をつなぐ
}
else
{
DnBuf[i] = ma0;
if(UpBuf[i + 1] != EMPTY_VALUE) DnBuf[i + 1] = ma1;
}
}
return rates_total;
}MQL5(MT5)
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots 1
#property indicator_type1 DRAW_COLOR_LINE
#property indicator_color1 clrDodgerBlue, clrRed
#property indicator_width1 2
// 上向きは青、下向きは赤で表示する移動平均線(MT5)
input int InpPeriod = 20; // 期間
double MaBuf[], ColorBuf[];
int hMa = INVALID_HANDLE;
int OnInit()
{
SetIndexBuffer(0, MaBuf, INDICATOR_DATA);
SetIndexBuffer(1, ColorBuf, INDICATOR_COLOR_INDEX); // 色の番号(0=青、1=赤)
hMa = iMA(_Symbol, PERIOD_CURRENT, InpPeriod, 0, MODE_SMA, PRICE_CLOSE);
return hMa == INVALID_HANDLE ? INIT_FAILED : INIT_SUCCEEDED;
}
void OnDeinit(const int reason)
{
IndicatorRelease(hMa);
}
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 + 1 || BarsCalculated(hMa) < rates_total) return 0;
if(CopyBuffer(hMa, 0, 0, rates_total, MaBuf) < rates_total) return 0; // バッファに直接コピー
int start = (prev_calculated > 1) ? prev_calculated - 1 : InpPeriod;
for(int i = start; i < rates_total; i++)
ColorBuf[i] = (MaBuf[i] >= MaBuf[i - 1]) ? 0 : 1;
return rates_total;
}MT4 版・MT5 版とも MetaEditor でコンパイル(エラー・警告なし)を確認済みです。実際の口座で使う前に、デモ口座やストラテジーテスターで動作を確かめてください。
カスタマイズのポイント
- MQL5 で色を3色にしたいときは、indicator_color1 に色を3つ並べ、色番号に 0・1・2 を入れます。
- 傾きではなく価格との位置関係(終値が上なら青)で色分けすることもできます。
注意点
- MQL4 の方法では、色が切り替わる足で前の足にも値を入れないと、線が途切れて見えます。
このコードで使っている関数
あわせて読みたいレシピ
「インジケーター作成」のレシピ一覧
- 移動平均線を表示するインジケーターを作りたい
- サブウィンドウにオシレーターを表示したい
- 上昇・下降で色が変わる線を作りたい












