iStochastic

この関数は Stochastic Oscillator(ストキャスティックス)指標のハンドルを返します。

int  iStochastic(
  string          symbol,          // 銘柄名
  ENUM_TIMEFRAMES  period,          // 期間
  int              Kperiod,        // K期間(計算に使用されるバーの数)
  int              Dperiod,        // D期間(初めの平滑化の期間)
  int              slowing,        // 最終の平滑化
  ENUM_MA_METHOD  ma_method,      // 平滑化の種類
  ENUM_STO_PRICE  price_field      // 確率論的計算方法
  );

パラメータ

symbol

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

period

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

Kperiod

[in]  %K 線計算の平均期間(バーの数)

Dperiod

[in]  %D 線計算の平均期間(バーの数)

slowing

[in]  スロー値

ma_method

[in]  平均化の方法ENUM_MA_METHOD 値のいずれか

price_field

[in]  計算に使用される価格選択のパラメータENUM_STO_PRICE 値のいずれか

戻り値

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

注意事項

バッファ番号は 0 - MAIN_LINE, 1 - SIGNAL_LINE です。

例:

//+------------------------------------------------------------------+
//|                                             Demo_iStochastic.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 iStochastic 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 standard Stochastic Oscillator."
 
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots   2
//--- ストキャスティックスプロット
#property indicator_label1 "Stochastic"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrLightSeaGreen
#property indicator_style1 STYLE_SOLID
#property indicator_width1  1
//--- シグナルプロット
#property indicator_label2 "Signal"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrRed
#property indicator_style2 STYLE_SOLID
#property indicator_width2  1
//--- 指標値を制限する
#property indicator_minimum 0
#property indicator_maximum 100
//--- 指標ウィンドウの水平レベル
#property indicator_level1  -100.0
#property indicator_level2  100.0
//+------------------------------------------------------------------+
//| 作成を処理する方法の列挙                                              |
//+------------------------------------------------------------------+
enum Creation
 {
  Call_iStochastic,       // use iStochastic を使用する
  Call_IndicatorCreate   // IndicatorCreateを使用する
 };
//--- 入力パラメータ
input Creation             type=Call_iStochastic;     // 関数の種類
input int                  Kperiod=5;                 // K 期間(計算に使用されるバーの数)
input int                  Dperiod=3;                 // D 期間(主要な平滑化の期間)
input int                  slowing=3;                 // 最終平滑化期間 
input ENUM_MA_METHOD       ma_method=MODE_SMA;       // 平滑化の種類  
input ENUM_STO_PRICE       price_field=STO_LOWHIGH;   // 確率の計算方法
input string               symbol=" ";               // シンボル
input ENUM_TIMEFRAMES     period=PERIOD_CURRENT;     // 時間軸
//--- 指標バッファ
double         StochasticBuffer[];
double         SignalBuffer[];
//--- iStochastic 指標ハンドルを格納する変数
int    handle;
//--- 格納に使用される変数
string name=symbol;
//--- チャートでの指標名
string short_name;
//--- ストキャスティックスー指標に値の数を保存
int    bars_calculated=0;
//+------------------------------------------------------------------+
//| カスタム指標を初期化する関数                                            |
//+------------------------------------------------------------------+
int OnInit()
 {
//--- 配列の指標バッファへの割り当て
  SetIndexBuffer(0,StochasticBuffer,INDICATOR_DATA);
  SetIndexBuffer(1,SignalBuffer,INDICATOR_DATA);
//--- 指標が描画するシンボルを決める
  name=symbol;
//--- 左右のスペースを削する
  StringTrimRight(name);
  StringTrimLeft(name);
//---「name」文字列の長さがゼロになった場合
  if(StringLen(name)==0)
    {
    //--- 指標が接続されているチャートのシンボルを取る
     name=_Symbol;
    }
//--- 指標ハンドルを作成する
  if(type==Call_iStochastic)
     handle=iStochastic(name,period,Kperiod,Dperiod,slowing,ma_method,price_field);
  else
    {
    //--- 構造体を指標のパラメータで記入    
     MqlParam pars[5];
    //--- 計算の K 期間
     pars[0].type=TYPE_INT;
     pars[0].integer_value=Kperiod;
    //--- 主要な平滑化の D 期間
     pars[1].type=TYPE_INT;
     pars[1].integer_value=Dperiod;
    //--- 最後の平滑化の K 期間
     pars[2].type=TYPE_INT;
     pars[2].integer_value=slowing;
    //--- 平滑化の種類
     pars[3].type=TYPE_INT;
     pars[3].integer_value=ma_method;
    //--- 確率の計算方法
     pars[4].type=TYPE_INT;
     pars[4].integer_value=price_field;
     handle=IndicatorCreate(name,period,IND_STOCHASTIC,5,pars);
    }
//--- ハンドルが作成されなかった場合
  if(handle==INVALID_HANDLE)
    {
    //--- 失敗した事実とエラーコードを出力する
    PrintFormat("Failed to create handle of the iStochastic indicator for the symbol %s/%s, error code %d",
                 name,
                EnumToString(period),
                GetLastError());
    //--- 指標が早期に中止された
    return(INIT_FAILED);
    }
//--- ストキャスティックスーが計算された銘柄/時間軸を表示
  short_name=StringFormat("iStochastic(%s/%s, %d, %d, %d, %s, %s)",name,EnumToString(period),
                          Kperiod,Dperiod,slowing,EnumToString(ma_method),EnumToString(price_field));
  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[])
 {
//--- iStochastic 指標から複製された値の数
  int values_to_copy;
//--- 指標で計算された値の数を決める
  int calculated=BarsCalculated(handle);
  if(calculated<=0)
    {
    PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError());
    return(0);
    }
