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, treasury_curves=self.env.treasury_curves, )
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 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.session_count), m=self.total_session_count)) 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 risk_report = risk.RiskReport( ars, self.sim_params, benchmark_returns=bms, algorithm_leverages=acl, trading_calendar=self.trading_calendar, treasury_curves=self.treasury_curves, ) return risk_report.to_dict()
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)
def _test_algorithm_sortino(self): # The sortino ratio is calculated by a empyrical function so testing # of period sortino ratios will be limited to determine if the value is # numerical. This tests for its existence and format. # This test needs a different result set that, with some # negative results, otherwise fails in a legitimate way. RETURNS = (np.random.rand(251) * 0.1) - 0.05 self.algo_returns = factory.create_returns_from_list( RETURNS, self.sim_params) self.metrics = risk.RiskReport( self.algo_returns, self.sim_params, benchmark_returns=self.benchmark_returns, trading_calendar=self.trading_calendar, treasury_curves=self.env.treasury_curves, ) for x in self.metrics.month_periods: print(type(x.sortino)) np.testing.assert_equal( all( isinstance(x.sortino, float) for x in self.metrics.month_periods), True) np.testing.assert_equal( all( isinstance(x.sortino, float) for x in self.metrics.three_month_periods), True) np.testing.assert_equal( all( isinstance(x.sortino, float) for x in self.metrics.six_month_periods), True) np.testing.assert_equal( all( isinstance(x.sortino, float) for x in self.metrics.year_periods), True)
def test_partial_month(self): start_session = self.trading_calendar.minute_to_session_label( pd.Timestamp("1993-02-01", tz='UTC') ) # 1992 and 1996 were leap years total_days = 365 * 5 + 2 end_session = start_session + datetime.timedelta(days=total_days) sim_params90s = SimulationParameters( start_session=start_session, end_session=end_session, trading_calendar=self.trading_calendar, ) 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_calendar=self.trading_calendar, treasury_curves=self.env.treasury_curves, benchmark_returns=self.env.benchmark_returns) total_months = 60 self.check_metrics(metrics, total_months, start_session)