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.
This commit is contained in:
Eddie Hebert
2013-04-01 18:51:19 -04:00
parent 210a43a306
commit dd172dd42a
2 changed files with 13 additions and 34 deletions
+10 -11
View File
@@ -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)
+3 -23
View File
@@ -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