How to create simple moving average (100 period) of OnBalanceVolumes data passed by array?

 

Hello guys, i hope that someone can please help me, cause i am (at this moment) at a dead end...


I want to create an array of the SMA (Period 100) of the On Balance Volumes (TICK_TYPE) data passed by iOBV function.

In other words, the new value of sma_Array at every candle formation must contain the sum of last 100 OBV values/100.  The problem is that i am not able to use iMA for data different from prices.

I know that perhaps i have to use OnCalculate() function, but i am not able, i have try with this code, which i had found here in another post, but on execution it doesn't work.

Please, if everyone can help me with this, i would be really grateful. This is the code i have tried, i only need to make works and understand this part.


I pass the handle with:    
                                     obvHandle=iOBV(_Symbol,_Period,VOLUME_TICK);

                                     ArraySetAsSeries(obvVal,true);     //obv array
                                     ArraySetAsSeries(smaVal,true);    //sma array
                                     candles=Bars(_Symbol,_Period);  // rates_total i pass to the function below...

Then i have a function NewCandle() to verify output at new candle formation, for example live on M1. Thanks :)

int SimpleMAOnBuffer(const int rates_total,const int prev_calculated,const int begin,
                     const int period,const double& price[],double& buffer[])
  {
   int i,limit;
//--- check for data
   if(period<=1 || rates_total-begin<period) return(0);
//--- save as_series flags
   bool as_series_price=ArrayGetAsSeries(price);
   bool as_series_buffer=ArrayGetAsSeries(buffer);
   if(as_series_price)  ArraySetAsSeries(price,false);
   if(as_series_buffer) ArraySetAsSeries(buffer,false);
//--- first calculation or number of bars was changed
   if(prev_calculated==0) // first calculation
     {
      limit=period+begin;
      //--- set empty value for first bars
      for(i=0;i<limit-1;i++) buffer[i]=0.0;
      //--- calculate first visible value
      double firstValue=0;
      for(i=begin;i<limit;i++)
         firstValue+=price[i];
      firstValue/=period;
      buffer[limit-1]=firstValue;
     }
   else limit=prev_calculated-1;
//--- main loop
   for(i=limit;i<rates_total && !IsStopped();i++)
      buffer[i]=buffer[i-1]+(price[i]-price[i-period])/period;
//--- restore as_series flags
   if(as_series_price)  ArraySetAsSeries(price,true);
   if(as_series_buffer) ArraySetAsSeries(buffer,true);

return(rates_total);
  }--
Documentation on MQL5: Language Basics / Functions / Event Handling Functions
Documentation on MQL5: Language Basics / Functions / Event Handling Functions
  • www.mql5.com
The MQL5 language provides processing of some predefined events. Functions for handling these events must be defined in a MQL5 program; function name, return type, composition of parameters (if there are any) and their types must strictly conform to the description of the event handler function. The event handler of the client terminal...
 
Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
          General rules and best pratices of the Forum. - General - MQL5 programming forum
          Messages Editor
Reason: