Detect last trading day of the month

 

Hello,

I have an EA that I am programming to utilize one of several different order close conditions. The conditions are: daily, weekly, and equity based (if equity gets too high or too low). These are all programmed and working successfully. I am also needing to add a monthly close feature that detects the last trading day of the month so the EA will close all the open positions before the close of that day. I didn't know if there was an easier way to detect the last trading day of each month other than manually writing a boat-load of code to tell the EA the number of days in each month, leap-year information, etc...

Anybody done this before or have any ideas? Thanks.

 
How about closing them the first day of the month as a compromise. Would that hurt the system?
 
ubzen:
How about closing them the first day of the month as a compromise. Would that hurt the system?

Thanks for the reply. No, it wouldn't hurt the system to do it that way either. The problems that I could foresee were: detecting the end day of the month and also detecting if the last day of the month fell on a day when the market's were not open (friday after 5 EST to Sunday at 4-6pm EST). Is there a way to detect the first day of the month and if the first day of the month is a valid trading day?
 
bstocking:

Hello,

I have an EA that I am programming to utilize one of several different order close conditions. The conditions are: daily, weekly, and equity based (if equity gets too high or too low). These are all programmed and working successfully. I am also needing to add a monthly close feature that detects the last trading day of the month so the EA will close all the open positions before the close of that day. I didn't know if there was an easier way to detect the last trading day of each month other than manually writing a boat-load of code to tell the EA the number of days in each month, leap-year information, etc...

Anybody done this before or have any ideas? Thanks.

Get the current trading day. Then add one to get tomorrow's day. (you need to go in loop here). If its a trading day (identify by DayOfWeek( )) & is in current month then do nothing(b'coz today is not the last trading day). If its not a trading-day but same month, then keep on adding one to current day till you get trading day in current month....and so on..

Hope you got the idea....let me know...

 
        if (TimeMonth(Time[0])!=TimeMonth(Time[1]) ) //close trades as new month 
 
bstocking: Thanks for the reply. No, it wouldn't hurt the system to do it that way either. The problems that I could foresee were: detecting the end day of the month and also detecting if the last day of the month fell on a day when the market's were not open (friday after 5 EST to Sunday at 4-6pm EST). Is there a way to detect the first day of the month and if the first day of the month is a valid trading day?

If you don't want to have the position opened over the weekend. Then expand on Ickyrus code like below. (Not-Tested).

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void start(){
    int TC=TimeCurrent();
    if( TimeMonth(TC)!=TimeMonth(TC+1) ){/*CloseAll*/}
    if( TimeDayOfWeek(TC)==5 ){bool Friday=true;}
    int _48hr_1Sec_=60*60*24*2+1;//Seconds*Minutes*Hours*Days+(1s)
    if(Friday){
        if( TimeMonth(TC)!=TimeMonth(TC+_48hr_1Sec_) ){
            /*CloseAll*/
        }
    }
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


 
bstocking:

Thanks for the reply. No, it wouldn't hurt the system to do it that way either. The problems that I could foresee were: detecting the end day of the month and also detecting if the last day of the month fell on a day when the market's were not open (friday after 5 EST to Sunday at 4-6pm EST). Is there a way to detect the first day of the month and if the first day of the month is a valid trading day?
  1. First day of the month is easy
    datetime now.srv = TimeCurrent();
    int      now.dom = TimeDay(now.srv);
    if (now.dom == 1) // First day of the month
    If the market is closed, you won't get a tick and the code isn't running. If dom < previous.dom it's the first trading day of the month.

  2. Last day of the month slightly harder
    if (now.dom == DaysOfMonth(now.srv)) // It's the last
    :
    bool IsLeapYear(int year){  return(year%4==0 && year%100!=0 && year%400==0); }
    int  DaysOfMonth(datetime when){
                        // 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 iDpm = TimeMonth(when) - 1;
        if (iDpm != 1)  return(dpm[iDpm]);
                        return(dpm[iDpm] + IsLeapYear(TimeYear(when)));
    }
    

  3. The hard one is when the market closes on Friday(UTC.) See my code. Testing for Friday is easy
    int now.dow = TimeDayOfWeek(now.utc); #define FRIDAY 5
    if (now.dow == FRIDAY) // It's Friday.
  4. friday after 5 EST to Sunday at 4-6pm EST
    Is wrong. Market closes 4 pm ET (4 pm EDT or 4 pm EST) and opens 6 pm ET. See my code
 

Thank you all so much for the quick responses. These examples help out a ton. I'll begin implementing and let you know how it turns out. Thanks again!

 

You can also try using OrderOpenTime(). Ex.

if (TimeMonth(OrderOpenTime())!=TimeMonth(TimeCurrent()) ) {CloseOrder();}

This could be helpful if the Expert was not Active on Friday_Nite to close the order; Then on Monday is a different Month. IMHO- time can be very tricky stuff to program. When you start adding Holidays, Local vs BrokerTime, and whatever else it usually becomes a mess. I'd take the easy way out and not try to bullet-proof it. Cross that bridge when you get there.

 
William Roeder:
  1. First day of the month is easy If the market is closed, you won't get a tick and the code isn't running. If dom < previous.dom it's the first trading day of the month.

  2. Last day of the month slightly harder
  3. The hard one is when the market closes on Friday(UTC.) See my code. Testing for Friday is easy
  4. Is wrong. Market closes 4 pm ET (4 pm EDT or 4 pm EST) and opens 6 pm ET. See my code

Thank you, all, and especially William. This has just helped me a lot.

One slight improvement was necessary in the IsLeapYear function, however, to get it (meaning "every 4 years, but not every 100 years, then again every 400 years") 100% correct:

bool IsLeapYear(int year){  return(year%4==0 && (year%100!=0 || year%400==0)); }

Hope this helps, and thanks again a million!

 
Ubzen #:
How about closing them the first day of the month as a compromise. Would that hurt the system?

Thank you Ubzen, I was looking for a solution to the OP question and you gave me a quick logical one. Close on open of the next month.

Us coders need to think what is the Goal, the Goal here is to run our EAs in Strategy Tester, to get an accurate estimation of past results. Not to squeeze every pip out of the backtest.

If we were running our EA Live, we would simply manually Close all trades last hour of the Month.