QuantConnect / Lean

Lean Algorithmic Trading Engine by QuantConnect (Python, C#)
https://lean.io
Apache License 2.0
9.84k stars 3.26k forks source link

Time Zone, DateTime conversions #7525

Closed BurnOutTrader closed 1 year ago

BurnOutTrader commented 1 year ago

Expected Behavior

When SetTimeZone("UTC"); Order time and algorithm time are in sync. When converting times to DateTimeOffset(time.ToUniversalTime()).ToUnixTimeSeconds()) The two times output are wildly different

20231021 10:43:09.836 TRACE:: Debug: Algo Time: 25/7/2019 7:32:01am Entry Time: 25/7/2019 7:32:01am 20231021 10:43:09.836 TRACE:: Debug: Algo Time: 1564014721 Entry Time: 1564039921 This difference is my system time zone, but you would expect using ToUTC and considering this is during a backtest this shouldn't occur, the same occurs without converting to utc before applying the offset.

When using .ToString("o"); 20231021 11:20:33.370 TRACE:: Debug: Algo Time: 28/7/2019 11:40:50pm Entry Time: 28/7/2019 11:40:50pm 20231021 11:20:33.370 TRACE:: Debug: Algo Time: 2019-07-28T23:40:50.0000000 Entry Time: 2019-07-28T23:40:50.0000000Z (note the Z for the order)

Hinting that the algorithm time zone is not set to UTC TimeZone at some level. therefore IBaseData.Time and IndicatorDataPoint.Time revert to local machine time on conversion to UnixTime, instead of the backtest TimeZone, this results in OrderTickets and TradeBuilder objects being misaligned with the Historical Data used for Chart exports etc (which is where it becomes obvious something is wrong).

I am not sure if this is a bug or I am just misunderstanding something about timezone offset functions.

Fix: no idea if it's even bugged 😅 If there is a bug in the engine it is likely that it only impacts backtests. Assuming that OrderTicket.Time, IBaseData.Time are all done using QCAlgorithm.Time in LiveMode.

The solution i found in my particular case where I am trying to export trades and their associated bar and indicator plots to LightWeight charts was to simply use,

var time = Algorithm.LiveMode ? Algorithm.Time : IBaseData.Time; Time = new DateTimeOffset(time.ToUniversalTime()).ToUnixTimeSeconds()

To ensure that the TimeZone does not revert to local TimeZone during the conversion to unix time in backtesting.

Martin-Molinero commented 1 year ago

Hey @BurnOutTrader! Lean engine is already synchronizing the data and emitting it when appropriately. If you want the algorithm time in UTC, you can directly use algorith.UtcTime no need to handle conversions manually. Data time is always in exchange time zone. For more information please take a look at our documentation https://www.quantconnect.com/docs/v2/writing-algorithms/key-concepts/time-modeling/time-zones

BurnOutTrader commented 1 year ago

Ah I see thankyou.