def test_representation(self): test_period = RiskMetricsPeriod( start_session=self.start_session, end_session=self.end_session, returns=self.algo_returns, benchmark_returns=self.benchmark_returns, trading_calendar=self.trading_calendar, treasury_curves=self.env.treasury_curves, ) metrics = [ "algorithm_period_returns", "benchmark_period_returns", "excess_return", "num_trading_days", "benchmark_volatility", "algorithm_volatility", "sharpe", "sortino", "information", "beta", "alpha", "max_drawdown", "max_leverage", "algorithm_returns", "benchmark_returns", ] representation = test_period.__repr__() assert all([metric in representation for metric in metrics])
def test_representation(self): test_period = RiskMetricsPeriod( start_session=self.start_session, end_session=self.end_session, returns=self.algo_returns, benchmark_returns=self.benchmark_returns, trading_calendar=self.trading_calendar, treasury_curves=self.env.treasury_curves, ) metrics = [ "algorithm_period_returns", "benchmark_period_returns", "excess_return", "num_trading_days", "benchmark_volatility", "algorithm_volatility", "sharpe", "sortino", "information", "beta", "alpha", "max_drawdown", "max_leverage", "algorithm_returns", "benchmark_returns", ] representation = test_period.__repr__() assert all([metric in representation for metric in metrics])
def test_algorithm_leverages(self): # Max leverage for an algorithm with 'None' as leverage is 0. np.testing.assert_equal( [x.max_leverage for x in self.metrics.month_periods], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]) np.testing.assert_equal( [x.max_leverage for x in self.metrics.three_month_periods], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]) np.testing.assert_equal( [x.max_leverage for x in self.metrics.six_month_periods], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]) np.testing.assert_equal( [x.max_leverage for x in self.metrics.year_periods], [0.0]) test_period = RiskMetricsPeriod( start_session=self.start_session, end_session=self.end_session, returns=self.algo_returns, benchmark_returns=self.benchmark_returns, trading_calendar=self.trading_calendar, algorithm_leverages=[.01, .02, .03], ) # This return period has a list instead of None for algorithm_leverages # Confirm that max_leverage is set to the max of those values assert test_period.max_leverage == .03
def test_sharpe_value_when_null(self): # Sharpe is displayed as '0.0' instead of np.nan null_returns = factory.create_returns_from_list([0.0] * 251, self.sim_params) test_period = RiskMetricsPeriod( start_session=self.start_session, end_session=self.end_session, returns=null_returns, benchmark_returns=self.benchmark_returns, trading_calendar=self.trading_calendar, treasury_curves=self.env.treasury_curves, ) assert test_period.sharpe == 0.0
def test_returns_beyond_treasury(self): # The last treasury value is used when return dates go beyond # treasury curve data treasury_curves = self.env.treasury_curves treasury = treasury_curves[treasury_curves.index < self.start_session] test_period = RiskMetricsPeriod( start_session=self.start_session, end_session=self.end_session, returns=self.algo_returns, benchmark_returns=self.benchmark_returns, trading_calendar=self.trading_calendar, treasury_curves=treasury, algorithm_leverages=[.01, .02, .03]) assert test_period.treasury_curves.equals(treasury[-1:]) # This return period has a list instead of None for algorithm_leverages # Confirm that max_leverage is set to the max of those values assert test_period.max_leverage == .03
def test_index_mismatch_exception(self): # An exception is raised when returns and benchmark returns # have indexes that do not match bench_params = SimulationParameters( start_session=pd.Timestamp("2006-02-01", tz='UTC'), end_session=pd.Timestamp("2006-02-28", tz='UTC'), trading_calendar=self.trading_calendar, ) benchmark = factory.create_returns_from_list([BENCHMARK_BASE] * 19, bench_params) with np.testing.assert_raises(Exception): RiskMetricsPeriod( start_session=self.start_session, end_session=self.end_session, returns=self.algo_returns, benchmark_returns=benchmark, trading_calendar=self.trading_calendar, treasury_curves=self.env.treasury_curves, )