Implementing Conditional Trading Days in Trading Strategies

01 Nov 2023
0 comment
0 attachment

This code snippet demonstrates how to implement a robustness tester for trading strategies based on conditional trading days, months, and years using the ProBuilder language. The purpose is to test the strategy under various temporal conditions to assess its performance consistency.

//Day Month and Year Robustness Tester
//By Vonasi
//20190901
//0 = all days
//1 = odd days only
//2 = even days only
//3 = odd months only
//4 = even months only
//5 = even years only
//6 = odd years only
//7 = odd days in odd months only
//8 = even days in even months only
//9 = even days in odd months only
//10 = odd days in even months only
//11 = even months in even years only
//12 = odd months in odd years only
//13 = even months in odd years only
//14 = odd months in even years only
//15 = odd days in odd months in odd years only
//16 = even days in even months in even years only
//17 = even days in odd months in odd years only
//18 = odd days in even months in even years only
//19 = odd days in even months in odd years only
//20 = even days in odd months in even years only
//21 = even days in even months in odd years only
//22 = odd days in odd months in even years only
oddday = opendayofweek mod 2 = 1
evenday = opendayofweek mod 2 = 0
oddmonth = openmonth mod 2 = 1
evenmonth = openmonth mod 2 = 0
oddyear = openyear mod 2 = 1
evenyear = openyear mod 2 = 0
tradeon = 0
if choice = 0 then tradeon = 1 endif
//odd days only
if choice = 1 and oddday then tradeon = 1 endif
//even days only
if choice = 2 and evenday then tradeon = 1 endif
//odd months only
if choice = 3 and oddmonth then tradeon = 1 endif
//even months only
if choice = 4 and evenmonth then tradeon = 1 endif
//even years only
if choice = 5 and evenyear then tradeon = 1 endif
//odd years only
if choice = 6 and oddyear then tradeon = 1 endif
//odd days in odd months only
if choice = 7 and oddday and oddmonth then tradeon = 1 endif
//even days in even months only
if choice = 8 and evenday and evenmonth then tradeon = 1 endif
//even days in odd months only
if choice = 9 and evenday and oddmonth then tradeon = 1 endif
//odd days in even months only
if choice = 10 and oddday and evenmonth then tradeon = 1 endif
//even months in even years only
if choice = 11 and evenmonth and evenyear then tradeon = 1 endif
//odd months in odd years only
if choice = 12 and oddmonth and oddyear then tradeon = 1 endif
//even months in odd years only
if choice = 13 and evenmonth and oddyear then tradeon = 1 endif
//odd months in even years only
if choice = 14 and oddmonth and evenyear then tradeon = 1 endif
//odd days in odd months in odd years only
if choice = 15 and oddday and oddmonth and oddyear then tradeon = 1 endif
//even days in even months in even years only
if choice = 16 and evenday and evenmonth and evenyear then tradeon = 1 endif
//even days in odd months in odd years only
if choice = 17 and evenday and oddmonth and oddyear then tradeon = 1 endif
//odd days in even months in even years only
if choice = 18 and oddday and evenmonth and evenyear then tradeon = 1 endif
//odd days in even months in odd years only
if choice = 19 and oddday and evenmonth and oddyear then tradeon = 1 endif
//even days in odd months in even years only
if choice = 20 and evenday and oddmonth and evenyear then tradeon = 1 endif
//even days in even months in odd years only
if choice = 21 and evenday and evenmonth and oddyear then tradeon = 1 endif
//odd days in odd months in even years only
if choice = 22 and oddday and oddmonth and evenyear then tradeon = 1 endif

Explanation of the Code:

  • The code starts by defining variables to determine whether the current day, month, or year is odd or even.
  • A variable tradeon is initialized to 0 (false). This variable will control whether a trade should be executed based on the specified conditions.
  • Multiple conditional statements check the value of choice (which is set during optimization) to determine under which conditions trades should be executed. For example, if choice is 1, trades will only be executed on odd days.
  • Each condition modifies the tradeon variable to 1 (true) if the current date matches the criteria set by the choice variable.
  • This setup allows the strategy to be tested under various combinations of day, month, and year conditions to evaluate its performance across different temporal segments.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/day-month-year-strategy-robustness-tester/#post-106072

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.
Vonasi Master
V-oyaging ON A S-mall I-ncome
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...