How to know which price series the user selected when the indicator is added to the chart ? - page 2

 
Tradingrobot2718 #:
I am sorry but I think I'm lazy.

That doesn't exactly connote a serious desire to be successful in the business of trading. Let's just say that you want to be "efficient" instead.

Tradingrobot2718 #:
If only the prices starting at index "begin" are relevant, then why isn't the first "begin" prices of the array removed and  the "rates_total" parameter adjusted ?

Whether you use the simple form of OnCalculate() or the long form of OnCalculate(), you still need to write a main loop to iterate through the bars of the chart:

int OnCalculate(const int rates_total,     // price[] array size 
                const int prev_calculated, // number of previously handled bars
                const int begin,           // where significant data start from 
                const double &price[])     // value array for handling
  {
//--- initial position for calculations
   int StartCalcPosition=(IntPeriod-1)+begin;
//---- if calculation data is insufficient
   if(rates_total<StartCalcPosition)
      return(0);  // exit with a zero value - the indicator is not calculated
//--- correct draw begin
   if(begin>0)
      PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,StartCalcPosition+(IntPeriod-1));
//--- start calculations, define the starting position
   int pos=prev_calculated-1;
   if(pos<StartCalcPosition)
      pos=begin+IntPeriod;
//--- main calculation loop
   for(int i=pos;i<rates_total && !IsStopped();i++)
      MomentumBuffer[i]=price[i]*100/price[i-IntPeriod];
//--- OnCalculate execution is complete. Return the new prev_calculated value for the subsequent call
   return(rates_total);
  }
Documentation on MQL5: OnCalculate / Event Handling
Documentation on MQL5: OnCalculate / Event Handling
  • www.mql5.com
The function is called in the indicators when the Calculate event occurs for processing price data changes. There are two function types. Only one...
 
Tradingrobot2718 #:

I am sorry but I think I'm lazy. I will write the code that deals with the "begin" parameter of the simplified version of OnCalculate and then use it with every custom indicator. To me, this parameter is useless. If only the prices starting at index "begin" are relevant, then why isn't the first "begin" prices of the array removed and  the "rates_total" parameter adjusted ?


The current approach ensures that all timeseries in the chart have the same indexing. If some buffers would start from some hidden "beginning" you should require to read this settings from other place anyway in case you need synchronized data from several indicators or built-in timeseries.
 
Ok, thank you all.
 

I've just fallen into another trap. When a moving average has a non-zero time shift parameter, than its buffer is smaller. The following access causes an "array out of range error":

MovingAverageBuffer[rates_total-1] //Array out of range error !


This one is ok:

MovingAverageBuffer[rates_total-1-MovingAverageTimeShift] //Ok
 
Tradingrobot2718 #:
When a moving average has a non-zero time shift parameter, than its buffer is smaller. The following access causes an "array out of range error"...
If you want to take that shortcut without a main loop, you have to return rates_total - 1 following OnCalculate().