Download MetaTrader 5

DaysOfMonth() question

To add comments, please log in or register
fulltilt
372
fulltilt  
if(Month()==1 && DaysOfMonth() == 30 || DaysOfMonth() == 31) return(0);
can I use DaysOfMonth() like above?
GreenMoney
379
GreenMoney  

Try:

if (Month() == 1 && Day() >= 30) return (0);
Simon Gniadkowski
Moderator
18003
Simon Gniadkowski  
fulltilt:
can I use DaysOfMonth() like above?
Please can you show the coding for your DaysOfMonth() function.

It's not a standard mql4 date & time function
whroeder1
14781
whroeder1  
bool  IsLeapYear(int year){   return(year%4==0 && year%100!=0 && year%400==0); }
int   DaysOfMonth(datetime when=0){
   if(when == 0)  when = TimeCurrent();
                        // Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
   static int     dpm[] =  {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
   int      mo = TimeMonth(when);
   if(mo != 2) return(dpm[mo-1]);
               return(dpm[mo-1] + IsLeapYear(TimeYear(when)));           // Feb
}
From my code
whroeder1
14781
whroeder1  
fulltilt: can I use DaysOfMonth() like above?
You can (if month==1 then daysOfMonth had better be 31.) But what are you trying to do? Not trade on the last day?
if( Day() == DaysOfMonth() ) return;
fulltilt
372
fulltilt  

thank you guys, I will try to add this function ;-)

but also Day() will not work or am I wrong?

fulltilt
372
fulltilt  

yes, it should not trade the last 2 day/s fex.

        if(!TradeMonthEnd && Month()==1 && DaysOfMonth() == 30 || DaysOfMonth() == 31) return(0);
        if(!TradeMonthEnd && Month()==2 && DayOfWeek() == 27 || DayOfWeek() == 28) return(0);
        if(!TradeMonthEnd && Month()==3 && DayOfWeek() == 30 || DayOfWeek() == 31) return(0);
        if(!TradeMonthEnd && Month()==4 && DayOfWeek() == 29 || DayOfWeek() == 30) return(0);
        if(!TradeMonthEnd && Month()==5 && DayOfWeek() == 30 || DayOfWeek() == 31) return(0);
        if(!TradeMonthEnd && Month()==6 && DayOfWeek() == 29 || DayOfWeek() == 30) return(0);
        if(!TradeMonthEnd && Month()==7 && DayOfWeek() == 30 || DayOfWeek() == 31) return(0);
        if(!TradeMonthEnd && Month()==8 && DayOfWeek() == 30 || DayOfWeek() == 31) return(0);
        if(!TradeMonthEnd && Month()==9 && DayOfWeek() == 29 || DayOfWeek() == 30) return(0);
        if(!TradeMonthEnd && Month()==10 && DayOfWeek() == 30 || DayOfWeek() == 31) return(0);
        if(!TradeMonthEnd && Month()==11 && DayOfWeek() == 29 || DayOfWeek() == 30) return(0);
        if(!TradeMonthEnd && Month()==12 && DayOfWeek() == 30 || DayOfWeek() == 31) return(0);
whroeder1
14781
whroeder1  
fulltilt: yes, it should not trade the last 2 day/s fex.
if( DaysOfMonth() - Day() < 2 ) return;
  1. Your code doesn't handle Feb 29
  2. DayOfWeek() returns 0=Sunday .. 6=Saturday. I will NEVER equal 28, 29 or 30
  3. if(!TradeMonthEnd && Month()==12 && DayOfWeek() == 30 || DayOfWeek() == 31)
    Always parentheses your code. Do you know the default precedence? What you wrote means which of the following and which did you mean?
    if( ( !TradeMonthEnd && Month()==12 && DayOfWeek() == 30 ) || DayOfWeek() == 31)
    if( !TradeMonthEnd && Month()==12 && ( DayOfWeek() == 30 || DayOfWeek() == 31 ) )

GreenMoney
379
GreenMoney  
fulltilt:

yes, it should not trade the last 2 day/s fex.

What about the following code (not compiled or tested):

int MonthsOfYear[12][2] = {1,31, 2,28, 3,31, 4,30, 5,31, 6,30, 7,31, 8,31, 9,30, 10,31, 11,30, 12,31};
if (Year() % 4 == 0 && Year() % 100 != 0 && Year() % 400 == 0)
   MonthsOfYear[1][1] = 29;  // adjust for leap year
if (MonthsOfYear[Month()-1][1] - Day() < 2)
   return (0);  // no trading on the last two days of the month
Tjipke de Vries
6325
Tjipke de Vries  
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool is_leap_year(int year1)
{
 
  if ((MathMod(year1,100)==0) && (MathMod(year1,400)==0))
    return(true);
  else if ((MathMod(year1,100)!=0) && (MathMod(year1,4)==0))  
    return(true);
  else 
    return (false); 
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int n_days(int year1,int month1)
{
  int ndays1;
  if (month1==1)
    ndays1=31;
  else if(month1==2)
  {
    if (is_leap_year(year1))
      ndays1=29;      
    else
      ndays1=28;  
  }    
  else if(month1==3)
    ndays1=31;  
  else if(month1==4)
    ndays1=30;  
  else if(month1==5)//mai
    ndays1=31;  
  else if(month1==6)//iun          
    ndays1=30;  
  else if(month1==7)//iul          
    ndays1=31;  
  else if(month1==8)//aug          
    ndays1=31;  
  else if(month1==9)//sep          
    ndays1=30;  
  else if(month1==10)//oct          
    ndays1=31;  
  else if(month1==11)//nov          
    ndays1=30;  
  else if(month1==12)          
    ndays1=31;  
  
  return(ndays1);

}

if (n_days(Year(),Month()) - Day() < 2)return(0);  

this code makes it only not trading last two days of month

but it should not trade the last 2 day/s fex.

if the last 2 days are sunday or saturday without trading then it might be we have to check also last day of month if it is a tradingday or not

then with differenttimezones of broker ..... it becomes a little difficult how to solve....

fulltilt
372
fulltilt  

thanks for the nice explanation ;-)

but what about the TimeDay() function, i found this ...

string localDate=TimeToStr(TimeLocal(),TIME_DATE);
if ( Month()==12 && localDate == 30 ) || localDate == 31)) return(0);
string localDate=TimeToStr(TimeCurrent(),TIME_DATE);
if ( Month()==12 && localDate == 30 ) || localDate == 31)) return(0);

would it work with ex. above ?

12
To add comments, please log in or register