iStochastic関数の書式や引数を調べても、実際にコンパイルして動かせるサンプルコードが見つからない——という人向けの記事です。
この記事では、iStochastic関数の書式・引数の意味に加えて、実際にMetaEditorでコンパイルしてストキャスティクスをサブウィンドウに表示できるサンプルコードを紹介します。
iStochastic関数
iStochastic関数とはストキャスティクスの値を取得するために使用します。
iStochastic関数の書式
double iStochastic( string symbol, // 通貨ペア int timeframe, // 時間軸 int Kperiod, // %K期間 int Dperiod, // %D期間 int slowing, // スローイング int method, // 平均化メソッド int price_field, // 価格(Low/HighまたはClose/Close) int mode, // ラインインデックス int shift // シフト );
iStochastic関数の引数の説明
string symbol
通貨ペア名を指定せず、チャートの通貨ペアのストキャスティクスの値を取得したい場合はNULLです。
int timeframe
ストキャスティクスの値を計算する時間軸の指定。
| 種類 | 定数 | 説明 |
|---|---|---|
| PERIOD_CURRENT | 0 | 現在の時間足 |
| PERIOD_M1 | 1 | 1分足 |
| PERIOD_M5 | 5 | 5分足 |
| PERIOD_M15 | 15 | 15分足 |
| PERIOD_M30 | 30 | 30分足 |
| PERIOD_H1 | 60 | 1時間足 |
| PERIOD_H4 | 240 | 4時間足 |
| PERIOD_D1 | 1440 | 日足 |
| PERIOD_W1 | 10080 | 週足 |
| PERIOD_MN1 | 43200 | 月足 |
int Kperiod
%K期間を指定。
int Dperiod
%D期間を指定。
int slowing
スローイング期間を指定。
int method
シグナルラインを算出する移動平均の種類を指定。
| 種類 | 定数 | 説明 |
|---|---|---|
| MODE_SMA | 0 | 単純移動平均線 |
| MODE_EMA | 1 | 指数移動平均線 |
| MODE_SMMA | 2 | 平滑移動平均線 |
| MODE_LWMA | 3 | 線形加重移動平均線 |
int price_field
ストキャスティクスの値の計算に使用する価格データを指定。
高値と安値(Low/High)からストキャスティクスの値を計算したい場合は「0」。
終値(Close/Close)からストキャスティクスの値を計算したい場合は「1」。
int mode
ライン番号の指定。
メインラインの値を取得したい場合は「0」。または、MODE_MAIN。
シグナルラインの値を取得したい場合は「1」。または、MODE_SIGNAL。
int shift
ストキャスティクスの値を取得したいバーの位置の指定。
ストキャスティクスの値を取得したいバーが現在のバーのときは「0」、
1本前のバーのときは「1」、2本前のバーのときは「2」……x本前のバーのときは「x」。
iStochastic関数を使ってインジケーターを作る
書式と引数が分かったところで、実際にiStochastic関数を使ってメインライン(%K)とシグナルライン(%D)をサブウィンドウに表示するカスタムインジケーターを作ってみましょう。
//+------------------------------------------------------------------+
//| iStochastic_Sample.mq4 |
//+------------------------------------------------------------------+
#property strict
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 DodgerBlue
#property indicator_color2 Red
#property indicator_minimum 0
#property indicator_maximum 100
input int Kperiod = 5;//%K期間
input int Dperiod = 3;//%D期間
input int Slowing = 3;//スローイング
double MainBuffer[];
double SignalBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
SetIndexBuffer(0, MainBuffer);
SetIndexStyle(0, DRAW_LINE);
SetIndexLabel(0, "%K");
SetIndexBuffer(1, SignalBuffer);
SetIndexStyle(1, DRAW_LINE);
SetIndexLabel(1, "%D");
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int counted = IndicatorCounted();
int limit = Bars - counted;
if(counted > 0)
limit++;
for(int i = limit - 1; i >= 0; i--)
{
MainBuffer[i] = iStochastic(NULL, 0, Kperiod, Dperiod, Slowing, MODE_SMA, 0, MODE_MAIN, i);
SignalBuffer[i] = iStochastic(NULL, 0, Kperiod, Dperiod, Slowing, MODE_SMA, 0, MODE_SIGNAL, i);
}
return(0);
}
//+------------------------------------------------------------------+
メインライン(MODE_MAIN)とシグナルライン(MODE_SIGNAL)を別々のバッファに格納し、2本のラインとして描画しています。Kperiod・Dperiod・Slowingはinput変数にしているので、プロパティ画面から数値を変更して試すこともできます。
コメント