EBParticipant
Senior
Hi,
Could somebody provide me some code how to easily avoid trading during bank holidays?
Thank you in advance!
If it is a bank holiday and the market is closed then no candles are produced and you can not trade a candle that does not exist! Try to find a candle for a Christmas day to prove it to yourself.
EBParticipant
Senior
Yes I know, but for instance if there is a bank holiday in US, the DAX may still be running. That may have an effect on the DAX once the US starts to open again the next day.
Beyond noting the date of each bank holiday that you want to avoid and then including it in a IF THEN condition within or surrounding your strategy I can not think of another way to do what you want. Creating a back test to prove whether this is worth doing would be quite labour intensive.
EBParticipant
Senior
Can you give me an example of such code?
date1 = 20180501
date2 = 20181225
if opendate <> date1 and opendate <> date2 then
(your strategy)
endif
I guess that if there is a holiday that is on exactly the same date every year then you could do this:
fixeddate1 = 0105 //1st May
date1 = (openyear * 10000) + fixeddate1
if opendate <> date1 then
(your strategy)
endif
You could also I guess code something to work out if the fixed date falls on a weekend and then move it to the Monday but I will leave that one to you as I’m not sure that the effort is worth the benefit. 🙂
You can find the day of the week number with this code snippet:
//find the day number with a date
myYear=Year
myMonth=Month
myDay=Day
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
return d
Replace myYear,myMonth and myDay with YYYY, MM, DD number format. In this example it basically returns the day of the week of the current day.
Isn’t that what (OPEN)DAYOFWEEK does?
@robertogozzi
Yes, but how do you know what opendayofweek was the 12th September 2002? This is the purpose of this snippet.
Isn’t that what (OPEN)DAYOFWEEK does?
Yes – but it isn’t such a good party trick. Memorise the above calculation and amaze your friends at every party by telling them what day of the week any date in history was!
What use is knowing what day of the week was July 4th (for Usa) or June 2nd (for Italy) or Christmas?
It was a Holiday for sure, this year, last year as well as many years ago!
So, once you hard code a holiday in your strategy, that’s all
If Month = 7 and Day = 4 then //July 4th
// it’s holiday, no matter if monday or sunday
Endif
In some countries, mostly christian/latin countries, Easter is a holiday AND it always occur on a sunday. In these countries (Italy, Spain, France…) the day following Easter (Monday) is a holiday, but since Easter is a mobile holiday you have to calculate it to know the following day will be a holiday. To calculate Easter, you can find formulas over the internet.
That’s what I meant, any calculation is necessary only for mobile holidays.
@nicolas thanks to that snippet I know I was born on a Sunday… many… many seasons ago!
In the UK bank holidays are always on a Monday. So for example there is always a bank holiday on the first Monday in May. In 2019 this will be on the 6th May. So if the OP wants to avoid trading on those days in other markets that are still open then he will need to calculate the first Monday in May every year. I think there are four bank holidays like this each year.
Easter in Greece is rarely the same as Easter in other countries as it is calculated using a different calender – but then I guess the Greek Easter will have little effect on markets. 🙂
[Old but just adding another way, if anyone interested]
The other way would be download the “CSV” of dates & times of holidays from any public government database, this would already be formatted in the tidy data style that aligns with PRT date and time formats, so just need to copy+paste it into your strategies code [For PRT people they have API’s for updating in real-time, if ever consider implementing holidays as function within PRT].
Most governments provide these on some public database site, for example data.gov.au for Australia would show all the full and half-day trading holidays for the upcoming years.
And regarding this using the earlier methods:
You could also I guess code something to work out if the fixed date falls on a weekend and then move it to the Monday
I assume something like this would work to move the holiday to the Monday or Friday:
fixeddate1 = 0105 //1st May
date1 = (openyear * 10000) + fixeddate1
//find the day number with a date
myYear=OpenYear
myMonth=OpenMonth
myDay=OPenDay
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
// Holiday Dates:
fixeddate1 = 0105 //1st May
date1 = (openyear * 10000) + fixeddate1
IF d >= 1 AND d<= 5 THEN
Holiday = date1
Elsif d < 1 THEN
Holiday = date1+1
Elsif d > 5 THEN
Holiday = date1-1
ENDIF
if opendate <> Holiday then
(your strategy)
endif