DRAW_LINE

DRAW_LINE は指標バッファの値によって指定された色の線を描画します。線の幅、スタイルと色はコンパイラディレクティブまたは 動的に PlotIndexSetInteger() 関数を使用して指定することが出来ます。プロットのプロパティの動的な変更は、指標の外観が現在の状況に応じて変わるようにして「盛り上げる」ことが出来ます。

DRAW_LINE のプロットに必要なバッファの数は 1 です。

バーの終値を使用して線を描画する指標の例です。線の色、幅とスタイルは N=5 ティックごとにランダムに変更されます。

DRAW_LINE の例

DRAW_LINE を持つ plot1では、3 つのプロパティは #property コンパイラディレクティブを使用して指定され、後に OnCalculate() 関数でランダムに設定されることにご注意ください。N パラメータは手動設定の可能性を持って(指標の「プロパティ」ウィンドウの「パラメータ」タブ)指標外部パラメータに設定されています。

//+------------------------------------------------------------------+
//|                                                    DRAW_LINE.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_LINE"
#property description "It draws a line of a specified color at Close prices"
#property description "Color, width and style of lines is changed randomly"
#property description "after every N ticks"
 
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1
//--- 線のプロパティはコンパイラディレクティブで設定される
#property indicator_label1 "Line"     // データウィンドウでのプロット名
#property indicator_type1   DRAW_LINE   // プロットの種類は「線」
#property indicator_color1  clrRed     // 線の色
#property indicator_style1 STYLE_SOLID // 線のスタイル
#property indicator_width1  1           // 線の幅
//--- 入力パラメータ
input int     N=5;         // 変化をもたらすティックの数
//--- プロットの指標バッファ
double         LineBuffer[];
//--- 色を格納する配列
color colors[]={clrRed,clrBlue,clrGreen};
//--- 線のスタイルを格納する配列
ENUM_LINE_STYLE styles[]={STYLE_SOLID,STYLE_DASH,STYLE_DOT,STYLE_DASHDOT,STYLE_DASHDOTDOT};
//+------------------------------------------------------------------+
//| カスタム指標を初期化する関数                                            |
//+------------------------------------------------------------------+
int OnInit()
 {
//--- 配列と指標バッファを関連付ける
  SetIndexBuffer(0,LineBuffer,INDICATOR_DATA);
//--- 擬似乱数生成器を初期化
  MathSrand(GetTickCount());
//---
  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;
    }
 
//--- 指標安倍を計算するブロック
  for(int i=0;i<rates_total;i++)
    {
     LineBuffer[i]=close[i];
    }
 
//--- 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+(string)colors[color_index];
 
//--- 線の幅を変更するブロック
  number=MathRand();
//--- 整数除算の余りの幅を取得
  int width=number%5; // 幅は 0〜4 に設定される
//--- 色を PLOT_LINE_WIDTH プロパティに設定
  PlotIndexSetInteger(0,PLOT_LINE_WIDTH,width);
//--- 線の幅を書く
  comm=comm+", Width="+IntegerToString(width);
 
//--- 線のスタイルを変更するブロック
  number=MathRand();
//--- 除数は、スタイルの配列の大きさに等しい
  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);
 }