MAINT: Moved DailyReturn to protocol module to break circular references

and removed code that solved that same problem with conditional imports.
This commit is contained in:
Richard Frank
2013-02-26 12:01:52 -05:00
committed by Eddie Hebert
parent f1ff9cee0d
commit ebdb5429aa
7 changed files with 40 additions and 41 deletions
+4 -3
View File
@@ -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
)
+4 -7
View File
@@ -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):
+2 -3
View File
@@ -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:
+2 -2
View File
@@ -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
)
+1 -19
View File
@@ -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."""
+21 -1
View File
@@ -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)
+6 -6
View File
@@ -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)