//+------------------------------------------------------------------+ //| Demo_iStdDev.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 "The indicator demonstrates how to obtain data" #property description "of indicator buffers for the iStdDev technical indicator." #property description "A symbol and timeframe used for calculation of the indicator," #property description "are set by the symbol and period parameters." #property description "The method of creation of the handle is set through the 'type' parameter (function type)." #property description "All the other parameters are similar to the normal Standard Deviation." #property indicator_separate_window #property indicator_buffers 1 #property indicator_plots 1 //--- iStdDev プロット #property indicator_label1 "iStdDev" #property indicator_type1 DRAW_LINE #property indicator_color1 clrMediumSeaGreen #property indicator_style1 STYLE_SOLID #property indicator_width1 1 //+------------------------------------------------------------------+ //| 作成を処理する方法の列挙 | //+------------------------------------------------------------------+ enum Creation { Call_iStdDev, // iStdDev を使用 Call_IndicatorCreate // IndicatorCreateを使用する }; //--- 入力パラメータ input Creation type=Call_iStdDev; // 関数の種類 input int ma_period=20; // 平均期間 input int ma_shift=0; // シフト input ENUM_MA_METHOD ma_method=MODE_SMA; // 平滑化の種類 input ENUM_APPLIED_PRICE applied_price=PRICE_CLOSE; // 価格の種類 input string symbol=" "; // シンボル input ENUM_TIMEFRAMES period=PERIOD_CURRENT; // 時間軸 //--- 指標バッファ double iStdDevBuffer[]; //--- iStdDev 指標ハンドルを格納する変数 int handle; //--- 格納に使用される変数 string name=symbol; //--- チャートでの指標名 string short_name; //--- 標準偏差指標に値の数を保存 int bars_calculated=0; //+------------------------------------------------------------------+ //| カスタム指標を初期化する関数 | //+------------------------------------------------------------------+ int OnInit() { //--- 配列の指標バッファへの割り当て SetIndexBuffer(0,iStdDevBuffer,INDICATOR_DATA); //--- シフトを設定 PlotIndexSetInteger(0,PLOT_SHIFT,ma_shift); //--- 指標が描画するシンボルを決める name=symbol; //--- 左右のスペースを削する StringTrimRight(name); StringTrimLeft(name); //---「name」文字列の長さがゼロになった場合 if(StringLen(name)==0) { //--- 指標が接続されているチャートのシンボルを取る name=_Symbol; } //--- 指標ハンドルを作成する if(type==Call_iStdDev) handle=iStdDev(name,period,ma_period,ma_shift,ma_method,applied_price); else { //--- 構造体を指標のパラメータで記入 MqlParam pars[4]; //--- 期間 pars[0].type=TYPE_INT; pars[0].integer_value=ma_period; //--- シフト pars[1].type=TYPE_INT; pars[1].integer_value=ma_shift; //--- 平滑化の種類 pars[2].type=TYPE_INT; pars[2].integer_value=ma_method; //--- 価格の種類 pars[3].type=TYPE_INT; pars[3].integer_value=applied_price; handle=IndicatorCreate(name,period,IND_STDDEV,4,pars); } //--- ハンドルが作成されなかった場合 if(handle==INVALID_HANDLE) { //--- 失敗した事実とエラーコードを出力する PrintFormat("Failed to create handle of the iStdDev indicator for the symbol %s/%s, error code %d", name, EnumToString(period), GetLastError()); //--- 指標が早期に中止された return(INIT_FAILED); } //--- 標準偏差指標が計算された銘柄/時間軸を表示 short_name=StringFormat("iStdDev(%s/%s, %d, %d, %s, %s)",name,EnumToString(period), ma_period,ma_shift,EnumToString(ma_method),EnumToString(applied_price)); IndicatorSetString(INDICATOR_SHORTNAME,short_name); //--- 通常の指標の初期化 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[]) { //--- iStdDev 指標から複製された値の数 int values_to_copy; //--- 指標で計算された値の数を決める int calculated=BarsCalculated(handle); if(calculated<=0) { PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError()); return(0); } //--- これが指標計算の初めであるか iStdDev 指標の値の数が変更した //---または、2 つ以上のバーの指標の計算が必要である場合(価格履歴で何かが変更された) if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1) { //--- iStdDevBuffer 配列が銘柄/期間で iStdDev 指標の値の数より大きい場合、全体のコピーはしない //--- 他の場合、指標バッファサイズより少ない量をコピーをする if(calculated>rates_total) values_to_copy=rates_total; else values_to_copy=calculated; } else { //--- これは初回の計算ではなく、 //--- 前回の OnCalculate() から、一以上のバーが加えられてない。 values_to_copy=(rates_total-prev_calculated)+1; } //--- 配列を標準偏差指標の値で記入 //--- FillArrayFromBuffer が false を返した場合、情報の準備が終わっていないので操作を終了する if(!FillArrayFromBuffer(iStdDevBuffer,ma_shift,handle,values_to_copy)) return(0); //--- メッセージを形成する string comm=StringFormat("%s ==> Updated value in the indicator %s: %d", TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS), short_name, values_to_copy); //--- チャートにサービスメッセージを表示する Comment(comm); //--- 標準偏差指標の値の数を覚える bars_calculated=calculated; //--- prev_calculated 値を次の関数呼び出しのために返す return(rates_total); } //+------------------------------------------------------------------+ //| iStdDev 指標から指標バッファを記入する | //+------------------------------------------------------------------+ bool FillArrayFromBuffer(double &std_buffer[], // 標準偏差線の指標バッファ int std_shift, // 標準偏差線のシフト int ind_handle, // iStdDev 指標ハンドル int amount // 複製された値の数 ) { //--- エラーコードをリセットする ResetLastError(); //--- インデックス0 を持つ指標バッファの値で iStdDevBuffer 配列の一部を記入する if(CopyBuffer(ind_handle,0,-std_shift,amount,std_buffer)<0) { //--- 複製が失敗したら、エラーコードを出す PrintFormat("Failed to copy data from the iStdDev indicator, error code %d",GetLastError()); //--- ゼロ結果で終了。 指標は計算されていないと見なされる return(false); } //--- 全てが成功 return(true); } //+------------------------------------------------------------------+ //| 指標初期化解除関数 | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { if(handle!=INVALID_HANDLE) IndicatorRelease(handle); //--- 指標の削除後チャートをクリアする Comment(""); } |