Simple C++ Question - HourOf

Examples step by step how to make your own automated strategy or user indicator
Message
Author
toriacht
Posts: 6
Joined: Tue Nov 20, 2007 4:37 am

Simple C++ Question - HourOf

#1 Postby toriacht » Thu Jul 01, 2010 4:06 pm

Hi,

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 = iTime(Symbol(), PERIOD_M1, 0);

if(HourOf(time)==8) {}

The ERROR tells me that HourOf is undefined.

Any help much appreciated.


Best regards,
T

toriacht
Posts: 6
Joined: Tue Nov 20, 2007 4:37 am

anybody?

#2 Postby toriacht » Mon Jul 05, 2010 11:26 am

Hi Guys,

anybody at all able to help me with this? I'm unsure if the API doesn't support this or if its a c++ newbie error.

Thanks,
T

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

#3 Postby FT Support » Tue Jul 06, 2010 11:49 am

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
Check our other products here:
www.fx-metropolis.com
www.forexcopier.com

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

#4 Postby FT Support » Tue Jul 06, 2010 1:33 pm

C++ programmers advise to take a look at "CTime" class and "SYSTEMTIME" structure, please see details here:
http://msdn.microsoft.com/en-us/library ... 85%29.aspx

http://msdn.microsoft.com/en-us/library/78zb0ese.aspx

http://www.codeproject.com/KB/datetime/ ... edisc.aspx
Check our other products here:

www.fx-metropolis.com

www.forexcopier.com

eklavya
Posts: 5
Joined: Wed Jun 11, 2008 11:26 am

#5 Postby eklavya » Fri Aug 06, 2010 11:48 am

Hi,

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
Posts: 905
Joined: Sat Jul 11, 2009 10:54 am
Contact:

#6 Postby FT Support » Sat Aug 07, 2010 9:00 am

Check our other products here:

www.fx-metropolis.com

www.forexcopier.com

HarriForexTester
Posts: 3
Joined: Wed Oct 13, 2010 11:45 am

#7 Postby HarriForexTester » Tue Oct 26, 2010 9:05 am

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.

tonyb
Posts: 3
Joined: Tue May 11, 2010 5:00 am
Location: England

c++ / FT date & time

#8 Postby tonyb » Wed Nov 17, 2010 10:40 am

Hi all,

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 */

/* ------------------------------------------------------------------------------*/

Hope this helps someone!

Happy testing, good trading


Cheers



Tony

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

#9 Postby FT Support » Wed Nov 24, 2010 3:11 am

Tony, thank you very much! Your code works well.
If you do not mind I'll post the sample strategy that shows how to use this approach:

Code: Select all

#include <windows.h>
#include "StrategyInterfaceUnit.h"
#include "TechnicalFunctions.h"
#include <time.h>
#include <ctime>

// variable definitions
int w_min, w_wday, w_hour, w_year, w_month, w_day;
char buff[200];

EXPORT void __stdcall InitStrategy()
{
  StrategyShortName("Time Test");
  StrategyDescription("Time Test"); 
}

EXPORT void __stdcall DoneStrategy()
{}

EXPORT void __stdcall  ResetStrategy()
{}

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 products here:

www.fx-metropolis.com

www.forexcopier.com

alpente
Posts: 4
Joined: Wed Dec 23, 2015 9:37 pm

Re: Simple C++ Question - HourOf

#10 Postby alpente » Wed Dec 23, 2015 11:32 pm

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).

Code: Select all

// 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;
}


You can see the function HourOf(TDateTime) working in the post "Thu Dec 24, 2015 5:29 am" at http://www.forextester.com/forum/viewtopic.php?f=9&t=1015&p=13719#p13719


violajsilver
Posts: 5
Joined: Mon May 09, 2016 11:42 pm

Re: Simple C++ Question - HourOf

#11 Postby violajsilver » Tue May 10, 2016 12:21 am

Thanks for this useful post.


Return to “Programming lessons”

Who is online

Users browsing this forum: No registered users and 2 guests