# DaysOfMonth() question

Have you found an interesting application? Publish it in Code Base - millions of traders will appreciate that!
367
2013.10.27 18:27
`if(Month()==1 && DaysOfMonth() == 30 || DaysOfMonth() == 31) return(0);`
can I use DaysOfMonth() like above?
379
2013.10.27 18:34

Try:

`if (Month() == 1 && Day() >= 30) return (0);`
Moderator
17990
2013.10.27 18:55
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
14201
2013.10.27 19:11
```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
14201
2013.10.27 19:14
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;`
367
2013.10.27 19:17

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

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

367
2013.10.27 19:20

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);```
14201
2013.10.27 19:52
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 ) )`

379
2013.10.27 20:39
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```
6298
2013.10.27 21:56
```//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
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....

367
2013.10.28 07:31

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