From dd172dd42a1d9925641b17c2eb32cc3e7dda0457 Mon Sep 17 00:00:00 2001 From: Eddie Hebert Date: Mon, 1 Apr 2013 18:51:19 -0400 Subject: [PATCH] MAINT: Use trading day increment instead of timedelta in test factory. In the test factory creation of returns, the date creation was using a timedelta of one day instead of incrementing by trading days. Working towards changing risk module behavior which would leverage the trading day map, but tests fail because non-trading days are created. Remove `factory.create_returns`, moving uses of that function to us `factory.create_returns_from_period`, since the number of days input for `create_returns` was more difficult to use when specifying ranges over arbirtray dates. --- tests/test_risk.py | 21 ++++++++++----------- zipline/utils/factory.py | 26 +++----------------------- 2 files changed, 13 insertions(+), 34 deletions(-) diff --git a/tests/test_risk.py b/tests/test_risk.py index e62a4a9d..25ea542a 100644 --- a/tests/test_risk.py +++ b/tests/test_risk.py @@ -728,8 +728,10 @@ class Risk(unittest.TestCase): [0.0500]) def test_benchmarkrange(self): - self.check_year_range(datetime.datetime(year=2008, month=1, day=1), - 2) + self.check_year_range( + datetime.datetime( + year=2008, month=1, day=1, tzinfo=pytz.utc), + 2) def test_partial_month(self): @@ -749,21 +751,18 @@ class Risk(unittest.TestCase): period_end=end ) - returns = factory.create_returns(total_days, sim_params90s) + returns = factory.create_returns_from_range(sim_params90s) returns = returns[:-10] # truncate the returns series to end mid-month metrics = risk.RiskReport(returns, sim_params90s) total_months = 60 self.check_metrics(metrics, total_months, start) def check_year_range(self, start_date, years): - if(start_date.month <= 2): - ld = calendar.leapdays(start_date.year, start_date.year + years) - else: - # because we may catch the leap of the last year, - # and i think this func is [start,end) - ld = calendar.leapdays(start_date.year, - start_date.year + years + 1) - returns = factory.create_returns(365 * years + ld, self.sim_params08) + sim_params = SimulationParameters( + period_start=start_date, + period_end=start_date.replace(year=(start_date.year + years)) + ) + returns = factory.create_returns_from_range(sim_params) metrics = risk.RiskReport(returns, self.sim_params) total_months = years * 12 self.check_metrics(metrics, total_months, start_date) diff --git a/zipline/utils/factory.py b/zipline/utils/factory.py index df531a38..6acc7342 100644 --- a/zipline/utils/factory.py +++ b/zipline/utils/factory.py @@ -184,50 +184,30 @@ def create_txn_history(sid, priceList, amtList, interval, sim_params): return txns -def create_returns(daycount, sim_params): - """ - For the given number of calendar (not trading) days return all the trading - days between start and start + daycount. - """ - test_range = [] - current = sim_params.first_open - one_day = timedelta(days=1) - - for day in range(daycount): - current = current + one_day - if trading.environment.is_trading_day(current): - r = DailyReturn(current, random.random()) - test_range.append(r) - - return test_range - - def create_returns_from_range(sim_params): current = sim_params.first_open end = sim_params.last_close - one_day = timedelta(days=1) test_range = [] while current <= end: r = DailyReturn(current, random.random()) test_range.append(r) - current = get_next_trading_dt(current, one_day) + current = trading.environment.next_trading_day(current) return test_range def create_returns_from_list(returns, sim_params): current = sim_params.first_open - one_day = timedelta(days=1) test_range = [] #sometimes the range starts with a non-trading day. if not trading.environment.is_trading_day(current): - current = get_next_trading_dt(current, one_day) + current = trading.environment.next_trading_day(current) for return_val in returns: r = DailyReturn(current, return_val) test_range.append(r) - current = get_next_trading_dt(current, one_day) + current = trading.environment.next_trading_day(current) return test_range