売買シグナルの表示


図の上矢印や下矢印で、条件を満たした際に、買いシグナルや売りシグナルを表示させる方法。

表示のためのポイントは下記2つかと思う。

・ポイント1
 シグナル用のバッファを用意すること。
 用意したバッファに、シグナルを表示させるポイント(価格の始値など)を格納する。
・ポイント2
 指標スタイルをシグナル用のバッファに対して設定すること。
 SetIndexStyle、SetIndexArrowの使用。

初期化関数initまでで、ポイントは全て登場する。

#property copyright "Copyright 2013, graySpace999."
#property link      "http://d.hatena.ne.jp/graySpace/"

#property indicator_chart_window

#property indicator_buffers 4
#property indicator_color1 Red
#property indicator_color2 Blue
#property indicator_color3 Red
#property indicator_color4 Blue

#property indicator_width1 2
#property indicator_width2 2

double BufFastMA[];
double BufSlowMA[];

//ポイント1
double BufBuy[];
double BufSell[];

string fastMaIndexLabel = "Fast MA";
string slowMaIndexLabel = "Slow MA";
string buyIndexLabel    = "Buy";
string sellIndexLabel   = "Sell";

extern int fastMaPeriod = 5;
extern int slowMaPeriod = 20;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   //Allocate Indicator Buffer
   SetIndexBuffer(0, BufFastMA);
   SetIndexBuffer(1, BufSlowMA);
   SetIndexBuffer(2, BufBuy);
   SetIndexBuffer(3, BufSell);
   
   //Set Indicator Label
   SetIndexLabel(0,fastMaIndexLabel);
   SetIndexLabel(1,slowMaIndexLabel);
   SetIndexLabel(2,buyIndexLabel);
   SetIndexLabel(3,sellIndexLabel);
   
   //Set Indicator Style
   //ポイント2
   SetIndexStyle(2,DRAW_ARROW,STYLE_SOLID,2,Yellow);
   SetIndexArrow(2,233);
   SetIndexStyle(3,DRAW_ARROW,STYLE_SOLID,2,White);
   SetIndexArrow(3,234);
   
   
   return(0);
  }

あとは、条件部分の計算をするだけ。

int start()
  {
   int counted_bars = IndicatorCounted();
   int limit = Bars - counted_bars;
   
   if(counted_bars == 0){
      limit -= slowMaPeriod;
   }
      
   for(int i=limit-1; 0<=i; i--){
     BufFastMA[i] = iMA(NULL, 0, fastMaPeriod, 0, MODE_SMA, PRICE_CLOSE, i);
     BufSlowMA[i] = iMA(NULL, 0, slowMaPeriod, 0, MODE_SMA, PRICE_CLOSE, i); 
   }

   if(counted_bars == 0){
     limit -= 2;
   }
   for(i=limit-1; 0<=i; i--){
      BufBuy[i] = EMPTY_VALUE;
      if(BufFastMA[i+2] < BufSlowMA[i+2] && BufSlowMA[i+1] <= BufFastMA[i+1]){
         BufBuy[i] = Open[i];
      }
      
      BufSell[i] = EMPTY_VALUE;
      if(BufFastMA[i+2] > BufSlowMA[i+2] && BufSlowMA[i+1] >= BufFastMA[i+1]){
         BufSell[i] = Open[i];
      }
   }
   
   return(0);
  }

このコードは殆どこの本の通り。

FXメタトレーダー入門―最先端システムトレードソフト使いこなし術 (現代の錬金術師シリーズ 56)

FXメタトレーダー入門―最先端システムトレードソフト使いこなし術 (現代の錬金術師シリーズ 56)