iWPR

この関数は Larry Williams' Percent Range(ラリーウィリアムパーセントレンジ)指標のハンドルを返します。バッファは 1 つです。

int  iWPR(
  string          symbol,          // 銘柄名
  ENUM_TIMEFRAMES  period,          // 期間
  int              calc_period      // 平均期間
  );

パラメータ

symbol

[in] データが指標計算に使用される有価証券の銘柄名(NULL は現在のシンボル)

period

[in] 期間の値は ENUM_TIMEFRAMES 列挙の値の 1 つで、0 は現在の時間軸の意味です。

calc_period

[in]  指標計算の平均期間(バーの数)

戻り値

指定されたテクニカル指標ハンドル。失敗の場合 INVALID_HANDLEIndicatorRelease() 関数に指標ハンドルを渡すことによって 、コンピュータメモリを利用されていない指標から解放することが出来ます。

例:

//+------------------------------------------------------------------+
//|                                                    Demo_iWPR.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 iWPR 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_separate_window
#property indicator_buffers 1
#property indicator_plots   1
//--- iWPR プロット
#property indicator_label1 "iWPR"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrCyan
#property indicator_style1 STYLE_SOLID
#property indicator_width1  1
//--- 指標値を制限する
#property indicator_minimum -100
#property indicator_maximum 0
//--- 指標ウィンドウの水平レベル
#property indicator_level1  -20.0
#property indicator_level2  -80.0
//+------------------------------------------------------------------+
//| 作成を処理する方法の列挙                                              |
//+------------------------------------------------------------------+
enum Creation
 {
  Call_iWPR,             // iWPRを使用する
  Call_IndicatorCreate   // IndicatorCreateを使用する
 };
//--- 入力パラメータ
input Creation             type=Call_iWPR;           // 関数の種類
input int                  calc_period=14;           // 期間
input string               symbol=" ";               // シンボル
input ENUM_TIMEFRAMES     period=PERIOD_CURRENT;     // 時間軸
//--- 指標バッファ
double         iWPRBuffer[];
//--- iWPR 指標ハンドルを格納する変数
int    handle;
//--- 格納に使用される変数
string name=symbol;
//--- チャートでの指標名
string short_name;
//--- ラリーウィリアムパーセントレンジ 指標に値の数を保存
int    bars_calculated=0;
//+------------------------------------------------------------------+
//| カスタム指標を初期化する関数                                            |
//+------------------------------------------------------------------+
int OnInit()
 {
//--- 配列の指標バッファへの割り当て
  SetIndexBuffer(0,iWPRBuffer,INDICATOR_DATA);
//--- 指標が描画するシンボルを決める
  name=symbol;
//--- 左右のスペースを削する
  StringTrimRight(name);
  StringTrimLeft(name);
//---「name」文字列の長さがゼロになった場合
  if(StringLen(name)==0)
    {
    //--- 指標が接続されているチャートのシンボルを取る
     name=_Symbol;
    }
//--- 指標ハンドルを作成する
  if(type==Call_iWPR)
     handle=iWPR(name,period,calc_period);
  else
    {
    //--- 構造体を指標のパラメータで記入    
     MqlParam pars[1];
    //--- 期間
     pars[0].type=TYPE_INT;
     pars[0].integer_value=calc_period;
     handle=IndicatorCreate(name,period,IND_WPR,1,pars);
    }
//--- ハンドルが作成されなかった場合
  if(handle==INVALID_HANDLE)
    {
    //--- 失敗した事実とエラーコードを出力する
    PrintFormat("Failed to create handle of the iWPR indicator for the symbol %s/%s, error code %d",
                 name,
                EnumToString(period),
                GetLastError());
    //--- 指標が早期に中止された
    return(INIT_FAILED);
    }
//--- ウィリアムパーセントレンジ 指標が計算された銘柄/時間軸を表示
  short_name=StringFormat("iWPR(%s/%s, %d)",name,EnumToString(period),calc_period);
  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[])
 {
//--- iWPR 指標から複製された値の数
  int values_to_copy;
//--- 指標で計算された値の数を決める
  int calculated=BarsCalculated(handle);
  if(calculated<=0)
    {
    PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError());
    return(0);
    }
//--- これが指標計算の初めであるか iWPR 指標の値の数が変更した
//---または、2 つ以上のバーの指標の計算が必要である場合(価格履歴で何かが変更された)
  if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1)
    {
    //--- iiWPRBuffer 配列が銘柄/期間で iWPR 指標の値の数より大きい場合、全体のコピーはしない
    //--- 他の場合、指標バッファサイズより少ない量をコピーをする
    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(iWPRBuffer,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);
 }
//+------------------------------------------------------------------+
//| iWPR 指標から指標バッファを記入する                                       |
//+------------------------------------------------------------------+
bool FillArrayFromBuffer(double &wpr_buffer[], // ウィリアムパーセントレンジ 値の指標バッファ
                        int ind_handle,       // iWPR 指標ハンドルr
                        int amount             // 複製された値の数
                        )
 {
//--- エラーコードをリセットする
  ResetLastError();
//--- インデックス0 を持つ指標バッファの値で iWPRBuffer 配列の一部を記入する
  if(CopyBuffer(ind_handle,0,0,amount,wpr_buffer)<0)
    {
    //--- 複製が失敗したら、エラーコードを出す
    PrintFormat("Failed to copy data from the iWPR indicator, error code %d",GetLastError());
    //--- ゼロ結果で終了。 指標は計算されていないと見なされる
    return(false);
    }
//--- 全てが成功
  return(true);
 }
//+------------------------------------------------------------------+
//| 指標初期化解除関数                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
 {
  if(handle!=INVALID_HANDLE)
    IndicatorRelease(handle);
//--- 指標の削除後チャートをクリアする
  Comment("");
 }