DRAW_BARS

DRAW_BARS スタイルは、始値、高値、安値、終値の値を含む 4 つの指標バッファからバーを描画します。これは、チャートの別々のサブウィンドウ内及びその他の金融商品のカスタム指標をバーとして作成するために使用されます。

バーの色はコンパイラディレクティブまたは動的に PlotIndexSetInteger() 関数で設定出来ます。プロットのプロパティの動的な変更は、指標の外観が現在の状況に応じて変わるようにして「盛り上げる」ことが出来ます。

指標は 4 つの指標バッファ 全部に空以外の値があるバーにのみ描画されます。「空」値を指定するには PLOT_EMPTY_VALUE プロパティで値を設定します。

//--- 0(空)値は描画されない
  PlotIndexSetDouble(index_of_plot_DRAW_BARS,PLOT_EMPTY_VALUE,0);

バッファの値は常に明示的に入力し、バーをスキップするにはバッファに空の値を設定します。

DRAW_BARS のプロットに必要なバッファの数は 4 です。プロットバッファは、始値、高値、安値、終値の順番であるべきです。いずれのバッファも空の値のみを含むことは出来ません。この場合プロットがなされません。

別のウィンドウで選択された金融商品のバーを描画する指標の例です。バーの色は N ティックごとにランダムに変わります。パラメータは手動設定の可能性を持って(指標の「プロパティ」ウィンドウの「パラメータ」タブ)指標外部パラメータに設定されています。

DRAW_BARS スタイルの例

DRAW_BARS スタイルを持つ plot1 では、色が #propertyコンパイラディレクティブによって指定された後、OnCalculate() 関数で事前に準備されたリストからランダムに設定されます。

//+------------------------------------------------------------------+
//|                                                    DRAW_BARS.mq5 |
//|                        Copyright 2011, MetaQuotes Software Corp. |
//|                                             https://www.MQL5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2000-2024, MetaQuotes Ltd."
#property link     "https://www.mql5.com"
#property version   "1.00"
 
#property description "An indicator to demonstrate DRAW_BARS"
#property description "It draws bars of a selected symbol in a separate window"
#property description "The color and width of bars, as well as the symbol are changed randomly"
#property description "every N ticks"
 
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_plots   1
//--- バーをプロットする
#property indicator_label1 "Bars"
#property indicator_type1   DRAW_BARS
#property indicator_color1 clrGreen
#property indicator_style1 STYLE_SOLID
#property indicator_width1  1
//--- 入力パラメータ
input int     N=5;             // 種類を変更するティック数
input int     bars=500;         // 表示するバーの数
input bool     messages=false;   // エキスパートアドバイザーログにメッセージを表示する
//--- 指標バッファ
double         BarsBuffer1[];
double         BarsBuffer2[];
double         BarsBuffer3[];
double         BarsBuffer4[];
//--- 銘柄名
string symbol;
//--- 色を格納する配列
color colors[]={clrRed,clrBlue,clrGreen,clrPurple,clrBrown,clrIndianRed};
//+------------------------------------------------------------------+
//| カスタム指標を初期化する関数                                            |
//+------------------------------------------------------------------+
int OnInit()
 {
//--- バーが小さい場合は、前もって完成する
  if(bars<50)
    {
    Comment("Please specify a larger number of bars! The operation of the indicator has been terminated");
    return(INIT_PARAMETERS_INCORRECT);
    }
//--- 指標バッファマッピング
  SetIndexBuffer(0,BarsBuffer1,INDICATOR_DATA);
  SetIndexBuffer(1,BarsBuffer2,INDICATOR_DATA);
  SetIndexBuffer(2,BarsBuffer3,INDICATOR_DATA);
  SetIndexBuffer(3,BarsBuffer4,INDICATOR_DATA);
//--- バーが描画される銘柄名
  symbol=_Symbol;
//--- シンボルの表示を設定
  PlotIndexSetString(0,PLOT_LABEL,symbol+" Open;"+symbol+" High;"+symbol+" Low;"+symbol+" Close");
  IndicatorSetString(INDICATOR_SHORTNAME,"DRAW_BARS("+symbol+")");
//--- 空の値
  PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);
