Пример #1
0
    def test_event_window(self):
        algo = BatchTransformAlgorithm()
        algo.run(self.source)

        self.assertEqual(algo.history_return_price_class[:2],
                         [None, None],
                         "First two iterations should return None")
        self.assertEqual(algo.history_return_price_decorator[:2],
                         [None, None],
                         "First two iterations should return None")
        self.assertEqual(algo.history_return_price_market_aware[:2],
                         [None, None],
                         "First two iterations should return None")

        # test overloaded class
        for test_history in [algo.history_return_price_class,
                             algo.history_return_price_decorator]:
            np.testing.assert_array_equal(
                    range(2, 8),
                    test_history[2].values.flatten()
            )

            np.testing.assert_array_equal(
                    range(2, 8),
                    test_history[3].values.flatten()
            )

            np.testing.assert_array_equal(
                    range(4, 12),
                    test_history[4].values.flatten()
            )
Пример #2
0
    def test_passing_of_args(self):
        algo = BatchTransformAlgorithm(1,
                                       kwarg='str',
                                       sim_params=self.sim_params,
                                       env=self.env)
        algo.run(self.source)
        self.assertEqual(algo.args, (1, ))
        self.assertEqual(algo.kwargs, {'kwarg': 'str'})

        expected_item = ((1, ), {'kwarg': 'str'})
        self.assertEqual(
            algo.history_return_args,
            [
                # 1990-01-01 - market holiday, no event
                # 1990-01-02 - window not full
                None,
                # 1990-01-03 - window not full
                None,
                # 1990-01-04 - window now full, 3rd event
                expected_item,
                # 1990-01-05 - window now full
                expected_item,
                # 1990-01-08 - window now full
                expected_item
            ])
Пример #3
0
    def test_event_window(self):
        algo = BatchTransformAlgorithm()
        algo.run(self.source)

        self.assertEqual(algo.history_return_price_class[:2], [None, None],
                         "First two iterations should return None")
        self.assertEqual(algo.history_return_price_decorator[:2], [None, None],
                         "First two iterations should return None")
        self.assertEqual(algo.history_return_price_market_aware[:2],
                         [None, None],
                         "First two iterations should return None")
        self.assertEqual(algo.history_return_more_days_than_refresh[:3],
                         [None, None, None],
                         "First five iterations should return None")
        self.assertTrue(
            isinstance(algo.history_return_more_days_than_refresh[4],
                       pd.DataFrame), "Sixth iteration should not be None")

        # test overloaded class
        for test_history in [
                algo.history_return_price_class,
                algo.history_return_price_decorator
        ]:
            np.testing.assert_array_equal(range(2, 8),
                                          test_history[2].values.flatten())

            np.testing.assert_array_equal(range(2, 8),
                                          test_history[3].values.flatten())

            np.testing.assert_array_equal(range(4, 12),
                                          test_history[4].values.flatten())
Пример #4
0
    def test_event_window(self):
        algo = BatchTransformAlgorithm()
        algo.run(self.source)
        wl = algo.window_length
        # The following assertion depend on window length of 3
        self.assertEqual(wl, 3)
        self.assertEqual(
            algo.history_return_price_class[:wl], [None] * wl,
            "First three iterations should return None." + "\n" +
            "i.e. no returned values until window is full'" + "%s" %
            (algo.history_return_price_class, ))
        self.assertEqual(
            algo.history_return_price_decorator[:wl], [None] * wl,
            "First three iterations should return None." + "\n" +
            "i.e. no returned values until window is full'" + "%s" %
            (algo.history_return_price_decorator, ))

        # After three Nones, the next value should be a data frame
        self.assertTrue(
            isinstance(algo.history_return_price_class[wl], pd.DataFrame))

        # Test whether arbitrary fields can be added to datapanel
        field = algo.history_return_arbitrary_fields[-1]
        self.assertTrue('arbitrary' in field.items,
                        'datapanel should contain column arbitrary')

        self.assertTrue(
            all(field['arbitrary'].values.flatten() == [123] *
                algo.window_length),
            'arbitrary dataframe should contain only "test"')

        for data in algo.history_return_sid_filter[wl:]:
            self.assertIn(0, data.columns)
            self.assertNotIn(1, data.columns)

        for data in algo.history_return_field_filter[wl:]:
            self.assertIn('price', data.items)
            self.assertNotIn('ignore', data.items)

        for data in algo.history_return_field_no_filter[wl:]:
            self.assertIn('price', data.items)
            self.assertIn('ignore', data.items)

        for data in algo.history_return_ticks[wl:]:
            self.assertTrue(isinstance(data, deque))

        for data in algo.history_return_not_full:
            self.assertIsNot(data, None)

        # test overloaded class
        for test_history in [
                algo.history_return_price_class,
                algo.history_return_price_decorator
        ]:
            # starting at window length, the window should contain
            # consecutive (of window length) numbers up till the end.
            for i in range(algo.window_length, len(test_history)):
                np.testing.assert_array_equal(
                    range(i - algo.window_length + 1, i + 1),
                    test_history[i].values.flatten())
