How convert data feed's (live) timezone?
-
My data feed time in UNIX time 1588157340000 in milliseconds.
Using this site - https://www.epochconverter.com/ I see that this is GMT timezone. How convert to US/Eastern? It's possible?timezone = pytz.timezone("US/Eastern") cerebro.run()
This code change timezone for logs, but not for data feed.
-
"The Unix epoch is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT)". So in this regard it is timezone agnostic - or one may think about it being always UTC.
Internally Backtrader maintains the data feed times in UTC as well - so if your data feed uses datetime in non-UTC timezone - it will be converted internally ( for CSV based feeds you may specify the feed timezone by specifying the
tzinput
parameter. Some other data feeds - mostly live feeds - can automatically determine the timezone )More here: https://www.backtrader.com/docu/timemgmt/#datetime-input
If the question was just how to convert the UNIX timestamp datetime to another timezone in python - this is simple:
from datetime import datetime, timezone import pytz dt_utc = datetime.fromtimestamp(1588762182, timezone.utc) dt_eastern = dt_utc.astimezone(pytz.timezone("US/Eastern")) print(dt_utc.isoformat()) print(dt_eastern.isoformat())
This will print:
2020-05-06T10:49:42+00:00 2020-05-06T06:49:42-04:00
-
@vladisld thank you for reply. Yes, I was asking about internal timezone for live data feed. So, you need add "tz" argument to Datafactory(tz=timezone). But if you want show in logs same timezone, need add to cerebro.run():
timezone = pytz.timezone("US/Eastern") cerebro.run(tz=timezone)
With last param I was confused because in logs I saw right timezone, but data feed has different (UTC).