//+------------------------------------------------------------------+
//| DRAW_ARROW.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_ARROW"
#property description "Draws arrows set by Unicode characters, on a chart"
#property description "The color, size, shift and symbol code of the arrow are changed in a random way"
#property description "after every N ticks"
#property description "The code parameter sets the base value: code=159 (a circle)"
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots 1
//--- 矢印をプロットする
#property indicator_label1 "Arrows"
#property indicator_type1 DRAW_ARROW
#property indicator_color1 clrGreen
#property indicator_width1 1
//--- 入力パラメータ
input int N=5; // 変化をもたらすティックの数
input ushort code=159; // DRAW_ARROW で描画するシンボルコード
//--- プロットの指標バッファ
double ArrowsBuffer[];
//--- 色を格納する配列
color colors[]={clrRed,clrBlue,clrGreen};
//+------------------------------------------------------------------+
//| カスタム指標を初期化する関数 |
//+------------------------------------------------------------------+
int OnInit()
{
//--- 指標バッファマッピング
SetIndexBuffer(0,ArrowsBuffer,INDICATOR_DATA);
//--- PLOT_ARROW の描画のためにシンボルコードを定義する
PlotIndexSetInteger(0,PLOT_ARROW,code);
//--- 矢印の垂直シフトをピクセル単位で設定する
PlotIndexSetInteger(0,PLOT_ARROW_SHIFT,5);
//--- 空の値の 0 を設定
PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0);
//---
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=1;
if(prev_calculated>0) start=prev_calculated-1;
//--- 計算ループ
for(int i=1;i<rates_total;i++)
{
//--- 現在の終値が以前の終値より高い場合、矢印を描画する
if(close[i]>close[i-1])
ArrowsBuffer[i]=close[i];
//--- 他の場合、ゼロを指定する
else
ArrowsBuffer[i]=0;
}
//--- 次の呼び出しのために 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 に設定
//--- 色を PLOT_LINE_WIDTH プロパティに設定
PlotIndexSetInteger(0,PLOT_LINE_WIDTH,width);
//--- 矢印のサイズを書く
comm=comm+"\r\nWidth="+IntegerToString(width);
//--- 矢印のコードを変更するブロック(PLOT_ARROW)
number=MathRand();
//--- 矢印の新しいコードを計算するために、整数の除算の剰余を取得(0〜19)
int code_add=number%20;
//--- 新しいシンボルコードをcode+code_add に設定
PlotIndexSetInteger(0,PLOT_ARROW,code+code_add);
//--- PLOT_ARROW シンボルコードを書く
comm="\r\n"+"PLOT_ARROW="+IntegerToString(code+code_add)+comm;
//--- 矢印の垂直シフトをピクセル単位で変更するブロック
number=MathRand();
//--- 整数除算の余りとしてシフトを取得
int shift=20-number%41;
//--- 新しいシフトを-20〜20で取得
PlotIndexSetInteger(0,PLOT_ARROW_SHIFT,shift);
//--- PLOT_ARROW_SHIFT シフトを書く
comm="\r\n"+"PLOT_ARROW_SHIFT="+IntegerToString(shift)+comm;
//--- コメントを使用して、チャート上の情報を表示する
Comment(comm);
}
|