MT4でインディケータを作成する方法(MQL4)

  • LINEで送る

この記事ではMT4を使ってインディケータを作成する方法に関して、解説していきます。
今回は例として、RSIを使って作成していきます!

インディケータを作成する方法

エディタ

MT4上にある赤枠内のマークをクリックしてください。
そうすると、「メタエディター」といってEAやインディケータを作成することができるフォームが立ち上がります。こちらはMQLといってMT4専用のプログラミング言語を使用して作成していきます!

Indicators_castam

新規作成⇒カスタムインディケータを選択して次へを押す。
※上のエキスパートアドバイザはEAを作成するときに使用します。

indicators00

indicators01

Indicators02

必要事項を記入して完了まで行います。
ここから実際にプログラミングを行っていきます!

  1. #property indicator_separate_window //サブウィンドウに表示
  2. #property indicator_buffers 1
  3. #property indicator_color1 clrWhite //インディケータの色
  4. #property indicator_width1 2 //インディケータの線幅
  5. #property indicator_minimum 0 //最低値
  6. #property indicator_maximum 100 //最高値
  7. #property indicator_level1 30.0 //閾値1
  8. #property indicator_level2 70.0 //閾値2
  9. #property indicator_levelcolor clrSilver //閾値の色
  10. #property indicator_levelstyle STYLE_DOT //閾値の種類
  11. 上記はインディケータを作成するために必要なパラメーターとなっております。
  12. extern int RSI_Period = 14; //RSIの期間(パラメーター画面で変更可能)
  13. //指標バッファを宣言
  14. double RSI[]; //RSIを入れておく箱を用意
  15. externを使用することでMT4上からでも値を変更できるようにしています。
  16. ここではRSIの期間を変更するために使用しています。
  17. int OnInit()
  18.   {
  19. //— indicator buffers mapping
  20.       SetIndexStyle(0, DRAW_LINE); //線の種類(実線)
  21.     SetIndexBuffer(0, RSI);
  22.     SetIndexLabel(0, “RSI”);
  23. //—
  24.    return(INIT_SUCCEEDED);
  25.   }

 

OnInit()内は上記のように記載し、インディケータ表示に必要な情報を記載していきます。
今回は「DRAW_LINE」を使用していますがその他に、

  • DRAW_SECTION・・・セクションを描画
  • DRAW_HISTOGRAM・・・ヒストグラムを描画
  • DRAW_ARROW・・・矢印を描画

があります。

続いて、インディケータの表示に必要な核となる部分に関して説明していきます!

  1. int OnCalculate(const int rates_total,
  2.                 const int prev_calculated,
  3.                 const datetime &time[],
  4.                 const double &open[],
  5.                 const double &high[],
  6.                 const double &low[],
  7.                 const double &close[],
  8.                 const long &tick_volume[],
  9.                 const long &volume[],
  10.                 const int &spread[])
  11.   {
  12. //—
  13.    int counted_bar = IndicatorCounted();
  14.    if(counted_bar == 0)
  15.    {
  16.       counted_bar = counted_bar + RSI_Period;
  17.    }
  18.    int limit = Bars – counted_bar;
  19.    //ループ処理でiが0になるまで実行し続ける
  20.     for(int i = limit; i >= 0; i–)
  21.     {
  22.       double rsi = iRSI(NULL,0,RSI_Period,PRICE_CLOSE,i); //RSIを代入
  23.       RSI[i] = rsi; //RSIを表示
  24.    }
  25. //— return value of prev_calculated for next call
  26.    return(rates_total);
  27.   }

 

過去にも表示をさせたい場合はfor文を使ってループ処理を行います。
今回はlimitから0(現在足)になるまで計算しています。
limitはチャート上のバー本数ー計算済みバー本数になります。
⇒インディケータを過去全てに表示することができます。

これでRSIを簡単に表示することができます!
実際にチャートに挿入していきましょう!

RSI_practice

上記画像のように表示することができましたね!
このように簡単に表示することができるので色々数値を変えたりしてみて試してみてください!
最後にコード全文を掲載しておきます。

サンプルコード

  1. #property indicator_separate_window //サブウィンドウに表示
  2. #property indicator_buffers 1
  3. #property indicator_color1 clrWhite //インディケータの色
  4. #property indicator_width1 2 //インディケータの線幅
  5. #property indicator_minimum 0 //最低値
  6. #property indicator_maximum 100 //最高値
  7. #property indicator_level1 30.0 //閾値1
  8. #property indicator_level2 70.0 //閾値2
  9. #property indicator_levelcolor clrSilver //閾値の色
  10. #property indicator_levelstyle STYLE_DOT //閾値の種類
  11. extern int RSI_Period = 14; //RSIの期間(パラメーター画面で変更可能)
  12. //指標バッファを宣言
  13. double RSI[]; //RSIを入れておく箱を用意
  14. //+——————————————————————+
  15. //| Custom indicator initialization function |
  16. //+——————————————————————+
  17. int OnInit()
  18.   {
  19. //— indicator buffers mapping
  20.    SetIndexStyle(0, DRAW_LINE); //線の種類(実線)
  21.     SetIndexBuffer(0, RSI);
  22.     SetIndexLabel(0, “RSI”);
  23. //—
  24.    return(INIT_SUCCEEDED);
  25.   }
  26. //+——————————————————————+
  27. //| Custom indicator iteration function |
  28. //+——————————————————————+
  29. int OnCalculate(const int rates_total,
  30.                 const int prev_calculated,
  31.                 const datetime &time[],
  32.                 const double &open[],
  33.                 const double &high[],
  34.                 const double &low[],
  35.                 const double &close[],
  36.                 const long &tick_volume[],
  37.                 const long &volume[],
  38.                 const int &spread[])
  39.   {
  40. //—
  41.    int counted_bar = IndicatorCounted();
  42.    if(counted_bar == 0)
  43.    {
  44.       counted_bar = counted_bar + RSI_Period;
  45.    }
  46.    int limit = Bars – counted_bar;
  47.    //ループ処理でiが0になるまで実行し続ける
  48.     for(int i = limit; i >= 0; i–)
  49.     {
  50.       double rsi = iRSI(NULL,0,RSI_Period,PRICE_CLOSE,i); //RSIを代入
  51.       RSI[i] = rsi; //RSIを表示
  52.    }
  53. //— return value of prev_calculated for next call
  54.    return(rates_total);
  55.   }

 

以上、ご活用ください!

SNSでもご購読できます。

友だち追加

コメントを残す

*

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください