2015年9月11日金曜日

[MT4インジケータ&プログラミング]他の時間足ボリンジャーバンドを表示する。

昨日は、中心線が選べるボリンジャーバンドを作りましたので、今日は時間足が選択できるボリンジャーバンドを作成したいと思います。
■EURUSD 1Hに1D21本BBを表示。
MTBB.JPG

ここで活躍するのは
iBarShift関数です
int iBarShift(
string symbol, // シンボル
int timeframe, // タイムフレーム
datetime time, // タイム
bool exact=false // モード
);
この関数は、timeで指定された時間に一番近いバーインデックスを調べてくれます。
たとえば、9/11 10:00に一番近い1時間足のバーインデックスは2ですよっていう感じです。
一番近いという判断は切り捨てで判断しているようです。
例 時間足を調べる場合、timeに10:00~10:59を入力すると、10:00のインデックスが帰ってきます。

exactをtrueに指定するとぴったり一致する場合にだけインデックス値を返してきます。
それ以外は-1です。

現在表示しているチャートのシンボルの場合は、symbolにはNULLを指定します。
タイムフレームはバーインデックスを検索したいタイムフレームを指定します。

プログラムの流れ的には
1.iBarShiftで、表示したい時間足のバーインデックスを取得
2.1.で取得したインデックスの移動平均と標準偏差を取得
3.チャートを描画

という形です。

このように、iBarShiftは、短い時間足のチャートに長い時間足の情報を書き込むのに大変役立ちます。

なお・・個人的には、上の時間足のチャートを表示したいのであれば、その分の倍数を掛けた期間のチャートを表示すればよいのでは?と思ったりもしています・・。
たとえば、1時間足に4時間足の21本移動平均BBを出したいのであれば1時間足84本移動平均BBとほぼ同じ値となります。こんな感じです。
■UERUSD1Hに対する4時間足21本BBと1時間足84本BBの違い。滑らかなほうが84本です。
MTBB2.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 ENUM_TIMEFRAMES TargetPeriod = PERIOD_H4;

// 入力パラメータ 移動平均期間
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[])            //スプレット
{
if( Period() > TargetPeriod ) return rates_total ;

for( int i = (rates_total - prev_calculated - 1); i >= 0 && !IsStopped(); i-- )
{
int shift = iBarShift(NULL, TargetPeriod, time[i], false);

center[i] = iMA(NULL, TargetPeriod, MaPeriod, 0, MaMethod, MaPrice, shift);
double std = iStdDev(NULL, TargetPeriod, StdPeriod, 0, StdMethod, MaPrice, shift);
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);
}