Please use SRC button when posting code. I edited your post for you.
Oh! Ok! Thanks for that. Will remember in future. Didn't know that
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); }
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); }
- 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.
- 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?
- 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!
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
I wrote this code but don't know why is the Historical Volatility Value coming incorrect when using istdevonarray
Can someone help
==================================================