Support Board
Date/Time: Sat, 23 Nov 2024 14:50:33 +0000
Post From: Python for Sierra Chart
[2024-08-17 16:42:00] |
User150671 - Posts: 69 |
Okay. So I fucked up my code to convert time to and from SCID files. I just noticed today as I was trying to do some backtesting on things in different timezones, and realised it was always converting from SCID to NYC time. Fixed Code: import datetime as dt import numpy as np # takes a scid time and returns it as a dt.datetime def un_sierra_time(scidtime): sierra_epoch = dt.datetime(1899, 12, 30, tzinfo=dt.timezone.utc) dtg = sierra_epoch + dt.timedelta(microseconds=int(scidtime)) return dtg # takes a dt.datetime object and returns it as a np.uint64 for working with scid files def re_sierra_time(dtg): sierra_epoch = dt.datetime(1899, 12, 30, tzinfo=dt.timezone.utc) delta = dtg - sierra_epoch scidtime = np.uint64(delta.total_seconds()*1000000) return scidtime Explanation: As per the Intraday File Format at Intraday Data File Format: DateTime The SCID DateTime is: "...a 64-bit integer. It represents the number of microseconds since the SCDateTime epoch of December 30, 1899."
un_sierra_time is fairly straight forward: We take the Sierra Epoch Date and add the value of the SCID DateTime to get a Date Time Object using dt.timedelta re_sierra_time is also fairly straight forward, we just do the opposite: We take the sierra epoch and subtract the SCID DateTime from this to get a datetime timedelta object. That delta object stores total days, seconds, and microseconds, and if you call it with total.seconds() you'll get a float with the total microseconds as the fraction. We can then multiply that float by 1000000 and cast it into an int in order to store in or compare with an SCID time value (in this case I use a numpy uint64 as thats the format I am working with SCIDs with) Apologies if anyone stole my code and was fucked around by it! |