Discussion of article "MQL5: Create Your Own Indicator" - page 5

 
okwh #:

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

 

Why use [i-1] to calculate [i] and start i=1 ?  no [0] ?

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


Hello.

Broadly speaking, if you use one of the native mql5 indicator functions that starts with "i", you don't need to pay attention to the route. The copybuffer will do it for you.

On the other hand if you go through a specific dev, you have to pay attention to the number of bars, especially for the first pass because otherwise you risk an out of range


look at the code of this rsi that uses Irsi, no position for the course and everything goes well.

On the other hand, this Rsi does not go through the function.

Everything is calculated by hand, so to speak, and you have to do the positioning well so that everything goes smoothly.

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
 
//+------------------------------------------------------------------+
//|                                          True Strength Index.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
//---- 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 parameters
input int      r=25;
input int      s=13;
//--- indicator buffers
double         TSIBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping

   SetIndexBuffer(0,TSIBuffer,INDICATOR_DATA);
//---
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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[])
  {
//---
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

why in oninit
it needs to return 0?

 
ziyang2048 #:

why in oninit
it needs to return 0?

In MQL5, the OnInit() function is called when the indicator, expert advisor (EA), or script is initialized. It is expected to return an integer value to signal the success or failure of the initialization process.

When OnInit() returns 0 , it indicates that the initialization was successful. If you need to signal that something went wrong during initialization, you can return a non-zero value, which will prevent the indicator, EA, or script from running properly.