記事"MQL5: 自分のインディケーターの作成"についてのディスカッション - ページ 5

 
okwh #:

for(int i=1;i<rates_total;i++)
{
MTMBuffer[i]=price[i]-price[i-1];
AbsMTMBuffer[i]=fabs(MTMBuffer[i]);
}.

なぜ[i-1]を使って[i]を計算し、i=1から始めるのか? 0]を使わないのか?

MTMBuffer[i]=price[i]-price[i-1];


こんにちは。

大まかに言って、"i "で始まるネイティブのmql5インジケータ関数のいずれかを使用する場合は、ルートに注意を払う必要はありません。コピーバッファーがやってくれる。

一方、特定のdevを経由する場合は、バーの数に注意を払う必要があります。


Irsiを使用するこのRsiの コードを見てください。

一方、このRsiは 関数を通過しません。

いわばすべてが手作業で計算されるため、すべてがスムーズに進むようにうまく位置決めをしなければなりません。

Rsi code for beginners by William210
Rsi code for beginners by William210
  • www.mql5.com
Rsi beginner tutorial to learn how to code in MQL5
 
//+------------------------------------------------------------------+
//|真の実力指数mq5||真の強さ指数
//| Copyright 2009, MetaQuotes Software Corp.|
//|https://www.mql5.com
//+------------------------------------------------------------------+
#property copyright "2009, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots   1
//---- プロットTSI
#property indicator_label1  "TSI"
#property indicator_type1   DRAW_LINE
#property indicator_color1  Blue
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- 入力パラメータ
input int      r=25;
input int      s=13;
//--- インジケータ・バッファ
double         TSIBuffer[];
//+------------------------------------------------------------------+
//| カスタムインジケータ初期化関数
//+------------------------------------------------------------------+
int OnInit()
  {
//--- インジケータ・バッファのマッピング

   SetIndexBuffer(0,TSIBuffer,INDICATOR_DATA);
//---
   return(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[])
  {
//---
//--- 次の呼び出しのためにprev_calculatedの値を返す
   return(rates_total);
  }
//+------------------------------------------------------------------+

なぜoninit
で0を返す必要があるのか?

 
ziyang2048 #:

なぜoninit
で0を返す必要があるのか?

MQL5では、インジケータ、エキスパートアドバイザー(EA)、またはスクリプトが初期化されるときにOnInit()関数が呼び出されます。初期化プロセスの成功または失敗を示す整数値を返すことが期待されています。

OnInit() が 0 を返す場合、初期化が成功したことを示します。初期化中に何か問題が発生したことを通知する必要がある場合は、0 以外の値を返すことができます。