MQL関数リファレンス

MQL4MQL5 イベント関数

OnCalculate()

できること
インジケーターの値を計算するために呼ばれる
対応
MQL4・MQL5

OnCalculate() とは

カスタムインジケーターで、価格データが更新されるたびに呼ばれる関数です。インジケーターバッファに値を入れて、チャートに線や矢印を表示します。2通りの書き方があり、ふつうは時刻・4本値・出来高を受け取る長いほうを使います。

OnCalculate() の書式と引数

引数の組み合わせが違う 2 通りの書き方があります。

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[]
)

int OnCalculate(
   const int rates_total,
   const int prev_calculated,
   const int begin,
   const double &price[]
)
引数型省略時説明
rates_totalconst int必須チャートにあるバーの本数(配列の要素数)。
prev_calculatedconst int必須前回の OnCalculate() が返した値。初回は 0 なので、0 なら全部のバーを計算し、そうでなければ新しいバーだけ計算する、という使い方をします。
timeconst datetime &[]必須各バーの開始時刻の配列。
openconst double &[]必須始値の配列。
highconst double &[]必須高値の配列。
lowconst double &[]必須安値の配列。
closeconst double &[]必須終値の配列。
tick_volumeconst long &[]必須ティック出来高の配列。
volumeconst long &[]必須実出来高の配列(FX では多くの場合 0)。
spreadconst int &[]必須スプレッドの配列。
beginconst int必須短いほうの書き方で、price[] のうち有効なデータが始まる位置。
priceconst double &[]必須短いほうの書き方で、パラメーター画面の「適用価格」で選んだ価格(終値など)の配列。

戻り値

計算し終えたバーの本数を返します。ふつうは rates_total をそのまま返します。この値が次回の prev_calculated になります。

OnCalculate() の使い方(コード例)

MQL4

#property strict
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 clrDodgerBlue

input int InpPeriod = 20;   // 期間
double Buf[];

int OnInit()
{
   SetIndexBuffer(0, Buf);
   SetIndexStyle(0, DRAW_LINE);
   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[])
{
   if(rates_total < InpPeriod) return 0;
   ArraySetAsSeries(close, true);   // 0 = 最新のバー
   ArraySetAsSeries(Buf, true);

   int limit = rates_total - prev_calculated;       // 計算が必要な本数
   if(prev_calculated == 0) limit = rates_total - InpPeriod;
   for(int i = limit; i >= 0; i--)
   {
      if(i > rates_total - InpPeriod) continue;
      double sum = 0;
      for(int k = 0; k < InpPeriod; k++) sum += close[i + k];
      Buf[i] = sum / InpPeriod;
   }
   return rates_total;
}

MQL5

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrDodgerBlue

input int InpPeriod = 20;   // 期間
double Buf[];

int OnInit()
{
   SetIndexBuffer(0, Buf, INDICATOR_DATA);
   return INIT_SUCCEEDED;
}

// 終値の単純移動平均を自分で計算する(0 = 最も古いバーのまま使う)
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[])
{
   if(rates_total < InpPeriod) return 0;
   int start = (prev_calculated == 0) ? InpPeriod - 1 : prev_calculated - 1;
   for(int i = start; i < rates_total; i++)
   {
      double sum = 0;
      for(int k = 0; k < InpPeriod; k++) sum += close[i - k];
      Buf[i] = sum / InpPeriod;
   }
   return rates_total;
}

MetaEditor でコンパイルできることを確認済みです。

OnCalculate() の注意点

  • 毎回すべてのバーを計算し直すと重くなります。prev_calculated を使って、新しいバー(と最新のバー)だけを計算します。
  • 配列の並び順に注意します。MQL5 では配列の 0 番が最も古いバーです。0 番を最新のバーとして扱いたいときは、ArraySetAsSeries(close, true) のように時系列の設定をします。MQL4 と MQL5 で同じ書き方にしたい場合も、ArraySetAsSeries() で向きを明示しておくと安全です。
  • OnCalculate() が1つもないインジケーターは「event handling function not found」でコンパイルエラーになります。

OnCalculate() でよく出るエラー

関連する関数

イベント関数の関数一覧

← MQL関数リファレンスで、ほかの関数を探す

サイバーシグナルインジケーターと8大特典プレゼント

MT4/MT5自動売買EAサイバーラッシュ無料プレゼント

MT4/MT5自動売買EAサイバードラゴン無料プレゼント

グルグルトレイン(グルトレ)EAを無料プレゼント

ライントレードEAサイバートレナビを無料プレゼント

ゴトー日の東京仲値が狙える自動売買EA

LINE友だち追加

注目記事ランキング

  1. 1

    【サクソバンクチャート】月曜日の早朝から動く為替レート

  2. 2

    【MT4/MT5対応】チャート上に手書きできる描画インジケーターの無料配布

  3. 3

    【サンデーダウ・サンデードル円】土日に動く中東レートの表示方法と見方を解説

  4. 4

    【先着50名】グルトレEAを無料プレゼントしています!

  5. 5

    保護中: 【勝率UP!】曜日や日・月別為替相場の動きでFXを見極める

  6. 6

    【MT4/MT5対応】「sign_alert」シグナルアラートやメール送信するインジケーターの無料配布

  7. 7

    無料メールマガジン「マナブ式FX」|毎朝6時、今日の為替相場予想が届く

  8. 8

    【原油・金】WTIとゴールドの商品レート一覧と見やすいチャート

  9. 9

    VQインジケーターとは?MT4/MT5版を無料配布【設定例つき】

  10. 10

    【バイナリーオプション攻略法】全勝!+497,363円の利益が出た手法とは?

TOP
CLOSE