How to work with time

How to create strategies and indicators
User avatar
Site Admin
Posts: 833
Joined: Sat Oct 21, 2006 4:39 pm

How to work with time

#1 Postby Terranin » Mon Apr 28, 2008 12:39 pm

ForexTester uses Borland Delphi time format for its functions like Time(), iTime() and others. So, if you use C++ compilers you should convert this time to C++ format.

Delphi format (TDateTime):

Delphi TDateTime is equals to double value. The integral part of a Delphi TDateTime value is the number of days that have passed since 12/30/1899. The fractional part of the TDateTime value is fraction of a 24 hour day that has elapsed.

Following are some examples of TDateTime values and their corresponding dates and times:

0 12/30/1899 12:00 am
2.75 1/1/1900 6:00 pm
-1.25 12/29/1899 6:00 am
35065 1/1/1996 12:00 am

C++ format (Unix format):

C++ time is an integer value that shows how many seconds passed sinse 01/01/1970.

Converting functions:

Code: Select all

unsigned int UnixStartDate = 25569;

unsigned int DateTimeToUnix(double ConvDate)
  return((unsigned int)((ConvDate - UnixStartDate) * 86400.0));

double UnixToDateTime(unsigned int USec)
  return((Usec/86400.0) + UnixStartDate);
Hasta la vista

Posts: 1
Joined: Sun Aug 01, 2010 3:48 pm

time and itime function with c++

#2 Postby hjalamo » Wed Nov 24, 2010 1:13 am


I need developer a strategy that it will entry at 00:00

I have tried with "time, timeframe, itime (c++)" but i have got problems.

can you help me how to develop this code sentence?


Best Regards,

Humberto Álamo.

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

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


Please refer to the last posts of this thread:
you can use "w_hour" and "w_min" variables to define an Entry point
Check our other product here:

Posts: 17
Joined: Fri Jul 12, 2013 3:47 am

#4 Postby amberwine » Sat Jul 13, 2013 3:37 pm

Is there a way to select only those values for indicators like MACD as well as Ask(), Bid prices, etc, in each invocation of GetSingleTick() which correspond to the times shown on the graph for the timeframe set in the FT GUI?

I'm developing a MACD strategy, which works, but there are what appear to be false buy and sell signals which cause some problems. I think these are occuring due to jitter around MACD zero at the tick level, since GetSingleTick() seems to be called for each tick (is that correct?), rather than once every simulated 15 minutes (say) if I have my timeframe set to 15 minutes.

So, in my code, I'm looking for MACD changing from negative to positive in GetSingleTick(), to signal a buy, and I'm not always seeing all of those buy signals in the MACD graph. I think this is because the graph is set to a 15 minute timeframe, so the signals are smoothed out, compared to what's happening at the tick-level. I really want my code to respond only to the signals shown on the graph, for the selected timeframe.

I think what I'd like ideally is a GetTimeframeInterval() (or similar) to be called once every simulated 15 minutes, or whatever the timeframe is set to, synchronised with the graph, but I guess that's not currently possible?

What's the best way to synchronize your code in GetSingleTick() to make sure it selects only data for the same times as the graph is showing, for any given timeframe?

Posts: 17
Joined: Fri Jul 12, 2013 3:47 am

#5 Postby amberwine » Sun Jul 14, 2013 8:29 am

I think I've worked out how to do this now, thanks.

I'm calling TimeCurrent() in GetSingleTick(), then using this value to work out the next expected time of interest based on the selected time-frame. So I ignore all the ticks apart from the ones that come through approx every 15 minutes, if the time-frame is set to 15 minutes. I hope this makes sense and is reasonable. If not, please let me know.

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

Re: How to work with time

#6 Postby alpente » Wed Dec 23, 2015 11:52 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

Posts: 1
Joined: Sat Feb 17, 2018 3:33 am

Re: How to work with time

#7 Postby gregory_ » Sat Feb 17, 2018 3:36 am

OMG. If I ever have to convert a Delphi date to a C based Unix date again...

So, none of the presented solutions worked for me. The ATL based solution (COleDateTime) is not available in the Express editions of MSVC++.

gmtime() did not work for me. gmtime() was off by 426 days, sometimes, and other times 427 days! Was driving me nuts. Finally found the right way to do this:

struct tm instantTM;
time_t inst = DateTimeToUnix(Time(0)); // Custom function below
_gmtime64_s(&instantTM, &inst);
struct tm * instant = &instantTM;

unsigned int DateTimeToUnix(double ConvDate)
return((unsigned int)((ConvDate - UnixStartDate) * 86400.0));

After that, can use instant to get the tm struct values.

Return to “FT API”

Who is online

Users browsing this forum: No registered users and 1 guest