Download MetaTrader 5
To add comments, please log in or register
Find out the latest MQL5.community news!
tahseenjamal
29
tahseenjamal 2014.02.08 12:12 

I wrote this code but don't know why is the Historical Volatility Value coming incorrect when using istdevonarray

Can someone help

==================================================

 #property indicator_separate_window

#property indicator_buffers 2

double HV[];

double RoC[];

int init()

{

IndicatorBuffers(2);

SetIndexBuffer(0,HV); SetIndexStyle(0,DRAW_LINE,0,1,SteelBlue);

SetIndexBuffer(1,RoC);

return(0);

}

int start()

{

int limit;

int counted_bars=IndicatorCounted();

//---- last counted bar will be recounted

if(counted_bars>0) counted_bars--;

limit=Bars-counted_bars;

for(int i=limit; i>=0; i--)

{

RoC[i]=MathLog(Close[i]/Close[i+1]);

HV[i]=iStdDevOnArray(RoC,0,100,0,MODE_SMA,0)*MathSqrt(262);

}

return(0);

}

Alain Verleyen
Moderator
28699
Alain Verleyen 2014.02.08 12:25  
Please use SRC button when posting code. I edited your post for you.
tahseenjamal
29
tahseenjamal 2014.02.08 12:50  

Oh! Ok! Thanks for that. Will remember in future. Didn't know that

tahseenjamal
29
tahseenjamal 2014.02.08 12:53  

Also one more thing is that if I make i=120 just for testing purpose, I see very high values of HV variable, which is not possible.

I mean I did the same calculation on Excel. So most probably there is something wrong that am doing with the iStdevonarray function

tahseenjamal
29
tahseenjamal 2014.02.08 16:07  

Anyway, I wrote historical volatility all by myself

//Historical Volatility by tahseen.jamal@gmail.com

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1  LimeGreen

extern int hist_vol_period=100;



double roc[];
double hv[];




int init()
{
   IndicatorBuffers(2);
   SetIndexBuffer(0,hv);SetIndexLabel(0,"Historical Volatility");
   SetIndexBuffer(1,roc);SetIndexLabel(1,"RoC");

   
   IndicatorShortName("");
   
   return(0);
}


int start()
{
   int counted_bars=IndicatorCounted();
     if(counted_bars<0) return(-1);
      if(counted_bars>0) counted_bars--;
           int limit=Bars-counted_bars;
           
           
  
   
   for(int i=limit; i>=0; i--)
   {   
      roc[i]  = 0;
      if (Close[i+1]!=0) roc[i] = MathLog(Close[i]/Close[i+1]);


      
               
      hv[i]=0.0;  
      for(int loop = 0;loop<hist_vol_period;loop++)
         hv[i]= hv[i] + roc[i+loop];
         
      
      
      double mean=hv[i]/hist_vol_period;
      
      double sum = 0.0;
      
      for (int a = 0; a < hist_vol_period; a++)
         sum += MathPow((roc[i+a] - mean), 2);
         
         
      hv[i]= MathSqrt(sum/(hist_vol_period-1))*MathSqrt(252)*100;
      
      
      
         
   }         
   
   
   return(0);
} 


 
Fernando Carreiro
1569
Fernando Carreiro 2014.02.08 16:49  
tahseen:

I wrote this code but don't know why is the Historical Volatility Value coming incorrect when using istdevonarray

Can someone help

for(int i=limit; i>=0; i--)
{
	RoC[i]=MathLog(Close[i]/Close[i+1]);
	HV[i]=iStdDevOnArray(RoC,0,100,0,MODE_SMA,0)*MathSqrt(262);
}



  1. Please note that you are calculating the Std. Dev. on the whole ("total" = 0) of the ROC Array before it is even completely populated. The second argument of the iStdDevOnArray is being set to "0" which means you are using the entire array for the calculation yet you are setting the contents within the same loop. I suggest you first fill the ROC array up on a separate loop or limit the scope of iStdDevOnArray to only calculate the part that has been assigned.
  2. You are also always using the same "shift" value for the iStdDevOnArray function (the last parameter is set to "0"). Is this your intention, to always use the same value in your calculations?
  3. Also, if you want to speed up calculations, first assign the MathSqrt(262) in the "init()" to a variable and then use the variable instead of calculating this on every single step.

Overall, your code logic is not very clear and I am not sure if certain things are intentional or bugs!!!

EDIT: Sorry I took too long to answer and other posts were up before I completed my comments!

/
To add comments, please log in or register