Пример #5
0
    def test_event_window(self):
        algo = BatchTransformAlgorithm()
        algo.run(self.source)
        wl = algo.window_length
        # The following assertion depend on window length of 3
        self.assertEqual(wl, 3)
        self.assertEqual(
            algo.history_return_price_class[:wl],
            [None] * wl,
            "First three iterations should return None."
            + "\n"
            + "i.e. no returned values until window is full'"
            + "%s" % (algo.history_return_price_class,),
        )
        self.assertEqual(
            algo.history_return_price_decorator[:wl],
            [None] * wl,
            "First three iterations should return None."
            + "\n"
            + "i.e. no returned values until window is full'"
            + "%s" % (algo.history_return_price_decorator,),
        )
        # After three Nones, the next value should be a data frame
        self.assertTrue(isinstance(algo.history_return_price_class[wl], pd.DataFrame))

        # Test whether arbitrary fields can be added to datapanel
        field = algo.history_return_arbitrary_fields[-1]
        self.assertTrue("arbitrary" in field.items, "datapanel should contain column arbitrary")

        self.assertTrue(
            all(field["arbitrary"].values.flatten() == [123] * algo.window_length),
            'arbitrary dataframe should contain only "test"',
        )

        for data in algo.history_return_sid_filter[wl:]:
            self.assertIn(0, data.columns)
            self.assertNotIn(1, data.columns)

        for data in algo.history_return_field_filter[wl:]:
            self.assertIn("price", data.items)
            self.assertNotIn("ignore", data.items)

        for data in algo.history_return_field_no_filter[wl:]:
            self.assertIn("price", data.items)
            self.assertIn("ignore", data.items)

        for data in algo.history_return_ticks[wl:]:
            self.assertTrue(isinstance(data, deque))

        for data in algo.history_return_not_full:
            self.assertIsNot(data, None)

        # test overloaded class
        for test_history in [algo.history_return_price_class, algo.history_return_price_decorator]:
            # starting at window length, the window should contain
            # consecutive (of window length) numbers up till the end.
            for i in range(algo.window_length, len(test_history)):
                np.testing.assert_array_equal(
                    range(i - algo.window_length + 1, i + 1), test_history[i].values.flatten()
                )
Пример #6
0
    def test_event_window(self):
        algo = BatchTransformAlgorithm()
        algo.run(self.source)
        wl = algo.window_length
        self.assertEqual(algo.history_return_price_class[:wl],
                         [None] * wl,
                         "First two iterations should return None")
        self.assertEqual(algo.history_return_price_decorator[:wl],
                         [None] * wl,
                         "First two iterations should return None")
        self.assertTrue(isinstance(
            algo.history_return_price_class[wl + 1],
            pd.DataFrame)
        )

        # Test whether arbitrary fields can be added to datapanel
        field = algo.history_return_arbitrary_fields[-1]
        self.assertTrue(
            'arbitrary' in field.items,
            'datapanel should contain column arbitrary'
        )

        self.assertTrue(all(
            field['arbitrary'].values.flatten() ==
            [123] * algo.window_length),
            'arbitrary dataframe should contain only "test"'
        )

        for data in algo.history_return_sid_filter[wl:]:
            self.assertIn(0, data.columns)
            self.assertNotIn(1, data.columns)

        for data in algo.history_return_field_filter[wl:]:
            self.assertIn('price', data.items)
            self.assertNotIn('ignore', data.items)

        for data in algo.history_return_field_no_filter[wl:]:
            self.assertIn('price', data.items)
            self.assertIn('ignore', data.items)

        for data in algo.history_return_ticks[wl:]:
            self.assertTrue(isinstance(data, deque))

        for data in algo.history_return_not_full:
            self.assertIsNot(data, None)

        # test overloaded class
        for test_history in [algo.history_return_price_class,
                             algo.history_return_price_decorator]:
            # starting at window length, the window should contain
            # consecutive (of window length) numbers up till the end.
            for i in range(algo.window_length, len(test_history)):
                np.testing.assert_array_equal(
                    range(i - algo.window_length + 1, i + 1),
                    test_history[i].values.flatten()
                )
