#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);
}
|