2015年5月8日金曜日

今度は移動平均3本で判定! 少し見にくい・・けど? PathFinderMAを作ってみた。

昨日の「シンプルだけど効果あり!MT4でDrawSimpleLineインジケータを作ってみた。」は移動平均4本の差でしたが、今度は3本で判断するインジケータ PathFinderMAを作ってみました。

PathFinderMA.PNG

短期移動平均の一本前差と、中期-長期移動平均差を眺めて判定するインジケータです。
ヒストグラム形式で出ているのが短期移動平均の一本前差です。今回はわかりやすいように、短期移動平均差が上昇の場合は水色、下降の場合は赤色で描画しています。黄色い線が中期移動平均-長期移動平均です。
見方としては、黄色い線が0以上の箇所で上昇から下降に転じた際、同時に短期の短期移動平均差が下降している場合は売り、黄色い線が0以下の箇所で下降から上昇に転じた場合は買いとなります。

うーん?少々短期の移動平均差が見えにくいですよね。短期移動平均差が0付近をうろうろしている場合は取引しない方がよさそうです。明確に上下しているときのみ取引といった形になるかと思います。
短期移動平均差をさらに移動平均で表現したほうがもしかしたらわかりやすいかもしれません。

このインジケータも移動平均ベースのモメンタムを表現していますので、ダイバージェンス現象(高値/安値を更新しているのにもかかわらずインジケータ上では前回位置より低い位置にピークが来ること)が見て取れます。組み合わせて判断するのがよさそうに思われます。

//------------------------------------------------------------------
// PathFinderMA

#property copyright "Copyright 2015,  Daisuke"
#property link      "http://mt4program.blogspot.jp/"
#property version   "1.00"
#property strict
#property indicator_separate_window

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

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

// 短期前足差 上げ
#property indicator_label1  "SHORT UP"
#property indicator_type1   DRAW_HISTOGRAM
#property indicator_color1  clrAqua
#property indicator_style1  STYLE_SOLID

// 短期前足差 下げ
#property indicator_label2  "SHORT DOWN"
#property indicator_type2   DRAW_HISTOGRAM
#property indicator_color2  clrIndianRed
#property indicator_style2  STYLE_SOLID

// 中期-長期
#property indicator_label3  "LINE"
#property indicator_type3   DRAW_LINE
#property indicator_color3  clrYellow
#property indicator_style3  STYLE_SOLID
#property indicator_width3  1


// 入力パラメータ 短期移動平均期間
input int MaShortPeriod = 6;
// 入力パラメータ 中期移動平均期間
input int MaMiddlePeriod = 9;
// 入力パラメータ 長期移動平均期間
input int MaLongPeriod = 18;

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

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

// バッファー
double shortUpBuffer[];
double shortDownBuffer[];
double lineBuffer[];

//------------------------------------------------------------------
//初期化
int OnInit()
{
// 短縮名を設定
string shortName = "PFMA (";
shortName += IntegerToString(MaShortPeriod) + "," ;
shortName += IntegerToString(MaMiddlePeriod) + ",";
shortName += IntegerToString(MaLongPeriod) + ")";
IndicatorShortName(shortName);

SetIndexBuffer(0, shortUpBuffer);
SetIndexBuffer(1, shortDownBuffer);
SetIndexBuffer(2, lineBuffer);

SetIndexDrawBegin(0, MaLongPeriod);
SetIndexDrawBegin(1, MaLongPeriod);
SetIndexDrawBegin(2, MaLongPeriod);

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; i-- )
{
lineBuffer[i] = iMA(NULL, PERIOD_CURRENT, MaMiddlePeriod, 0, MaMethod, MaPrice, i) - iMA(NULL, PERIOD_CURRENT, MaLongPeriod, 0, MaMethod, MaPrice, i);

if( i == rates_total - 1 )
{
shortUpBuffer[i] = 0 ;
shortDownBuffer[i] = 0 ;
}
else
{
double beforeDiff = shortUpBuffer[i + 1] == 0 ? shortDownBuffer[i + 1] : shortUpBuffer[i + 1] ;
double shortDiff = iMA(NULL, PERIOD_CURRENT, MaShortPeriod, 0, MaMethod, MaPrice, i) - iMA(NULL, PERIOD_CURRENT, MaShortPeriod, 0, MaMethod, MaPrice, i + 1);

if( shortDiff < beforeDiff )
{
shortUpBuffer[i] = 0;
shortDownBuffer[i] = shortDiff;
}
else
{
shortUpBuffer[i] = shortDiff;
shortDownBuffer[i] = 0;
}
}
}

return(rates_total - 1);
}


ここ最近、モメンタム系オシレータのインジばかり作成していますので、次はレンジブレイク系のインジ作成する予定です。