First and Last trade days of the month

Hi,

Can someone please tell me how I can check to see if it’s the first trading day of the month and the last trading day of the month.

There are multiple ways to do this. Here’s one:

isFirstOfMonth = Month() != Ref(Month(),-1);

And here’s another:

isLastOfMonth = TimeframeExpand(1, inMonthly, expandPoint);
12 Likes

Thank you for your answer.

Just to be clear, would this give me a signal for the first trade-able day of a month and a signal for the last trade-able day of a month?

The resulting arrays will be true on the first and last day of each month that the current symbol traded. If you use Pad & Align to some well-known, reliable symbol (I use $SPX), then yes, it will be the first and last tradable day of each month for any security. However, you shouldn’t just take my word for it; write a simple Exploration and try it out for yourself. Make sure you understand why these work, and under what conditions they may not work as expected. For example, the isLastOfMonth logic given here will always tell you that the most recent data bar in your database is the end of a month, no matter which day it really is.

2 Likes

Thank you for the additional information and guidance, it’s much appreciated.

My code below was designed to address the issue of identifying the last trade day of the month accurately when you are considering "today" for a signal. (In other words, there is no future bar to yet reference and you need to anticipate whether tomorrow will be a new month when making your trading decision today.) It appears to work. I am sure someone else could write it more efficiently. I hope some of you find it helpful.

//This code allows for the current day to appropriately be recognized as the last trade day of the month
///The dates look back as far as 1950 and apply to NYSE only. (Non-US markets may have other dates / holidays to consider.)

function LastTradeDayOfMonth()
{
 ////Is it a leap year where Feb will have 29 days?  This line of code is from @beppe in this post: https://forum.amibroker.com/t/loop-between-specific-dates/3120/6 
  isLeapYr = ( ( Year() % 4 == 0 ) AND( Year() % 100 != 0 ) ) OR( Year() % 400 == 0 );				

/////Establish the number of days this month			 
  is31month = Month() == 1 OR Month() == 3 OR Month() == 5 OR Month() == 7 OR Month() == 8 OR Month() == 10 OR Month() == 12;
  is30month = Month() == 4 OR Month() == 6 OR Month() == 9 OR Month() == 11;
  is29month = Month() == 2 AND isLeapYr;
  is28Month = Month() == 2 AND !isLeapYr;
 
 ///Is today the last weekday of the month (also includes a Memorial Day check for May)  
  eom31 = (is31month AND (Day() == 31 OR (DayOfWeek() == 5 AND (Day() == 30 OR Day() == 29)))) OR (Month() == 5 AND Day() == 28 AND (DayOfWeek() == 5));
  eom30 = is30month AND (Day() == 30 OR (DayOfWeek() == 5 AND (Day() == 29 OR Day() == 28)));
  eom29 = is29month AND (Day() == 29 OR (DayOfWeek() == 5 AND (Day() == 28 OR Day() == 27)));
  eom28 = is28month AND (Day() == 28 OR (DayOfWeek() == 5 AND (Day() == 27 OR Day() == 26)));

///Certain Months have ended "early" - like if Easter came near the end of March. A natural disaster or Presidential funeral could also cause the market to close, so this
///section will need to be updated if a month ends prematurely.
 otherEOM = DateNum() == 520529 OR DateNum() == 560329 OR DateNum() == 610330 OR DateNum() == 640528 OR DateNum() == 680730 OR DateNum() == 690328 OR DateNum() == 690529 OR 
			DateNum() == 580529 OR DateNum() == 720330 OR DateNum() == 1020328 OR DateNum() == 910328 OR DateNum() == 1130328 OR DateNum() == 1180329;
  
	
	LTDOM = eom31 OR eom30 OR eom29 OR eom28 OR otherEOM;

  return LTDOM;
}
8 Likes

Years after your post, this saved me a lot of time - much appreciated.