Handling Date-Based Trading Restrictions in ProBuilder

10 May 2019
0 comment
0 attachment

This ProBuilder code snippet is designed to prevent trading on the last working day of the month. It calculates the end of the month and sets a flag to disable trading on that day.


If OpenMonth <> OpenMonth[1] Then
    OM = OpenMonth
    OD = OpenDay
    ODW = OpenDayOfWeek
    OY = OpenYear
    LastDay = 31
    If OM = 4 or OM = 6 or OM = 9 or OM = 11 Then 
LastDay = 30 
Endif
    If OM = 2 Then
        LastDay = 28
        If OY mod 4 = 0 Then
            If OY mod 100 = 0 Then
                If OY mod 400 = 0 Then 
                 LastDay = 29 
                Endif
            Else
                LastDay = 29
            Endif
        Endif
    Endif
    EOM = OD //EOM=End Of Month
    For i = OD + 1 To LastDay
        ODW = ODW + 1
        If ODW <= 5 Then EOM = i
        Elsif ODW = 7 Then ODW = 0
    Next
Endif
// TradeON = 1
If Day = EOM Then 
TradeON = 0 
Endif
// If MyLongConditions And TradeON And Not OnMarket Then Buy 1 contract at Market Endif

Explanation of the code:

  • The code starts by checking if the month has changed (OpenMonth <> OpenMonth[1]). This is crucial to reset calculations at the start of each new month.
  • Variables for the current month (OM), day (OD), day of the week (ODW), and year (OY) are initialized.
  • The last day of the month is initially set to 31, but adjusted based on the month. February has special checks for leap years to determine if it has 28 or 29 days.
  • The loop from the current day to the last day of the month calculates the last working day, skipping weekends (assuming Saturday and Sunday as non-working days).
  • The variable EOM (End Of Month) is set to the last working day calculated.
  • A flag TradeON is used to enable or disable trading. It is set to 0 (disabled) if the current day is the last working day of the month.
  • The final conditional statement checks if trading conditions are met and if trading is enabled, then a trade is executed.

This snippet is useful for traders using automated systems who wish to avoid entering new positions on the last working day of the month, potentially due to increased volatility or accounting reasons.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/codes-for-not-opening-trades-at-month-end/#post-175920

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.
robertogozzi Master
Roberto https://www.ots-onlinetradingsoftware.com
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...