Trading sessions or how important time is - page 9

 

Here are some useful functions for dealing with time (you can determine if summer or winter time is in effect for any date).

#property copyright "Erics"
#property link      "erics.fx@tut.by"

#define EUR 0
#define USA 1

//+------------------------------------------------------------------+
//| скрипт, демонстрирующий использование некоторых функций времени  |
//+------------------------------------------------------------------+

void start()
{
  int tl = TimeLocal();
  MessageBox("Сейчас: " + TimeToStr(tl) + ", летнее время = " + getDST(EUR, tl) +
             "\n\n20.03.2008 0:00, летнее время в Европе  = " + getDST(EUR, MTtime(DateSerial(2008,3,20))) +
             "\n20.03.2008 0:00, летнее время в Америке = " + getDST(USA, MTtime(DateSerial(2008,3,20))) +
             "\n\n1.04.2008 0:00, летнее время в Европе  = " + getDST(EUR, MTtime(DateSerial(2008,4,1))) +
             "\n1.04.2008 0:00, летнее время в Америке = " + getDST(USA, MTtime(DateSerial(2008,4,1))));
}

int MTtime(double serialTime)
{ // переводит дату/время, заданную в днях от 1900 года (Windows)
  // в дату, заданную в секундах от 1970 года (Unix)
  return(86400.*(serialTime - 25569.));
}

int DateSerial(int yyyy, int mm, int dd)
{ // возвращает дату в формате Windows (от 1.01.1900 г.)
  yyyy -= 1900; mm++;
  if (mm < 4) { mm += 12; yyyy--; }
  int res = MathFloor(365.25*yyyy) + MathFloor(30.6*mm) + dd - 62;
  return(res);
}

int getDST(int country, int time)
{ // Daylight Saving Time - возвращает 1, если на время time действует летнее время и 0 - если поясное (зимнее) время
  int DSTstarts, DSTends, y, yr = TimeYear(time);

  switch(country)
  {
    case EUR:
      // летнее время в Европе и России начинается в последнее восресенье марта, в 2 часа ночи
      y = MathFloor(1.25 * yr + 4.);
      DSTstarts = MTtime(DateSerial(yr, 3, 31 - y % 7) + 2./24.);
      // летнее время в Европе и России заканчивается в последнее восресенье октября, в 3 часа ночи
      y = MathFloor(1.25 * yr + 1.);
      DSTends   = MTtime(DateSerial(yr,10, 31 - y % 7) + 3./24.);
      break;

    case USA:
      // летнее время в США и Канаде с 2007 г. начинается во второе восресенье марта, в 3 часа ночи
      y = MathFloor(1.25 * yr + 1.);
      DSTstarts = MTtime(DateSerial(yr, 3, 14 - y % 7) + 3./24.);
      // летнее время в США и Канаде с 2007 г. заканчивается в первое восресенье ноября, в 3 часа ночи
      DSTends   = MTtime(DateSerial(yr,11, 7 - y % 7) + 3./24.);
      break;
  }  
  if (time>=DSTstarts && time<=DSTends) return(1); else return(0);
}

P.S. In America before 2007, the switch to and from DST was the same time as in Europe. Use EUR when calling.

If anyone would convert this to a library, willing to help (asia, australia).

Files:
 

I did DST functions for Europe and US too, they return true for summer time and false for winter time. They are designed to work with server time, for US only from 2007 onwards.

//+------------------------------------------------------------------+
bool DST_EU(int CurrentTime) {
  int CurMonth = TimeMonth(CurrentTime);
  int CurDayOfWeek; 
  int CurDay; 
  if (CurMonth > 3  &&  CurMonth < 10) return(true);
  else { 
    if (CurMonth < 3  ||  CurMonth > 10) return(false);
    else {
      CurDayOfWeek = TimeDayOfWeek(CurrentTime); 
      CurDay = TimeDay(CurrentTime); 
      switch (CurMonth) {
        case  3: if (CurDay-CurDayOfWeek > 24) return(true);
                 else return(false);
                 break;
        case 10: if (CurDay-CurDayOfWeek < 25) return(true);
                 else return(false);
                 break;
      }
    }
  }
}
//+------------------------------------------------------------------+
bool DST_US(int CurrentTime) {
  int CurMonth = TimeMonth(CurrentTime);
  int CurDayOfWeek; 
  int CurDay; 
  if (CurMonth > 3  &&  CurMonth < 11) return(true);
  else { 
    if (CurMonth < 3  ||  CurMonth > 11) return(false);
    else {
      CurDayOfWeek = TimeDayOfWeek(CurrentTime); 
      CurDay = TimeDay(CurrentTime); 
      switch (CurMonth) {
        case  3: if (CurDay-CurDayOfWeek > 7) return(true);
                 else return(false); 
                 break;
        case 11: if (CurDay-CurDayOfWeek < 1) return(true);
                 else return(false); 
                 break;
      }
    }
  }
}


