//+------------------------------------------------------------------+
//| DRAW_HISTOGRAM.mq5 |
//| Copyright 2011, MetaQuotes Software Corp. |
//| https://www.MQL5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2011, MetaQuotes Software Corp."
#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);
}
|