Programmed Daylight Savings Time Change Code Block
Forums › ProRealTime English forum › ProOrder support › Programmed Daylight Savings Time Change Code Block
- This topic has 14 replies, 3 voices, and was last updated 3 years ago by clasch.
-
-
03/22/2018 at 11:38 AM #66074
Hello coders I’m wondering if anyone has written some code to automatically identify when a daylight saving time change is going to happen for example based on the last Sunday of the March?
I was looking at the documentation and I couldn’t think of anything quick to use to work out the last Sunday of March.
The initial ideas I have are:
- a bunch of IF statements or just enough for a 1 year period.
- loop counter to count the number of Sunday for the month of March but this might vary year to year so I have to look into it further
Any other suggestion or premade solutions would be highly appreciated.
Thanks for your suggestions and help.
03/22/2018 at 12:37 PM #66081I think I have a start of a solution but I know this isn’t correct yet because I need to account for the days after Sunday in March and days before the last Friday in October:
1234567891011// UK summer time starts last sunday of marchsummerTimeStart = ( currentMonth >= 3 AND currentDayOfWeek = 0 AND DAY[0] > 25) OR (currentMonth >3 or CurrentMonth <10)summerTimeEnd = (currentMonth <= 10 AND currentDayOfWeek = 5 AND DAY[0] > 25)// UK winter time starts last sunday of octoberIf summerTimeStart AND summerTimeEnd THENsummerTime = 1ELSEwinterTime = 0ENDIFI will try to solve it over the weekend if nobody else has.
03/22/2018 at 1:30 PM #66086I use the following code snippet to close positions on friday nights. It also takes care of daylight saving. The attached snippet is for EUR/USD and has to be adapted to the underlying in question depending on where the asset trades.
123456789101112131415//--------------- daylight-saving corrections ------------------if currentmonth=3 and day>=15 thendlc=1elsif currentmonth=11 and day<8 thendlc=1elsedlc=0endiffridaynight=currentdayofweek=5 and hour>=22-dlcIf fridaynight and onmarket ThenSell at marketExitshort at marketEndIf03/22/2018 at 1:57 PM #6608703/26/2018 at 8:06 AM #66406Nicolas I have worked out the code for the last Friday of the Month (I think) but I’m not sure how to get the days before the last friday of the month = 10?
12345IF dayofweek >= 0 and month = 1 AND 31-day<7 OR dayofweek = 5 AND month = 10 AND day > 31-7 THENdst = 1elsedst = 0endif03/26/2018 at 8:34 AM #6640703/29/2018 at 4:17 AM #66644Nicolas I still can’t solve this – I’ve tried many combinations but no solution 🙁
Well I can solve this but its not pretty I have to use AND + OR statements to trade on the last 6 days to the last Friday but there must be a better or more elegant way.
Also, if you use Result it the graph doesn’t show a trade on the Sunday but if you use BUY and SELL for some reason it will execute a trade on Sunday in October?
1234567891011121314IF ( dayofweek >= 0 and month = 3 AND 31-day<7 ) OR (dayofweek = 5 AND month = 10 AND 31-day<7) OR ( month = 10 AND 31-day > 6 ) THENdst = 1elsedst = 0endifIF dst = 1 THENBUY 1 CONTRACT AT MARKETENDIFIF ONMARKET THENSELL AT MARKETENDIF04/01/2018 at 3:23 AM #66888I came up with a reasonable solution
1IF ( dayofweek >= 0 and month = 3 AND 31-day<7 ) OR ( month = 10 AND 31-day > 6 ) OR (dayofweek = 4 AND month = 10 AND day<31) OR (dayofweek = 3 AND month = 10 AND day+1<31) OR (dayofweek = 2 AND month = 10 AND day+2<31) OR (dayofweek = 1 AND month = 10 AND day+3<31) OR (dayofweek = 0 AND month = 10 AND day+4<31) OR (dayofweek = 5 AND month = 10 AND 31-day<7) THENI also remembered it’s possible to use currentdayofweek which should produce the same results and it does for most parts. However when I run a backtest for AUDUSD h1 22K for some reason a comparison of the 2 reports shows there’s difference in results. I compare the list of trades in a spreadsheet (here as a web page and as spreadsheet) and all the trades are the same they match but the statistics are not the same?
Then I increase to 100K bars and scanning through the trades visually I find an erroneous trades on:
- Sunday 27 October 2002 at 20:00
- Sunday 1 November 2003 at 20:00
- etc…
- and the good thing about this error is that it doesn trade at the start of the Sunday when the market opens but I guess its delaying the trade.
Also, when you look at the candles and the spreadsheet sometimes trades close every hour but other times they do not and they close on the following hour with 2 units closing/exiting.
Is there an error with the backtest engine?
04/03/2018 at 12:44 AM #66966Then I increase to 100K bars and scanning through the trades visually I find an erroneous trades on:Sunday 27 October 2002 at 20:00Sunday 1 November 2003 at 20:00etc…and the good thing about this error is that it doesn trade at the start of the Sunday when the market opens but I guess its delaying the trade.
On 2nd inspection with fresh eyes the actual problem is there are extra bars starting from: Sunday 27 October 2002 at 15:00?
04/27/2018 at 1:50 AM #69257Nicolas I think there’s a bug with PRT – I know you’re very busy but last time I submitted my time code related issue to PRT, I received no response.
This is what happened. PRT Timezone set to UTC 0
1234567891011121314151617181920212223242526272829303132333435// --------- UK DAY LIGHT SAVINGS MONTHS ---------------- //mar = month = 3 // MONTH STARToct = month = 10 // MONTH ENDIF ( dayofweek >= 0 and mar AND 31-day<7 ) OR ( month > mar AND month < oct ) OR ( oct AND 31-day > 6 ) OR (dayofweek = 4 AND oct AND day<31) OR (dayofweek = 3 AND oct AND day+1<31) OR (dayofweek = 2 AND oct AND day+2<31) OR (dayofweek = 1 AND oct AND day+3<31) OR (dayofweek = 0 AND oct AND day+4<31) OR (dayofweek = 5 AND oct AND 31-day<7) THEN// DAY LIGHT SAVINGS HOURSweekDayStart = 0 // SUNDAYweekTimeStart = 200000 + 30000 // FX opens 20:00 + add 3 hours to avoid poor spreadsweekDayEnd = 5 // FRIDAYweekTimeEnd = 210000 // FX closes 20:59else// NON DAY LIGHT SAVINGSweekDayStart = 0 // SUNDAYweekTimeStart = 210000 + 30000 // FX opens 21:00 + add 3 hours to avoid poor spreadsweekDayEnd = 5 // FRIDAYweekTimeEnd = 210000 // FX closes 20:59endifweekStart = (dayofweek = weekDayStart AND TIME <= weekTimeStart)weekEnd = (dayofweek = weekDayEnd AND TIME >= weekTimeEnd)IF weekStart OR weekEnd THENtradingHours = OFFELSEtradingHours = ONENDIF// --------- END KEY TIME INSTRUCTIONS ---------------- //The rest of my instructions a wrapped in the following condition if tradingHours = OFF then don’t open new positions or close positions.So for some reason the system decided to edit the positions by removing the limit orders UTC 0, Thursday, at: 23:00, and then UTC 0, Friday 00:00 reinstate them.I hope there’s an error somewhere in my code so that this can be resolved quickly but I’m struggling to see it.Thanks for your help.05/20/2018 at 10:05 AM #70922UPDATE: IG users if you trade FX and want to use TIME conditions please ensure you have the timezone settings set to the default local times zone setting for FX instruments.
If you don’t, then time settings will not work properly because even if you set the system to some other time zone and code for it eg. +10 it will still treat dayofweek/currentdayofweek as UTC: 0/+1
It would have been really good if someone at IG or someone from PRT could have provided this information to me/us but sadly no.
10/13/2021 at 3:46 PM #17959910/13/2021 at 4:09 PM #179610Only post in the language of the forum that you are posting in. For example English only in the English speaking forums and French only in the French speaking forums.
Thank you 🙂
10/13/2021 at 11:37 PM #17963410/14/2021 at 8:18 AM #179638 -
AuthorPosts
Find exclusive trading pro-tools on