Пример #7
0
    def test_passing_of_args(self):
        algo = BatchTransformAlgorithm(1, kwarg='str')
        self.assertEqual(algo.args, (1,))
        self.assertEqual(algo.kwargs, {'kwarg': 'str'})

        algo.run(self.source)
        expected_item = ((1, ), {'kwarg': 'str'})
        self.assertEqual(
            algo.history_return_args,
            [None, None, expected_item, expected_item,
             expected_item, expected_item])
Пример #8
0
    def test_event_window(self):
        days = 50
        algo = BatchTransformAlgorithm(days=days, refresh_period=days)
        algo.run(self.data)

        self.assertEqual(
            algo.history_return_price_market_aware[:days], [None] * days,
            "First {days} iterations should return None".format(days=days))
        self.assertFalse(
            algo.history_return_price_market_aware[days + 1] is None,
            "Window is contains too many Nones.")
Пример #9
0
    def test_passing_of_args(self):
        algo = BatchTransformAlgorithm(1, kwarg='str')
        self.assertEqual(algo.args, (1, ))
        self.assertEqual(algo.kwargs, {'kwarg': 'str'})

        algo.run(self.source)
        expected_item = ((1, ), {'kwarg': 'str'})
        self.assertEqual(algo.history_return_args, [
            None, None, expected_item, expected_item, expected_item,
            expected_item
        ])
Пример #10
0
    def test_event_window(self):
        days = 50
        algo = BatchTransformAlgorithm(days=days, refresh_period=days)
        algo.run(self.data)

        self.assertEqual(algo.history_return_price_market_aware[:days],
                         [None] * days,
                         "First {days} iterations should return None"
                         .format(days=days))
        self.assertFalse(algo.history_return_price_market_aware[days + 1]
                         is None,
                         "Window is contains too many Nones.")
Пример #11
0
 def test_core_functionality(self):
     algo = BatchTransformAlgorithm(sim_params=self.sim_params)
     algo.run(self.source)
     wl = algo.window_length
     # The following assertion depend on window length of 3
     self.assertEqual(wl, 3)
     # If window_length is 3, there should be 2 None events, as the
     # window fills up on the 3rd day.
     n_none_events = 2
     self.assertEqual(algo.history_return_price_class[:n_none_events],
                      [None] * n_none_events,
                      "First two iterations should return None." + "\n" +
                      "i.e. no returned values until window is full'" +
                      "%s" % (algo.history_return_price_class,))
Пример #12
0
    def test_passing_of_args(self):
        algo = BatchTransformAlgorithm(1, kwarg='str')
        self.assertEqual(algo.args, (1,))
        self.assertEqual(algo.kwargs, {'kwarg': 'str'})

        algo.run(self.source)
        expected_item = ((1, ), {'kwarg': 'str'})
        self.assertEqual(
            algo.history_return_args,
            [
                # 1990-01-03 - window not full
                None,
                # 1990-01-04 - window not full
                None,
                # 1990-01-05 - window not full, 3rd event
                None,
                # 1990-01-08 - window now full
                expected_item
            ])
