トレンド系のインジケータとしてボリンジャーバンドは人気があります。このインジケータを作成してみましょう。
ボリンジャーバンド
 
ボリンジャーバンドはMQL4ではiBands()関数を使います。
 
| 1 2 3 4 5 6 7 8 9 10 | double iBands(    string       symbol,           // symbol    int          timeframe,        // timeframe    int          period,           // averaging period    double       deviation,        // standard deviations    int          bands_shift,      // bands shift    int          applied_price,    // applied price    int          mode,             // line index    int          shift             // shift    ); | 
Ref: iBands – MQL4 Documentation
 
目次
1. サンプルコード
1. サンプルコード
サンプルコード
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | //+------------------------------------------------------------------+ // BollingerBand_01.mq4  //+------------------------------------------------------------------+ #property strict // インジケーターをチャートウィンドウに表示させる #property indicator_chart_window // インジケーター描画用のバッファー数 #property indicator_buffers 3 // 表示するインジケータのプロット数 #property indicator_plots 3 // 1番目のインジケータの描画スタイル #property indicator_type1   DRAW_LINE // 1番目のインジケータの描画色 #property indicator_color1  Aqua // 2番目 #property indicator_type2   DRAW_LINE #property indicator_color2  Magenta // 3番目 #property indicator_type3   DRAW_LINE #property indicator_color3  Magenta input int AvaregePeriod = 20;  // 平均期間 input int Deviation = 1;       // 標準偏差 // インジケータのバッファ double BufferMain[]; double BufferUpper[]; double BufferLower[]; //+------------------------------------------------------------------+ //| Custom indicator initialization function                         | //+------------------------------------------------------------------+ int OnInit(){     // Buffer配列を表示用として設定     SetIndexBuffer(0, BufferMain);     SetIndexBuffer(1, BufferUpper);     SetIndexBuffer(2, BufferLower);     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 limit = Bars - prev_calculated;     for(int i = limit - 1; i >= 0; i--){         // Main         BufferMain[i] = iBands(             NULL,            // 通貨ペア名, NULLは現在の通貨ペア             0,               // 時間足, 0は現在の時間足、ENUM_TIMEFRAMES             AvaregePeriod,   // 平均する期間             Deviation,       // 標準偏差             0,               // バンドシフト             PRICE_CLOSE,     // サンプリングする価格, ENUM_APPLIED_PRICE             MODE_MAIN,       // インジケータライン             i                // 0が現在値で過去へのシフト         );         // Upper         BufferUpper[i] = iBands(NULL, 0, AvaregePeriod, Deviation, 0, PRICE_CLOSE, MODE_UPPER, i);         // Lower         BufferLower[i] = iBands(NULL, 0, AvaregePeriod, Deviation, 0, PRICE_CLOSE, MODE_LOWER, i);     }     return rates_total; } | 
 
このサンプルではデフォルトで+/-1σのボリンジャーバンドを表示するためにiBands()をMainと+1σ,-1σの3つを記述しています。+/2σはパラメータで標準偏差を2にすることで可能です。(つまりMT4にあるものと同じ)
#property strict
を宣言することでコンパイラが厳しく警告を出してきますが、
| 1 | input int Deviation = 1; // 標準偏差 | 
のパラメータがコメントアウトした「標準偏差」と表示されます。

 
 
References:
Program Properties (#property) – MQL4 Documentation
iBands – MQL4 Documentation
ENUM_TIMEFRAMES – MQL4 Documentation
Price Constants – MQL4 Reference















