Implementing Custom Trading Days and Hours in ProBuilder

20 May 2017
0 comment
0 attachment

This code snippet demonstrates how to set custom trading days and hours using conditional statements in ProBuilder. It allows traders to specify which days of the week and what times they want their trading strategy to be active.


ONCE startT = 000000 //Starting TIME
ONCE endT = 240000 //Ending TIME
td0 = 0 AND OpenDayOfWeek = 0 AND OpenTime >= startT AND OpenTime <= endT //Sunday
td1 = 1 AND OpenDayOfWeek = 1 AND OpenTime >= startT AND OpenTime <= endT //Monday
td2 = 1 AND OpenDayOfWeek = 2 AND OpenTime >= startT AND OpenTime <= endT //Tuesday
td3 = 1 AND OpenDayOfWeek = 3 AND OpenTime >= startT AND OpenTime <= endT //Wednesday
td4 = 1 AND OpenDayOfWeek = 4 AND OpenTime >= startT AND OpenTime <= endT //Thursday
td5 = 1 AND OpenDayOfWeek = 5 AND OpenTime >= startT AND OpenTime <= endT //Friday
td6 = 0 AND OpenDayOfWeek = 6 AND OpenTime >= startT AND OpenTime <= endT //Saturday
td7 = 0 AND OpenDayOfWeek > 6 AND OpenTime >= startT AND OpenTime <= endT //unpredictable :)
td8 = 0 AND OpenDayOfWeek < 0 AND OpenTime >= startT AND OpenTime <= endT //very unpredictable :)
tdCond = td0 OR td1 OR td2 OR td3 OR td4 OR td5 OR td6 OR td7 OR td8
IF MyConditions AND Not OnMarket AND tdCond THEN
    . . 
ENDIF

Explanation of the Code:

  • The ONCE keyword is used to define variables that are set once and do not change throughout the execution of the script. Here, startT and endT are set to define the trading time window from midnight to the end of the day.
  • Variables td0 to td8 represent conditions for each day of the week (and two additional conditions for days outside the normal range, which are more hypothetical). Each variable checks if the current day and time fall within the specified ranges. A value of 1 enables trading on that day, while 0 disables it.
  • The tdCond variable combines all day conditions using the OR operator. This means if any of the day conditions are true, tdCond will be true.
  • The final IF statement checks multiple conditions: MyConditions (user-defined trading conditions), Not OnMarket (ensures that the trading system is not already in a trade), and tdCond (the day/time conditions). If all are true, the code inside the IF block will execute, allowing a trade to be placed or other actions to be taken.

This approach provides flexibility in defining active trading periods and can be easily adjusted to accommodate different trading strategies and market conditions.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/time-and-day-conditions/#post-134372

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
33
indicator
132
strategy
171

Recent Snippets

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) [...]
Calculating Break-Even Points in ProBuilder
strategy
This ProBuilder code snippet demonstrates how to calculate the break-even point for a trading position. The break-even [...]
Implementing Time and Day Restrictions in Trading Algorithms
strategy
This code snippet demonstrates how to set restrictions on trading activities based on specific days of the week and [...]
Implementing Partial Position Closure Based on Price Retracement in ProBuilder
strategy
This code snippet demonstrates how to partially close a trading position when the price retraces to a certain level in [...]
Logo Logo
Loading...