def test_drawdown(self): returns = factory.create_returns_from_list( [1.0, -0.5, 0.8, .17, 1.0, -0.1, -0.45], self.sim_params) #200, 100, 180, 210.6, 421.2, 379.8, 208.494 metrics = risk.RiskMetricsBatch(returns[0].date, returns[-1].date, returns) self.assertEqual(metrics.max_drawdown, 0.505)
def test_risk_metrics_returns(self): trading.environment = trading.TradingEnvironment() # Advance start date to first date in the trading calendar if trading.environment.is_trading_day(self.start_date): start_date = self.start_date else: start_date = trading.environment.next_trading_day(self.start_date) self.all_benchmark_returns = pd.Series({ x.date: x.returns for x in trading.environment.benchmark_returns if x.date >= self.start_date }) start_index = trading.environment.trading_days.searchsorted(start_date) end_date = trading.environment.trading_days[start_index + len(RETURNS)] sim_params = SimulationParameters(start_date, end_date) risk_metrics_refactor = risk.RiskMetricsIterative(sim_params) todays_date = start_date cur_returns = [] for i, ret in enumerate(RETURNS): todays_return_obj = DailyReturn(todays_date, ret) cur_returns.append(todays_return_obj) try: risk_metrics_original = risk.RiskMetricsBatch( start_date=start_date, end_date=todays_date, returns=cur_returns) except Exception as e: #assert that when original raises exception, same #exception is raised by risk_metrics_refactor np.testing.assert_raises( type(e), risk_metrics_refactor.update, todays_date, self.all_benchmark_returns[todays_return_obj.date]) continue risk_metrics_refactor.update( todays_date, ret, self.all_benchmark_returns[todays_return_obj.date]) # Move forward day counter to next trading day todays_date = trading.environment.next_trading_day(todays_date) self.assertEqual(risk_metrics_original.start_date, risk_metrics_refactor.start_date) self.assertEqual(risk_metrics_original.end_date, risk_metrics_refactor.algorithm_returns.index[-1]) self.assertEqual(risk_metrics_original.treasury_period_return, risk_metrics_refactor.treasury_period_return) np.testing.assert_allclose(risk_metrics_original.benchmark_returns, risk_metrics_refactor.benchmark_returns, rtol=0.001) np.testing.assert_allclose(risk_metrics_original.algorithm_returns, risk_metrics_refactor.algorithm_returns, rtol=0.001) risk_original_dict = risk_metrics_original.to_dict() risk_refactor_dict = risk_metrics_refactor.to_dict() self.assertEqual(set(risk_original_dict.keys()), set(risk_refactor_dict.keys())) err_msg_format = """\ "In update step {iter}: {measure} should be {truth} but is {returned}!""" for measure in risk_original_dict.iterkeys(): if measure == 'max_drawdown': np.testing.assert_almost_equal( risk_refactor_dict[measure], risk_original_dict[measure], err_msg=err_msg_format.format( iter=i, measure=measure, truth=risk_original_dict[measure], returned=risk_refactor_dict[measure])) else: if isinstance(risk_original_dict[measure], numbers.Real): np.testing.assert_allclose( risk_original_dict[measure], risk_refactor_dict[measure], rtol=0.001, err_msg=err_msg_format.format( iter=i, measure=measure, truth=risk_original_dict[measure], returned=risk_refactor_dict[measure])) else: np.testing.assert_equal( risk_original_dict[measure], risk_refactor_dict[measure], err_msg=err_msg_format.format( iter=i, measure=measure, truth=risk_original_dict[measure], returned=risk_refactor_dict[measure]))
def test_risk_metrics_returns(self): risk_metrics_refactor = risk.RiskMetricsIterative( self.start_date, self.trading_env) todays_date = self.start_date cur_returns = [] for i, ret in enumerate(RETURNS): todays_return_obj = risk.DailyReturn( todays_date, ret ) cur_returns.append(todays_return_obj) # Move forward day counter to next trading day todays_date += self.oneday while not self.trading_env.is_trading_day(todays_date): todays_date += self.oneday try: risk_metrics_original = risk.RiskMetricsBatch( start_date=self.start_date, end_date=todays_date, returns=cur_returns, trading_environment=self.trading_env ) except Exception as e: #assert that when original raises exception, same #exception is raised by risk_metrics_refactor np.testing.assert_raises( type(e), risk_metrics_refactor.update, ret) continue risk_metrics_refactor.update(ret) self.assertEqual( risk_metrics_original.start_date, risk_metrics_refactor.start_date) self.assertEqual( risk_metrics_original.end_date, risk_metrics_refactor.end_date) self.assertEqual( risk_metrics_original.treasury_duration, risk_metrics_refactor.treasury_duration) self.assertEqual( risk_metrics_original.treasury_curve, risk_metrics_refactor.treasury_curve) self.assertEqual( risk_metrics_original.treasury_period_return, risk_metrics_refactor.treasury_period_return) self.assertEqual( risk_metrics_original.benchmark_returns, risk_metrics_refactor.benchmark_returns) self.assertEqual( risk_metrics_original.algorithm_returns, risk_metrics_refactor.algorithm_returns) risk_original_dict = risk_metrics_original.to_dict() risk_refactor_dict = risk_metrics_refactor.to_dict() self.assertEqual(set(risk_original_dict.keys()), set(risk_refactor_dict.keys())) err_msg_format = """\ "In update step {iter}: {measure} should be {truth} but is {returned}!""" for measure in risk_original_dict.iterkeys(): if measure == 'max_drawdown': np.testing.assert_almost_equal( risk_refactor_dict[measure], risk_original_dict[measure], err_msg=err_msg_format.format( iter=i, measure=measure, truth=risk_original_dict[measure], returned=risk_refactor_dict[measure])) else: np.testing.assert_equal( risk_original_dict[measure], risk_refactor_dict[measure], err_msg_format.format( iter=i, measure=measure, truth=risk_original_dict[measure], returned=risk_refactor_dict[measure]) )