Example #1
0
 def test_trading_days_06(self):
     returns = factory.create_returns_from_range(self.trading_env)
     metrics = risk.RiskReport(returns, self.trading_env)
     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])
Example #2
0
    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])
Example #3
0
    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])
Example #4
0
    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])
Example #5
0
    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),
        )
Example #6
0
    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 = ClassicRiskMetrics.risk_report(
            algorithm_returns=returns,
            # use returns from the fixture to ensure that we have enough data.
            benchmark_returns=self.BENCHMARK_RETURNS,
            algorithm_leverages=pd.Series(0.0, index=returns.index),
        )
        total_months = 60
        self.check_metrics(metrics, total_months, start_session)
Example #7
0
    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])
Example #8
0
    def test_benchmark_volatility_06(self):
        returns = factory.create_returns_from_range(self.sim_params)
        metrics = risk.RiskReport(returns, self.sim_params)
        answer_key_month_periods = ANSWER_KEY.get_values(
            AnswerKey.BENCHMARK_PERIOD_VOLATILITY['Monthly'],
            decimal=3)
        self.assertEqual([np.round(x.benchmark_volatility, 3)
                          for x in metrics.month_periods],
                         answer_key_month_periods)

        answer_key_three_month_periods = ANSWER_KEY.get_values(
            AnswerKey.BENCHMARK_PERIOD_VOLATILITY['3-Month'],
            decimal=3)
        self.assertEqual([np.round(x.benchmark_volatility, 3)
                          for x in metrics.three_month_periods],
                         answer_key_three_month_periods)

        answer_key_six_month_periods = ANSWER_KEY.get_values(
            AnswerKey.BENCHMARK_PERIOD_VOLATILITY['6-month'],
            decimal=3)
        self.assertEqual([np.round(x.benchmark_volatility, 3)
                          for x in metrics.six_month_periods],
                         answer_key_six_month_periods)

        answer_key_year_periods = ANSWER_KEY.get_values(
            AnswerKey.BENCHMARK_PERIOD_VOLATILITY['year'],
            decimal=3)
        self.assertEqual([np.round(x.benchmark_volatility, 3)
                          for x in metrics.year_periods],
                         answer_key_year_periods)
Example #9
0
    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 = ClassicRiskMetrics.risk_report(
            algorithm_returns=returns,
            # use returns from the fixture to ensure that we have enough data.
            benchmark_returns=self.BENCHMARK_RETURNS,
            algorithm_leverages=pd.Series(0.0, index=returns.index)
        )
        total_months = 60
        self.check_metrics(metrics, total_months, start_session)
Example #10
0
    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)
Example #11
0
    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)
Example #12
0
 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])
Example #13
0
    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])
Example #14
0
    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])
Example #15
0
    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])
Example #16
0
 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)
Example #17
0
 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)), env=self.env
     )
     returns = factory.create_returns_from_range(sim_params)
     metrics = risk.RiskReport(returns, self.sim_params, env=self.env)
     total_months = years * 12
     self.check_metrics(metrics, total_months, start_date)
Example #18
0
    def test_trading_days_08(self):
        returns = factory.create_returns_from_range(self.sim_params08)
        metrics = risk.RiskReport(returns, self.sim_params08)
        self.assertEqual([x.trading_days for x in metrics.year_periods],
                         [253])

        self.assertEqual([x.trading_days for x in metrics.month_periods],
                         [21, 20, 20, 22, 21, 21, 22, 21, 21, 23, 19, 22])
Example #19
0
 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])
Example #20
0
    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])
Example #21
0
    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])
Example #22
0
 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)
Example #23
0
 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)
Example #24
0
    def test_treasury_returns(self):
        returns = factory.create_returns_from_range(self.sim_params)
        metrics = ClassicRiskMetrics.risk_report(
            algorithm_returns=returns,
            benchmark_returns=self.benchmark_returns,
            algorithm_leverages=pd.Series(0.0, index=returns.index))

        # These values are all expected to be zero because we explicity zero
        # out the treasury period returns as they are no longer actually used.
        for period in PERIODS:
            self.assertEqual(
                [x['treasury_period_return'] for x in metrics[period]],
                [0.0] * len(metrics[period]),
            )
Example #25
0
    def test_benchmark_volatility_08(self):
        returns = factory.create_returns_from_range(self.sim_params08)
        metrics = risk.RiskReport(returns, self.sim_params08,
                                  trading_calendar=self.trading_calendar,
                                  treasury_curves=self.env.treasury_curves,
                                  benchmark_returns=self.env.benchmark_returns)

        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])