//--- これが指標計算の初めであるか iStochastic 指標の値の数が変更した
//---または、2 つ以上のバーの指標の計算が必要である場合(価格履歴で何かが変更された)
  if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1)
    {
    //--- StochasticBuffer 配列が銘柄/期間で iStochastic 指標の値の数より大きい場合、全体のコピーはしない
    //--- 他の場合、指標バッファサイズより少ない量をコピーをする
    if(calculated>rates_total) values_to_copy=rates_total;
    else                       values_to_copy=calculated;
    }
  else
    {
    //--- これは初回の計算ではなく、
    //--- 前回の OnCalculate() から、一以上のバーが加えられてない。
     values_to_copy=(rates_total-prev_calculated)+1;
    }
//--- f配列を iStochastic 指標の値で記入
//--- FillArraysFromBuffer が false を返した場合、情報の準備が終わっていないので操作を終了する
  if(!FillArraysFromBuffers(StochasticBuffer,SignalBuffer,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);
 }
//+------------------------------------------------------------------+
//| iStochastic 指標から指標バッファを記入する                               |
//+------------------------------------------------------------------+
bool FillArraysFromBuffers(double &main_buffer[],   // ストキャスティックスー値の指標バッファ
                          double &signal_buffer[], // シグナル線の指標バッファ
                          int ind_handle,           // iStochastic 指標ハンドル
                          int amount               // 複製された値の数
                          )
 {
//--- エラーコードをリセットする
  ResetLastError();
//--- インデックス0 を持つ指標バッファの値で StochasticBuffer 配列の一部を記入する
  if(CopyBuffer(ind_handle,MAIN_LINE,0,amount,main_buffer)<0)
    {
    //--- 複製が失敗したら、エラーコードを出す
    PrintFormat("Failed to copy data from the iStochastic indicator, error code %d",GetLastError());
    //--- ゼロ結果で終了。 指標は計算されていないと見なされる
    return(false);
    }
//--- インデックス 1 を持つ指標バッファの値で SignalBuffer 配列の一部を記入する
  if(CopyBuffer(ind_handle,SIGNAL_LINE,0,amount,signal_buffer)<0)
    {
    //--- 複製が失敗したら、エラーコードを出す
    PrintFormat("Failed to copy data from the iStochastic indicator, error code %d",GetLastError());
    //--- ゼロ結果で終了。 指標は計算されていないと見なされる
    return(false);
    }
//--- 全てが成功
  return(true);
 }
//+------------------------------------------------------------------+
//| 指標初期化解除関数                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
 {
  if(handle!=INVALID_HANDLE)
    IndicatorRelease(handle);
//--- 指標の削除後チャートをクリアする
  Comment("");
 }