Almost every trader loves selecting trading hours and trading days.
The most common way is to use:
TimeCond = OpenTime >= HHMMSS and OpenTime <= HHMMSS
DayCond = OpenDayOfWeek >= 1 AND OpenDayOfWeek <= 5
IF MyConditions AND Not OnMarket AND TimeCond AND DayCond THEN
.
.
ENDIF
Selecting different days and custom different hours each day is a bit longer, I suggest:
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
so you can select DAYS by simply setting 0 or 1 after “=”, as you can select custom hours by changing starting and ending hours, such as starting later on Monday morning or Ending earlier on Friday night or not trading at all on Wednesday.
You may want to use TIME instead of OPENTIME or DAYOFWEEK instead of OPENDAYOFWEEK as best suits your needs.
You can use tdCond to (not) enter a trade:
IF MyConditions AND Not OnMarket AND tdCond THEN
.
.
ENDIF
Link to above added as Log 220 to here …
Snippet Link Library
Thanks for the Time/day codes robertogozzi!
I would love to hear how you guys and gals reason regarding Time and Date. Do you let the algos run 24/7 or have some time restrictions?
I let them run 24/7 (I am not a scalper).
Some strategies of mine based on a 4h+ TF may stay open even a 2-3 weeks. On shorter TF’s they normally close quite soon, the same day or within a couple of days at the latest.
Well… tht was a bit off topic. Yes, I always select custom operating time ranges (but once trades are opened I let them run).
robertogozzi, so you have a time frame where you allow entering a trade, say for example between 9.00 – 18.00. Rest of the time is not allowed. But when in a trade you allow it to run 24/7…did I understand you correct?
If so, is the time frame because of potential difference in spreads or different market behaviours? Or maby both?
Thanks again for all nice codes and support!
I mainly trade DAX, so spread is higher (even 6 or more pips) and Volatility is lower after 22 (but I usually stop trading at 18/19) till 9 the next morning.
Hi Roberto,
What is the most accurate and effective code of referencing the:
- 8am bar of the current day AND;
- The 16:15pm bar of the previous day AND;
- The 8am bar of the previous day
Irrespective of weekends (i.e. if Tuesday it references Tuesday and Monday and if Monday it references Monday and Friday)
There’s no way to access those bars with direct instructions, you need to save those BarIndex’s in variables as they show.
Each new day each bar becomes yesterday’s bar:
If IntraDayBarIndex = 0 Then
Yesterday8 = Today8
Today8 = 0
Yesterday1615 = Today1615
Today1615 = 0
Endif
If OpenTime = 080000 Then
Today8 = BarIndex
Endif
//
If OpenTime = 161500 Then
Today1615 = BarIndex
Endif
To make it work only Mon to Fri:
If OpenDayOfWeek >= 1 and OpenDayOfWeek <= 5 Then
If IntraDayBarIndex = 0 Then
Yesterday8 = Today8
Today8 = 0
Yesterday1615 = Today1615
Today1615 = 0
Endif
If OpenTime = 080000 Then
Today8 = BarIndex
Endif
//
If OpenTime = 161500 Then
Today1615 = BarIndex
Endif
Endif