diff --git a/tests/test_risk_compare_batch_iterative.py b/tests/test_risk_compare_batch_iterative.py index 4d5a7828..2d1901e1 100644 --- a/tests/test_risk_compare_batch_iterative.py +++ b/tests/test_risk_compare_batch_iterative.py @@ -1,5 +1,5 @@ # -# Copyright 2012 Quantopian, Inc. +# Copyright 2013 Quantopian, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -21,8 +21,9 @@ import pytz import numpy as np import zipline.finance.risk as risk - import zipline.finance.trading as trading +from zipline.protocol import DailyReturn + from test_risk import RETURNS @@ -52,7 +53,7 @@ class RiskCompareIterativeToBatch(unittest.TestCase): cur_returns = [] for i, ret in enumerate(RETURNS): - todays_return_obj = trading.DailyReturn( + todays_return_obj = DailyReturn( todays_date, ret ) diff --git a/zipline/data/benchmarks.py b/zipline/data/benchmarks.py index 8bdaa20a..38b4badd 100644 --- a/zipline/data/benchmarks.py +++ b/zipline/data/benchmarks.py @@ -1,5 +1,5 @@ # -# Copyright 2012 Quantopian, Inc. +# Copyright 2013 Quantopian, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -25,11 +25,10 @@ import requests from loader_utils import ( date_conversion, - source_to_records + source_to_records, + Mapping ) - -from loader_utils import Mapping - +from zipline.protocol import DailyReturn _BENCHMARK_MAPPING = { # Need to add 'symbol' @@ -94,8 +93,6 @@ def get_benchmark_data(symbol): def get_benchmark_returns(symbol): - from zipline.finance.trading import DailyReturn - benchmark_returns = [] for data_point in get_benchmark_data(symbol): diff --git a/zipline/data/loader.py b/zipline/data/loader.py index 19702f90..0becf432 100644 --- a/zipline/data/loader.py +++ b/zipline/data/loader.py @@ -1,5 +1,5 @@ # -# Copyright 2012 Quantopian, Inc. +# Copyright 2013 Quantopian, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -23,6 +23,7 @@ from collections import OrderedDict from treasuries import get_treasury_data from benchmarks import get_benchmark_returns +from zipline.protocol import DailyReturn from zipline.utils.date_utils import tuple_to_date from operator import attrgetter @@ -91,8 +92,6 @@ def get_benchmark_filename(symbol): def load_market_data(bm_symbol='^GSPC'): - from zipline.finance.trading import DailyReturn - try: fp_bm = get_datafile(get_benchmark_filename(bm_symbol), "rb") except IOError: diff --git a/zipline/finance/performance.py b/zipline/finance/performance.py index 5c84e8af..a282b434 100644 --- a/zipline/finance/performance.py +++ b/zipline/finance/performance.py @@ -1,5 +1,5 @@ # -# Copyright 2012 Quantopian, Inc. +# Copyright 2013 Quantopian, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -281,7 +281,7 @@ class PerformanceTracker(object): self.cumulative_performance.update_dividends(todays_date) self.todays_performance.update_dividends(todays_date) - todays_return_obj = trading.DailyReturn( + todays_return_obj = zp.DailyReturn( todays_date, self.todays_performance.returns ) diff --git a/zipline/finance/trading.py b/zipline/finance/trading.py index 644f50ca..cf15aa7d 100644 --- a/zipline/finance/trading.py +++ b/zipline/finance/trading.py @@ -23,6 +23,7 @@ from collections import defaultdict, OrderedDict from delorean import Delorean from pandas import DatetimeIndex +from zipline.data.loader import load_market_data import zipline.protocol as zp from zipline.finance.slippage import ( VolumeShareSlippage, @@ -111,7 +112,6 @@ class TradingEnvironment(object): self.trading_day_map = OrderedDict() self.bm_symbol = bm_symbol if not load: - from zipline.data.loader import load_market_data load = load_market_data self.benchmark_returns, self.treasury_curves = \ @@ -321,24 +321,6 @@ class SimulationParameters(object): }) -class DailyReturn(object): - - def __init__(self, date, returns): - - assert isinstance(date, datetime.datetime) - self.date = date.replace(hour=0, minute=0, second=0) - self.returns = returns - - def to_dict(self): - return { - 'dt': self.date, - 'returns': self.returns - } - - def __repr__(self): - return str(self.date) + " - " + str(self.returns) - - class use_environment(object): """A decorator to wrap a method in a particular trading environment.""" diff --git a/zipline/protocol.py b/zipline/protocol.py index be4d73a3..c6cf2982 100644 --- a/zipline/protocol.py +++ b/zipline/protocol.py @@ -1,5 +1,5 @@ # -# Copyright 2012 Quantopian, Inc. +# Copyright 2013 Quantopian, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -14,6 +14,8 @@ # limitations under the License. +import datetime + from utils.protocol_utils import Enum # Datasource type should completely determine the other fields of a @@ -113,3 +115,21 @@ class SIDData(object): def __repr__(self): return "SIDData({0})".format(self.__dict__) + + +class DailyReturn(object): + + def __init__(self, date, returns): + + assert isinstance(date, datetime.datetime) + self.date = date.replace(hour=0, minute=0, second=0) + self.returns = returns + + def to_dict(self): + return { + 'dt': self.date, + 'returns': self.returns + } + + def __repr__(self): + return str(self.date) + " - " + str(self.returns) diff --git a/zipline/utils/factory.py b/zipline/utils/factory.py index 87831705..57b3eaad 100644 --- a/zipline/utils/factory.py +++ b/zipline/utils/factory.py @@ -1,5 +1,5 @@ # -# Copyright 2012 Quantopian, Inc. +# Copyright 2013 Quantopian, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -27,7 +27,7 @@ from pandas.io.data import DataReader import numpy as np from datetime import datetime, timedelta -from zipline.protocol import Event, DATASOURCE_TYPE +from zipline.protocol import DailyReturn, Event, DATASOURCE_TYPE from zipline.sources import (SpecificEquityTrades, DataFrameSource, DataPanelSource) @@ -62,7 +62,7 @@ def create_noop_environment(): bm_returns = [] tr_curves = OrderedDict() for day in date_gen(start=start, delta=oneday, count=252): - dr = trading.DailyReturn(day, 0.01) + dr = DailyReturn(day, 0.01) bm_returns.append(dr) curve = { '10year': 0.0799, @@ -194,7 +194,7 @@ def create_returns(daycount, sim_params): for day in range(daycount): current = current + one_day if trading.environment.is_trading_day(current): - r = trading.DailyReturn(current, random.random()) + r = DailyReturn(current, random.random()) test_range.append(r) return test_range @@ -206,7 +206,7 @@ def create_returns_from_range(sim_params): one_day = timedelta(days=1) test_range = [] while current <= end: - r = trading.DailyReturn(current, random.random()) + r = DailyReturn(current, random.random()) test_range.append(r) current = get_next_trading_dt(current, one_day) @@ -223,7 +223,7 @@ def create_returns_from_list(returns, sim_params): current = get_next_trading_dt(current, one_day) for return_val in returns: - r = trading.DailyReturn(current, return_val) + r = DailyReturn(current, return_val) test_range.append(r) current = get_next_trading_dt(current, one_day)