Example #26
0
 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'])
Example #27
0
    def test_treasury_returns(self):
        returns = factory.create_returns_from_range(self.sim_params)
        metrics = ClassicRiskMetrics.risk_report(
            algorithm_returns=returns,
            benchmark_returns=self.env.benchmark_returns,
            algorithm_leverages=pd.Series(0.0, index=returns.index)
        )

        # These values are all expected to be zero because we explicity zero
        # out the treasury period returns as they are no longer actually used.
        for period in PERIODS:
            self.assertEqual(
              [x['treasury_period_return'] for x in metrics[period]],
              [0.0] * len(metrics[period]),
            )
Example #28
0
    def test_benchmark_returns_08(self):
        returns = factory.create_returns_from_range(self.sim_params08)
        metrics = risk.RiskReport(returns, self.sim_params08,
                                  trading_calendar=self.trading_calendar,
                                  treasury_curves=self.env.treasury_curves,
                                  benchmark_returns=self.env.benchmark_returns)

        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])
Example #29
0
    def test_benchmark_returns_08(self):
        returns = factory.create_returns_from_range(self.trading_env08)
        metrics = risk.RiskReport(returns, self.trading_env08)

        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])
Example #30
0
    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])
Example #31
0
    def test_benchmark_volatility_08(self):
        returns = factory.create_returns_from_range(self.trading_env08)
        metrics = risk.RiskReport(returns, self.trading_env08)
        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])
Example #32
0
    def test_treasury_returns_06(self):
        returns = factory.create_returns_from_range(self.sim_params)
        metrics = risk.RiskReport(returns, self.sim_params, env=self.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.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])
Example #33
0
    def test_benchmark_volatility_06(self):
        returns = factory.create_returns_from_range(self.trading_env)
        metrics = risk.RiskReport(returns, self.trading_env)
        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])
Example #34
0
 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'])
Example #35
0
    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])
Example #36
0
 def test_benchmark_returns_06(self):
     returns = factory.create_returns_from_range(self.trading_env)
     metrics = risk.RiskReport(returns, self.trading_env)
     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])
Example #37
0
 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])
Example #38
0
    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)
Example #39
0
    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 = ClassicRiskMetrics.risk_report(
            algorithm_returns=returns,
            # use returns from the fixture to ensure that we have enough data.
            benchmark_returns=self.BENCHMARK_RETURNS,
            algorithm_leverages=pd.Series(0.0, index=returns.index))

        self.check_metrics(metrics, 24, start_session)
Example #40
0
    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)
Example #41
0
    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 = ClassicRiskMetrics.risk_report(
            algorithm_returns=returns,
            benchmark_returns=self.env.benchmark_returns,
            algorithm_leverages=pd.Series(0.0, index=returns.index)
        )

        self.check_metrics(metrics, 24, start_session)
Example #42
0
    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)
Example #43
0
 def test_benchmark_returns_06(self):
     returns = factory.create_returns_from_range(self.sim_params)
     metrics = risk.RiskReport(returns, self.sim_params)
     answer_key_month_periods = ANSWER_KEY.get_values(
         AnswerKey.BENCHMARK_PERIOD_RETURNS['Monthly'])
     self.assertEqual([round(x.benchmark_period_returns, 4)
                       for x in metrics.month_periods],
                      answer_key_month_periods)
     answer_key_three_month_periods = ANSWER_KEY.get_values(
         AnswerKey.BENCHMARK_PERIOD_RETURNS['3-Month'])
     self.assertEqual([round(x.benchmark_period_returns, 4)
                       for x in metrics.three_month_periods],
                      answer_key_three_month_periods)
     answer_key_six_month_periods = ANSWER_KEY.get_values(
         AnswerKey.BENCHMARK_PERIOD_RETURNS['6-month'])
     self.assertEqual([round(x.benchmark_period_returns, 4)
                       for x in metrics.six_month_periods],
                      answer_key_six_month_periods)
     answer_key_year_periods = ANSWER_KEY.get_values(
         AnswerKey.BENCHMARK_PERIOD_RETURNS['year'])
     self.assertEqual([round(x.benchmark_period_returns, 4)
                       for x in metrics.year_periods],
                      answer_key_year_periods)
Example #44
0
    def test_partial_month(self):

        start_session = self.trading_calendar.minute_to_session_label(
            pd.Timestamp("1991-01-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)