def test_imbalance_tick_bars(self):
        """
        Tests the imbalance tick bars implementation.
        """
        exp_num_ticks_init = 100
        num_prev_bars = 3

        db1 = ds.get_tick_imbalance_bars(self.path,
                                         exp_num_ticks_init=exp_num_ticks_init,
                                         num_prev_bars=num_prev_bars,
                                         batch_size=1000,
                                         verbose=False)
        db2 = ds.get_tick_imbalance_bars(self.path,
                                         exp_num_ticks_init=exp_num_ticks_init,
                                         num_prev_bars=num_prev_bars,
                                         batch_size=50,
                                         verbose=False)
        db3 = ds.get_tick_imbalance_bars(self.path,
                                         exp_num_ticks_init=exp_num_ticks_init,
                                         num_prev_bars=num_prev_bars,
                                         batch_size=10,
                                         verbose=False)
        ds.get_tick_imbalance_bars(self.path,
                                   exp_num_ticks_init=exp_num_ticks_init,
                                   num_prev_bars=num_prev_bars,
                                   batch_size=50,
                                   verbose=False,
                                   to_csv=True,
                                   output_path='test.csv')
        db4 = pd.read_csv('test.csv')

        # Assert diff batch sizes have same number of bars
        self.assertTrue(db1.shape == db2.shape)
        self.assertTrue(db1.shape == db3.shape)
        self.assertTrue(db1.shape == db4.shape)

        # Assert same values
        self.assertTrue(np.all(db1.values == db2.values))
        self.assertTrue(np.all(db1.values == db3.values))

        # Assert OHLC is correct
        self.assertTrue(db1.loc[0, 'open'] == 1306.0)
        self.assertTrue(db1.loc[0, 'high'] == 1306.00)
        self.assertTrue(db1.loc[0, 'low'] == 1305.75)
        self.assertTrue(db1.loc[0, 'close'] == 1305.75)
        self.assertTrue((db1.loc[:, 'high'] >= db1.loc[:, 'low']).all())

        # delete generated csv file (if it wasn't generated test would fail)
        os.remove('test.csv')
    def test_imbalance_tick_bars_add_features(self):
        """
        Tests the additional features functionality with tick imbalance bars.
        """

        # Arrange
        exp_num_ticks_init = 100
        num_prev_bars = 3
        high_over_low = BarFeature(
            name='high_over_low',
            function=lambda df: df['Price'].max() / df['Price'].min())
        low_over_high = BarFeature(
            name='low_over_high',
            function=lambda df: df['Price'].min() / df['Price'].max())

        # Act
        bars = ds.get_tick_imbalance_bars(
            self.path,
            exp_num_ticks_init=exp_num_ticks_init,
            num_prev_bars=num_prev_bars,
            batch_size=1000,
            verbose=False,
            additional_features=[high_over_low, low_over_high])

        # Assert
        self.assertTrue(
            np.all(bars['high_over_low'] == bars['high'] / bars['low']))
        self.assertTrue(
            np.all(bars['low_over_high'] == bars['low'] / bars['high']))
Example #3
0
    def test_imbalance_tick_bars(self):
        """
        Tests the imbalance tick bars implementation.
        """
        exp_num_ticks_init = 1000
        num_prev_bars = 3
        num_ticks_ewma_window = 10

        db1 = ds.get_tick_imbalance_bars(
            self.path,
            exp_num_ticks_init=exp_num_ticks_init,
            num_prev_bars=num_prev_bars,
            num_ticks_ewma_window=num_ticks_ewma_window,
            batch_size=1000)
        db2 = ds.get_tick_imbalance_bars(
            self.path,
            exp_num_ticks_init=exp_num_ticks_init,
            num_prev_bars=num_prev_bars,
            num_ticks_ewma_window=num_ticks_ewma_window,
            batch_size=50)
        db3 = ds.get_tick_imbalance_bars(
            self.path,
            exp_num_ticks_init=exp_num_ticks_init,
            num_prev_bars=num_prev_bars,
            num_ticks_ewma_window=num_ticks_ewma_window,
            batch_size=10)

        # Assert diff batch sizes have same number of bars
        self.assertTrue(db1.shape == db2.shape)
        self.assertTrue(db1.shape == db3.shape)

        # Assert same values
        self.assertTrue(np.all(db1.values == db2.values))
        self.assertTrue(np.all(db1.values == db3.values))

        # Assert OHLC is correct
        self.assertTrue(db1.loc[0, 'open'] == 1306)
        self.assertTrue(db1.loc[0, 'high'] == 1306.00)
        self.assertTrue(db1.loc[0, 'low'] == 1304.25)
        self.assertTrue(db1.loc[0, 'close'] == 1305.25)
        self.assertTrue((db1.loc[:, 'high'] >= db1.loc[:, 'low']).all())