DRAW_HISTOGRAM

DRAW_HISTOGRAM スタイルはゼロから指定された値に列の順序として、指定された色のヒストグラムを描画します。値は指標バッファから取られます。列の幅、色とスタイルは DRAW_LINE スタイルのようにコンパイラディレクティブまたは 動的に PlotIndexSetInteger() 関数を使用して指定することが出来ます。プロットプロパティの動的な変更は、現在の状況に基づいてのヒストグラムの外観の変更を可能にします。

ゼロレベルからの列が各足の上に描画されているので、DRAW_HISTOGRAM は、別のチャートウィンドウで使用される必要があります。ほとんどの場合、この種類のプロットは、Bears Power または OsMA のようなオシレーター型の指標を作成するために使用されます。空の表示不可能な値には、ゼロ値を指定する必要があります。

DRAW_HISTOGRAM のプロットに必要なバッファの数は 1 です。

MathSin() 関数に基づいて指定された色の正弦波を描く指標の例です。ヒストグラムの列の色、幅とスタイルは全て N ティックごとにランダムに変更されます。bars パラメータは正弦波の期間を指定し、バーの指定された数の後で、正弦波のサイクルが繰り返すことになります。

DRAW_HISTOGRAM スタイルの例

DRAW_HISTOGRAM を持つplot1 では、3 つのプロパティは #propertyコンパイラディレクティブを使用して指定され、後にOnCalculate() 関数でランダムに設定されることにご注意ください。N パラメータは手動設定の可能性を持って(指標の「プロパティ」ウィンドウの「パラメータ」タブ)指標外部パラメータに設定されています。

//+------------------------------------------------------------------+
//|                                               DRAW_HISTOGRAM.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_HISTOGRAM"
#property description "It draws a sinusoid as a histogram in a separate window"
#property description "The color and width of columns are changed randomly"
#property description "after every N ticks"
#property description "The bars parameter sets the number of bars in the cycle of the sinusoid"
 
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots   1
//--- ヒストグラムをプロットする
#property indicator_label1 "Histogram"
#property indicator_type1   DRAW_HISTOGRAM
#property indicator_color1 clrBlue
#property indicator_style1 STYLE_SOLID
#property indicator_width1  1
//--- 入力パラメータ
input int     bars=30;         // バーでの正弦波の期間
input int     N=5;             // ヒストグラムを変更するティック数
//--- 指標バッファ
double         HistogramBuffer[];
//--- bars パラメータを掛けた時ラジアンで2PI角を取得する要因
double    multiplier;
//--- 色を格納する配列
color colors[]={clrRed,clrBlue,clrGreen};
//--- 線のスタイルを格納する配列
ENUM_LINE_STYLE styles[]={STYLE_SOLID,STYLE_DASH,STYLE_DOT,STYLE_DASHDOT,STYLE_DASHDOTDOT};
//+------------------------------------------------------------------+
//| カスタム指標を初期化する関数                                            |
//+------------------------------------------------------------------+
int OnInit()
 {
//--- 指標バッファマッピング
  SetIndexBuffer(0,HistogramBuffer,INDICATOR_DATA);
//--- 乗数を計算する
  if(bars>1)multiplier=2.*M_PI/bars;
  else
    {
    PrintFormat("Set the value of bars=%d greater than 1",bars);
    //--- 指標の早期終了
    return(INIT_PARAMETERS_INCORRECT);
    }
//---
  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)
    {
    //--- 線のプロパティを変更する
     ChangeLineAppearance();
    //--- ティックカウンタをゼロにリセットする
     ticks=0;
    }
 
//--- 指標値を計算する
  int start=0;
//--- すでにOnCalculate の前回の開始時に算出した場合
  if(prev_calculated>0) start=prev_calculated-1; // 最後から2 つ目のバーから計算する
//--- 指標バッファに値を記入する
  for(int i=start;i<rates_total;i++)
    {
     HistogramBuffer[i]=sin(i*multiplier);
    }
//--- prev_calculated 値を次の関数呼び出しのために返す
  return(rates_total);
 }
//+------------------------------------------------------------------+
//| 指標での線の外観を変更する                                             |
//+------------------------------------------------------------------+
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 に設定される
//--- 幅を設定
  PlotIndexSetInteger(0,PLOT_LINE_WIDTH,width);
//--- 線の幅を書く
  comm=comm+"\r\nWidth="+IntegerToString(width);
 
//--- 線のスタイルを変更するブロック
  number=MathRand();
//--- 除数は、スタイルの配列の大きさに等しい
  size=ArraySize(styles);
//--- 新しいスタイルを選択するためのインデックスを整数除算の余りから取得
  int style_index=number%size;
//--- 線のスタイルを設定
  PlotIndexSetInteger(0,PLOT_LINE_STYLE,styles[style_index]);
//--- 線のスタイルを書く
  comm="\r\n"+EnumToString(styles[style_index])+""+comm;
//--- コメントを使用して、チャート上の情報を表示する
  Comment(comm);
 }