From 147e806bb015f34b5b66286c0debc103d9e1d0d3 Mon Sep 17 00:00:00 2001 From: Eddie Hebert Date: Thu, 28 Mar 2013 12:29:52 -0400 Subject: [PATCH] MAINT: Moves exchange to utc conversion inside of environment object. So that the environments' exchange time is used without having to specify it independently. Also, moves uses of Delorean.shift for the exchange conversion inside of environment to use the exchange_dt_to_utc method. --- tests/test_perf_tracking.py | 9 ++++----- zipline/finance/trading.py | 12 +++++------- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/tests/test_perf_tracking.py b/tests/test_perf_tracking.py index 45445e7b..1cfacfcd 100644 --- a/tests/test_perf_tracking.py +++ b/tests/test_perf_tracking.py @@ -1033,11 +1033,10 @@ class TestPerformanceTracker(unittest.TestCase): def test_minute_tracker(self): """ Tests minute performance tracking.""" - exc_tz = pytz.timezone('US/Eastern') - start_dt = trading.exchange_dt_in_utc( - datetime.datetime(2013, 3, 1, 9, 30, tzinfo=exc_tz)) - end_dt = trading.exchange_dt_in_utc( - datetime.datetime(2013, 3, 1, 16, 0, tzinfo=exc_tz)) + start_dt = trading.environment.exchange_dt_in_utc( + datetime.datetime(2013, 3, 1, 9, 30)) + end_dt = trading.environment.exchange_dt_in_utc( + datetime.datetime(2013, 3, 1, 16, 0)) sim_params = SimulationParameters( period_start=start_dt, diff --git a/zipline/finance/trading.py b/zipline/finance/trading.py index f282c378..c3d22b8e 100644 --- a/zipline/finance/trading.py +++ b/zipline/finance/trading.py @@ -74,11 +74,6 @@ log = logbook.Logger('Transaction Simulator') environment = None -def exchange_dt_in_utc(dt): - delorean = Delorean(dt, dt.tzinfo) - return delorean.shift(pytz.utc.zone).datetime - - class TransactionSimulator(object): def __init__(self): @@ -156,6 +151,10 @@ class TradingEnvironment(object): tzinfo=pytz.utc ) + def exchange_dt_in_utc(self, dt): + delorean = Delorean(dt, self.exchange_tz) + return delorean.shift(pytz.utc.zone).datetime + @property def period_trading_days(self): if self._period_trading_days is None: @@ -223,8 +222,7 @@ Last successful date: %s" % self.market_open) ) # create a new Delorean with the next_open naive date and # the correct timezone for the exchange. - open_delorean = Delorean(next_open, self.exchange_tz) - open_utc = open_delorean.shift(pytz.utc.zone).datetime + open_utc = self.exchange_dt_in_utc(next_open) market_open = open_utc market_close = market_open + self.get_trading_day_duration(open_utc)