Getting last n days of months in AFL

I am trying to implement a strategy which trades on last n trading day of the month.
I have gone through this thread but its hard to figure out how to to do what I want to do: First and Last trade days of the month

Does anyone have suggestions on how to implement this?

There's plenty of examples throughout this forum!

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

Buy = isFirstOfMonth /*AND your buy rules*/;
1 Like

Something like the following might be a start.

buycond=Month() AND Day()>=26;
mybuy=buycond ;
1 Like

I have to make trading decisions based on the last n'th day of the month.

So I am using the following code to figure that out:

isFirstOfMonth = Month() != Ref(Month(), -1);
isLeapYr = ( ( Year() % 4 == 0 ) AND( Year() % 100 != 0 ) ) OR( Year() % 400 == 0 );				

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;

if (Ref(is31month, 0))
	doi = 31;
if (Ref(is30month, 0))
	doi = 30;
if (Ref(is29month, 0))
	doi = 29
if (Ref(is28month, 0))
	doi = 28;

if ((Day() + n + 1) == doi);
	Buy = True;

I am getting the following error in the if expressions:

Error 6. Condition in IF, WHILE, FOR statements has to be Numeric or Boolean type. You can not use array here, please use [] (array subscript operator) to access array elements

I am using the Ref function, so I shouldn't get the error in my understanding. What am I doing wrong?

buycond=Month() AND Day()>=28;

So the above line will catch all the days in the listed months / days I believe whether it is a leap year or not. This line of code could be expanded to include specific months. Something like:

buycond=Month() == 1 or Month() == 3  AND Day()>=28;

Endless combinations to acquire the desired results.

1 Like

Your understanding is incorrect. Study Ref.


Thank you for the example.
As I understand from your code, buycond will be true for all days greater than 28. Can you tell me what the Month() in the condition does?
Month() returns the current month number so that condition will be always true...

@xterminator You need to study and understand the difference between the if statement and the iif function. It been discussed many times on this forum.

1 Like

Condition will be true for every month - 1 thru 12 and day greater than or equal to 28 . I was interpreting your posted code.

1 Like

Thanks @Anthony .. would it be possible to detect last Thursday of every month like your code ? Please advise

Here is a code snippet that seems to do it. Author unknown.

Note: This does not take into account holidays.

function Lastthursday()
 Daysinmonth=IIf(Month()==1 OR Month()==3 OR Month()==5 OR Month()==7 OR
Month()==8 OR Month()==10 OR Month()==12,31,30);
 Daysinmonthfeb=IIf(Year()%4 == 0 AND Year()%100!=0,29,28);
 returnvalue=IIf(Daysinmonthfinal-Day()<7 AND DayOfWeek()==4,1,0);
 return returnvalue;

Color=IIf(Lastthursday(),Coloryellow,Colorblack );
Plot( C, "Close", color,stylecandle |styleThick) ;

Title=writeIf(lastthursday(),"Thursday "+numtostr(Datetime(),formatDateTime),"");


Thank you @Anthony .Appreciate your help