Erics писал (а) >>

P.S. In America before 2007 the switch to and from DST was the same as in Europe. Use EUR when called.

This is only half true, only the ending matched.

 
kombat писал (а) >>

10:00 - European launch

kombat, you are mistaken. Europe opens at 09:00 MSK (summer time)

 
A question for the seasoned market... Do you have any particularly lucky months in the game and vice versa? Of course it depends on your trading tactics, so the question is too abstract. But one way or another?
 

As promised, I "digested" all I could concerning trading sessions and daylight saving time. I have made an excel file with calendar of daylight saving time and calculation of session start and end time.

The first sheet is just a transition calendar, no formulas. This calendar is used to calculate the sessions on the second sheet. On the second sheet, you can set only three parameters: the current date for which you want to calculate everything, and two timezones: one for the timezone of the DC server, and the other for your local time (which you have on your wristwatch). By these you calculate tables with local times of exchange times, and by them - the times of the beginning and end (minimum and maximum) trading sessions.

It seems to work, but I (out of habit) can not believe that everything was done without errors. Why? Well, for example Australia and the ocean session. They are in the other hemisphere and their daylight saving time is in winter, so if our months are like this LLLLLLLLLLLLLLLLLLLLLLLLLLLLLL. Accordingly we obtain different formulas for calculations: here we are looking for daylight saving time, while they have to look for daylight saving time. In general there are some difficulties :(

For this reason, the file I have laid on my site and not posted on the forum - as errors are eliminated, I will update the file and the link from the forum will always be downloaded fresh and correct version (I will post updates in this thread). You can download the file here: http: //forextools.com.ua/uploads/files/DST.zip

It looks like this:


If anyone has any constructive suggestions - write a letter. I can not promise a quick implementation (due to full-time on the main job), but when I will have time - all sensible and useful will be done ;)

 

ForexTools, good job!

But I can't figure out what you mean by it. Explain more specifically what "additive" means (just to make sure).

And what about TMZ server and TMZ local? What is the difference between local time and the time of the DC? You have numbers 2 and 3 under each respectively? What does it mean?

 

There are simply two exactly the same time calculations, just for convenience - if the terminal time does not match the local computer time.

The addition is how many hours (taking into account summer or winter time) will be added to the UTC time to convert the UTC time of the respective exchange to the local time of the selected timezone. This field is only needed to facilitate the calculations in excel.

The rules are very simple: we define (depending on the Rule field) and the specified date of time. It is entered in the field Letn opposite the calculated date. Then to the exchange working time according to UTC, add the number of TMZ hours (from the top table) and add to it the correction Letn (if it is summer time - then +1) and +TMZ of brokerage house server or TMZ local. As a result, the UTC time is converted to the local time of the respective TMZ.

 
Erics писал (а) >>

There is a particular standard of time in forex. This is Central European Time, CET.

This is GMT (or UTC) +1 (+2 in summer).

In theory, daily candles should start at midnight CET in any brokerage house.

This is probably not the case, but most brokerage companies use CET time.

Later I will send you a script which defines GMT, summer/winter time, translation dates in Europe and America in any year (in America since 2007, i.e. according to new rules).

Clarify: if I look only at server clock (GMT+1), will the start/end time of sessions be constant?

In the prgram I use the hour restrictions with the Hour() command, i.e. I check the current server hour, and in or out.


If there is a transition at two o'clock, say backwards, then the hour candle does not contain double volume?

 
Parabellum писал (а) >>

If there is a transition at two o'clock in the morning, say, backwards, does the one o'clock candle contain a double volume?

The transition is always on Sunday, when the trading servers (MT servers anyway) are "not trading" and so there are no single or double volumes - there's just a hole in the history.

 
ForexTools писал (а) >>

The transition is always on Sunday, when the trading servers (MT's anyway) are "not trading" and so there are no single or double volumes there - there's just a hole in the history.

>> I see, thank you.

And the first question? I don't need to know the local time. My clock is the trading server. Are the session opening times constant at this time?

The program has a restriction: if ( Hour()>t1 && Hout()<t2 ), then go ahead, otherwise close / sleep; roughly. Without any corrections.

Reason: