Пример #1
0
 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,
     )
Пример #2
0
    def test_simulation_parameters(self):
        sp = SimulationParameters(
            start_session=pd.Timestamp("2008-01-01", tz='UTC'),
            end_session=pd.Timestamp("2008-12-31", tz='UTC'),
            capital_base=100000,
            trading_calendar=self.trading_calendar,
        )

        self.assertTrue(sp.last_close.month == 12)
        self.assertTrue(sp.last_close.day == 31)
Пример #3
0
 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,
         )
Пример #4
0
    def init_instance_fixtures(self):
        super(TestRisk, self).init_instance_fixtures()

        start_session = pd.Timestamp("2006-01-01", tz='UTC')
        end_session = pd.Timestamp("2006-12-29", tz='UTC')

        self.sim_params = SimulationParameters(
            start_session=start_session,
            end_session=end_session,
            trading_calendar=self.trading_calendar,
        )
        self.algo_returns = factory.create_returns_from_list(
            RETURNS, self.sim_params)
        self.cumulative_metrics = risk.RiskMetricsCumulative(
            self.sim_params,
            treasury_curves=self.env.treasury_curves,
            trading_calendar=self.trading_calendar,
        )
        for dt, returns in self.algo_returns.iteritems():
            self.cumulative_metrics.update(dt, returns, BENCHMARK_BASE, 0.0)
Пример #5
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)
Пример #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 = 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)
Пример #7
0
    def test_sim_params_days_in_period(self):

        #     January 2008
        #  Su Mo Tu We Th Fr Sa
        #         1  2  3  4  5
        #   6  7  8  9 10 11 12
        #  13 14 15 16 17 18 19
        #  20 21 22 23 24 25 26
        #  27 28 29 30 31

        params = SimulationParameters(
            start_session=pd.Timestamp("2007-12-31", tz='UTC'),
            end_session=pd.Timestamp("2008-01-07", tz='UTC'),
            capital_base=100000,
            trading_calendar=self.trading_calendar,
        )

        expected_trading_days = (
            datetime(2007, 12, 31, tzinfo=pytz.utc),
            # Skip new years
            # holidays taken from: http://www.nyse.com/press/1191407641943.html
            datetime(2008, 1, 2, tzinfo=pytz.utc),
            datetime(2008, 1, 3, tzinfo=pytz.utc),
            datetime(2008, 1, 4, tzinfo=pytz.utc),
            # Skip Saturday
            # Skip Sunday
            datetime(2008, 1, 7, tzinfo=pytz.utc)
        )

        num_expected_trading_days = 5
        self.assertEquals(
            num_expected_trading_days,
            len(params.sessions)
        )
        np.testing.assert_array_equal(expected_trading_days,
                                      params.sessions.tolist())
Пример #8
0
def create_simulation_parameters(year=2016,
                                 start=None,
                                 end=None,
                                 capital_base=float("1.0e5"),
                                 num_days=None,
                                 data_frequency='daily',
                                 emission_rate='daily',
                                 trading_calendar=None):

    if not trading_calendar:
        trading_calendar = get_calendar("OPEN")

    if start is None:
        start = pd.Timestamp("{0}-01-01".format(year), tz='UTC')
    elif type(start) == datetime:
        start = pd.Timestamp(start)

    if end is None:
        if num_days:
            start_index = trading_calendar.all_sessions.searchsorted(start)
            end = trading_calendar.all_sessions[start_index + num_days - 1]
        else:
            end = pd.Timestamp("{0}-12-31".format(year), tz='UTC')
    elif type(end) == datetime:
        end = pd.Timestamp(end)

    sim_params = SimulationParameters(
        start_session=start,
        end_session=end,
        capital_base=capital_base,
        data_frequency=data_frequency,
        emission_rate=emission_rate,
        trading_calendar=trading_calendar,
    )

    return sim_params
