I am trying to write Lesson 3 in Visual C++ (I am a C++ novice). I am trying to convert the following Delphi segment to C++
time := iTime(Symbol, PERIOD_M1, 0);
if (HourOf(time) = 8) and (OrdersTotal = 0) then
The API in ForexTester Professional tells me that the HourOf function exists in C++ version.
**************************
Delphi
if HourOf(Time(0)) <> 22 then …
C++
if (HourOf(Time(0)) != 22) { … }
*****************************
However my code does not compile when I try to call the function...This is my code attempt...
Time(0) returns time in delphi format. its a double value with the left part representing days elapsed and right part indicating the time of the day elapsed.
How can I convert this into C++ time so that I can do datetime operations like compare, add, etc on it.
Been really struggling with this aspect. Any help will be appreciated.
FT Support wrote:Sorry cannot help you with this right now, i'm not C++ programmer. It is not API error you just need to know how to extract hour from C++ time
That is a pathetic excuse - the ForexTester API CLEARLY states that this function exists - it says so in the helpfiles. So either you (Forex Tester) implemented this function (HourOf) in C++, or you didn't. Don't fob off us users.
This has been a real pain for me too! No C++ HourOf() function as already noted by several people.
I'm still wearing my c++ 'L' plates so if you're a pro don't laugh to hard!
With no guarantee or support, this is how I got around the problem:
/* ------------------------------------------------------------------------------*/
/* Example code hacked from one of my programs */
/* ------------------------------------------------------------------------------*/
// time related includes
#include <time.h>
#include <ctime>
// c++ variable definitions
int w_min, w_wday, w_hour, w_year, w_month, w_day;
// code in GetSingleTick()
/* Establish date/time variables for each tick */
time_t mytime; // define structure of type time_t, c++ base date is 01/01/1970 (25569 days after FT start date of 30/12/1899)
mytime = (time_t) ((TimeCurrent() - 25569) * 86400); // Convert FT date/time to c++ equivalent
struct tm * timeinfo; // define local structure 'tm' to hold calendar date in component parts
timeinfo = gmtime(&mytime); // Copy that time to modifiable structure 'timeinfo (note GMT)
w_year = 1900 + timeinfo->tm_year; // save year, adjusted (years since 1900)
w_month = 1 + timeinfo->tm_mon; // save month number, adjusted (runs 0 - 11)
w_day = timeinfo->tm_mday; // save day of month (runs 1 - 31)
w_wday = timeinfo->tm_wday; // save day of week (runs 0 - 6, sunday=0)
w_hour = timeinfo->tm_hour; // save hour of day (runs 0 - 23)
w_min = timeinfo->tm_min; // save minute of hour (runs 0 - 59)
mktime ( timeinfo ); // call mktime to set timeinfo structure variables
//Now you can refer to minute, hour, day etc
if (w_hour > 6 && w_hour < 18) // so time now is: GMT 07:00 - 17:59
{
// Imaginary code illustrating debug print to ForexTester.log file
session_open = TRUE;
n=sprintf (buffer,"Price=(%f) Time=%d:%d",price[1],w_hour,w_min);
Print(buffer);
}
// Info
// structure tm, holds calendar data
//
//int tm_sec; /* seconds (0 - 60) */
//int tm_min; /* minutes (0 - 59) */
//int tm_hour; /* hours (0 - 23) */
//int tm_mday; /* day of month (1 - 31) */
//int tm_mon; /* month of year (0 - 11) */
//int tm_year; /* year - 1900 */
//int tm_wday; /* day of week (Sunday = 0) */
//int tm_yday; /* day of year (0 - 365) */
//int tm_isdst; /* is summer time in effect? */
//char *tm_zone; /* abbreviation of timezone name */
//long tm_gmtoff; /* offset from UTC in seconds */
void EstablishDateTimeValues() { time_t mytime; // define structure of type time_t, c++ base date is 01/01/1970 (25569 days after FT start date of 30/12/1899) mytime = (time_t) ((TimeCurrent() - 25569) * 86400); // Convert FT date/time to c++ equivalent struct tm * timeinfo; // define local structure 'tm' to hold calendar date in component parts timeinfo = gmtime(&mytime); // Copy that time to modifiable structure 'timeinfo (note GMT) w_year = 1900 + timeinfo->tm_year; // save year, adjusted (years since 1900) w_month = 1 + timeinfo->tm_mon; // save month number, adjusted (runs 0 - 11) w_day = timeinfo->tm_mday; // save day of month (runs 1 - 31) w_wday = timeinfo->tm_wday; // save day of week (runs 0 - 6, sunday=0) w_hour = timeinfo->tm_hour; // save hour of day (runs 0 - 23) w_min = timeinfo->tm_min; // save minute of hour (runs 0 - 59) mktime ( timeinfo ); // call mktime to set timeinfo structure variables }
EXPORT void __stdcall GetSingleTick() { /* Establish date/time variables for each tick */ EstablishDateTimeValues();
//Now you can refer to minute, hour, day etc sprintf (buff,"Current DateTime values are: Year=%d Month=%d Date=%d DayOfWeek=%d Time=%d:%d", w_year, w_month, w_day, w_wday, w_hour,w_min); Print(buff); }
Check our other product here: http://www.forexcopier.com
The function "HourOf" is not in FT2 API, nevertheless this function is used in more than one Example code in "Scripts API", and so, I think "HarriForexTester" is right. The function "HourOf" must be added to library.
HourOf is a Delphi function (*1). The type of input of the HourOf function is TDateTime that really is a double type value (*2). In VisualC++ is there the type COleDateTime that stores date and time the same way TDateTime does (*2)(*3)(*4). The function COleDateTime::GetHour is the VisualC++ equivalent for HourOf (*5).
// This is the function that does the job int HourOf(TDateTime time) // TDateTime is the same as double { COleDateTime t = COleDateTime((DATE)time); int hour = t.GetHour() return hour; }