Calculating Relative Strenght Index
Set array as series after copying.
datetime time[]; int size = CopyTime(Symbol(),0, istart, tocopy, time); ArraySetAsSeries(time,true);
I wonder why did I started using ArraySetSeries in wrong way ...
Anyways, it appears that Rsi in mt is using different price avarage, guessing its EMA.
Here's a working source code for code in main post:
Anyways, it appears that Rsi in mt is using different price avarage, guessing its EMA.
Here's a working source code for code in main post:
double rsi(int period, double *open, double *high, double *low, double *close, int pos) { double nsum = 0.0, psum = 0.0, dif; for (int a = pos - 1024; a < pos + period; a++) { // indicator needs at least 1024+period candles in order to work because of the EMA dif = close[a] - close[a - 1]; if (dif > 0.0) psum += dif; else nsum -= dif; } psum /= period; nsum /= period; for (int a = (pos - 1024 ) + period; a <= pos; a++) { dif = close[a] - close[a - 1]; psum = (psum * (period - 1) + (dif>0.0 ? dif : 0.0)) / period; nsum = (nsum * (period - 1) + (dif<0.0 ? -dif : 0.0)) / period; } if (nsum != 0.0) return 100.0 - 100.0 / (1 + psum / nsum); else { if (psum != 0.0) return 100.0; return 50.0; } return 50.0; }
The first one is ok because you only need the previous values.
double rsi(int period,double *open,double *high,double *low,double *close,int pos) { double nsum=0.0,psum=0.0,dif; static int prevpos=-1; if(prevpos<0) { for(int a=period-1; a>=0; a--) { dif=close[(pos+a)]-close[(pos+a)+1]; if(dif>0.0) psum+=dif; else nsum-=dif; } psum /= period; nsum /= period; } static double prevnsum=0.0,prevpsum=0.0; dif = close[pos] - close[pos + 1]; psum = (prevpsum * (period - 1) + (dif>0.0 ? dif : 0.0)) / period; nsum = (prevnsum * (period - 1) + (dif<0.0 ? -dif : 0.0)) / period; if(prevpos!=pos) { prevpsum = psum; prevnsum = nsum; prevpos = pos; } if(nsum!=0.0) { return 100.0 - (100.0 / (1.0 + psum / nsum)); } if(psum!=0.0) { return 100.0; } return 50.0; }
Hello everyone!
I'm trying to calculate rsi in my dll:
Using rsi function:
The problem is that its not showing the numbers that Rsi(2) shows in metatrader.
What am i doing wrong?