def test_algo_without_rl_violation_after_delete(self): sim_params = factory.create_simulation_parameters( start=self.extra_knowledge_date, num_days=4, ) equities = pd.DataFrame.from_records([{ 'symbol': 'BZQ', 'start_date': sim_params.start_session, 'end_date': sim_params.end_session, 'exchange': "TEST", }]) with TempDirectory() as new_tempdir, \ security_list_copy(), \ tmp_trading_env(equities=equities, load=self.make_load_function()) as env: # add a delete statement removing bzq # write a new delete statement file to disk add_security_data([], ['BZQ']) data_portal = create_data_portal( env.asset_finder, new_tempdir, sim_params, range(0, 5), trading_calendar=self.trading_calendar, ) algo = RestrictedAlgoWithoutCheck(symbol='BZQ', sim_params=sim_params, env=env) algo.run(data_portal)
def test_algo_with_rl_violation_after_add(self): with security_list_copy(): add_security_data(['AAPL'], []) algo = RestrictedAlgoWithoutCheck(symbol='AAPL', sim_params=self.sim_params2, env=self.env2) with self.assertRaises(TradingControlViolation) as ctx: algo.run(self.data_portal2) self.check_algo_exception(algo, ctx, 2)
def test_security_add_delete(self): with security_list_copy(): def get_datetime(): return pd.Timestamp("2015-01-27", tz='UTC') rl = SecurityListSet(get_datetime, self.env.asset_finder) self.assertNotIn( "BZQ", rl.leveraged_etf_list.current_securities(get_datetime())) self.assertNotIn( "URTY", rl.leveraged_etf_list.current_securities(get_datetime()))
def test_security_add(self): def get_datetime(): return pd.Timestamp("2015-01-27", tz='UTC') with security_list_copy(): add_security_data(['AAPL', 'GOOG'], []) rl = SecurityListSet(get_datetime, self.env.asset_finder) should_exist = [ asset.sid for asset in [ self.env.asset_finder.lookup_symbol( symbol, as_of_date=self.extra_knowledge_date) for symbol in ["AAPL", "GOOG", "BZQ", "URTY"] ] ] for sid in should_exist: self.assertIn( sid, rl.leveraged_etf_list.current_securities(get_datetime()))
def test_algo_with_rl_violation_cumulative(self): """ Add a new restriction, run a test long after both knowledge dates, make sure stock from original restriction set is still disallowed. """ sim_params = factory.create_simulation_parameters(start=self.start + timedelta(days=7), num_days=4) with security_list_copy(): add_security_data(['AAPL'], []) algo = RestrictedAlgoWithoutCheck(symbol='BZQ', sim_params=sim_params, env=self.env) with self.assertRaises(TradingControlViolation) as ctx: algo.run(self.data_portal) self.check_algo_exception(algo, ctx, 0)