この記事ではMT4を使ってインディケータを作成する方法に関して、解説していきます。
今回は例として、RSIを使って作成していきます!
インディケータを作成する方法
MT4上にある赤枠内のマークをクリックしてください。
そうすると、「メタエディター」といってEAやインディケータを作成することができるフォームが立ち上がります。こちらはMQLといってMT4専用のプログラミング言語を使用して作成していきます!
新規作成⇒カスタムインディケータを選択して次へを押す。
※上のエキスパートアドバイザはEAを作成するときに使用します。
必要事項を記入して完了まで行います。
ここから実際にプログラミングを行っていきます!
- #property indicator_separate_window //サブウィンドウに表示
- #property indicator_buffers 1
- #property indicator_color1 clrWhite //インディケータの色
- #property indicator_width1 2 //インディケータの線幅
- #property indicator_minimum 0 //最低値
- #property indicator_maximum 100 //最高値
- #property indicator_level1 30.0 //閾値1
- #property indicator_level2 70.0 //閾値2
- #property indicator_levelcolor clrSilver //閾値の色
- #property indicator_levelstyle STYLE_DOT //閾値の種類
- 上記はインディケータを作成するために必要なパラメーターとなっております。
- extern int RSI_Period = 14; //RSIの期間(パラメーター画面で変更可能)
- //指標バッファを宣言
- double RSI[]; //RSIを入れておく箱を用意
- externを使用することでMT4上からでも値を変更できるようにしています。
- ここではRSIの期間を変更するために使用しています。
- int OnInit()
- {
- //— indicator buffers mapping
- SetIndexStyle(0, DRAW_LINE); //線の種類(実線)
- SetIndexBuffer(0, RSI);
- SetIndexLabel(0, “RSI”);
- //—
- return(INIT_SUCCEEDED);
- }
OnInit()内は上記のように記載し、インディケータ表示に必要な情報を記載していきます。
今回は「DRAW_LINE」を使用していますがその他に、
- DRAW_SECTION・・・セクションを描画
- DRAW_HISTOGRAM・・・ヒストグラムを描画
- DRAW_ARROW・・・矢印を描画
があります。
続いて、インディケータの表示に必要な核となる部分に関して説明していきます!
- 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 counted_bar = IndicatorCounted();
- if(counted_bar == 0)
- {
- counted_bar = counted_bar + RSI_Period;
- }
- int limit = Bars – counted_bar;
- //ループ処理でiが0になるまで実行し続ける
- for(int i = limit; i >= 0; i–)
- {
- double rsi = iRSI(NULL,0,RSI_Period,PRICE_CLOSE,i); //RSIを代入
- RSI[i] = rsi; //RSIを表示
- }
- //— return value of prev_calculated for next call
- return(rates_total);
- }
過去にも表示をさせたい場合はfor文を使ってループ処理を行います。
今回はlimitから0(現在足)になるまで計算しています。
limitはチャート上のバー本数ー計算済みバー本数になります。
⇒インディケータを過去全てに表示することができます。
これでRSIを簡単に表示することができます!
実際にチャートに挿入していきましょう!
上記画像のように表示することができましたね!
このように簡単に表示することができるので色々数値を変えたりしてみて試してみてください!
最後にコード全文を掲載しておきます。
サンプルコード
- #property indicator_separate_window //サブウィンドウに表示
- #property indicator_buffers 1
- #property indicator_color1 clrWhite //インディケータの色
- #property indicator_width1 2 //インディケータの線幅
- #property indicator_minimum 0 //最低値
- #property indicator_maximum 100 //最高値
- #property indicator_level1 30.0 //閾値1
- #property indicator_level2 70.0 //閾値2
- #property indicator_levelcolor clrSilver //閾値の色
- #property indicator_levelstyle STYLE_DOT //閾値の種類
- extern int RSI_Period = 14; //RSIの期間(パラメーター画面で変更可能)
- //指標バッファを宣言
- double RSI[]; //RSIを入れておく箱を用意
- //+——————————————————————+
- //| Custom indicator initialization function |
- //+——————————————————————+
- int OnInit()
- {
- //— indicator buffers mapping
- SetIndexStyle(0, DRAW_LINE); //線の種類(実線)
- SetIndexBuffer(0, RSI);
- SetIndexLabel(0, “RSI”);
- //—
- return(INIT_SUCCEEDED);
- }
- //+——————————————————————+
- //| Custom indicator iteration function |
- //+——————————————————————+
- 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 counted_bar = IndicatorCounted();
- if(counted_bar == 0)
- {
- counted_bar = counted_bar + RSI_Period;
- }
- int limit = Bars – counted_bar;
- //ループ処理でiが0になるまで実行し続ける
- for(int i = limit; i >= 0; i–)
- {
- double rsi = iRSI(NULL,0,RSI_Period,PRICE_CLOSE,i); //RSIを代入
- RSI[i] = rsi; //RSIを表示
- }
- //— return value of prev_calculated for next call
- return(rates_total);
- }
以上、ご活用ください!