Determining the Last Trading Day of the Current Month in ProBuilder

19 May 2018
0 comment
0 attachment

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:

  • Leap Year Calculation: The code first checks if the current year is a leap year to determine the number of days in February.
  • Month and Day Calculation: Based on the month, the code calls a helper function to find the last day of that month considering the number of days each month typically has. For February, it uses the previously determined day count based on whether it’s a leap year.
  • Weekend Adjustment: The helper function “#get_lastdayofmonth” adjusts the last day if it falls on a weekend (Saturday or Sunday). This is crucial for trading strategies as markets are typically closed on weekends.

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.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/coding-help/#post-46525

Visit Link
What is a Snippet? A snippet is a small, reusable chunk of code designed to solve specific tasks quickly. Think of it as a shortcut that helps you achieve your coding goals without reinventing the wheel. How to Use: Simply copy the snippet and paste it into your project where needed. Don't forget to tweak it to fit your context. Snippets are not just time-savers; they're also learning tools to help you become a more efficient coder.
Nicolas Master
I created ProRealCode because I believe in the power of shared knowledge. I spend my time coding new tools and helping members solve complex problems. If you are stuck on a code or need a fresh perspective on a strategy, I am always willing to help. Welcome to the community!
Author’s Profile

Comments

Search Snippets

Showing some results...
Sorry, no result found!

Snippets Categories

global
35
indicator
133
strategy
171

Recent Snippets

How to Create a Simple MTF Trend Dashboard with EMA and SMA
indicator
This indicator builds a compact multi-timeframe (MTF) dashboard that shows whether price is trading above or below a [...]
How to Display Per-Bar Volume Accumulation in Real Time (Intrabar Updates)
global
This snippet tracks and displays the current bar’s accumulated volume while the bar is still forming, instead of only [...]
Ticks Counter: Count Tick Updates Per Bar on Tick or Time Charts
global
This snippet counts how many tick updates have occurred for the current bar by incrementing a per-bar counter on each [...]
How to Build a Step-Based Trailing Stop That Moves to Break-Even First
strategy
This snippet implements a step trailing stop that advances in fixed increments once price reaches predefined profit [...]
Utilizing Arrays to Track and Compare Indicator Values Within the Same Bar in ProBuilder
indicator
This ProBuilder code snippet demonstrates how to use arrays to compare the values of an indicator (RSI in this case) [...]
Logo Logo
Loading...