From 7650a4c9165dcad1fddd49147a9cf6e3e591f77e Mon Sep 17 00:00:00 2001 From: Zhiyi Sun Date: Sun, 24 Sep 2023 20:07:58 +0800 Subject: [PATCH 1/2] fix: generalize values for before_trading_start_minutes Use open_times and timezone info from trading_calendar for before_trading_start_minutes. Replace the original 8:45 "US/Eastern". Signed-off-by: Zhiyi Sun --- src/zipline/algorithm.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/zipline/algorithm.py b/src/zipline/algorithm.py index dbccd82b18..a3ab55849e 100644 --- a/src/zipline/algorithm.py +++ b/src/zipline/algorithm.py @@ -16,7 +16,7 @@ from collections import namedtuple from copy import copy import warnings -from datetime import tzinfo, time, timezone +from datetime import tzinfo, time, timezone, timedelta, date, datetime import logging import pytz import pandas as pd @@ -516,11 +516,18 @@ def _create_clock(self): execution_closes = market_closes execution_opens = market_closes - # FIXME generalize these values + # Default to 8:45am if trading_calendar doesn't have valid open_time + before_trading_time = time(8,45) + for open_time in self.trading_calendar.open_times: + if open_time[0] is None and open_time[1] is not None: + opentime = datetime.combine(date(1,1,1), open_time[1]) + before_trading_time = (opentime - timedelta(minutes=45)).time() + break + before_trading_start_minutes = days_at_time( self.sim_params.sessions, - time(8, 45), - "US/Eastern", + before_trading_time, + self.trading_calendar.tz.zone, day_offset=0, ) From d158c37a1b9011ec06470b9016bdc249e038a68b Mon Sep 17 00:00:00 2001 From: Zhiyi Sun Date: Tue, 26 Sep 2023 21:02:40 +0800 Subject: [PATCH 2/2] Support both zoneinfo and pytz in Exchange_Calendar exchange_calendar is migrating from pytz to zoneinfo. Using str(self.trading_calendar.tz) can support both pytz and zoneinfo. --- src/zipline/algorithm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/zipline/algorithm.py b/src/zipline/algorithm.py index a3ab55849e..c126e27579 100644 --- a/src/zipline/algorithm.py +++ b/src/zipline/algorithm.py @@ -527,7 +527,7 @@ def _create_clock(self): before_trading_start_minutes = days_at_time( self.sim_params.sessions, before_trading_time, - self.trading_calendar.tz.zone, + str(self.trading_calendar.tz), day_offset=0, )