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]