//+------------------------------------------------------------------+
//| DRAW_COLOR_ZIGZAG.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_COLOR_ZIGZAG"
#property description "It draws a broken line as a sequence of colored sections, the color depends on the number of the day of the week"
#property description "The color, width and style of segments are changed randomly"
#property description " every N ticks"
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_plots 1
//--- Color_Zigzag をプロットする
#property indicator_label1 "Color_Zigzag"
#property indicator_type1 DRAW_COLOR_ZIGZAG
//--- セクションを塗るために(特殊な配列に格納されている)8 色を定義する
#property indicator_color1 clrRed,clrBlue,clrGreen,clrYellow,clrMagenta,clrCyan,clrLime,clrOrange
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
//--- 入力パラメータ
input int N=5; // 変化をもたらすティックの数
int color_sections;
//--- セグメントの端の値のバッファ
double Color_ZigzagBuffer1[];
double Color_ZigzagBuffer2[];
//--- セグメントの端のカラーインデックを格納するバッファ
double Color_ZigzagColors[];
//--- 色を格納する14 要素の配列
color colors[]=
{
clrRed,clrBlue,clrGreen,clrChocolate,clrMagenta,clrDodgerBlue,clrGoldenrod,
clrIndigo,clrLightBlue,clrAliceBlue,clrMoccasin,clrWhiteSmoke,clrCyan,clrMediumPurple
};
//--- 線のスタイルを格納する配列
ENUM_LINE_STYLE styles[]={STYLE_SOLID,STYLE_DASH,STYLE_DOT,STYLE_DASHDOT,STYLE_DASHDOTDOT};
//+------------------------------------------------------------------+
//| カスタム指標を初期化する関数 |
//+------------------------------------------------------------------+
int OnInit()
{
//--- 指標バッファマッピング
SetIndexBuffer(0,Color_ZigzagBuffer1,INDICATOR_DATA);
SetIndexBuffer(1,Color_ZigzagBuffer2,INDICATOR_DATA);
SetIndexBuffer(2,Color_ZigzagColors,INDICATOR_COLOR_INDEX);
//----ジグザグを塗る色の数
color_sections=8; // #property indicator_color1 プロパティのコメントを参照
//---
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();
//--- セクションをプロットする色を変更する
ChangeColors(colors,color_sections);
//--- ティックカウンタをゼロにリセットする
ticks=0;
}
//--- 各足の曜日を取得するのに時間の構造体が必要
MqlDateTime dt;
//--- 計算開始位置
int start=0;
//--- 指標が以前に計算されている場合には、計算開始点を最後から2 つ目のバーに設定する
if(prev_calculated!=0) start=prev_calculated-1;
//--- 計算ループ
for(int i=start;i<rates_total;i++)
{
//--- バーのオープン時間を書く
TimeToStruct(time[i],dt);
//--- バー番号が偶数の場合
if(i%2==0)
{
//--- 1 番目のバッファの高値と2 番目のバッファの安値を書く
Color_ZigzagBuffer1[i]=high[i];
Color_ZigzagBuffer2[i]=low[i];
//--- セグメントの色
Color_ZigzagColors[i]=dt.day_of_year%color_sections;
}
//--- バー番号が奇数
else
{
//--- 逆の順序でバーに記入
Color_ZigzagBuffer1[i]=low[i];
Color_ZigzagBuffer2[i]=high[i];
//--- セグメントの色
Color_ZigzagColors[i]=dt.day_of_year%color_sections;
}
}
//--- 次の呼び出しのために prev_calculated の値を返す
return(rates_total);
}
//+------------------------------------------------------------------+
//| ジグザグセグメントの色を変更する |
//+------------------------------------------------------------------+
void ChangeColors(color &cols[],int plot_colors)
{
//--- 色の数
int size=ArraySize(cols);
//---
string comm=ChartGetString(0,CHART_COMMENT)+"\r\n\r\n";
//--- それぞれのカラーインデックに新しい色をランダムに定義する
for(int plot_color_ind=0;plot_color_ind<plot_colors;plot_color_ind++)
{
//--- 乱数を取得
int number=MathRand();
//--- 整数除算の余りから col[] 配列のインデックスを取得
int i=number%size;
//--- 各インデックスの色をPLOT_LINE_COLOR として設定する
PlotIndexSetInteger(0, // グラフィックスタイルの番号
PLOT_LINE_COLOR, // プロパティの識別子
plot_color_ind, // 色を書き込む色インデックス
cols[i]); // 新しい色
//--- 色を書く
comm=comm+StringFormat("ZigzagColorIndex[%d]=%s \r\n",plot_color_ind,ColorToString(cols[i],true));
ChartSetString(0,CHART_COMMENT,comm);
}
//---
}
//+------------------------------------------------------------------+
//| ジグザグセグメントの外観を変更する |
//+------------------------------------------------------------------+
void ChangeLineAppearance()
{
//--- Color_ZigZag のプロパティに関する情報を形成するための文字列
string comm="";
//--- 線の幅を変更するブロック
int number=MathRand();
//--- 整数除算の余りの幅を取得
int width=number%5; // 幅は 0〜4 に設定される
//--- 色を PLOT_LINE_WIDTH プロパティに設定
PlotIndexSetInteger(0,PLOT_LINE_WIDTH,width);
//--- 線の幅を書く
comm=comm+"\r\nWidth="+IntegerToString(width);
//--- 線のスタイルを変更するブロック
number=MathRand();
//--- 除数は、スタイルの配列の大きさに等しい
int size=ArraySize(styles);
//--- 新しいスタイルを選択するためのインデックスを整数除算の余りから取得
int style_index=number%size;
//--- 色をPLOT_LINE_COLOR プロパティとして設定
PlotIndexSetInteger(0,PLOT_LINE_STYLE,styles[style_index]);
//--- 線のスタイルを書く
comm="\r\n"+EnumToString(styles[style_index])+""+comm;
//--- コメントを使用して、チャート上の情報を表示する
Comment(comm);
}
|