I get a different EMA with GetMA. C++

Examples step by step how to make your own automated strategy or user indicator
Message
Author
b4gt
Posts: 23
Joined: Mon Jul 12, 2010 12:41 pm

I get a different EMA with GetMA. C++

#1 Postby b4gt » Sun Oct 31, 2010 1:25 pm

Can someone please tell me what is wrong with this code?

Code: Select all

#include <Windows.h>
#include <math.h>
#include "IndicatorInterfaceUnit.h"
#include "TechnicalFunctions.h"


int EMAperiod;
int ATRperiod;


TIndexBuffer ATRup;
TIndexBuffer ATRdown;
TIndexBuffer ATR;
TIndexBuffer tempBuffer;
TIndexBuffer EMA;

EXPORT void __stdcall Init()
{
   IndicatorShortName("Ponzi ATR Stop");
   SetOutputWindow(ow_ChartWindow);

   AddSeparator("Options");
   RegOption("Length of EMA", ot_Integer, &EMAperiod);
   EMAperiod = 10;
//   RegOption("Color of EMA", ot_Color, &emaColor);

   RegOption("ATR period", ot_Integer, &ATRperiod);
   ATRperiod = 14;

   //Set buffers
   ATRup = CreateIndexBuffer();
   ATRdown = CreateIndexBuffer();
   ATR = CreateIndexBuffer();
   tempBuffer = CreateIndexBuffer();
   EMA = CreateIndexBuffer();

   IndicatorBuffers(3);
   SetIndexBuffer(0, ATRup);
   SetIndexBuffer(1, ATRdown);
   SetIndexBuffer(2, EMA);

   SetIndexStyle(0,ds_Line, psSolid, 1,clRed);
   SetIndexLabel(0, "Trailing Stop Up");

   SetIndexStyle(1,ds_Line, psSolid, 1,clRed);
   SetIndexLabel(1, "Trailing Stop Down");

   SetIndexStyle(2,ds_Line, psSolid, 1,clRed);
   SetIndexLabel(2, "EMA");
}


void GetATR(int period, int index)
{
   int i;
   double P_High, P_Low, prevClose, sum;

   
   P_High = High(index);
   P_Low = Low(index);

   if(index == Bars() - 1)
   {
      tempBuffer[index] = P_High - P_Low;
   }
   else
   {
      //prevClose = GetPrice(index +1, pt_Close);
      prevClose = Close(index +1);
      tempBuffer[index] = max(P_High, prevClose) - min(P_Low, prevClose);
   }

   sum = 0;
   for(i = 0; i < period; i++)
   {
      
         sum = sum + tempBuffer[index + i];
         ATR[index] = sum/period;
         
   }
//   return ATR;

   
}

void __stdcall Done()
{}

void __stdcall Calculate(int index)
{
   

   ///---DEBUG CODE---
   char buff[30];
   sprintf(buff,"ATR = %f",ATR[index+1]);
   Print(buff);

   GetATR(ATRperiod, index);
   EMA[index] = GetMA(index , 0, EMAperiod , ma_EMA, pt_Close);
   ATRup[index] = EMA[index] - ATR[index]/2;
   ATRdown[index] = EMA[index] - ATR[index]/2;

}


I tried to port the ATR code from the Delphi example to C++. I think I did a good job except that my C++ port has a precision of 4 decimals while the built in version has more.

But what really troubles me is that I can't get a decent EMA with GetMA. I am attaching a screen shot to explain what is the problem.

The red line is the built in EMA. The green line that follows the price is the EMA buffer that it supposed to be plotted from my code. Those two lines should match. Also if you see in the left data window their values are different.

I did apply the fix from here http://forextester.com/forum/viewtopic. ... ight=getma but the problem still remains.

Any ideas what might be wrong?[/code]
Attachments
Untitled2.png
Untitled2.png (195.45 KiB) Viewed 6205 times

b4gt
Posts: 23
Joined: Mon Jul 12, 2010 12:41 pm

#2 Postby b4gt » Sun Oct 31, 2010 2:22 pm

OK guys I made it work. The line is

Code: Select all

EMA[index] = GetMA(index , 0, EMAperiod, ma_EMA, pt_Close, EMA[index +1]);

Wessel
Posts: 63
Joined: Tue Oct 12, 2010 6:45 pm

#3 Postby Wessel » Sun Oct 31, 2010 4:02 pm

Great you've found it. Appreciate you posted the fix as well. Could help others as well..

W


Return to “Programming lessons”

Who is online

Users browsing this forum: No registered users and 6 guests