Wrong calculation of DEMA

 

(Sorry for my bad English)

I try to calculate DEMA and I got wrong value when I compare the value of DEMA with this website (http://www.forexlive.com/LiveCharts). I have no idea about the reason that make my DEMA value different to DEMA value in the website.

I want to calculate a DEMA value with period 95 in 30 minute timeframe and below is the code I wrote. Please help :)

double ema_on_ema;
double dema;
int period = 95;
string symbol = "EURUSD";
int timeframe = 30;
double current_ema;

int OnInit()
{
   DemaCal();
   return(INIT_SUCCEEDED);
}

void DemaCal()
{
   double old_ema = 0;
   double ema_follow_index = 0;
   ema_on_ema = 0;
   dema = 0;
   //put ema in an array and sum value of ema in ema_on_ema to find average after this loop
   for(int i=0; i<period; i++)
   {
      ema_follow_index = 0;
      ema_follow_index = iMA(symbol, timeframe, period, 0, MODE_EMA, PRICE_CLOSE, i);
      if(i == 0){
         old_ema = ema_follow_index;
      }
      ema_on_ema += ema_follow_index;
   }
   //find average of last_thirteen_ema
   ema_on_ema = ema_on_ema/period;
   current_ema = old_ema;
   dema = (2 * current_ema) - ema_on_ema;
   Alert("dema = " + dema);
   Alert("ema = " + current_ema);
}
EURUSD Live Chart | Forexlive
  • www.forexlive.com
Live EURUSDchart. Plus all major currency pairs, realtime Indices Charts, Commodities Charts, Futures Charts and more.
 
The formula is: DEMA = 2 ∗ E M A − E M A ( E M A )
          Double exponential moving average - Wikipedia
  1. Adding ema_follow_index to ema_on_ema is bogus. That's not an EMA. Dividing by Period is bogus, that's not how you calculate EMA's alpha
  2. Your global variables are not initialized, bogus
  3. Don't use global variables in indicators. They are supposed to have the previous value, that means you can not process bar zero, which breaks it. bogus.
  4. You count up (toward past) so the indicator is repainting, bogus.