//+------------------------------------------------------------------+ //| Demo_iBands.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 iBands 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 indicator_chart_window #property indicator_buffers 3 #property indicator_plots 3 //--- 上のプロット #property indicator_label1 "Upper" #property indicator_type1 DRAW_LINE #property indicator_color1 clrMediumSeaGreen #property indicator_style1 STYLE_SOLID #property indicator_width1 1 //--- 下のプロット #property indicator_label2 "Lower" #property indicator_type2 DRAW_LINE #property indicator_color2 clrMediumSeaGreen #property indicator_style2 STYLE_SOLID #property indicator_width2 1 //--- 真ん中のプロット #property indicator_label3 "Middle" #property indicator_type3 DRAW_LINE #property indicator_color3 clrMediumSeaGreen #property indicator_style3 STYLE_SOLID #property indicator_width3 1 //+------------------------------------------------------------------+ //| 作成を処理する方法の列挙 | //+------------------------------------------------------------------+ enum Creation { Call_iBands, // iBands を使用する Call_IndicatorCreate // IndicatorCreateを使用する }; //--- 入力パラメータ input Creation type=Call_iBands; // 関数の種類 input int bands_period=20; // 移動平均の期間 input int bands_shift=0; // シフト input double deviation=2.0; // 標準偏差の数 input ENUM_APPLIED_PRICE applied_price=PRICE_CLOSE; // 価格の種類 input string symbol=" "; // シンボル input ENUM_TIMEFRAMES period=PERIOD_CURRENT; // 時間軸 //--- 指標バッファ double UpperBuffer[]; double LowerBuffer[]; double MiddleBuffer[]; //--- iBands 指標ハンドルを格納する変数 int handle; //--- 格納に使用される変数 string name=symbol; //--- チャートでの指標名 string short_name; //--- ボリンジャーバンド指標に値の数を保存 int bars_calculated=0; //+------------------------------------------------------------------+ //| カスタム指標を初期化する関数 | //+------------------------------------------------------------------+ int OnInit() { //--- 配列の指標バッファへの割り当て SetIndexBuffer(0,UpperBuffer,INDICATOR_DATA); SetIndexBuffer(1,LowerBuffer,INDICATOR_DATA); SetIndexBuffer(2,MiddleBuffer,INDICATOR_DATA); //--- それぞれの線のシフトを設定 PlotIndexSetInteger(0,PLOT_SHIFT,bands_shift); PlotIndexSetInteger(1,PLOT_SHIFT,bands_shift); PlotIndexSetInteger(2,PLOT_SHIFT,bands_shift); //--- 指標が描画するシンボルを決める name=symbol; //--- 左右のスペースを削する StringTrimRight(name); StringTrimLeft(name); //---「name」文字列の長さがゼロになった場合 if(StringLen(name)==0) { //--- 指標が接続されているチャートのシンボルを取る name=_Symbol; } //--- 指標ハンドルを作成する if(type==Call_iBands) handle=iBands(name,period,bands_period,bands_shift,deviation,applied_price); else { //--- 構造体を指標のパラメータで記入 MqlParam pars[4]; //--- ma の期間 pars[0].type=TYPE_INT; pars[0].integer_value=bands_period; //--- シフト pars[1].type=TYPE_INT; pars[1].integer_value=bands_shift; //--- 標準偏差の数 pars[2].type=TYPE_DOUBLE; pars[2].double_value=deviation; //--- 価格の種類 pars[3].type=TYPE_INT; pars[3].integer_value=applied_price; handle=IndicatorCreate(name,period,IND_BANDS,4,pars); } //--- ハンドルが作成されなかった場合 if(handle==INVALID_HANDLE) { //--- 失敗した事実とエラーコードを出力する PrintFormat("Failed to create handle of the iBands indicator for the symbol %s/%s, error code %d", name, EnumToString(period), GetLastError()); //--- 指標が早期に中止された return(INIT_FAILED); } //--- ボリンジャーバンド指標が計算された銘柄/時間軸を表示 short_name=StringFormat("iBands(%s/%s, %d,%d,%G,%s)",name,EnumToString(period), bands_period,bands_shift,deviation,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[]) { //--- iBands 指標から複製された値の数 int values_to_copy; //--- 指標で計算された値の数を決める int calculated=BarsCalculated(handle); if(calculated<=0) { PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError()); return(0); } //--- これが指標計算の初めであるか iBands 指標の値の数が変更した changed //---または、2 つ以上のバーの指標の計算が必要である場合(価格履歴で何かが変更された) if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1) { //--- もし iBands 配列が銘柄/期間で iAC 指標の値の数より大きい場合、全体のコピーはしない //--- 他の場合、指標バッファサイズより少ない量をコピーをする if(calculated>rates_total) values_to_copy=rates_total; else values_to_copy=calculated; } else { //--- これは初回の計算ではなく、 //--- 前回の OnCalculate() から、一以上のバーが加えられてない。 values_to_copy=(rates_total-prev_calculated)+1; } //--- 配列をボリンジャーバンド指標の値で記入 //--- FillArraysFromBuffer が false を返した場合、情報の準備が終わっていないので操作を終了する if(!FillArraysFromBuffers(MiddleBuffer,UpperBuffer,LowerBuffer,bands_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); } //+------------------------------------------------------------------+ //| iBands 指標から指標バッファを記入する | //+------------------------------------------------------------------+ bool FillArraysFromBuffers(double &base_values[], // ボリンジャーバンドの中線の指標バッファ double &upper_values[], // 上縁の指標バッファ double &lower_values[], // 下縁の指標バッファ int shift, // シフト int ind_handle, // iBands 指標ハンドル int amount // 複製された値の数 ) { //--- エラーコードをリセットする ResetLastError(); //--- インデックス0 を持つ指標バッファの値で MiddleBuffer 配列の一部を記入する if(CopyBuffer(ind_handle,0,-shift,amount,base_values)<0) { //--- 複製が失敗したら、エラーコードを出す PrintFormat("Failed to copy data from the iBands indicator, error code %d",GetLastError()); //--- ゼロ結果で終了。 指標は計算されていないと見なされる return(false); } //--- インデックス 1 を持つ指標バッファの値で UpperBuffer 配列の一部を記入する if(CopyBuffer(ind_handle,1,-shift,amount,upper_values)<0) { //--- 複製が失敗したら、エラーコードを出す PrintFormat("Failed to copy data from the iBands indicator, error code %d",GetLastError()); //--- ゼロ結果で終了。 指標は計算されていないと見なされる return(false); } //--- インデックス 2 を持つ指標バッファの値で LowerBuffer 配列の一部を記入する if(CopyBuffer(ind_handle,2,-shift,amount,lower_values)<0) { //--- 複製が失敗したら、エラーコードを出す PrintFormat("Failed to copy data from the iBands indicator, error code %d",GetLastError()); //--- ゼロ結果で終了。 指標は計算されていないと見なされる return(false); } //--- 全てが成功 return(true); } //+------------------------------------------------------------------+ //| 指標初期化解除関数 | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { if(handle!=INVALID_HANDLE) IndicatorRelease(handle); //--- 指標の削除後チャートをクリアする Comment(""); } |