ChartIndicatorAdd

指定されたチャートウィンドウに指定されたハンドルと指標を追加します。指標とチャートは同一のシンボルと時間軸に生成されるべきです。

bool  ChartIndicatorAdd(
  long  chart_id,                // チャート識別子
  int  sub_window               // サブウィンドウ番号
  int  indicator_handle         // 指標ハンドル
  );

パラメータ

chart_id

[in]  チャート識別子。( 0 は現在のチャート)

sub_window

[in]  チャートサブウィンドウの番号( 0 はメインチャートウィンドウ)新しいウィンドウで指標を追加するには、パラメータは、CHART_WINDOWS_TOTALに等しく、最後の既存のウィンドウの指標より 1 大きくなければなりません。パラメータの値が CHART_WINDOWS_TOTALよりも大きい場合、新しいウィンドウが作成されず、表示が追加されません。

indicator_handle

[in]  指標ハンドル

戻り値

成功の場合は true、それ以外の場合は false。エラー情報を取得するには、GetLastError() 関数が呼ばれます。エラー 4114 は、チャートとコメントを追加した指標の記号または時間軸が異なることを意味します。

注意事項

サブウィンドウに描画されるべき指標がメインチャートウィンドウに適応されると、指標はリスト内には存在しても表示されないことがあります (例えば、内蔵された iMACD または #property indicator_separate_window プロパティの指定されたカスタム指標) これは、指標の規模は価格チャートの規模とは異なり、適用された指標の値は、価格チャートの表示範囲に収まらないことを意味します。この場合、GetLastError() はエラーが存在しないことを示すゼロコードを返します。このような「見えない」指標の値は、データウィンドウに表示し、他のMQL5アプリケーションから受け取ることが出来ます。

例:

#property description "Expert Advisor demonstrating the work with ChartIndicatorAdd() function."
#property description "After launching on the chart (and receiving the error in Journal), open"
#property description "the Expert Advisor's properties and specify correct <symbol> and <period> parameters."
#property description "MACD indicator will be added on the chart."
 
//--- 入力パラメータ
input string         symbol="AUDUSD";   // 銘柄名
input ENUM_TIMEFRAMES period=PERIOD_M12; // 時間軸
input int   fast_ema_period=12;         // MACDが高速期間
input int   slow_ema_period=26;         // MACDが低速期間
input int     signal_period=9;           // シグナル期間
input ENUM_APPLIED_PRICE apr=PRICE_CLOSE; // MACD計算に使用する値の型
 
int indicator_handle=INVALID_HANDLE;
//+------------------------------------------------------------------+
//| エキスパート初期化に使用される関数                                        |
//+------------------------------------------------------------------+
int OnInit()
 {
//---
  indicator_handle=iMACD(symbol,period,fast_ema_period,slow_ema_period,signal_period,apr);
//--- チャートに指標を追加しようとする
  if(!AddIndicator())
    {
    //--- AddIndicator() 関数が、チャートに指標を追加することを拒否した
    int answer=MessageBox("Do you want to add MACD on the chart anyway?",
                          "Incorrect symbol and/or time frame for adding the indicator",
                          MB_YESNO // 「はい(Y)」 と 「いいえ(N)」 ボタンが表示される
                           );
    //--- ユーザが ChartIndicatorAdd() の不正な使用方法を主張する場合
    if(answer==IDYES)
       {
        //--- まず、操作ログにこのことを記載する
        PrintFormat("Attention! %s: Trying to add MACD(%s/%s) indicator on %s/%s chart. Receiving error 4114",
                    __FUNCTION__,symbol,EnumToString(period),_Symbol,EnumToString(_Period));
        //--- 新たなサブウィンドウの数を受け取り指標を追加しようとする
        int subwindow=(int)ChartGetInteger(0,CHART_WINDOWS_TOTAL);
        //--- 試んでエラーを発生する
        if(!ChartIndicatorAdd(0,subwindow,indicator_handle))
          PrintFormat("Failed to add MACD indicator on %d chart window. Error code  %d",
                       subwindow,GetLastError());
       }
    }
//---
  return(INIT_SUCCEEDED);
 }
//+------------------------------------------------------------------+
//| エキスパートティック関数                                                 |
//+------------------------------------------------------------------+
void OnTick()
 {
// エキスパートアドバイザーは何もしない
 }
//+------------------------------------------------------------------+
//| チェックしてチャートに指標を追加する関数                                     |
//+------------------------------------------------------------------+
bool AddIndicator()
 {
//--- 表示メッセージ
  string message;
//--- 指標シンボルとチャートシンボルが一致するかどうかを確認する
  if(symbol!=_Symbol)
    {
     message="Displaying the use of Demo_ChartIndicatorAdd() function:";
     message=message+"\r\n";
     message=message+"Unable to add the indicator calculated on another symbol on the chart.";
     message=message+"\r\n";
     message=message+"Specify the chart symbol in Expert Advisor's property - "+_Symbol+".";
    Alert(message);
    //--- 早期の終了。指標はチャート上に追加されない
    return false;
    }
//---指標とチャートの時間軸が一致しているかどうかを確認する
  if(period!=_Period)
    {
     message="Unable to add the indicator calculated on another time frame on the chart.";
     message=message+"\r\n";
     message=message+"Specify the chart time frame in Expert Advisor properties - "+EnumToString(_Period)+".";
    Alert(message);
    //--- 早期の終了。指標はチャート上に追加されない
    return false;
    }
//--- 全てのチェックが完了し、シンボルと指標時間軸はチャートに一致する
  if(indicator_handle==INVALID_HANDLE)
    {
    Print(__FUNCTION__,"  Creating MACD indicator");
     indicator_handle=iMACD(symbol,period,fast_ema_period,slow_ema_period,signal_period,apr);
    if(indicator_handle==INVALID_HANDLE)
       {
        Print("Failed to create MACD indicator. Error code ",GetLastError());
       }
    }
//--- エラーコードをリセットする
  ResetLastError();
//--- 指標をチャートに適用する
  Print(__FUNCTION__,"  Adding MACD indicator on the chart");
  Print("MACD is generated on ",symbol,"/",EnumToString(period));
//--- MACD指標が付加された新たなサブウィンドウの番号を受け取る
  int subwindow=(int)ChartGetInteger(0,CHART_WINDOWS_TOTAL);
  PrintFormat("Adding MACD indicator on %d chart window",subwindow);
  if(!ChartIndicatorAdd(0,subwindow,indicator_handle))
    {
    PrintFormat("Failed to add MACD indicator on %d chart window. Error code  %d",
                 subwindow,GetLastError());
    }
//--- 指標が正常に追加された
  return(true);
 }

参照

ChartIndicatorDelete(), ChartIndicatorName(), ChartIndicatorsTotal(), iCustom(), IndicatorCreate()