MAINT: Removes the ability to reference a global TradingEnvironment

This commit removes the ability to reference a shared TradingEnvironment through the zipline.finance.trading module. In place, the classes that require a TradingEnvironment, or its child AssetFinder, contain their own references to those objects.

This commit also adds serialization utilities that allow for the pickling/unpickling of objects without unintentionally their TradingEnvironments or AssetFinders.
This commit is contained in:
jfkirk
2015-09-10 11:53:28 -04:00
parent 661314ce49
commit dc964a7e7d
45 changed files with 1484 additions and 1173 deletions
+16 -11
View File
@@ -39,7 +39,6 @@ import zipline.utils.simfactory as simfactory
from zipline.finance.blotter import Blotter
from zipline.gens.composites import date_sorted_sources
from zipline.finance import trading
from zipline.finance.trading import TradingEnvironment
from zipline.finance.execution import MarketOrder, LimitOrder
from zipline.finance.trading import SimulationParameters
@@ -59,9 +58,12 @@ _multiprocess_can_split_ = False
class FinanceTestCase(TestCase):
@classmethod
def setUpClass(cls):
cls.env = TradingEnvironment()
cls.env.write_data(equities_identifiers=[1, 133])
def setUp(self):
trading.environment = trading.TradingEnvironment()
trading.environment.write_data(equities_identifiers=[1, 133])
self.zipline_test_config = {
'sid': 133,
}
@@ -76,7 +78,8 @@ class FinanceTestCase(TestCase):
sim_params = factory.create_simulation_parameters()
trade_source = factory.create_daily_trade_source(
[133],
sim_params
sim_params,
env=self.env,
)
prev = None
for trade in trade_source:
@@ -94,7 +97,6 @@ class FinanceTestCase(TestCase):
# No transactions can be filled on the first trade, so
# we have one extra trade to ensure all orders are filled.
self.zipline_test_config['trade_count'] = 101
trading.environment = trading.TradingEnvironment()
full_zipline = simfactory.create_test_zipline(
**self.zipline_test_config)
assert_single_position(self, full_zipline)
@@ -231,7 +233,8 @@ class FinanceTestCase(TestCase):
price,
volume,
trade_interval,
sim_params
sim_params,
env=self.env,
)
if alternate:
@@ -265,7 +268,7 @@ class FinanceTestCase(TestCase):
self.assertEqual(order.sid, sid)
self.assertEqual(order.amount, order_amount * alternator ** i)
tracker = PerformanceTracker(sim_params)
tracker = PerformanceTracker(sim_params, env=self.env)
benchmark_returns = [
Event({'dt': dt,
@@ -273,7 +276,7 @@ class FinanceTestCase(TestCase):
'type':
zipline.protocol.DATASOURCE_TYPE.BENCHMARK,
'source_id': 'benchmarks'})
for dt, ret in trading.environment.benchmark_returns.iteritems()
for dt, ret in self.env.benchmark_returns.iteritems()
if dt.date() >= sim_params.period_start.date() and
dt.date() <= sim_params.period_end.date()
]
@@ -412,6 +415,7 @@ class TradingEnvironmentTestCase(TestCase):
period_start=datetime(2008, 1, 1, tzinfo=pytz.utc),
period_end=datetime(2008, 12, 31, tzinfo=pytz.utc),
capital_base=100000,
env=self.env,
)
self.assertTrue(env.last_close.month == 12)
@@ -428,10 +432,11 @@ class TradingEnvironmentTestCase(TestCase):
# 20 21 22 23 24 25 26
# 27 28 29 30 31
env = SimulationParameters(
params = SimulationParameters(
period_start=datetime(2007, 12, 31, tzinfo=pytz.utc),
period_end=datetime(2008, 1, 7, tzinfo=pytz.utc),
capital_base=100000,
env=self.env,
)
expected_trading_days = (
@@ -447,9 +452,9 @@ class TradingEnvironmentTestCase(TestCase):
)
num_expected_trading_days = 5
self.assertEquals(num_expected_trading_days, env.days_in_period)
self.assertEquals(num_expected_trading_days, params.days_in_period)
np.testing.assert_array_equal(expected_trading_days,
env.trading_days.tolist())
params.trading_days.tolist())
@timed(DEFAULT_TIMEOUT)
def test_market_minute_window(self):