Пример #9
0
    def _test_simple_transforms(self):
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("ignore", PerformanceWarning)
            warnings.simplefilter("default", ZiplineDeprecationWarning)

            sim_params = SimulationParameters(
                start_session=self.sim_params.sessions[8],
                end_session=self.sim_params.sessions[-1],
                data_frequency="minute",
                trading_calendar=self.trading_calendar,
            )

            algo = self.create_algo(simple_transforms_algo,
                                    sim_params=sim_params)
            algo.run(self.data_portal)

            self.assertEqual(8, len(w))
            transforms = ["mavg", "vwap", "stddev", "returns"]

            for idx, line_no in enumerate(range(8, 12)):
                warning1 = w[idx * 2]
                warning2 = w[(idx * 2) + 1]

                self.assertEqual("<string>", warning1.filename)
                self.assertEqual("<string>", warning2.filename)

                self.assertEqual(line_no, warning1.lineno)
                self.assertEqual(line_no, warning2.lineno)

                self.assertEqual(
                    "`data[sid(N)]` is deprecated. Use "
                    "`data.current`.", str(warning1.message))
                self.assertEqual(
                    "The `{0}` method is "
                    "deprecated.".format(transforms[idx]),
                    str(warning2.message))

            # now verify the transform values
            # minute price
            # 2016-01-11 14:31:00+00:00    1561
            # ...
            # 2016-01-14 20:59:00+00:00    3119
            # 2016-01-14 21:00:00+00:00    3120
            # 2016-01-15 14:31:00+00:00    3121
            # 2016-01-15 14:32:00+00:00    3122
            # 2016-01-15 14:33:00+00:00    3123

            # volume
            # 2016-01-11 14:31:00+00:00    156100
            # ...
            # 2016-01-14 20:59:00+00:00    311900
            # 2016-01-14 21:00:00+00:00    312000
            # 2016-01-15 14:31:00+00:00    312100
            # 2016-01-15 14:32:00+00:00    312200
            # 2016-01-15 14:33:00+00:00    312300

            # daily price (last day built with minute data)
            # 2016-01-14 00:00:00+00:00       9
            # 2016-01-15 00:00:00+00:00    3123

            # mavg = average of all the prices = (1561 + 3123) / 2 = 2342
            # vwap = sum(price * volume) / sum(volumes)
            #      = 889119531400.0 / 366054600.0
            #      = 2428.9259891830343
            # stddev = stddev(price, ddof=1) = 451.3435498597493
            # returns = (todayprice - yesterdayprice) / yesterdayprice
            #         = (3123 - 9) / 9 = 346
            self.assertEqual(2342, algo.mavg)
            self.assertAlmostEqual(2428.92599, algo.vwap, places=5)
            self.assertAlmostEqual(451.34355, algo.stddev, places=5)
            self.assertAlmostEqual(346, algo.returns)
Пример #10
0
    def _test_old_new_data_api_paths(self):
        """
        Test that the new and old data APIs hit the same code paths.

        We want to ensure that the old data API(data[sid(N)].field and
        similar)  and the new data API(data.current(sid(N), field) and
        similar) hit the same code paths on the DataPortal.
        """
        test_start_minute = self.trading_calendar.minutes_for_session(
            self.sim_params.sessions[0])[1]
        test_end_minute = self.trading_calendar.minutes_for_session(
            self.sim_params.sessions[0])[-1]
        bar_data = self.create_bardata(lambda: test_end_minute, )
        ohlcvp_fields = [
            "open",
            "high",
            "low"
            "close",
            "volume",
            "price",
        ]
        spot_value_meth = 'catalyst.data.data_portal.DataPortal.get_spot_value'

        def assert_get_spot_value_called(fun, field):
            """
            Assert that get_spot_value was called during the execution of fun.

            Takes in a function fun and a string field.
            """
            with patch(spot_value_meth) as gsv:
                fun()
                gsv.assert_called_with(self.asset1, field, test_end_minute,
                                       'minute')

        # Ensure that data.current(sid(n), field) has the same behaviour as
        # data[sid(n)].field.
        for field in ohlcvp_fields:
            assert_get_spot_value_called(
                lambda: getattr(bar_data[self.asset1], field),
                field,
            )
            assert_get_spot_value_called(
                lambda: bar_data.current(self.asset1, field),
                field,
            )

        history_meth =\
            'catalyst.data.data_portal.DataPortal.get_history_window'

        def assert_get_history_window_called(fun, is_legacy):
            """
            Assert that get_history_window was called during fun().

            Takes in a function fun and a boolean is_legacy.
            """
            with patch(history_meth) as ghw:
                fun()
                # Slightly hacky, but done to get around the fact that
                # history( explicitly passes an ffill param as the last arg,
                # while data.history doesn't.
                if is_legacy:
                    ghw.assert_called_with(
                        [self.asset1, self.asset2, self.asset3],
                        test_end_minute, 5, "1m", "volume", "minute", True)
                else:
                    ghw.assert_called_with(
                        [self.asset1, self.asset2, self.asset3],
                        test_end_minute,
                        5,
                        "1m",
                        "volume",
                        "minute",
                    )

        test_sim_params = SimulationParameters(
            start_session=test_start_minute,
            end_session=test_end_minute,
            data_frequency="minute",
            trading_calendar=self.trading_calendar,
        )

        history_algorithm = self.create_algo(history_algo,
                                             sim_params=test_sim_params)
        assert_get_history_window_called(
            lambda: history_algorithm.run(self.data_portal), is_legacy=True)
        assert_get_history_window_called(lambda: bar_data.history(
            [self.asset1, self.asset2, self.asset3], "volume", 5, "1m"),
                                         is_legacy=False)