This ProBuilder code snippet is designed to determine the last trading day of the current month, which can be particularly useful for trading strategies that require execution around month-end. The code consists of two parts: a main indicator and a helper function.
// Main Indicator: Calculate the last trading day of the current month
// Check if the year is a leap year
If Year Mod 4 = 0 And (Year Mod 100 <> 0 Or Year Mod 400 = 0) Then
IsBisextil = 1
Else
IsBisextil = 0
EndIf
// Define the number of days in February based on whether it is a leap year
if IsBisextil then
feb=29
else
feb=28
endif
// Determine the last day of the current month
if month=1 or month=3 or month=5 or month=7 or month=10 or month=12 then
lastdayofmonth = call "#get_lastdayofmonth"[year,month,31]
elsif month=4 or month=6 or month=8 or month=9 or month=11 then
lastdayofmonth = call "#get_lastdayofmonth"[year,month,30]
elsif month=2 then
lastdayofmonth = call "#get_lastdayofmonth"[year,month,feb]
endif
return lastdayofmonth
// Helper Function: Adjust the last day of the month if it falls on a weekend
if myMonth >= 3 then
D = (((23*myMonth)/9) + myDay + 4 + myYear + (myYear/4) - (myYear/100) + (myYear/400) - 2) mod 7
else
z = myYear - 1
D = (((23*myMonth)/9) + myDay + 4 + myYear + (z/4) - (z/100) + (z/400) ) mod 7
endif
lastday=myDay
if D=6 then
lastday=myDay-1
elsif D=0 then
lastday=myDay-2
endif
return lastday
The code is divided into two main sections:
This snippet is useful for implementing trading strategies that need to execute trades at the end of the month, taking into account weekends when markets are closed.
Check out this related content for more information:
https://www.prorealcode.com/topic/coding-help/#post-46525
Visit Link