【MQL】矢印の様々な表示方法

今回は3連続陽線が発生したら矢印を表示するプログラムを使って、矢印の様々な表示方法をまとめました。

ケース1 確定足で矢印を表示する

まずは、確定足で矢印を表示する場合です。足の確定を待ってシグナルを出すインジケータに利用できるのではないかと思います。

//・・・・・・・・・・・・・・・・・・・・・・・・・・・
//3本連続陽線で矢印を表示(確定足で判断する場合)
//・・・・・・・・・・・・・・・・・・・・・・・・・・・

#property version   "1.00"
#property strict

#property indicator_chart_window
#property indicator_buffers 1

#property indicator_color1 clrRed
#property indicator_width1 2

double ThreeUp[];

int OnInit()
{
  SetIndexBuffer(0, ThreeUp);
  SetIndexStyle(0, DRAW_ARROW, STYLE_SOLID);
  SetIndexArrow(0, SYMBOL_ARROWUP);
  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[])
{
  int limit = rates_total - prev_calculated;
  int n = 2;                       //array out of range対処
  if(limit > 1)                    //初期状態(インジケータ挿入直後)の場合のlimit定義
  {
     limit = rates_total -n -1;
  }
  
  if(limit == 1)                   //足が更新された場合のlimit定義
  {
     limit = 2;
  }
  
  for(int i=limit; i>=1; i --)     //足が更新されていない場合は、rates_total - prev_calculated = 0 となるので何もしない。
  {
     if(open[i+2] < close[i+2] &&    //i + "2" なので array out of range の対処として n=2 とする
        open[i+1] < close[i+1] &&
        open[ i ] < close[ i ] )
     {
        ThreeUp[i] = high[i] + 20*Point;
     }
  }     
  return(rates_total);
}

【実行結果】

足の確定後に矢印が表示されていることが分かります。


ケース2 現在足で矢印を表示する

次に、現在足で矢印を表示する場合です。足の確定を待たないため、トレードで使用する際にはダマシに引っかかる可能性もあります。

//・・・・・・・・・・・・・・・・・・・・・・・・・・・
//3本連続陽線で矢印を表示(現在足で判断する場合)
//・・・・・・・・・・・・・・・・・・・・・・・・・・・


#property version   "1.00"
#property strict

#property indicator_chart_window
#property indicator_buffers 1

#property indicator_color1 clrRed
#property indicator_width1 2

double ThreeUp[];

int OnInit()
{
  SetIndexBuffer(0, ThreeUp);
  SetIndexStyle(0, DRAW_ARROW, STYLE_SOLID);
  SetIndexArrow(0, SYMBOL_ARROWUP);
  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[])
{
  int limit = rates_total - prev_calculated;
  int n = 2;                       //array out of range対処
  if(limit > 1)                    //初期状態(インジケータ挿入直後)の場合のlimit定義
  {
     limit = rates_total - n - 1;
  }
  
  for(int i=limit; i>=0; i --)     
  {
     ThreeUp[i] = EMPTY_VALUE;
     if(open[i+2] < close[i+2] &&    //i + "2" なので array out of range の対処として n=2 とする
        open[i+1] < close[i+1] &&
        open[ i ] < close[ i ] )
     {
        ThreeUp[i] = high[i] + 20*Point;
     }
  }     
  return(rates_total);
}

【実行結果】

一番先頭の足を見ると、矢印がついたり消えたりしています。現在足で矢印が表示されていることが分かります。


ケース3 現在足で一度でも条件が発生したら矢印を表示する

最後に、現在足で一度でも条件が発生したら矢印を表示する場合です。一度条件が発生したら矢印を表示するため、ダマシが発生した後のトレード検証に役立つかもしれません。

//・・・・・・・・・・・・・・・・・・・・・・・・・・・
//3本連続陽線で矢印を表示(現在足で判断する場合)
//現在足で一度でも条件が発生したら矢印を表示
//・・・・・・・・・・・・・・・・・・・・・・・・・・・


#property version   "1.00"
#property strict

#property indicator_chart_window
#property indicator_buffers 1

#property indicator_color1 clrRed
#property indicator_width1 2

double ThreeUp[];

int OnInit()
{
  SetIndexBuffer(0, ThreeUp);
  SetIndexStyle(0, DRAW_ARROW, STYLE_SOLID);
  SetIndexArrow(0, SYMBOL_ARROWUP);
  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[])
{
  int limit = rates_total - prev_calculated;
  int n = 2;                       //array out of range対処
  if(limit > 1)                    //初期状態(インジケータ挿入直後)の場合のlimit定義
  {
     limit = rates_total - n - 1;
  }
  
  for(int i=limit; i>=0; i --)     
  {
     if(open[i+2] < close[i+2] &&    //i + "2" なので array out of range の対処として n=2 とする
        open[i+1] < close[i+1] &&
        open[ i ] < close[ i ] )
     {
        ThreeUp[i] = high[i] + 20*Point;
     }
  }     
  return(rates_total);
}

【実行結果】

現在足で3連続陽線が確定した場合には、矢印が残り続けることが確認できました。


この記事が気に入ったらサポートをしてみませんか?