Can anyone help to solve my code

 

Dear All,

Why my indicator can not show anything? I want to design a indicator for weighted rate of change with formula as below






#property  indicator_separate_window

#property  indicator_buffers 1

#property  indicator_color1  Red
//---- indicator parameters
extern int RPeriod = 10;
extern bool UsePercent = true;
double RateOfChange[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- drawing settings
   SetIndexStyle(0,DRAW_LINE);
   SetIndexDrawBegin(0, RPeriod);
   IndicatorDigits(Digits + 1);
//---- indicator buffers mapping
   if(!SetIndexBuffer(0, RateOfChange))
       Print("cannot set indicator buffers!");
//---- name for DataWindow and indicator subwindow label
   IndicatorShortName("ROC(" + RPeriod + ")");
//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence                           |
//+------------------------------------------------------------------+
int start()
  {
   int limit;
   double WROC[];
   double ROC[];
   double CurrentClose, PrevClose;
   int counted_bars = IndicatorCounted();
//---- check for possible errors
   if(counted_bars < 0) 
       return(-1);
//---- last counted bar will be recounted
   if(counted_bars > 0) 
       counted_bars--;
   limit = Bars - counted_bars - RPeriod;
//---- ROC calculation
   for(int i = 0; i < limit; i++)
     {
       CurrentClose = iClose(NULL,0,i);
       for (int j=0; j < limit-1; j++)
         {
            PrevClose = iClose(NULL,0,j+1);
            ROC[j] = 100*(RPeriod-j)*(CurrentClose-PrevClose)/PrevClose;
            RateOfChange[j]=ROC[j];
         }           
      }   
//---- done
   return(0);
  }
//+------------------------------------------------------------------+
 
   double WROC[];
   double ROC[];

these bad boys here are empty ,so you probably run into "array out of range issues" ,you can confirm that in the "Experts tab

if you cant see the Experts Tab , hit Ctrl+T ,and click on "Experts" 

You want to weigh by volume yeah ?

 
  1. 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 2019.05.06
              Messages Editor

  2. If you had looking in the experts tab, you would see the array exceeded message.
     int start(){
       ⋮
       double WROC[];
       double ROC[];
       ⋮
                ROC[j] = …

 

Try this , i think you did not want volume weighting eventually 

Its not as complex as it looks , the averaging function at the bottom is what interests you .

#property copyright "Forum.Thread"
#property link      "https://www.mql5.com/en/forum/351677"
#property version   "1.00"
#property description "Telegram  : https://t.me/lorentzor\nInstagram : @rlorentzo\nTwitter : @lorentzo_r\nLinkedIn : https://www.linkedin.com/in/lorentzor\nYoutube : https://www.youtube.com/channel/UCM0Lj06cAJagFWvSpb9N5zA\nFacebook  : @LorentzoR"
/* 
ways to connect .> : 
Telegram  : https://t.me/lorentzor
Instagram : https://www.instagram.com/rlorentzo /OR/ @rlorentzo
Twitter   : https://twitter.com/lorentzo_r /OR/ @lorentzo_r
LinkedIn  : https://www.linkedin.com/in/lorentzor
Youtube   : https://www.youtube.com/channel/UCM0Lj06cAJagFWvSpb9N5zA
Facebook  : https://www.facebook.com/LorentzoR /OR/ @LorentzoR
Mql5.com  : https://www.mql5.com/en/users/lorio
*/
#property strict
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots   2
//--- plot ROC
#property indicator_label1  "ROC"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrLimeGreen
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot avgROC
#property indicator_label2  "avgROC"
#property indicator_type2  DRAW_LINE
#property indicator_color2 clrWhite
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
input int ROCPeriod=14;//Period 
input bool W_Recency=false;//Weight by recency
input bool W_Volume=false;//Weight by volume 
//--- indicator buffers
double         AvgROC[],ROC[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,ROC);
   SetIndexBuffer(1,AvgROC);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| 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[])
  {
//---
  //basic indicator loop ignore
  int nuevo=rates_total-prev_calculated;
  int i_from=rates_total-2,i_to=0;//if as series 
  if(nuevo<i_from){i_from=nuevo;}
  //loop 
  for(int i=i_from;i>=0;i--)
  {
  int j=rates_total-i-1;//mt5 adress - ignore
  //basic indicator loop ends here ---- your code follows
  //>>>
    //raw roc for this bar using your unWeighted formula -> current close - prev_close / prev close  
      ROC[i]=(Close[i]-Close[i+1])/Close[i+1];
    //weighted average 
      AvgROC[i]=RecencyAndVolumeWeightedAverage(ROC,i,ROCPeriod,W_Recency,W_Volume,volume);
  //<<<
  }
  //loop ends here   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//for series +1 is in the past -1 is in the future
double RecencyAndVolumeWeightedAverage(double &series[],
                                       int i_from,
                                       int period,
                                       bool weight_by_recency,
                                       bool weight_by_volume,
                                       const long &vol[]){
double rovwa=0;
//recency weight
double reweight=0;
//volume weight 
double volweight=0;
//sum
double sum=0;
//divider 
double div=0;
//start 
int i_sta=i_from+period-1;//we include the from bar in the calculation  
if(i_sta>(ArraySize(series)-1)){i_sta=ArraySize(series)-1;}
//loop
  for(int i=i_sta;i>=i_from;i--)
  {
  //this weight 
    double this_weight=1;
    //by recency
    if(weight_by_recency)
    {
    reweight++;
    this_weight*=reweight;
    }
    //by volume
    if(weight_by_volume&&i<ArraySize(vol))
    {
    volweight=((double)vol[i]/1000);
    this_weight*=volweight;
    }
  sum+=series[i]*this_weight;
  div+=this_weight;
  } 
  if(div>0){rovwa=sum/div;}
//loop
return(rovwa);
}
                                      
Files:
ROC.ex4  10 kb
ROC.mq4  5 kb
 
William Roeder:
  1. 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 2019.05.06
              Messages Editor

  2. If you had looking in the experts tab, you would see the array exceeded message.
Thanks~ I will post as code format and follow the general rules next time. 
 
Lorentzos Roussos:

Try this , i think you did not want volume weighting eventually 

Its not as complex as it looks , the averaging function at the bottom is what interests you .

Thanks for your great help~ I want to be weighted by recency or by volume. I will try to understand the logic of your code. It is very useful information for me as a beginner of mql4. Thanks 
Reason: