Page 1 of 1

How to work with time

Posted: Mon Apr 28, 2008 12:39 pm
by Terranin
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);
}

time and itime function with c++

Posted: Wed Nov 24, 2010 1:13 am
by hjalamo
Please.

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?

Thanks.

Best Regards,

Humberto Álamo.

Posted: Wed Nov 24, 2010 3:15 am
by FT Support
Hello,

Please refer to the last posts of this thread:
http://www.forextester.com/forum/viewtopic.php?t=2008
you can use "w_hour" and "w_min" variables to define an Entry point

Posted: Sat Jul 13, 2013 3:37 pm
by amberwine
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?

Posted: Sun Jul 14, 2013 8:29 am
by amberwine
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.

Re: How to work with time

Posted: Wed Dec 23, 2015 11:52 pm
by alpente
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


Re: How to work with time

Posted: Sat Feb 17, 2018 3:36 am
by gregory_
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.

Re: How to work with time

Posted: Thu May 20, 2021 2:03 pm
by imamushroom
A more accurate way of doing this is presented in this post : https://stackoverflow.com/questions/67625006/how-to-convert-a-pascal-tdatetimedouble-time-to-a-unix-epoch-in-c/67626181#67626181

Code reproduced here in case the link gets deleted:

Code: Select all

#define MSecsPerDay 86400000
#define MSecsPerSec 1000
#define UnixDateDelta 25569 // Days between TDateTime basis (12/31/1899) and Unix time_t basis (1/1/1970)
#define DateDelta 693594    // Days between 1/1/0001 and 12/31/1899

   const float FMSecsPerDay = MSecsPerDay;
   const int IMSecsPerDay = MSecsPerDay;

   struct TTimeStamp
   {
      int Time; // Number of milliseconds since midnight
      int Date; // One plus number of days since 1/1/0001
   };

//   typedef double TDateTime;

   TTimeStamp DateTimeToTimeStamp(TDateTime DateTime)
   {
      __int64 LTemp = std::round(DateTime * FMSecsPerDay); // <-- this might require tweaking!
      __int64 LTemp2 = LTemp / IMSecsPerDay;
      TTimeStamp Result;
      Result.Date = DateDelta + LTemp2;
      Result.Time = std::abs(LTemp) % IMSecsPerDay;
      return Result;
   }

   __int64 DateTimeToMilliseconds(const TDateTime ADateTime)
   {
      TTimeStamp LTimeStamp = DateTimeToTimeStamp(ADateTime);
      return (__int64(LTimeStamp.Date) * MSecsPerDay) + LTimeStamp.Time;
   }

   __int64 SecondsBetween(const TDateTime ANow, const TDateTime AThen)
   {
      return std::abs(DateTimeToMilliseconds(ANow) - DateTimeToMilliseconds(AThen)) / MSecsPerSec;
   }

   __int64 DateTimeToUnix(const TDateTime AValue)
   {
      __int64 Result = SecondsBetween(UnixDateDelta, AValue);
      if (AValue < UnixDateDelta)
         Result = -Result;
      return Result;
   }

Re: How to work with time

Posted: Sat May 22, 2021 4:48 am
by imamushroom
I've played with the more elegant solution of using the date/date.h libray and here's the corresponding code.

You need to include

Code: Select all

#define NOMINMAX


before any includes on every file for this to compile free from errors.

Code: Select all

   
   time_t DateTimeToUnix(double d)
   {
      using namespace date;
      using namespace std::chrono;
      using ddays = duration<double, days::period>;

      return system_clock::to_time_t(round<seconds>(sys_days{ 1899_y / 12 / 30 } + ddays{ d }));
   }

   double UnixToDateTime(time_t t)
   {
      using namespace date;
      using namespace std::chrono;
      using ddays = duration<double, days::period>;

      const auto tp = system_clock::from_time_t(t);
      return (tp - sys_days{ 1899_y / 12 / 30 }) / ddays{ 1 };
   }
}


Hope this helps.

Re: How to work with time

Posted: Mon Jul 19, 2021 11:54 pm
by niamul21
Terranin wrote: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);
}



You safe my time. Thanks for covering up the things here.