2012年6月9日土曜日

FXのチャート分析ソフトMT4でゴールデンクロス、デッドクロスに矢印のマークを付けるには

ゴールデンクロスとは、短期の移動平均線が、長期の移動平均線を下から上へ突き抜けた状態のことです。ゴールデンクロスは、買いのエントリーポイントになります。

デッドクロスとは、短期の移動平均線が、長期の移動平均線を上から下へ突き抜けた状態のことです。デッドクロスは、売りのエントリーポイントになります。



上の図は、チャート画面に短期と長期の移動平均線を描画したものです。赤色の丸で囲んだ部分がゴールデンクロス、黄色の丸で囲んだ部分がデッドクロスになります。

ここでは、FX(外国為替証拠金取引)のチャート分析ソフトMT4(Meta Trader 4)で2本の移動平均線を描画して、ゴールデンクロス、デッドクロスの地点に矢印のマークを付けるインディケーターを作成します。

以下のソースリストを「MA.mq4」のファイル名で保存します。保存先は、MT4のインストールされているフォルダの「experts」内の「indicators」です。

「MA.mq4」
//+------------------------------------------------------------------+
//| MA.mq4 |
//| sample |
//| http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "sample"
#property link "http://www.metaquotes.net/"
#property indicator_chart_window
#property indicator_color1 Yellow
#property indicator_color2 Red
#property indicator_color3 Yellow
#property indicator_color4 Red
#property indicator_buffers 4
//
double MA1[];
double MA2[];
double BuyBuffer[];
double SellBuffer[];
extern int Short_Period=7;
extern int Long_period=26;
//
int init()
{
SetIndexBuffer(0,MA1);
SetIndexBuffer(1,MA2);
SetIndexBuffer(2,BuyBuffer);
SetIndexBuffer(3,SellBuffer);
SetIndexStyle(2,DRAW_ARROW,STYLE_SOLID,1,Yellow);
SetIndexStyle(3,DRAW_ARROW,STYLE_SOLID,1,Red);
SetIndexArrow(2,241);
SetIndexArrow(3,242);
}

int start()
{
int i,j;
int counted_bars=IndicatorCounted();
int limit=Bars-counted_bars;
if(counted_bars == 0) limit -= Long_period-1;
for(i=limit-1; i>=0; i--)
{
MA1[i]=0;
for(j=0; j<=Short_Period-1; j++) { MA1[i] += Close[i+j]; } MA1[i] /= Short_Period; MA2[i]=0; for(j=0; j<=Long_period-1; j++) { MA2[i] += Close[i+j]; } MA2[i] /= Long_period; // SellBuffer[i]=EMPTY_VALUE; if(MA1[i+1]<=MA2[i+1] && MA1[i+2]>=MA2[i+2]) SellBuffer[i]=Open[i]*1.01;
BuyBuffer[i]=EMPTY_VALUE;
if(MA1[i+1]>=MA2[i+1] && MA1[i+2]<=MA2[i+2]) BuyBuffer[i]=Open[i]*0.99; } return(0); } 次の図は、チャート画面に「MA.mq4」を描画したものです。