Last day of month

Please help with how to program an AFL to check a signal based on closing price of the last day of the month and take the trade on the 1st day of the next month at Open.

I am using the following but it is taking the signal based on 2nd last day of the month and trading on last day of the month.

LastDayOfMonth = IIf( (Month() == Ref( Month(), 1) AND (Month() != Ref( Month(), 2)) ), 1, 0);
TradeDay = LastDayOfMonth ;
SetTradeDelays( 1, 1, 1, 1);

It's helpful to use a simple plot for bugtesting.

I'm not sure if you know this but I'm going to mention it: your ref statement is looking into the future. In this particular case I do not see an issue in doing this, but in general you will want to use negative ref's so that you're looking back.

LastDayOfMonth = IIf(Month() != Ref(Month(), 1) , 1, 0);
TradeDay = LastDayOfMonth ;
SetTradeDelays( 1, 1, 1, 1);
1 Like

@techtrader2 you appear to be new to the forum, so welcome to the forum! But for future questions or posts please read the proper method of entering code in your post,

And when you run into difficulty with something you have coded, @Pinecone has helped illustrate one of many different methods of debugging your code and finding your mistakes. But here are some excellent suggestions,

And @Pinecone code can be simplified to

LastDayOfMonth  = Month() != Ref( Month(), 1);

i.e. no need to use the Immediate If statement.

Lastly there are many posts on the forum that have covered coding the "last trading day" of the month (assuming we are discussing daily bar data).

LastDayofMonth = Day() > Ref( Day(), 1 ); 
AlternateVersion = Month() != Ref( Month(), 1 );
isEndOfMonth    = TimeFrameExpand( 1, inMonthly, expandPoint  );

Or perhaps if you do not want to be looking forward,


Ha! I never knew that. Is there any computing speed difference in the two ways?

Array conditions returning true (1) / false (0) do not require IIF function to return one/zero. Things done twice.

Every additional code line / algorithm adds up to overall execution time. More work, more time.


Thanks @Pinecone this worked! Could you please advise should I do if I want to check for the same thing on the last day of the week i.e. on Friday candle closing

friday = DayOfWeek() == 5;

This is really simple stuff, you'd be better off reading documentation and learning what's going on in code if you ever want to do something significant with it.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.