2015年9月10日木曜日

[MT4インジケータ&プログラミング]初心に戻って、中心線が選べるボリンジャーバンド

FX-ONの開発者サミットで知ったのですが、皆様ボリンジャーバンドが大好きです。

抵抗線としてみた場合のボリンジャーバンドはオカルトに近いものがあるのですが、単純線形を描くと上昇トレンド時は1σ付近、下降トレンド時は-1σ付近に現在値がくるため、これをトレンドの判断として使っているかたが多いようです。

中心線が単純移動平均の場合遅延が大きい為、これをEMAはLWMAにして遅延を少なくして人より早く判断しようという方も結構いらっしゃいました。
MT4付属ボリンジャーバンドはSMAに固定されてしまっているのですよね。これを各種MAに対応できるようにします。世の中にいっぱい同じものがありますが、アラーム出したりする基礎となるため、あえて作ってみました。

■SMAを中心線にした場合
MyBBSMA.JPG

■EMAを中心線にした場合
MyBBEMA.JPG

//------------------------------------------------------------------
// 中心線が選べるボリンジャーバンド
#property copyright "Copyright 2015,  Daisuke"
#property link      "http://mt4program.blogspot.jp/"
#property version   "1.00"
#property strict
#property indicator_chart_window

//バッファーを指定する。
#property indicator_buffers 5

//プロット数を指定する。
#property indicator_plots   5

#property indicator_label1  "Center"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrAqua
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1

#property indicator_label2  "HIGH1"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrIndianRed
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1

#property indicator_label3  "HIGH2"
#property indicator_type3   DRAW_LINE
#property indicator_color3  clrIndianRed
#property indicator_style3  STYLE_SOLID
#property indicator_width3  1

#property indicator_label4  "LOW1"
#property indicator_type4   DRAW_LINE
#property indicator_color4  clrIndianRed
#property indicator_style4  STYLE_SOLID
#property indicator_width4  1

#property indicator_label5  "LOW2"
#property indicator_type5   DRAW_LINE
#property indicator_color5  clrIndianRed
#property indicator_style5  STYLE_SOLID
#property indicator_width5  1

// 入力パラメータ 移動平均期間
extern int MaPeriod = 21;

//入力パラメータ 表示移動
extern int   MaShift = 0;

//入力パラメータ 移動平均種別
extern ENUM_MA_METHOD MaMethod = MODE_SMA;

//入力パラメータ 適用価格
extern ENUM_APPLIED_PRICE MaPrice = PRICE_MEDIAN;

//標準偏差期間
extern int StdPeriod = 21;

//標準偏差算出方法
extern ENUM_MA_METHOD StdMethod = MODE_SMA;

//σ1
extern double Sigma1 = 1;

//σ2
extern double Sigma2 = 2;

double center[];
double high1[];
double high2[];
double low1[];
double low2[];

//------------------------------------------------------------------
//初期化
int OnInit()
{
//インジケーターバッファを初期化する。
SetIndexBuffer(0,center);
SetIndexBuffer(1,high1);
SetIndexBuffer(2,high2);
SetIndexBuffer(3,low1);
SetIndexBuffer(4,low2);
SetIndexDrawBegin(0, MaPeriod);
SetIndexDrawBegin(1, MaPeriod);
SetIndexDrawBegin(2, MaPeriod);
SetIndexDrawBegin(3, MaPeriod);
SetIndexDrawBegin(4, MaPeriod);

string short_name = "MyBB(" + IntegerToString(MaPeriod) + ")";
IndicatorShortName(short_name);

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[])            //スプレット
{
for( int i = (rates_total - prev_calculated - 1); i >= 0 && !IsStopped(); i-- )
{
center[i] = iMA(NULL, PERIOD_CURRENT, MaPeriod, 0, MaMethod, MaPrice, i);
double std = iStdDev(NULL, PERIOD_CURRENT, StdPeriod, 0, StdMethod, MaPrice, i);
high1[i] = center[i] + std * Sigma1;
high2[i] = center[i] + std * Sigma2;
low1[i] = center[i] - std * Sigma1;
low2[i] = center[i] - std * Sigma2;
}
//元となる値を計算する。
return(rates_total - 1);
}