//+------------------------------------------------------------------+
//| DRAW_COLOR_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_COLOR_BARS"
#property description "It draws different-color 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 5
#property indicator_plots 1
//--- ColorBars をプロットする
#property indicator_label1 "ColorBars"
#property indicator_type1 DRAW_COLOR_BARS
//--- バーを塗るために(特殊な配列に格納されている)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; // 種類を変更するティック数
input int bars=500; // 表示するバーの数
input bool messages=false; // エキスパートアドバイザー操作ログにメッセージを表示する
//--- 指標バッファ
double ColorBarsBuffer1[];
double ColorBarsBuffer2[];
double ColorBarsBuffer3[];
double ColorBarsBuffer4[];
double ColorBarsColors[];
//--- 銘柄名
string symbol;
int bars_colors;
//--- 色を格納する14 要素の配列
color colors[]=
{
clrRed,clrBlue,clrGreen,clrChocolate,clrMagenta,clrDodgerBlue,clrGoldenrod,
clrIndigo,clrLightBlue,clrAliceBlue,clrMoccasin,clrMagenta,clrCyan,clrMediumPurple
};
//+------------------------------------------------------------------+
//| カスタム指標を初期化する関数 |
//+------------------------------------------------------------------+
int OnInit()
{
//--- 指標バッファマッピング
SetIndexBuffer(0,ColorBarsBuffer1,INDICATOR_DATA);
SetIndexBuffer(1,ColorBarsBuffer2,INDICATOR_DATA);
SetIndexBuffer(2,ColorBarsBuffer3,INDICATOR_DATA);
SetIndexBuffer(3,ColorBarsBuffer4,INDICATOR_DATA);
SetIndexBuffer(4,ColorBarsColors,INDICATOR_COLOR_INDEX);
//---- バーを塗る色の数
bars_colors=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)
{
//--- 「気配値表示」 ウィンドウで新しいシンボルを選ぶ
symbol=GetRandomSymbolName();
//--- 線のプロパティを変更する
ChangeLineAppearance();
//--- ローソク足を塗る色を変更する
ChangeColors(colors,bars_colors);
int tries=0;
//--- シンボルからの価格でバッファを埋めることを五回試みる
while(!CopyFromSymbolToBuffers(symbol,rates_total,bars_colors) && tries<5)
{
//--- CopyFromSymbolToBuffers() 関数呼び出しのカウンタ
tries++;
}
//--- ティックカウンタをゼロにリセットする
ticks=0;
}
//--- 次の呼び出しのために prev_calculated の値を返す
return(rates_total);
}
//+------------------------------------------------------------------+
//| 指標バッファに価格を記入する |
//+------------------------------------------------------------------+
bool CopyFromSymbolToBuffers(string name,int total,int bar_colors)
{
//--- 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_COLOR_BARS("+name+")");
}
//--- バッファを空の値で初期化する
ArrayInitialize(ColorBarsBuffer1,0.0);
ArrayInitialize(ColorBarsBuffer2,0.0);
ArrayInitialize(ColorBarsBuffer3,0.0);
ArrayInitialize(ColorBarsBuffer4,0.0);
//--- 価格をバッファに複製する
for(int i=0;i<copied;i++)
{
//--- バッファの適切なインデックスを計算する
int buffer_index=total-copied+i;
//--- 価格をバッファに書く
ColorBarsBuffer1[buffer_index]=rates[i].open;
ColorBarsBuffer2[buffer_index]=rates[i].high;
ColorBarsBuffer3[buffer_index]=rates[i].low;
ColorBarsBuffer4[buffer_index]=rates[i].close;
//---
ColorBarsColors[buffer_index]=i%bar_colors;
}
return(true);
}
//+------------------------------------------------------------------+
//| 「気配値表示」 からランダムにシンボルを返す |
//+------------------------------------------------------------------+
string GetRandomSymbolName()
{
//--- 「気配値表示」 ウィンドウに表示されるシンボルの数
int symbols=SymbolsTotal(true);
//--- リスト内のシンボルの位置 - 0〜シンボル数の乱数
int number=MathRand()%symbols;
//--- 指定された位置のシンボルの名称を返す
return SymbolName(number,true);
}
//+------------------------------------------------------------------+
//| ジグザグセグメントの色を変更する |
//+------------------------------------------------------------------+
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("BarColorIndex[%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+"\r\nWidth="+IntegerToString(width);
//--- 銘柄名を書く
comm="\r\n"+symbol+comm;
//--- コメントを使用して、チャート上の情報を表示する
Comment(comm);
}
|