Count Open Market Day+Holiday bank EA

 

Hi

I have 2 question:

1-how can I write code in order to go shift bar(like iBarShift) to for example 100 working day ago(count 100 market is on day)??

2-I want to design a EA rely on my strategy,Can I insert a code that tell to mt4 if that day was Bank Holiday Not Open any order???(this link maybe to be useful :http://www.forexfactory.com/showthread.php?t=326551)

 
ahmadrezaahmad:

Hi

I have 2 question:

1-how can I write code in order to go shift bar(like iBarShift) to for example 100 working day ago(count 100 market is on day)??

2-I want to design a EA rely on my strategy,Can I insert a code that tell to mt4 if that day was Bank Holiday Not Open any order???(this link maybe to be useful :http://www.forexfactory.com/showthread.php?t=326551)

For #1:

First, lets make a couple of assumptions: (a) your broker's server is configured to GMT+2 timezone, which means you don't have bars on Sundays; and (b) when the market is closed, your broker is closed--meaning that if no ticks were received by the Terminal for a whole day, your broker wasn't open.

Here is some code that would determine the 100th day in the past that your broker was open:

int NumberOfDays = 100;
datetime Today = StrToTime(StringConcatenate(Year(), ".", Month(), ".", Day()));
   
Print (NumberOfDays, " days in the past (including today) is: ", TimeToStr(iTime(Symbol(), PERIOD_D1, NumberOfDays-1)), " to ", TimeToStr(Today));

When using the "i" functions, it is best to test for a 4066 error and, if present, retry after a few seconds.

For #2:

The short answer is yes. You can either code the bank holidays by hand or you can probably (I say "probably" because I've not attempted this) have the EA gather that information by checking a website or other external informational source. Once you have that information, then you can instruct the EA not to place orders on a bank holiday, even if the market is open.

 

#1:

my broker is exness and the mt4 time exactly same with gmt,mt4 server dosnt show any candle from friday 21:00 to sunday 21:00 pm!

screen shot

Dose your code count not working day(like saturady an sunday and new year holiday,...) or not count?

(I want when choose 100 day ago,not count not working day,for example if in last 110 day,10 day is not working day,mt go to 110 day ago when I go insert 100 day ago!!)

 
ahmadrezaahmad:

my broker is exness and the mt4 time exactly same with gmt,mt4 server dosnt show any candle from friday 21:00 to sunday 21:00 pm!

Dose your code count not working day(like saturady an sunday and new year holiday,...) or not count?

(I want when choose 100 day ago,not count not working day,for example if in last 110 day,10 day is not working day,mt go to 110 day ago when I go insert 100 day ago!!)

Ok...so the question becomes: since you have a few bars that occur on sundays, do you want to count sundays as a working day? The following code is an example where sundays (and saturdays) are not counted as workdays.

int FindShiftFor_xPreviousDay(int xDayToFind) {
   int WorkDayCount = 0, index = 0, dow = 0;
   int number_of_error_iterations = 0;  // to prevent infinite loop
        
   GetLastError(); // clear error flag
   while (number_of_error_iterations < 40) {
      // determine day-of-week
      dow = TimeDayOfWeek(iTime(Symbol(), PERIOD_D1, index));
                
      // error handling
      int error_code = GetLastError();
      if (error_code == 4066) 
         // if ERR_HISTORY_WILL_UPDATED, sleep for half second, reset error code, and continue 
         { Sleep(500); error_code = 0; number_of_error_iterations++; continue; }
      else 
         // if error but not 4066, exit function 
         if (error_code > 0) return (-1);
                
      // only count weekdays, not saturday or sunday
      switch (dow) {
         case 0:
         case 6:
            break;
         default:
            WorkDayCount++;
      }
                
      // if xDay found, return the shift for xDay; otherwise continue
      if (WorkDayCount == xDayToFind)
         return (index);
      else
         { index++; continue; }
   }
}

int start() {
   Print ("100th Day: ", TimeToStr(iTime(Symbol(), PERIOD_D1, FindShiftFor_xPreviousDay(100))));
   return(0);
}

Obviously, the above example code assumes that your broker isn't open during a holiday.