Does iMAOnArray give the true result ?
I try to compute MA to check if the fuction iMAOnArray gives the true result. I find the same result for SMA, but not for EMA, can someone check my code ?
- How to give an indicator my own data?
- EMA calculation different everywhere
- Using iMA for variables
bertrand125: I try to compute MA to check if the fuction iMAOnArray gives the true result. I find the same result for SMA, but not for EMA, can someone check my code ?
for(i = n-ma_period-1; i <= n-1-shift; i++) { if(i == n-ma_period-1) price = array[i]; else { price = array[i]*pr+priceprec*(1-pr); } priceprec = price; }
- I assume your array is timeseries (a buffer,) so you need to loop from [n-1 .. shift] so your i <= n-1-shift means no loop. bogus
- There is no need to not use the first ma_period values.
- a[]*pr + prev*(1-pr) magnifies round off error. Use the form below which fades round off errors. Moving average - Wikipedia, the free encyclopedia
i = n - 1; price = array[i]; while(--i >= shift) price += ( array[i] - price )*pr;
Thank you. Your code works, but i have to use ArraySetAsSeries(array,true). I will try to rewrite it in order to not use this function.
If you create your array with newer elements having higher indexes, you have to undo it with ASAS. Create your array with newest @ zero.
.