2012年6月7日木曜日

FXのチャート分析ソフトMT4のインディケーターで矢印を表示するには

FX(外国為替証拠金取引)のチャート分析ソフトMT4(Meta Trader 4)にはさまざまなインディケーターが用意されています。

インディケーターは、売買のエントリーポイントを見つける手段の1つとして用いられています。インディケーターの中には、「↑」や「↓」などの矢印を付けて売買のエントリーポイントが一目でわかるようにしたものもあります。しかし、すべてのインディケーターに矢印が付いているわけではありません。

そこで、既存のインディケーターを改良して矢印を表示してみます。例として、「RSI.mq4」を取り上げます。

まず、MetaEditorを起動します。MetaEditorは、MT4のメニューから「ツール」、「MetaQuotes Language Editor」を選びます。



次に、「File」から「Open」を選び、「experts」フォルダの「indicators」内にある「RSI.mq4」を選択して「開く」のボタンをクリックします。「RSI.mq4」のソースリストが表示されます。



ソースリストを改良する所は以下の通りです。

▼RSIの売買のエントリーポイントになる数値の設定

RSIの売買のエントリーポイントになる数値を設定します。ここでは、売りのエントリーポイントを75、買いのエントリーポイントを25に設定します。

ソースリストの「//---- input parameters」の行の下に次のソースリストを追加します。

extern int RSIUpper=75;
extern int RSILower=25;

なお、数値は自由に変更して構いません。また、「extern」で設定した値は、インディケーターを表示する際に変更ができます。

▼矢印表示用のバッファの確保

売買のエントリーポイントに矢印を表示するためのバッファを確保します。「//---- buffers」の行の下に次のソースリストを追加します。

double ExtUpperBuffer[];
double ExtLowerBuffer[];

次に、インディケーターのバッファを確保します。ソースリストの

#property indicator_buffers 1



#property indicator_buffers 5

に変更します。

また、
IndicatorBuffers(3);



IndicatorBuffers(5);

に変更します。

▼矢印の定義

矢印を表示するための定義をします。「int init()」内に以下のソースリストを追加します。

//arrow
SetIndexBuffer(3,ExtUpperBuffer);
SetIndexBuffer(4,ExtLowerBuffer);
SetIndexStyle(3,DRAW_ARROW,STYLE_SOLID,1,Yellow);
SetIndexStyle(4,DRAW_ARROW,STYLE_SOLID,1,Red);
SetIndexArrow(3,241);
SetIndexArrow(4,242);

▼矢印の表示

売買のエントリーポイントかどうかを判定して矢印を表示します。「int start()」の「i--;」の直前に以下のソースリストを追加します。

//arrow
ExtUpperBuffer[i]=EMPTY_VALUE;
if(RSIBuffer[i]<=RSILower) ExtUpperBuffer[i]=5; ExtLowerBuffer[i]=EMPTY_VALUE; if(RSIBuffer[i]>=RSIUpper) ExtLowerBuffer[i]=95;

ソースリストの修正が終わったら、「RSI_arrow.mq4」のファイル名で保存してから「Compile」を実行します。

「RSI_arrow.mq4」のソースリストは次の通りです。
//+------------------------------------------------------------------+
//| RSI.mq4 |
//| Copyright ゥ 2004, MetaQuotes Software Corp. |
//| http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright ゥ 2004, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net/"

#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_buffers 5
#property indicator_color1 DodgerBlue
//---- input parameters
extern int RSIPeriod=14;
extern int RSIUpper=75;
extern int RSILower=25;
//---- buffers
double RSIBuffer[];
double PosBuffer[];
double NegBuffer[];
double ExtUpperBuffer[];
double ExtLowerBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
string short_name;
//---- 2 additional buffers are used for counting.
IndicatorBuffers(5);
SetIndexBuffer(1,PosBuffer);
SetIndexBuffer(2,NegBuffer);
//---- indicator line
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,RSIBuffer);
//---- name for DataWindow and indicator subwindow label
short_name="RSI("+RSIPeriod+")";
IndicatorShortName(short_name);
SetIndexLabel(0,short_name);
//----
SetIndexDrawBegin(0,RSIPeriod);
//arrow
SetIndexBuffer(3,ExtUpperBuffer);
SetIndexBuffer(4,ExtLowerBuffer);
SetIndexStyle(3,DRAW_ARROW,STYLE_SOLID,1,Yellow);
SetIndexStyle(4,DRAW_ARROW,STYLE_SOLID,1,Red);
SetIndexArrow(3,241);
SetIndexArrow(4,242);
return(0);
}
//+------------------------------------------------------------------+
//| Relative Strength Index |
//+------------------------------------------------------------------+
int start()
{
int i,counted_bars=IndicatorCounted();
double rel,negative,positive;

//----
if(Bars<=RSIPeriod) return(0); //---- initial zero if(counted_bars<1) for(i=1;i<=RSIPeriod;i++) RSIBuffer[Bars-i]=0.0; //---- i=Bars-RSIPeriod-1; if(counted_bars>=RSIPeriod) i=Bars-counted_bars-1;
while(i>=0)
{
double sumn=0.0,sump=0.0;
if(i==Bars-RSIPeriod-1)
{
int k=Bars-2;
//---- initial accumulation
while(k>=i)
{
rel=Close[k]-Close[k+1];
if(rel>0) sump+=rel;
else sumn-=rel;
k--;
}
positive=sump/RSIPeriod;
negative=sumn/RSIPeriod;
}
else
{
//---- smoothed moving average
rel=Close[i]-Close[i+1];
if(rel>0) sump=rel;
else sumn=-rel;
positive=(PosBuffer[i+1]*(RSIPeriod-1)+sump)/RSIPeriod;
negative=(NegBuffer[i+1]*(RSIPeriod-1)+sumn)/RSIPeriod;
}
PosBuffer[i]=positive;
NegBuffer[i]=negative;
if(negative==0.0) RSIBuffer[i]=0.0;
else RSIBuffer[i]=100.0-100.0/(1+positive/negative);

//arrow
ExtUpperBuffer[i]=EMPTY_VALUE;
if(RSIBuffer[i]<=RSILower) ExtUpperBuffer[i]=5; ExtLowerBuffer[i]=EMPTY_VALUE; if(RSIBuffer[i]>=RSIUpper) ExtLowerBuffer[i]=95;

//
i--;
}
//----
return(0);
}
//+------------------------------------------------------------------+

「RSI_arrow.mq4」をチャート画面に表示すると、RSIのラインと併せて矢印が表示されているのがわかります。



他のインディケーターでも、同様の方法で売買のエントリーポイントに矢印を付けたりすることができます。