文章 "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 开始?

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


您好。

大体上说,如果您使用的是以 "i "开头的本地 mql5 指标函数之一,就不需要注意路径。copybuffer 会帮你完成。

另一方面,如果通过特定的 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
//| 2009 年 MetaQuotes 软件公司版权所有。|
//|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
//---- plot 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 时,表示初始化成功。如果需要提示初始化过程中出现了错误,可以返回一个非零值,这将阻止指标、EA 或脚本正常运行。