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);
}
Basic Indicator not working in C++
-
- Posts: 1
- Joined: Mon Oct 31, 2011 7:23 pm
- Location: United States
- Contact:
-
- Posts: 905
- Joined: Sat Jul 11, 2009 10:54 am
-
- Posts: 63
- Joined: Tue Oct 12, 2010 6:45 pm
Re: Basic Indicator not working in C++
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:
You could use instead which is overflow save:
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]
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]
Who is online
Users browsing this forum: No registered users and 13 guests