//---
  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[])
 {
  static int ticks=0;
//--- 線のスタイル、色、及び幅を変更するティックを計算する
  ticks++;
//--- 充分なティックの数が蓄積されている場合
  if(ticks>=N)
    {
    //--- 「気配値表示」 ウィンドウで新しいシンボルを選ぶ
    symbol=GetRandomSymbolName();
    //--- 線のプロパティを変更する
     ChangeLineAppearance();
 
    int tries=0;
    //--- シンボルからの価格でバッファを埋めることを五回試みる
    while(!CopyFromSymbolToBuffers(symbol,rates_total) && tries<5)
       {
        //--- CopyFromSymbolToBuffers() 関数呼び出しのカウンタ
        tries++;
       }
    //--- ティックカウンタをゼロにリセットする
     ticks=0;
    }
//--- 次の呼び出しのために prev_calculated の値を返す
  return(rates_total);
 }
//+------------------------------------------------------------------+
//| 指標バッファに価格を記入する                        |
//+------------------------------------------------------------------+
bool CopyFromSymbolToBuffers(string name,int total)
 {
//--- rates[] 配列で、始値、高値、安値、終値を複製する
  MqlRates rates[];
//--- 試みのカウンタ
  int attempts=0;
//--- 複製された量
  int copied=0;
//--- 希望する記号の時系列を取得するための25の試みをする
  while(attempts<25 && (copied=CopyRates(name,_Period,0,bars,rates))<0)
    {
    Sleep(100);
     attempts++;
    if(messages) PrintFormat("%s CopyRates(%s) attempts=%d",__FUNCTION__,name,attempts);
    }
//--- 充分な数のバーの複製が失敗した場合
  if(copied!=bars)
    {
    //--- メッセージ文字列を作る
    string comm=StringFormat("For the symbol %s, managed to receive only %d bars of %d requested ones",
                              name,
                              copied,
                              bars
                              );
    //--- メインチャートウィンドウでコメントを表示する
    Comment(comm);
    //--- メッセージを表示する
    if(messages) Print(comm);
    return(false);
    }
  else
    {
    //--- シンボルの表示を設定する
    PlotIndexSetString(0,PLOT_LABEL,name+" Open;"+name+" High;"+name+" Low;"+name+" Close");
    IndicatorSetString(INDICATOR_SHORTNAME,"DRAW_BARS("+name+")");
    }
//--- バッファを空の値で初期化する
  ArrayInitialize(BarsBuffer1,0.0);  
  ArrayInitialize(BarsBuffer2,0.0);  
  ArrayInitialize(BarsBuffer3,0.0);  
  ArrayInitialize(BarsBuffer4,0.0);  
//--- 価格をバッファに複製する
  for(int i=0;i<copied;i++)
    {
    //--- バッファの適切なインデックスを計算する
    int buffer_index=total-copied+i;
    //--- 価格をバッファに書く
     BarsBuffer1[buffer_index]=rates[i].open;
     BarsBuffer2[buffer_index]=rates[i].high;
     BarsBuffer3[buffer_index]=rates[i].low;
     BarsBuffer4[buffer_index]=rates[i].close;
    }
  return(true);
 }
//+------------------------------------------------------------------+
//| 「気配値表示」 からランダムにシンボルを返す                                  |
//+------------------------------------------------------------------+
string GetRandomSymbolName()
 {
//--- 「気配値表示」 ウィンドウに表示されるシンボルの数
  int symbols=SymbolsTotal(true);
//--- リスト内のシンボルの位置 - 0〜シンボル数の乱数
  int number=MathRand()%symbols;
//--- 指定された位置のシンボルの名称を返す
  return SymbolName(number,true);
 }
//+------------------------------------------------------------------+
//| バーの外観を変更する                                                  |
//+------------------------------------------------------------------+
void ChangeLineAppearance()
 {
//--- バーのプロパティに関する情報を形成するための文字列
  string comm="";
//--- バーの色を変更するブロック
  int number=MathRand(); // 乱数を取得
//--- 除数は colors[] 配列のサイズと同じ
  int size=ArraySize(colors);
//--- 新しい色を選択するためのインデックスを整数除算の余りから取得
  int color_index=number%size;
//--- 色をPLOT_LINE_COLOR プロパティとして設定
  PlotIndexSetInteger(0,PLOT_LINE_COLOR,colors[color_index]);
//--- 線の色を書く
  comm=comm+"\r\n"+(string)colors[color_index];
 
//--- バーの幅を変更するブロック
  number=MathRand();
//--- 整数除算の余りの幅を取得
  int width=number%5;   // 幅は 0〜4 に設定される
//--- 色を PLOT_LINE_WIDTH プロパティに設定
  PlotIndexSetInteger(0,PLOT_LINE_WIDTH,width);
//--- 線の幅を書く
  comm=comm+"\r\nWidth="+IntegerToString(width);
 
//--- 銘柄名を書く
  comm="\r\n"+symbol+comm;
 
//--- コメントを使用して、チャート上の情報を表示する
  Comment(comm);
 }