Пример #13
0
    def test_event_window(self):
        algo = BatchTransformAlgorithm()
        algo.run(self.source)

        self.assertEqual(algo.history_return_price_class[:2],
                         [None, None],
                         "First two iterations should return None")
        self.assertEqual(algo.history_return_price_decorator[:2],
                         [None, None],
                         "First two iterations should return None")
        self.assertEqual(algo.history_return_price_market_aware[:2],
                         [None, None],
                         "First two iterations should return None")
        self.assertEqual(algo.history_return_more_days_than_refresh[:3],
                         [None, None, None],
                         "First five iterations should return None")
        self.assertTrue(isinstance(
            algo.history_return_more_days_than_refresh[4],
            pd.DataFrame),
            "Sixth iteration should not be None"
        )

        # test overloaded class
        for test_history in [algo.history_return_price_class,
                             algo.history_return_price_decorator]:
            np.testing.assert_array_equal(
                range(2, 8),
                test_history[2].values.flatten()
            )

            np.testing.assert_array_equal(
                range(2, 8),
                test_history[3].values.flatten()
            )

            np.testing.assert_array_equal(
                range(4, 12),
                test_history[4].values.flatten()
            )
Пример #14
0
    def test_passing_of_args(self):
        algo = BatchTransformAlgorithm(1, kwarg='str',
                                       sim_params=self.sim_params,
                                       env=self.env)
        algo.run(self.source)
        self.assertEqual(algo.args, (1,))
        self.assertEqual(algo.kwargs, {'kwarg': 'str'})

        expected_item = ((1, ), {'kwarg': 'str'})
        self.assertEqual(
            algo.history_return_args,
            [
                # 1990-01-01 - market holiday, no event
                # 1990-01-02 - window not full
                None,
                # 1990-01-03 - window not full
                None,
                # 1990-01-04 - window now full, 3rd event
                expected_item,
                # 1990-01-05 - window now full
                expected_item,
                # 1990-01-08 - window now full
                expected_item
            ])
Пример #15
0
    def test_core_functionality(self):
        algo = BatchTransformAlgorithm(sim_params=self.sim_params)
        algo.run(self.source)
        wl = algo.window_length
        # The following assertion depend on window length of 3
        self.assertEqual(wl, 3)
        # If window_length is 3, there should be 2 None events, as the
        # window fills up on the 3rd day.
        n_none_events = 2
        self.assertEqual(algo.history_return_price_class[:n_none_events],
                         [None] * n_none_events,
                         "First two iterations should return None." + "\n" +
                         "i.e. no returned values until window is full'" +
                         "%s" % (algo.history_return_price_class,))
        self.assertEqual(algo.history_return_price_decorator[:n_none_events],
                         [None] * n_none_events,
                         "First two iterations should return None." + "\n" +
                         "i.e. no returned values until window is full'" +
                         "%s" % (algo.history_return_price_decorator,))

        # After three Nones, the next value should be a data frame
        self.assertTrue(isinstance(
            algo.history_return_price_class[wl],
            pd.DataFrame)
        )

        # Test whether arbitrary fields can be added to datapanel
        field = algo.history_return_arbitrary_fields[-1]
        self.assertTrue(
            'arbitrary' in field.items,
            'datapanel should contain column arbitrary'
        )

        self.assertTrue(all(
            field['arbitrary'].values.flatten() ==
            [123] * algo.window_length),
            'arbitrary dataframe should contain only "test"'
        )

        for data in algo.history_return_sid_filter[wl:]:
            self.assertIn(0, data.columns)
            self.assertNotIn(1, data.columns)

        for data in algo.history_return_field_filter[wl:]:
            self.assertIn('price', data.items)
            self.assertNotIn('ignore', data.items)

        for data in algo.history_return_field_no_filter[wl:]:
            self.assertIn('price', data.items)
            self.assertIn('ignore', data.items)

        for data in algo.history_return_ticks[wl:]:
            self.assertTrue(isinstance(data, deque))

        for data in algo.history_return_not_full:
            self.assertIsNot(data, None)

        # test overloaded class
        for test_history in [algo.history_return_price_class,
                             algo.history_return_price_decorator]:
            # starting at window length, the window should contain
            # consecutive (of window length) numbers up till the end.
            for i in range(algo.window_length, len(test_history)):
                np.testing.assert_array_equal(
                    range(i - algo.window_length + 2, i + 2),
                    test_history[i].values.flatten()
                )
Пример #16
0
 def test_passing_of_args(self):
     algo = BatchTransformAlgorithm(1, kwarg='str',
                                    sim_params=self.sim_params)