def test_partial_month(self): start = datetime.datetime(year=1991, month=1, day=1, hour=0, minute=0, tzinfo=pytz.utc) # 1992 and 1996 were leap years total_days = 365 * 5 + 2 end = start + datetime.timedelta(days=total_days) sim_params90s = SimulationParameters( period_start=start, period_end=end, trading_schedule=self.trading_schedule, ) returns = factory.create_returns_from_range(sim_params90s) returns = returns[:-10] # truncate the returns series to end mid-month metrics = risk.RiskReport(returns, sim_params90s, trading_schedule=self.trading_schedule, treasury_curves=self.env.treasury_curves, benchmark_returns=self.env.benchmark_returns) total_months = 60 self.check_metrics(metrics, total_months, start)
def test_benchmark_returns_08(self): returns = factory.create_returns_from_range(self.sim_params08) metrics = risk.RiskReport(returns, self.sim_params08) self.assertEqual([ round(x.benchmark_period_returns, 3) for x in metrics.month_periods ], [ -0.061, -0.035, -0.006, 0.048, 0.011, -0.086, -0.01, 0.012, -0.091, -0.169, -0.075, 0.008 ]) self.assertEqual([ round(x.benchmark_period_returns, 3) for x in metrics.three_month_periods ], [ -0.099, 0.005, 0.052, -0.032, -0.085, -0.084, -0.089, -0.236, -0.301, -0.226 ]) self.assertEqual([ round(x.benchmark_period_returns, 3) for x in metrics.six_month_periods ], [-0.128, -0.081, -0.036, -0.118, -0.301, -0.36, -0.294]) self.assertEqual([ round(x.benchmark_period_returns, 3) for x in metrics.year_periods ], [-0.385])
def handle_simulation_end(self): """ When the simulation is complete, run the full period risk report and send it out on the results socket. """ log_msg = "Simulated {n} trading days out of {m}." log.info(log_msg.format(n=int(self.day_count), m=self.total_days)) log.info("first open: {d}".format(d=self.sim_params.first_open)) log.info("last close: {d}".format(d=self.sim_params.last_close)) bms = pd.Series( index=self.cumulative_risk_metrics.cont_index, data=self.cumulative_risk_metrics.benchmark_returns_cont) ars = pd.Series( index=self.cumulative_risk_metrics.cont_index, data=self.cumulative_risk_metrics.algorithm_returns_cont) acl = self.cumulative_risk_metrics.algorithm_cumulative_leverages self.risk_report = risk.RiskReport(ars, self.sim_params, benchmark_returns=bms, algorithm_leverages=acl) risk_dict = self.risk_report.to_dict() return risk_dict
def test_benchmark_returns_08(self): returns = factory.create_returns_from_range(self.sim_params08) metrics = risk.RiskReport(returns, self.sim_params08) monthly = [ round(x.benchmark_period_returns, 3) for x in metrics.month_periods ] self.assertEqual(monthly, [ -0.051, -0.039, 0.001, 0.043, 0.011, -0.075, -0.007, 0.026, -0.093, -0.160, -0.072, 0.009 ]) self.assertEqual([ round(x.benchmark_period_returns, 3) for x in metrics.three_month_periods ], [ -0.087, 0.003, 0.055, -0.026, -0.072, -0.058, -0.075, -0.218, -0.293, -0.214 ]) self.assertEqual([ round(x.benchmark_period_returns, 3) for x in metrics.six_month_periods ], [-0.110, -0.069, -0.006, -0.099, -0.274, -0.334, -0.273]) self.assertEqual([ round(x.benchmark_period_returns, 3) for x in metrics.year_periods ], [-0.353])
def setUp(self): start_date = datetime.datetime(year=2006, month=1, day=1, hour=0, minute=0, tzinfo=pytz.utc) end_date = datetime.datetime(year=2006, month=12, day=31, tzinfo=pytz.utc) self.sim_params = SimulationParameters(period_start=start_date, period_end=end_date) self.algo_returns_06 = factory.create_returns_from_list( RETURNS, self.sim_params) self.metrics_06 = risk.RiskReport(self.algo_returns_06, self.sim_params) start_08 = datetime.datetime(year=2008, month=1, day=1, hour=0, minute=0, tzinfo=pytz.utc) end_08 = datetime.datetime(year=2008, month=12, day=31, tzinfo=pytz.utc) self.sim_params08 = SimulationParameters(period_start=start_08, period_end=end_08)
def handle_simulation_end(self): """ When the simulation is complete, run the full period risk report and send it out on the results socket. """ # the stream will end on the last trading day, but will # not trigger an end of day, so we trigger the final # market close(s) here perf_messages = [] while self.last_close > self.market_close: perf_messages.append(self.handle_market_close()) perf_messages.append(self.handle_market_close()) log_msg = "Simulated {n} trading days out of {m}." log.info(log_msg.format(n=int(self.day_count), m=self.total_days)) log.info("first open: {d}".format( d=self.sim_params.first_open)) log.info("last close: {d}".format( d=self.sim_params.last_close)) self.risk_report = risk.RiskReport(self.returns, self.sim_params) risk_dict = self.risk_report.to_dict() return perf_messages, risk_dict
def test_benchmark_volatility_08(self): returns = factory.create_returns_from_range(self.sim_params08) metrics = risk.RiskReport(returns, self.sim_params08) self.assertEqual( [round(x.benchmark_volatility, 3) for x in metrics.month_periods], [ 0.069, 0.056, 0.080, 0.049, 0.040, 0.052, 0.068, 0.055, 0.150, 0.230, 0.188, 0.137 ]) self.assertEqual([ round(x.benchmark_volatility, 3) for x in metrics.three_month_periods ], [ 0.118, 0.108, 0.101, 0.083, 0.094, 0.102, 0.172, 0.277, 0.328, 0.323 ]) self.assertEqual([ round(x.benchmark_volatility, 3) for x in metrics.six_month_periods ], [0.144, 0.143, 0.143, 0.190, 0.292, 0.342, 0.364]) # TODO: ugly, but I can't get the rounded float to match. # maybe we need a different test that checks the # difference between the numbers self.assertEqual( [round(x.benchmark_volatility, 3) for x in metrics.year_periods], [0.391])
def init_instance_fixtures(self): super(TestRisk, self).init_instance_fixtures() self.start_session = pd.Timestamp("2006-01-01", tz='UTC') self.end_session = self.trading_calendar.minute_to_session_label( pd.Timestamp("2006-12-31", tz='UTC'), direction="previous" ) self.sim_params = SimulationParameters( start_session=self.start_session, end_session=self.end_session, trading_calendar=self.trading_calendar, ) self.algo_returns = factory.create_returns_from_list( RETURNS, self.sim_params ) self.benchmark_returns = factory.create_returns_from_list( BENCHMARK, self.sim_params ) self.metrics = risk.RiskReport( self.algo_returns, self.sim_params, benchmark_returns=self.benchmark_returns, trading_calendar=self.trading_calendar, )
def test_partial_month(self): start = datetime.datetime( year=1991, month=1, day=1, hour=0, minute=0, tzinfo=pytz.utc) #1992 and 1996 were leap years total_days = 365 * 5 + 2 end = start + datetime.timedelta(days=total_days) trading_env90s = TradingEnvironment( self.benchmark_returns, self.treasury_curves, period_start=start, period_end=end ) returns = factory.create_returns(total_days, trading_env90s) returns = returns[:-10] # truncate the returns series to end mid-month metrics = risk.RiskReport(returns, trading_env90s) total_months = 60 self.check_metrics(metrics, total_months, start)
def test_treasury_returns(self): returns = factory.create_returns_from_range(self.sim_params) metrics = risk.RiskReport(returns, self.sim_params, trading_calendar=self.trading_calendar, benchmark_returns=self.env.benchmark_returns) # These values are all expected to be zero because we explicity zero # out the treasury period returns as they are no longer actually used. self.assertEqual( [x.treasury_period_return for x in metrics.month_periods], [0.0] * len(metrics.month_periods), ) self.assertEqual( [x.treasury_period_return for x in metrics.three_month_periods], [0.0] * len(metrics.three_month_periods), ) self.assertEqual( [x.treasury_period_return for x in metrics.six_month_periods], [0.0] * len(metrics.six_month_periods), ) self.assertEqual( [x.treasury_period_return for x in metrics.year_periods], [0.0] * len(metrics.year_periods), )
def test_treasury_returns(self): returns = factory.create_returns_from_range(self.sim_params) metrics = risk.RiskReport(returns, self.sim_params, trading_calendar=self.trading_calendar, treasury_curves=self.env.treasury_curves, benchmark_returns=self.env.benchmark_returns) self.assertEqual([ round(x.treasury_period_return, 4) for x in metrics.month_periods ], [ 0.0037, 0.0034, 0.0039, 0.0038, 0.0040, 0.0037, 0.0043, 0.0043, 0.0038, 0.0044, 0.0043, 0.004 ]) self.assertEqual([ round(x.treasury_period_return, 4) for x in metrics.three_month_periods ], [ 0.0114, 0.0116, 0.0122, 0.0125, 0.0129, 0.0127, 0.0123, 0.0128, 0.0125, 0.0127 ]) self.assertEqual([ round(x.treasury_period_return, 4) for x in metrics.six_month_periods ], [0.0260, 0.0257, 0.0258, 0.0252, 0.0259, 0.0256, 0.0257]) self.assertEqual( [round(x.treasury_period_return, 4) for x in metrics.year_periods], [0.0500])
def test_trading_days_06(self): returns = factory.create_returns_from_range(self.sim_params) metrics = risk.RiskReport(returns, self.sim_params) self.assertEqual([x.trading_days for x in metrics.year_periods], [251]) self.assertEqual([x.trading_days for x in metrics.month_periods], [20, 19, 23, 19, 22, 22, 20, 23, 20, 22, 21, 20])
def test_benchmark_volatility_06(self): returns = factory.create_returns_from_range(self.sim_params) metrics = risk.RiskReport(returns, self.sim_params) self.assertEqual( [round(x.benchmark_volatility, 3) for x in metrics.month_periods], [ 0.031, 0.026, 0.024, 0.025, 0.037, 0.047, 0.039, 0.022, 0.022, 0.021, 0.025, 0.019 ]) self.assertEqual([ round(x.benchmark_volatility, 3) for x in metrics.three_month_periods ], [ 0.047, 0.043, 0.050, 0.064, 0.070, 0.064, 0.049, 0.037, 0.039, 0.037 ]) self.assertEqual([ round(x.benchmark_volatility, 3) for x in metrics.six_month_periods ], [0.079, 0.082, 0.081, 0.081, 0.08, 0.074, 0.061]) self.assertEqual( [round(x.benchmark_volatility, 3) for x in metrics.year_periods], [0.100])
def test_benchmark_volatility_08(self): returns = factory.create_returns_from_range(self.sim_params08) metrics = risk.RiskReport(returns, self.sim_params08) self.assertEqual( [round(x.benchmark_volatility, 3) for x in metrics.month_periods], [ 0.07, 0.058, 0.082, 0.054, 0.041, 0.057, 0.068, 0.06, 0.157, 0.244, 0.195, 0.145 ]) self.assertEqual([ round(x.benchmark_volatility, 3) for x in metrics.three_month_periods ], [0.12, 0.113, 0.105, 0.09, 0.098, 0.107, 0.179, 0.293, 0.344, 0.34]) self.assertEqual([ round(x.benchmark_volatility, 3) for x in metrics.six_month_periods ], [0.15, 0.149, 0.15, 0.2, 0.308, 0.36, 0.383]) # TODO: ugly, but I can't get the rounded float to match. # maybe we need a different test that checks the # difference between the numbers self.assertEqual( [round(x.benchmark_volatility, 3) for x in metrics.year_periods], [0.411])
def check_year_range(self, start_date, years): sim_params = SimulationParameters( period_start=start_date, period_end=start_date.replace(year=(start_date.year + years))) returns = factory.create_returns_from_range(sim_params) metrics = risk.RiskReport(returns, self.sim_params) total_months = years * 12 self.check_metrics(metrics, total_months, start_date)
def test_trading_days_08(self): returns = factory.create_returns_from_range(self.sim_params08) metrics = risk.RiskReport(returns, self.sim_params08, env=self.env) self.assertEqual([x.num_trading_days for x in metrics.year_periods], [253]) self.assertEqual([x.num_trading_days for x in metrics.month_periods], [21, 20, 20, 22, 21, 21, 22, 21, 21, 23, 19, 22])
def setUp(self): start_date = datetime.datetime( year=2006, month=1, day=1, hour=0, minute=0, tzinfo=pytz.utc) end_date = datetime.datetime( year=2006, month=12, day=31, tzinfo=pytz.utc) self.benchmark_returns, self.treasury_curves = \ factory.load_market_data() self.trading_env = TradingEnvironment( self.benchmark_returns, self.treasury_curves, period_start=start_date, period_end=end_date ) self.onesec = datetime.timedelta(seconds=1) self.oneday = datetime.timedelta(days=1) self.tradingday = datetime.timedelta(hours=6, minutes=30) self.dt = datetime.datetime.utcnow() self.algo_returns_06 = factory.create_returns_from_list( RETURNS, self.trading_env ) self.metrics_06 = risk.RiskReport( self.algo_returns_06, self.trading_env ) start_08 = datetime.datetime( year=2008, month=1, day=1, hour=0, minute=0, tzinfo=pytz.utc) end_08 = datetime.datetime( year=2008, month=12, day=31, tzinfo=pytz.utc ) self.trading_env08 = TradingEnvironment( self.benchmark_returns, self.treasury_curves, period_start=start_08, period_end=end_08 )
def test_trading_days_06(self): returns = factory.create_returns_from_range(self.sim_params) metrics = risk.RiskReport(returns, self.sim_params, trading_calendar=self.trading_calendar, treasury_curves=self.env.treasury_curves, benchmark_returns=self.env.benchmark_returns) self.assertEqual([x.num_trading_days for x in metrics.year_periods], [251]) self.assertEqual([x.num_trading_days for x in metrics.month_periods], [20, 19, 23, 19, 22, 22, 20, 23, 20, 22, 21, 20])
def test_trading_days_08(self): returns = factory.create_returns_from_range(self.sim_params08) metrics = risk.RiskReport(returns, self.sim_params08, trading_schedule=self.trading_schedule, treasury_curves=self.env.treasury_curves, benchmark_returns=self.env.benchmark_returns) self.assertEqual([x.num_trading_days for x in metrics.year_periods], [253]) self.assertEqual([x.num_trading_days for x in metrics.month_periods], [21, 20, 20, 22, 21, 21, 22, 21, 21, 23, 19, 22])
def check_year_range(self, start_date, years): if(start_date.month <= 2): ld = calendar.leapdays(start_date.year, start_date.year + years) else: # because we may catch the leap of the last year, # and i think this func is [start,end) ld = calendar.leapdays(start_date.year, start_date.year + years + 1) returns = factory.create_returns(365 * years + ld, self.sim_params08) metrics = risk.RiskReport(returns, self.sim_params) total_months = years * 12 self.check_metrics(metrics, total_months, start_date)
def setUp(self): start_date = datetime.datetime( year=2006, month=1, day=1, hour=0, minute=0, tzinfo=pytz.utc) end_date = datetime.datetime( year=2006, month=12, day=31, tzinfo=pytz.utc) self.sim_params = SimulationParameters( period_start=start_date, period_end=end_date, env=self.env, ) self.algo_returns_06 = factory.create_returns_from_list( RETURNS, self.sim_params ) self.benchmark_returns_06 = \ answer_key.RETURNS_DATA['Benchmark Returns'] self.metrics_06 = risk.RiskReport( self.algo_returns_06, self.sim_params, benchmark_returns=self.benchmark_returns_06, env=self.env, ) start_08 = datetime.datetime( year=2008, month=1, day=1, hour=0, minute=0, tzinfo=pytz.utc) end_08 = datetime.datetime( year=2008, month=12, day=31, tzinfo=pytz.utc ) self.sim_params08 = SimulationParameters( period_start=start_08, period_end=end_08, env=self.env, )
def test_sparse_benchmark(self): benchmark_returns = self.benchmark_returns_06.copy() # Set every other day to nan. benchmark_returns.iloc[::2] = np.nan report = risk.RiskReport( self.algo_returns_06, self.sim_params, benchmark_returns=benchmark_returns, env=self.env, ) for risk_period in chain.from_iterable(itervalues(report.to_dict())): self.assertIsNone(risk_period['beta'])
def check_year_range(self, start_date, years): sim_params = SimulationParameters( period_start=start_date, period_end=start_date.replace(year=(start_date.year + years)), trading_schedule=self.trading_schedule, ) returns = factory.create_returns_from_range(sim_params) metrics = risk.RiskReport(returns, self.sim_params, trading_schedule=self.trading_schedule, treasury_curves=self.env.treasury_curves, benchmark_returns=self.env.benchmark_returns) total_months = years * 12 self.check_metrics(metrics, total_months, start_date)
def setUp(self): start_date = datetime.datetime( year=2006, month=1, day=1, hour=0, minute=0, tzinfo=pytz.utc) end_date = datetime.datetime( year=2006, month=12, day=31, tzinfo=pytz.utc) self.sim_params = SimulationParameters( period_start=start_date, period_end=end_date ) self.onesec = datetime.timedelta(seconds=1) self.oneday = datetime.timedelta(days=1) self.tradingday = datetime.timedelta(hours=6, minutes=30) self.dt = datetime.datetime.utcnow() self.algo_returns_06 = factory.create_returns_from_list( RETURNS, self.sim_params ) self.metrics_06 = risk.RiskReport( self.algo_returns_06, self.sim_params ) start_08 = datetime.datetime( year=2008, month=1, day=1, hour=0, minute=0, tzinfo=pytz.utc) end_08 = datetime.datetime( year=2008, month=12, day=31, tzinfo=pytz.utc ) self.sim_params08 = SimulationParameters( period_start=start_08, period_end=end_08 )
def init_instance_fixtures(self): super(TestRisk, self).init_instance_fixtures() start_date = datetime.datetime(year=2006, month=1, day=1, hour=0, minute=0, tzinfo=pytz.utc) end_date = datetime.datetime(year=2006, month=12, day=31, tzinfo=pytz.utc) self.sim_params = SimulationParameters( period_start=start_date, period_end=end_date, trading_schedule=self.trading_schedule, ) self.algo_returns_06 = factory.create_returns_from_list( RETURNS, self.sim_params) self.benchmark_returns_06 = \ answer_key.RETURNS_DATA['Benchmark Returns'] self.metrics_06 = risk.RiskReport( self.algo_returns_06, self.sim_params, benchmark_returns=self.benchmark_returns_06, trading_schedule=self.trading_schedule, treasury_curves=self.env.treasury_curves, ) start_08 = datetime.datetime(year=2008, month=1, day=1, hour=0, minute=0, tzinfo=pytz.utc) end_08 = datetime.datetime(year=2008, month=12, day=31, tzinfo=pytz.utc) self.sim_params08 = SimulationParameters( period_start=start_08, period_end=end_08, trading_schedule=self.trading_schedule, )
def test_benchmark_returns_06(self): returns = factory.create_returns_from_range(self.sim_params) metrics = risk.RiskReport(returns, self.sim_params) np.testing.assert_almost_equal( [x.benchmark_period_returns for x in metrics.month_periods], ANSWER_KEY.BENCHMARK_PERIOD_RETURNS['Monthly']) np.testing.assert_almost_equal( [x.benchmark_period_returns for x in metrics.three_month_periods], ANSWER_KEY.BENCHMARK_PERIOD_RETURNS['3-Month']) np.testing.assert_almost_equal( [x.benchmark_period_returns for x in metrics.six_month_periods], ANSWER_KEY.BENCHMARK_PERIOD_RETURNS['6-month']) np.testing.assert_almost_equal( [x.benchmark_period_returns for x in metrics.year_periods], ANSWER_KEY.BENCHMARK_PERIOD_RETURNS['year'])
def test_treasury_returns_06(self): returns = factory.create_returns_from_range(self.trading_env) metrics = risk.RiskReport(returns, self.trading_env) self.assertEqual([round(x.treasury_period_return, 4) for x in metrics.month_periods], [0.0037, 0.0034, 0.0039, 0.0038, 0.0040, 0.0037, 0.0043, 0.0043, 0.0038, 0.0044, 0.0043, 0.0041]) self.assertEqual([round(x.treasury_period_return, 4) for x in metrics.three_month_periods], [0.0114, 0.0118, 0.0122, 0.0125, 0.0129, 0.0127, 0.0123, 0.0128, 0.0125, 0.0128]) self.assertEqual([round(x.treasury_period_return, 4) for x in metrics.six_month_periods], [0.0260, 0.0257, 0.0258, 0.0252, 0.0259, 0.0256, 0.0258]) self.assertEqual([round(x.treasury_period_return, 4) for x in metrics.year_periods], [0.0500])
def handle_simulation_end(self): """ When the simulation is complete, run the full period risk report and send it out on the results socket. """ log_msg = "Simulated {n} trading days out of {m}." log.info(log_msg.format(n=self.day_count, m=self.total_days)) log.info( "first open: {d}".format(d=self.trading_environment.first_open)) # the stream will end on the last trading day, but will not trigger # an end of day, so we trigger the final market close here. self.handle_market_close() self.risk_report = risk.RiskReport(self.returns, self.trading_environment) risk_dict = self.risk_report.to_dict() return risk_dict
def test_benchmark_returns_06(self): returns = factory.create_returns_from_range(self.sim_params) metrics = risk.RiskReport(returns, self.sim_params) self.assertEqual([round(x.benchmark_period_returns, 4) for x in metrics.month_periods], [0.0255, 0.0004, 0.0110, 0.0057, -0.0290, 0.0021, 0.0061, 0.0221, 0.0247, 0.0324, 0.0189, 0.0139]) self.assertEqual([round(x.benchmark_period_returns, 4) for x in metrics.three_month_periods], [0.0372, 0.0171, -0.0128, -0.0214, -0.0211, 0.0305, 0.0537, 0.0813, 0.0780, 0.0666]) self.assertEqual([round(x.benchmark_period_returns, 4) for x in metrics.six_month_periods], [0.015, -0.0043, 0.0173, 0.0311, 0.0586, 0.1108, 0.1239]) self.assertEqual([round(x.benchmark_period_returns, 4) for x in metrics.year_periods], [0.1407])
def test_benchmarkrange(self): start_session = self.trading_calendar.minute_to_session_label( pd.Timestamp("2008-01-01", tz='UTC')) end_session = self.trading_calendar.minute_to_session_label( pd.Timestamp("2010-01-01", tz='UTC'), direction="previous") sim_params = SimulationParameters( start_session=start_session, end_session=end_session, trading_calendar=self.trading_calendar, ) returns = factory.create_returns_from_range(sim_params) metrics = risk.RiskReport(returns, self.sim_params, trading_calendar=self.trading_calendar, treasury_curves=self.env.treasury_curves, benchmark_returns=self.env.benchmark_returns) self.check_metrics(metrics, 24, start_session)