//+------------------------------------------------------------------+
//| DRAW_COLOR_SECTION.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_SECTION"
#property description "It draws colored sections with the length equal to the specified number of bars"
#property description "The color, width and style of sections are changed randomly"
#property description "after every N ticks"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots 1
//--- ColorSection をプロットする
#property indicator_label1 "ColorSection"
#property indicator_type1 DRAW_COLOR_SECTION
//--- セクションを塗るために(特殊な配列に格納されている)8 色を定義する
#property indicator_color1 clrRed,clrGold,clrMediumBlue,clrLime,clrMagenta,clrBrown,clrTan,clrMediumVioletRed
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
//--- 入力パラメータ
input int N=5; // 変化をもたらすティックの数
input int bars_in_section=5; // バーのセクションの長さ
//--- セクションの端を計算する補助変数
int divider;
int color_sections;
//--- プロットに使用されるバッファ
double ColorSectionBuffer[];
//--- バーの色を格納するバッファ
double ColorSectionColors[];
//--- 色を格納する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,ColorSectionBuffer,INDICATOR_DATA);
SetIndexBuffer(1,ColorSectionColors,INDICATOR_COLOR_INDEX);
//--- 0(空)値は描画されない
PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0);
//---- セクションを塗る色の数
int color_sections=8; // #property indicator_color1コメントを参照
//--- 指標パラメータをチェックする
if(bars_in_section<=0)
{
PrintFormat("Invalid section length=%d",bars_in_section);
return(INIT_PARAMETERS_INCORRECT);
}
else divider=color_sections*bars_in_section;
//---
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;
}
//--- 指標値の算出が開始されるバーの番号
int start=0;
//--- 指標が以前に計算されている場合には、開始点を前のバーに設定する
if(prev_calculated>0) start=prev_calculated-1;
//--- 指標値の計算
for(int i=start;i<rates_total;i++)
{
//--- section_lengthで割り切れるバー番号は セクションの終わりを意味する
if(i%bars_in_section==0)
{
//--- この足の高値でセクションの終わりを設定する
ColorSectionBuffer[i]=high[i];
//--- バーの数を scetion_length*number_of_colors で割った際の余り
int rest=i%divider;
// 0〜number_of_colors-1 である色の番号を取得する
int color_indext=rest/bars_in_section;
ColorSectionColors[i]=color_indext;
}
//---除算の剰余は、バーに等しい場合
else
{
//--- 何もない場合、バーを無視して 0 に設定
else ColorSectionBuffer[i]=0;
}
}
//--- 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("SectionColorIndex[%d]=%s \r\n",plot_color_ind,ColorToString(cols[i],true));
ChartSetString(0,CHART_COMMENT,comm);
}
//---
}
//+------------------------------------------------------------------+
//| 指標に表示された線の外観を変更する |
//+------------------------------------------------------------------+
void ChangeLineAppearance()
{
//--- 線のプロパティに関する情報を形成するための文字列
string comm="";
//--- 線の幅を変更するブロック
int number=MathRand();
//--- 整数除算の余りの幅を取得
int width=number%5; // 幅は 0〜4 に設定される
//--- 色を PLOT_LINE_WIDTH プロパティに設定
PlotIndexSetInteger(0,PLOT_LINE_WIDTH,width);
//--- 線の幅を書く
comm=comm+" Width="+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=EnumToString(styles[style_index])+", "+comm;
//--- コメントを使用して、チャート上の情報を表示する
Comment(comm);
}
|