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.period_start, 'end_date': sim_params.period_end, }]) with TempDirectory() as new_tempdir, \ security_list_copy(), \ tmp_trading_env(equities=equities) 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_schedule=self.trading_schedule, ) algo = RestrictedAlgoWithoutCheck(symbol='BZQ', sim_params=sim_params, env=env) algo.run(data_portal)
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) self.assertNotIn("URTY", rl.leveraged_etf_list)
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=list( LEVERAGED_ETFS.keys())[0] + timedelta(days=7), num_days=4) with security_list_copy(): add_security_data(['AAPL'], []) trade_history = factory.create_trade_history( 'BZQ', [10.0, 10.0, 11.0, 11.0], [100, 100, 100, 300], timedelta(days=1), sim_params, env=self.env, ) self.source = SpecificEquityTrades(event_list=trade_history, env=self.env) algo = RestrictedAlgoWithoutCheck( symbol='BZQ', sim_params=sim_params, env=self.env) with self.assertRaises(TradingControlViolation) as ctx: algo.run(self.source) self.check_algo_exception(algo, ctx, 0)
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) 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_security_add_delete(self): with security_list_copy(): def get_datetime(): return datetime(2015, 1, 27, tzinfo=pytz.utc) rl = SecurityListSet(get_datetime, self.env.asset_finder) self.assertNotIn("BZQ", rl.leveraged_etf_list) self.assertNotIn("URTY", rl.leveraged_etf_list)
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(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)
def test_algo_with_rl_violation_after_add(self): sim_params = factory.create_simulation_parameters( start=self.trading_day_before_first_kd, num_days=4, ) with security_list_copy(): add_security_data(['AAPL'], []) algo = self.make_algo( algo_class=RestrictedAlgoWithoutCheck, symbol='AAPL', sim_params=sim_params, ) with self.assertRaises(TradingControlViolation) as ctx: algo.run() self.check_algo_exception(algo, ctx, 2)
def test_algo_without_rl_violation_after_delete(self): sim_params = factory.create_simulation_parameters( start=self.extra_knowledge_date, num_days=4, ) with security_list_copy(): # add a delete statement removing bzq # write a new delete statement file to disk add_security_data([], ['BZQ']) algo = self.make_algo( algo_class=RestrictedAlgoWithoutCheck, symbol='BZQ', sim_params=sim_params, ) algo.run()
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=list( LEVERAGED_ETFS.keys())[0] + 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)
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)
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_DATE + timedelta(days=7), num_days=4 ) with security_list_copy(): add_security_data(["AAPL"], []) algo = self.make_algo( algo_class=RestrictedAlgoWithoutCheck, symbol="BZQ", sim_params=sim_params, ) with self.assertRaises(TradingControlViolation) as ctx: algo.run() self.check_algo_exception(algo, ctx, 0)
def test_algo_with_rl_violation_after_add(self): with security_list_copy(): add_security_data(['AAPL'], []) sim_params = factory.create_simulation_parameters( start=self.trading_day_before_first_kd, num_days=4) trade_history = factory.create_trade_history( 'AAPL', [10.0, 10.0, 11.0, 11.0], [100, 100, 100, 300], timedelta(days=1), sim_params, env=self.env ) self.source = SpecificEquityTrades(event_list=trade_history, env=self.env) algo = RestrictedAlgoWithoutCheck( symbol='AAPL', sim_params=sim_params, env=self.env) with self.assertRaises(TradingControlViolation) as ctx: algo.run(self.source) self.check_algo_exception(algo, ctx, 2)
def test_algo_without_rl_violation_after_delete(self): with security_list_copy(): # add a delete statement removing bzq # write a new delete statement file to disk add_security_data([], ['BZQ']) sim_params = factory.create_simulation_parameters( start=self.extra_knowledge_date, num_days=3) trade_history = factory.create_trade_history( 'BZQ', [10.0, 10.0, 11.0, 11.0], [100, 100, 100, 300], timedelta(days=1), sim_params, env=self.env, ) self.source = SpecificEquityTrades(event_list=trade_history, env=self.env) algo = RestrictedAlgoWithoutCheck( symbol='BZQ', sim_params=sim_params, env=self.env ) algo.run(self.source)
def test_algo_without_rl_violation_after_delete(self): new_tempdir = TempDirectory() try: with security_list_copy(): # add a delete statement removing bzq # write a new delete statement file to disk add_security_data([], ['BZQ']) # now fast-forward to self.extra_knowledge_date. requires # a new env, simparams, and dataportal env = TradingEnvironment() sim_params = factory.create_simulation_parameters( start=self.extra_knowledge_date, num_days=4, env=env) env.write_data(equities_data={ "0": { 'symbol': 'BZQ', 'start_date': sim_params.period_start, 'end_date': sim_params.period_end, } }) data_portal = create_data_portal( env, new_tempdir, sim_params, range(0, 5) ) algo = RestrictedAlgoWithoutCheck( symbol='BZQ', sim_params=sim_params, env=env ) algo.run(data_portal) finally: new_tempdir.cleanup()