Basic Indicator not working in C++

How to create strategies and indicators
Message
Author
sunman4008
Posts: 1
Joined: Mon Oct 31, 2011 7:23 pm
Location: United States
Contact:

Basic Indicator not working in C++

#1 Postby sunman4008 » Mon Oct 31, 2011 7:25 pm

Hello,

I took the basic example and created an indicator. Looking at the print statement...it is always 0. What am I doing wrong?


#include <windows.h>
#include "IndicatorInterfaceUnit.h"
#include "TechnicalFunctions.h"
#include <iostream>
#include <algorithm>


// External variables
char buffer[1000];


TIndexBuffer distance,ATR,tempBuffer;

//---------------------------------------------------------------------------
// Initialize indicator
//---------------------------------------------------------------------------
EXPORT void __stdcall Init()
{
IndicatorShortName("test");
// SetOutputWindow(ow_ChartWindow);
SetOutputWindow(ow_SeparateWindow); // paint in separate window

SetFixedMinMaxValues(0, 15); // scale between 0 and 100




// register options
AddSeparator("Common");


RegOption("Period", ot_Integer, &IchiPeriod);
IchiPeriod = 14;

distance = CreateIndexBuffer();
tempBuffer = CreateIndexBuffer();
ATR = CreateIndexBuffer();
IndicatorBuffers(1);
SetIndexBuffer(0,distance);
SetIndexStyle(0,ds_Line, psSolid, 1, clGreen);

}


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;

}
}


EXPORT void __stdcall Done()
{

}

//---------------------------------------------------------------------------
// Calculate requested bar
//---------------------------------------------------------------------------
EXPORT void __stdcall Calculate(int index)
{


///---DEBUG CODE---
char buff[30];


sprintf(buff,"ATR = %f",ATR[index+1]);
Print(buff);

GetATR(14, index);

}

FT Support
Posts: 905
Joined: Sat Jul 11, 2009 10:54 am

#2 Postby FT Support » Tue Nov 01, 2011 10:57 am

Please add more "Print" commands into "GetATR" function or use a debugger ti find an error in code.

Let us know if this does not help.
Check our other product here:
http://www.forexcopier.com

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

Re: Basic Indicator not working in C++

#3 Postby Wessel » Sat Nov 05, 2011 12:20 pm

Sunman,

There is no need to use C style with an char[] for debug statements as it can be risky if the buffer is overflown.

I've overloaded the Print functions extensively with other C++ formats:

Code: Select all

void Print(PChar s);        // s - PChar
void Print(string s);      // s - string
void Print(stringstream& s);// s - stringstream
void Print(int i);          // s - int
void Print(double d);       // s - double



You could use instead which is overflow save:

Code: Select all

stringstream s;
s<<"ATR="<<ATR[index+1];
Print(s);


Don't forget to also set the:
using namespace std;

And read through the include of the API, almost all C calls have been overloaded with C++ versions like String or stringstreams, and a massive amount of colors are available.

Wessel
[/code]


Return to “FT API”

Who is online

Users browsing this forum: No registered users and 13 guests