mirror of
https://github.com/wassname/catalyst.git
synced 2026-07-19 11:22:06 +08:00
TST: Adds tearDownClass methods to delete TradingEnvironments
This commit is contained in:
@@ -244,6 +244,11 @@ class FrameInputTestCase(TestCase):
|
||||
cls.env.write_data(equities_df=asset_info)
|
||||
cls.asset_finder = cls.env.asset_finder
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
del cls.env
|
||||
del cls.asset_finder
|
||||
|
||||
def setUp(self):
|
||||
self.dates = FrameInputTestCase.dates
|
||||
self.assets = FrameInputTestCase.assets
|
||||
@@ -375,6 +380,7 @@ class SyntheticBcolzTestCase(TestCase):
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
del cls.env
|
||||
cls.temp_dir.cleanup()
|
||||
|
||||
def test_SMA(self):
|
||||
|
||||
@@ -102,6 +102,10 @@ class FFCAlgorithmTestCase(TestCase):
|
||||
|
||||
cls.dates = cls.raw_data[cls.AAPL].index.tz_localize('UTC')
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
del cls.env
|
||||
|
||||
@classmethod
|
||||
def create_bar_reader(cls, tempdir):
|
||||
resources = {
|
||||
|
||||
@@ -33,6 +33,10 @@ class TestRisk(unittest.TestCase):
|
||||
def setUpClass(cls):
|
||||
cls.env = TradingEnvironment()
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
del cls.env
|
||||
|
||||
def setUp(self):
|
||||
start_date = datetime.datetime(
|
||||
year=2006,
|
||||
|
||||
@@ -37,6 +37,10 @@ class TestRisk(unittest.TestCase):
|
||||
def setUpClass(cls):
|
||||
cls.env = TradingEnvironment()
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
del cls.env
|
||||
|
||||
def setUp(self):
|
||||
|
||||
start_date = datetime.datetime(
|
||||
|
||||
+37
-5
@@ -112,6 +112,10 @@ class TestRecordAlgorithm(TestCase):
|
||||
cls.env = TradingEnvironment()
|
||||
cls.env.write_data(equities_identifiers=[133])
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
del cls.env
|
||||
|
||||
def setUp(self):
|
||||
self.sim_params = factory.create_simulation_parameters(num_days=4,
|
||||
env=self.env)
|
||||
@@ -193,6 +197,10 @@ class TestMiscellaneousAPI(TestCase):
|
||||
equities_data=metadata,
|
||||
futures_data=futures_metadata)
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
del cls.env
|
||||
|
||||
def setUp(self):
|
||||
setup_logger(self)
|
||||
self.sim_params = factory.create_simulation_parameters(
|
||||
@@ -516,6 +524,10 @@ class TestTransformAlgorithm(TestCase):
|
||||
cls.env.write_data(equities_identifiers=[0, 1, 133],
|
||||
futures_data=futures_metadata)
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
del cls.env
|
||||
|
||||
def setUp(self):
|
||||
setup_logger(self)
|
||||
self.sim_params = factory.create_simulation_parameters(num_days=4,
|
||||
@@ -756,6 +768,10 @@ class TestAlgoScript(TestCase):
|
||||
equities_identifiers=[0, 1, 133]
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
del cls.env
|
||||
|
||||
def setUp(self):
|
||||
days = 251
|
||||
# Note that create_simulation_parameters creates
|
||||
@@ -813,7 +829,7 @@ class TestAlgoScript(TestCase):
|
||||
metadata = {3: {'symbol': 'TEST',
|
||||
'asset_type': 'equity'}}
|
||||
algo = TradingAlgorithm(script=api_get_environment_algo,
|
||||
asset_metadata=metadata,
|
||||
equities_metadata=metadata,
|
||||
platform=platform)
|
||||
algo.run(self.df)
|
||||
self.assertEqual(algo.environment, platform)
|
||||
@@ -823,7 +839,7 @@ class TestAlgoScript(TestCase):
|
||||
metadata = {3: {'symbol': 'TEST',
|
||||
'asset_type': 'equity'}}
|
||||
algo = TradingAlgorithm(script=api_symbol_algo,
|
||||
asset_metadata=metadata)
|
||||
equities_metadata=metadata)
|
||||
algo.run(self.df)
|
||||
|
||||
def test_fixed_slippage(self):
|
||||
@@ -1093,6 +1109,10 @@ class TestHistory(TestCase):
|
||||
)
|
||||
cls.env.write_data(equities_identifiers=[0, 1])
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
del cls.env
|
||||
|
||||
@property
|
||||
def source(self):
|
||||
return RandomWalkSource(start=self._start, end=self._end)
|
||||
@@ -1154,6 +1174,10 @@ class TestGetDatetime(TestCase):
|
||||
cls.env = TradingEnvironment()
|
||||
cls.env.write_data(equities_identifiers=[0, 1])
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
del cls.env
|
||||
|
||||
def setUp(self):
|
||||
setup_logger(self)
|
||||
|
||||
@@ -1218,6 +1242,10 @@ class TestTradingControls(TestCase):
|
||||
cls.env = TradingEnvironment()
|
||||
cls.env.write_data(equities_identifiers=[cls.sid])
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
del cls.env
|
||||
|
||||
def setUp(self):
|
||||
self.sim_params = factory.create_simulation_parameters(num_days=4,
|
||||
env=self.env)
|
||||
@@ -1513,7 +1541,7 @@ class TestTradingControls(TestCase):
|
||||
metadata = {0: {'start_date': '1990-01-01',
|
||||
'end_date': '2020-01-01'}}
|
||||
algo = SetAssetDateBoundsAlgorithm(
|
||||
asset_metadata=metadata,
|
||||
equities_metadata=metadata,
|
||||
sim_params=self.sim_params,
|
||||
env=temp_env,
|
||||
)
|
||||
@@ -1525,7 +1553,7 @@ class TestTradingControls(TestCase):
|
||||
metadata = {0: {'start_date': '1989-01-01',
|
||||
'end_date': '1990-01-01'}}
|
||||
algo = SetAssetDateBoundsAlgorithm(
|
||||
asset_metadata=metadata,
|
||||
equities_metadata=metadata,
|
||||
sim_params=self.sim_params,
|
||||
env=temp_env,
|
||||
)
|
||||
@@ -1538,7 +1566,7 @@ class TestTradingControls(TestCase):
|
||||
metadata = {0: {'start_date': '2020-01-01',
|
||||
'end_date': '2021-01-01'}}
|
||||
algo = SetAssetDateBoundsAlgorithm(
|
||||
asset_metadata=metadata,
|
||||
equities_metadata=metadata,
|
||||
sim_params=self.sim_params,
|
||||
env=temp_env,
|
||||
)
|
||||
@@ -1556,6 +1584,10 @@ class TestAccountControls(TestCase):
|
||||
equities_identifiers=[cls.sidint]
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
del cls.env
|
||||
|
||||
def setUp(self):
|
||||
self.sim_params = factory.create_simulation_parameters(
|
||||
num_days=4, env=self.env
|
||||
|
||||
@@ -91,6 +91,10 @@ class AlgorithmGeneratorTestCase(TestCase):
|
||||
cls.env = trading.TradingEnvironment()
|
||||
cls.env.write_data(equities_identifiers=[8229])
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
del cls.env
|
||||
|
||||
def setUp(self):
|
||||
setup_logger(self)
|
||||
|
||||
|
||||
@@ -721,6 +721,10 @@ class TestFutureChain(TestCase):
|
||||
env.write_data(futures_data=metadata)
|
||||
cls.asset_finder = env.asset_finder
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
del cls.asset_finder
|
||||
|
||||
def test_len(self):
|
||||
""" Test the __len__ method of FutureChain.
|
||||
"""
|
||||
|
||||
@@ -139,6 +139,10 @@ class TestBatchTransformMinutely(TestCase):
|
||||
cls.env = TradingEnvironment()
|
||||
cls.env.write_data(equities_identifiers=[0])
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
del cls.env
|
||||
|
||||
def setUp(self):
|
||||
setup_logger(self)
|
||||
start = pd.datetime(1990, 1, 3, 0, 0, 0, 0, pytz.utc)
|
||||
@@ -184,6 +188,10 @@ class TestBatchTransform(TestCase):
|
||||
cls.env = TradingEnvironment()
|
||||
cls.env.write_data(equities_identifiers=[0])
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
del cls.env
|
||||
|
||||
def setUp(self):
|
||||
setup_logger(self)
|
||||
self.sim_params = factory.create_simulation_parameters(
|
||||
|
||||
@@ -40,6 +40,10 @@ class BlotterTestCase(TestCase):
|
||||
cls.env = trading.TradingEnvironment()
|
||||
cls.env.write_data(equities_identifiers=[24])
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
del cls.env
|
||||
|
||||
def setUp(self, env=None):
|
||||
setup_logger(self)
|
||||
|
||||
|
||||
@@ -47,6 +47,10 @@ class TestEventsThroughRisk(unittest.TestCase):
|
||||
cls.env = TradingEnvironment()
|
||||
cls.env.write_data(equities_identifiers=[1])
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
del cls.env
|
||||
|
||||
def test_daily_buy_and_hold(self):
|
||||
|
||||
start_date = datetime.datetime(
|
||||
|
||||
@@ -45,6 +45,10 @@ class ExceptionTestCase(TestCase):
|
||||
cls.env = TradingEnvironment()
|
||||
cls.env.write_data(equities_identifiers=[133])
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
del cls.env
|
||||
|
||||
def setUp(self):
|
||||
self.zipline_test_config = {
|
||||
'sid': 133,
|
||||
|
||||
@@ -63,6 +63,10 @@ class FinanceTestCase(TestCase):
|
||||
cls.env = TradingEnvironment()
|
||||
cls.env.write_data(equities_identifiers=[1, 133])
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
del cls.env
|
||||
|
||||
def setUp(self):
|
||||
self.zipline_test_config = {
|
||||
'sid': 133,
|
||||
@@ -372,6 +376,10 @@ class TradingEnvironmentTestCase(TestCase):
|
||||
def setUpClass(cls):
|
||||
cls.env = TradingEnvironment()
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
del cls.env
|
||||
|
||||
@timed(DEFAULT_TIMEOUT)
|
||||
def test_is_trading_day(self):
|
||||
# holidays taken from: http://www.nyse.com/press/1191407641943.html
|
||||
|
||||
@@ -150,6 +150,10 @@ class TestHistoryIndex(TestCase):
|
||||
def setUpClass(cls):
|
||||
cls.environment = TradingEnvironment()
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
del cls.environment
|
||||
|
||||
@parameterized.expand(
|
||||
[(name, case['input'], case['expected'])
|
||||
for name, case in INDEX_TEST_CASES.items()]
|
||||
@@ -169,6 +173,10 @@ class TestHistoryContainer(TestCase):
|
||||
def setUpClass(cls):
|
||||
cls.env = TradingEnvironment()
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
del cls.env
|
||||
|
||||
def bar_data_dt(self, bar_data, require_unique=True):
|
||||
"""
|
||||
Get a dt to associate with the given BarData object.
|
||||
@@ -415,6 +423,10 @@ class TestHistoryAlgo(TestCase):
|
||||
cls.env = trading.TradingEnvironment()
|
||||
cls.env.write_data(equities_identifiers=[0, 1])
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
del cls.env
|
||||
|
||||
def setUp(self):
|
||||
np.random.seed(123)
|
||||
|
||||
@@ -1119,6 +1131,10 @@ class TestHistoryContainerResize(TestCase):
|
||||
def setUpClass(cls):
|
||||
cls.env = TradingEnvironment()
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
del cls.env
|
||||
|
||||
@parameterized.expand(
|
||||
(freq, field, data_frequency, construct_digest)
|
||||
for freq in ('1m', '1d')
|
||||
|
||||
@@ -514,6 +514,10 @@ class TestDividendPerformance(unittest.TestCase):
|
||||
cls.env = TradingEnvironment()
|
||||
cls.env.write_data(equities_identifiers=[1, 2])
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
del cls.env
|
||||
|
||||
def setUp(self):
|
||||
self.sim_params, self.dt, self.end_dt = \
|
||||
create_random_simulation_parameters()
|
||||
@@ -967,6 +971,10 @@ class TestPositionPerformance(unittest.TestCase):
|
||||
cls.env = TradingEnvironment()
|
||||
cls.env.write_data(equities_identifiers=[1, 2])
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
del cls.env
|
||||
|
||||
def setUp(self):
|
||||
self.sim_params, self.dt, self.end_dt = \
|
||||
create_random_simulation_parameters()
|
||||
@@ -1738,6 +1746,10 @@ class TestPerformanceTracker(unittest.TestCase):
|
||||
cls.env = TradingEnvironment()
|
||||
cls.env.write_data(equities_identifiers=[1, 2, 133, 134])
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
del cls.env
|
||||
|
||||
NumDaysToDelete = collections.namedtuple(
|
||||
'NumDaysToDelete', ('start', 'middle', 'end'))
|
||||
|
||||
@@ -2157,6 +2169,10 @@ class TestPositionTracker(unittest.TestCase):
|
||||
cls.env.write_data(equities_data=equities_metadata,
|
||||
futures_data=futures_metadata)
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
del cls.env
|
||||
|
||||
def test_empty_positions(self):
|
||||
"""
|
||||
make sure all the empty position stats return a numeric 0
|
||||
|
||||
@@ -32,6 +32,10 @@ class TestRollingPanel(unittest.TestCase):
|
||||
def setUpClass(cls):
|
||||
cls.env = TradingEnvironment()
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
del cls.env
|
||||
|
||||
def test_alignment(self):
|
||||
items = ('a', 'b')
|
||||
sids = (1, 2)
|
||||
|
||||
@@ -65,6 +65,10 @@ class SecurityListTestCase(TestCase):
|
||||
cls.env.write_data(equities_identifiers=['AAPL', 'GOOG', 'BZQ',
|
||||
'URTY', 'JFT'])
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
del cls.env
|
||||
|
||||
def setUp(self, env=None):
|
||||
|
||||
self.extra_knowledge_date = \
|
||||
|
||||
@@ -41,6 +41,10 @@ class SerializationTestCase(TestCase):
|
||||
def setUpClass(cls):
|
||||
cls.env = TradingEnvironment()
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
del cls.env
|
||||
|
||||
@parameterized.expand(object_serialization_cases())
|
||||
def test_object_serialization(self,
|
||||
_,
|
||||
|
||||
@@ -144,6 +144,10 @@ class TransformTestCase(TestCase):
|
||||
)),
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
del cls.env
|
||||
|
||||
def tearDown(self):
|
||||
"""
|
||||
Each test consumes a source, we need to rewind it.
|
||||
|
||||
@@ -37,6 +37,10 @@ class TestTALIB(TestCase):
|
||||
def setUpClass(cls):
|
||||
cls.env = TradingEnvironment()
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
del cls.env
|
||||
|
||||
def setUp(self):
|
||||
setup_logger(self)
|
||||
sim_params = factory.create_simulation_parameters(
|
||||
|
||||
@@ -217,6 +217,10 @@ class RuleTestCase(TestCase):
|
||||
cls.env = TradingEnvironment()
|
||||
cls.class_ = None # Mark that this is the base class.
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
del cls.env
|
||||
|
||||
def test_completeness(self):
|
||||
"""
|
||||
Tests that all rules are being tested.
|
||||
|
||||
@@ -148,7 +148,7 @@ class TradingAlgorithm(object):
|
||||
Whether to fill orders immediately or on next bar.
|
||||
asset_finder : An AssetFinder object
|
||||
A new AssetFinder object to be used in this TradingEnvironment
|
||||
asset_metadata: can be either:
|
||||
equities_metadata : can be either:
|
||||
- dict
|
||||
- pandas.DataFrame
|
||||
- object with 'read' property
|
||||
@@ -166,7 +166,7 @@ class TradingAlgorithm(object):
|
||||
with the other metadata fields.
|
||||
identifiers : List
|
||||
Any asset identifiers that are not provided in the
|
||||
asset_metadata, but will be traded by this TradingAlgorithm
|
||||
equities_metadata, but will be traded by this TradingAlgorithm
|
||||
"""
|
||||
self.sources = []
|
||||
|
||||
@@ -199,8 +199,9 @@ class TradingAlgorithm(object):
|
||||
|
||||
# Update the TradingEnvironment with the provided asset metadata
|
||||
self.trading_environment.write_data(
|
||||
equities_data=kwargs.pop('asset_metadata', {}),
|
||||
equities_data=kwargs.pop('equities_metadata', {}),
|
||||
equities_identifiers=kwargs.pop('identifiers', []),
|
||||
futures_data=kwargs.pop('futures_metadata', {}),
|
||||
)
|
||||
|
||||
# set the capital base
|
||||
|
||||
@@ -405,14 +405,6 @@ class PositionTracker(object):
|
||||
state_dict['asset_finder'] = self.asset_finder
|
||||
state_dict['positions'] = dict(self.positions)
|
||||
state_dict['unpaid_dividends'] = self._unpaid_dividends
|
||||
|
||||
# Asset-finder dependent dicts must be serialized
|
||||
state_dict['position_value_multipliers'] = \
|
||||
serialize_ordered_dict(self._position_value_multipliers)
|
||||
state_dict['position_exposure_multipliers'] = \
|
||||
serialize_ordered_dict(self._position_exposure_multipliers)
|
||||
state_dict['position_payout_multipliers'] = \
|
||||
serialize_ordered_dict(self._position_payout_multipliers)
|
||||
state_dict['auto_close_position_sids'] = self._auto_close_position_sids
|
||||
|
||||
STATE_VERSION = 3
|
||||
@@ -433,36 +425,14 @@ class PositionTracker(object):
|
||||
self._positions_store = zp.Positions()
|
||||
|
||||
self._unpaid_dividends = state['unpaid_dividends']
|
||||
|
||||
# AssetFinder-dependent dicts are de-serialized
|
||||
self._position_value_multipliers = \
|
||||
deserialize_ordered_dict(state['position_value_multipliers'])
|
||||
self._position_exposure_multipliers = \
|
||||
deserialize_ordered_dict(state['position_exposure_multipliers'])
|
||||
self._position_payout_multipliers = \
|
||||
deserialize_ordered_dict(state['position_payout_multipliers'])
|
||||
self._auto_close_position_sids = state['auto_close_position_sids']
|
||||
|
||||
# Arrays for quick calculations of positions value
|
||||
self._position_amounts = OrderedDict()
|
||||
self._position_last_sale_prices = OrderedDict()
|
||||
self._position_value_multipliers = OrderedDict()
|
||||
self._position_exposure_multipliers = OrderedDict()
|
||||
self._position_payout_multipliers = OrderedDict()
|
||||
|
||||
# Update positions is called without a finder
|
||||
self.update_positions(state['positions'])
|
||||
|
||||
|
||||
def serialize_ordered_dict(ordered_dict):
|
||||
"""
|
||||
Converts an OrderedDict in to a list of key/value pair tuples
|
||||
"""
|
||||
return [(key, value) for key, value in ordered_dict.items()]
|
||||
|
||||
|
||||
def deserialize_ordered_dict(serialized_ordered_dict):
|
||||
"""
|
||||
Converts a list of key/value pair tuples in to an OrderedDict
|
||||
"""
|
||||
result = OrderedDict()
|
||||
for key, value in serialized_ordered_dict:
|
||||
result[key] = value
|
||||
return result
|
||||
|
||||
@@ -240,7 +240,7 @@ def run_pipeline(print_algo=True, **kwargs):
|
||||
namespace=kwargs.get('namespace', {}),
|
||||
capital_base=float(kwargs['capital_base']),
|
||||
algo_filename=kwargs.get('algofile'),
|
||||
asset_metadata=asset_metadata,
|
||||
equities_metadata=asset_metadata,
|
||||
identifiers=symbols,
|
||||
start=start,
|
||||
end=end)
|
||||
|
||||
Reference in New Issue
Block a user