MQL4MQL5 インジケーター インジケーター作成
サブウィンドウにオシレーターを表示したい
結論:#property indicator_separate_window を書くと、チャートの下のサブウィンドウに表示されます。
考え方
RSI のようにチャートの下の別の窓に表示するインジケーターは、indicator_chart_window の代わりに indicator_separate_window を宣言します。水平線(レベル)や表示範囲も #property で決められます。ここでは「終値が移動平均から何%離れているか(移動平均乖離率)」をサブウィンドウに表示するインジケーターを例にしています。
- #property indicator_separate_window でサブウィンドウに表示する
- #property indicator_level1 などで水平線を引く
- 各足の乖離率 =(終値 − 移動平均)÷ 移動平均 × 100 を計算してバッファに入れる
そのまま使えるコード
MetaEditor の「新規作成」で「カスタムインディケータ」を選び、できたファイルの中身をすべてこのコードに置き換えてコンパイル(F7)します。ファイルは Indicators フォルダに保存され、ナビゲーターからチャートにドラッグして使います。
MQL4(MT4)
#property strict
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 clrDodgerBlue
#property indicator_level1 0.0
// 移動平均乖離率(%)をサブウィンドウに表示するインジケーター(MT4)
input int InpPeriod = 25; // 移動平均の期間
double DevBuf[];
int OnInit()
{
SetIndexBuffer(0, DevBuf);
SetIndexStyle(0, DRAW_LINE);
IndicatorShortName("乖離率(" + IntegerToString(InpPeriod) + ")");
IndicatorDigits(2);
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);
int limit = rates_total - prev_calculated;
if(prev_calculated == 0) limit = rates_total - InpPeriod;
for(int i = limit; i >= 0; i--)
{
double ma = iMA(_Symbol, 0, InpPeriod, 0, MODE_SMA, PRICE_CLOSE, i);
DevBuf[i] = (ma != 0) ? (close[i] - ma) / ma * 100.0 : 0;
}
return rates_total;
}MQL5(MT5)
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots 1
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrDodgerBlue
#property indicator_level1 0.0
// 移動平均乖離率(%)をサブウィンドウに表示するインジケーター(MT5)
input int InpPeriod = 25; // 移動平均の期間
double DevBuf[];
int hMa = INVALID_HANDLE;
int OnInit()
{
SetIndexBuffer(0, DevBuf, INDICATOR_DATA);
IndicatorSetString(INDICATOR_SHORTNAME, "乖離率(" + IntegerToString(InpPeriod) + ")");
IndicatorSetInteger(INDICATOR_DIGITS, 2);
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 || BarsCalculated(hMa) < rates_total) return 0;
double ma[];
if(CopyBuffer(hMa, 0, 0, rates_total, ma) < rates_total) return 0;
int start = (prev_calculated > 0) ? prev_calculated - 1 : InpPeriod - 1;
for(int i = start; i < rates_total; i++)
DevBuf[i] = (ma[i] != 0) ? (close[i] - ma[i]) / ma[i] * 100.0 : 0;
return rates_total;
}MT4 版・MT5 版とも MetaEditor でコンパイル(エラー・警告なし)を確認済みです。実際の口座で使う前に、デモ口座やストラテジーテスターで動作を確かめてください。
カスタマイズのポイント
- ±1% などの水平線を追加したいときは、#property indicator_level2 1.0 のように番号を増やします。
- 表示範囲を固定したいときは #property indicator_minimum / indicator_maximum を書きます。
このコードで使っている関数
あわせて読みたいレシピ
「インジケーター作成」のレシピ一覧
- 移動平均線を表示するインジケーターを作りたい
- サブウィンドウにオシレーターを表示したい
- 上昇・下降で色が変わる線を作りたい












