Exemple #1
0
 def test_generate_snapshot_3(self):
     """ counts the number of snapshots submitted over two different connection windows with a mid starting point 
     in the first window
     """
     dummy_db.insert_agg_order(self.session)
     dummy_db.insert_coins(self.session)
     dummy_db.insert_instable_connection_events(self.session)
     dummy_db.insert_exchange_markets(self.session)
     dummy_db.insert_exchanges(self.session)
     dummy_db.insert_markets(self.session)
     self.session.flush()
     generator = ob_snapshot_generator.OBSnapshotGenerator(
         "hitbtc",
         datetime(2019, 5, 15, 19, 32, 41, 0),
         session=self.session)
     connection_events = generator._query_connection_events()
     generator._parse_connection_events(connection_events)
     generator.snapshot_interval = 1
     market = dict(exchange_id=1, buy_sym_id="ETH", sell_sym_id="BTC")
     exchange = "hitbtc"
     snapshot_time = datetime(2019, 5, 15, 19, 30, 45, 0)
     connect_time, disconnect_time = generator._get_connection_window(
         snapshot_time, "hitbtcETHBTC")
     generator._generate_all_snapshots(connect_time, disconnect_time,
                                       snapshot_time, market, exchange)
     created_snapshots = list(
         self.session.execute(
             "select count(*) from order_book_snapshots"))[0]
     self.assertEqual(created_snapshots[0], 109)
Exemple #2
0
 def _insert_data(self):
     dummy_db.insert_agg_order(self.session)
     dummy_db.insert_coins(self.session)
     dummy_db.insert_events(self.session)
     dummy_db.insert_exchange_markets(self.session)
     dummy_db.insert_exchanges(self.session)
     dummy_db.insert_markets(self.session)
     self.session.flush()
Exemple #3
0
    def test_run_quartile(self):
        """testing the computation of order book snapshots using a quartile approach
        """
        dummy_db.insert_agg_orders_snapshot(self.session)
        dummy_db.insert_coins(self.session)
        dummy_db.insert_events_snapshot(self.session)
        dummy_db.insert_exchange_markets(self.session)
        dummy_db.insert_exchanges(self.session)
        dummy_db.insert_markets(self.session)
        self.session.flush()
        interval = 60
        mid_price_range = 0

        # Check 1
        generator = ob_snapshot_generator.OBSnapshotGenerator(
            "hitbtc",
            datetime(2019, 5, 1, 1, 6, 0, 0),
            mid_price_range,
            interval,
            session=self.session)
        generator.run()
        all_snapshots = self.session.execute(
            "select timestamp, spread, bids_count, asks_count, bids_volume, asks_volume, bids_price_stddev, asks_price_stddev, bids_price_mean, asks_price_mean from order_book_snapshots snap inner join exchanges ex on snap.exchange_id = ex.id where ex.name = 'hitbtc' order by timestamp asc"
        )
        results = list(all_snapshots)
        self.assertEqual(len(results), 5)
        self.assertAlmostEqual(results[4][1], 0.1)
        self.assertEqual(results[4][2], 2)
        self.assertEqual(results[4][3], 1)
        self.assertAlmostEqual(results[4][4], 97.8)
        self.assertAlmostEqual(results[4][5], 92.0)
        self.assertAlmostEqual(results[4][6], 0.0499, delta=0.001)
        self.assertAlmostEqual(results[4][7], 0.0)
        self.assertAlmostEqual(results[4][8], 4.45)
        self.assertAlmostEqual(results[4][9], 4.6)

        # Check 2
        generator = ob_snapshot_generator.OBSnapshotGenerator(
            "hitbtc",
            datetime(2019, 5, 1, 1, 11, 0, 0),
            mid_price_range,
            interval,
            session=self.session)
        generator.run()
        all_snapshots = self.session.execute(
            "select timestamp, spread, bids_count, asks_count, bids_volume, asks_volume, bids_price_stddev, asks_price_stddev, bids_price_mean, asks_price_mean from order_book_snapshots snap inner join exchanges ex on snap.exchange_id = ex.id where ex.name = 'hitbtc' order by timestamp asc"
        )
        results = list(all_snapshots)
        self.assertEqual(len(results), 10)
        self.assertAlmostEqual(results[9][1], 0.2)
        self.assertEqual(results[9][2], 2)
        self.assertEqual(results[9][3], 2)
        self.assertAlmostEqual(results[9][4], 87.2)
        self.assertAlmostEqual(results[9][5], 195.4)
        self.assertAlmostEqual(results[9][6], 0.05, delta=0.001)
        self.assertAlmostEqual(results[9][7], 0.05, delta=0.001)
        self.assertAlmostEqual(results[9][8], 4.35)
        self.assertAlmostEqual(results[9][9], 4.65)
    def test_get_existing_markets(self):
        dummy_db.insert_coins(self.session)
        dummy_db.insert_exchanges(self.session)
        dummy_db.insert_markets(self.session)
        dummy_db.insert_exchange_markets(self.session)
        self.session.flush()
        markets = [em.original_name for em in self.session.query(models.ExchangeMarket).all()]

        dummy_idex_exchange = models.Exchange(id=1, name="idex")
        idex_listener = IdexListener(
            dummy_idex_exchange,
            self.on_event_mock,
            session=self.session,
            markets=markets,
            max_markets=3,
        )
        self.assertEqual(len(idex_listener.markets), 3)
        expected_markets = ["ETH_FTM", "ETH_LTO", "ETH_FSN"]
        self.assertEqual(idex_listener.markets, expected_markets)
Exemple #5
0
    def test_run_mid_price(self):
        """testing the computation of order book snapshots using a mid price range approach
        """
        dummy_db.insert_agg_orders_snapshot(self.session)
        dummy_db.insert_coins(self.session)
        dummy_db.insert_events_snapshot(self.session)
        dummy_db.insert_exchange_markets(self.session)
        dummy_db.insert_exchanges(self.session)
        dummy_db.insert_markets(self.session)
        self.session.flush()
        interval = 60
        mid_price_range = 0.1

        # Check 1
        generator = ob_snapshot_generator.OBSnapshotGenerator(
            "hitbtc",
            datetime(2019, 5, 1, 1, 2, 0, 0),
            mid_price_range,
            interval,
            session=self.session)
        generator.run()
        all_snapshots = self.session.execute(
            "select timestamp, spread, bids_count, asks_count, bids_volume, asks_volume, bids_price_stddev, asks_price_stddev, bids_price_mean, asks_price_mean from order_book_snapshots snap inner join exchanges ex on snap.exchange_id = ex.id where ex.name = 'hitbtc' order by timestamp asc"
        )
        results = list(all_snapshots)
        self.assertEqual(len(results), 1)
        self.assertAlmostEqual(results[0][1], 0.1)
        self.assertEqual(results[0][2], 1)
        self.assertEqual(results[0][3], 1)
        self.assertEqual(results[0][4], 45.0)
        self.assertEqual(results[0][5], 92.0)

        # Check 2
        generator = ob_snapshot_generator.OBSnapshotGenerator(
            "hitbtc",
            datetime(2019, 5, 1, 1, 6, 0, 0),
            mid_price_range,
            interval,
            session=self.session)
        generator.run()
        all_snapshots = self.session.execute(
            "select timestamp, spread, bids_count, asks_count, bids_volume, asks_volume, bids_price_stddev,\
                                             asks_price_stddev, bids_price_mean, asks_price_mean from order_book_snapshots snap inner join exchanges\
                                             ex on snap.exchange_id = ex.id where ex.name = 'hitbtc' order by timestamp asc"
        )
        results = list(all_snapshots)
        # five snapshots have been generated - last bid added is not included due to being out of range
        self.assertEqual(len(results), 5)
        self.assertAlmostEqual(results[4][1], 0.1)
        self.assertEqual(results[4][2], 4)
        self.assertEqual(results[4][3], 4)
        self.assertAlmostEqual(results[4][4], 174.2)
        self.assertAlmostEqual(results[4][5], 248.7)
        self.assertAlmostEqual(results[4][6], 0.11180339887498948)
        self.assertAlmostEqual(results[4][7], 0.11180339887498948)

        # Check 3
        generator = ob_snapshot_generator.OBSnapshotGenerator(
            "hitbtc",
            datetime(2019, 5, 1, 1, 9, 0, 0),
            mid_price_range,
            interval,
            session=self.session)
        generator.run()
        all_snapshots = self.session.execute(
            "select timestamp, spread, bids_count, asks_count, bids_volume, asks_volume, bids_price_stddev,\
                                             asks_price_stddev, bids_price_mean, asks_price_mean from order_book_snapshots snap inner join exchanges\
                                             ex on snap.exchange_id = ex.id where ex.name = 'hitbtc' order by timestamp asc"
        )
        results = list(all_snapshots)
        # eight snapshots have been generated - last five orders are ignored due to being out of range
        self.assertEqual(len(results), 8)
        self.assertAlmostEqual(results[7][1], 0.1)
        self.assertEqual(results[7][2], 4)
        self.assertEqual(results[7][3], 4)
        self.assertAlmostEqual(results[7][4], 174.2)
        self.assertAlmostEqual(results[7][5], 248.7)
        self.assertAlmostEqual(results[7][6], 0.11180339887498948)
        self.assertAlmostEqual(results[7][7], 0.11180339887498948)

        # Check 4
        generator = ob_snapshot_generator.OBSnapshotGenerator(
            "hitbtc",
            datetime(2019, 5, 1, 1, 10, 0, 0),
            mid_price_range,
            interval,
            session=self.session)
        generator.run()
        all_snapshots = self.session.execute(
            "select timestamp, spread, bids_count, asks_count, bids_volume, asks_volume, bids_price_stddev,\
                                             asks_price_stddev, bids_price_mean, asks_price_mean from order_book_snapshots snap inner join exchanges\
                                             ex on snap.exchange_id = ex.id where ex.name = 'hitbtc' order by timestamp asc"
        )
        results = list(all_snapshots)
        # nine snapshots have been generated - last order sets a new mid price
        self.assertEqual(len(results), 9)
        self.assertAlmostEqual(results[8][1], 0.2)
        self.assertEqual(results[8][2], 3)
        self.assertEqual(results[8][3], 4)
        self.assertAlmostEqual(results[8][4], 129.2)
        self.assertAlmostEqual(results[8][5], 248.7)
        self.assertAlmostEqual(results[8][6], 0.08164965809277268)
        self.assertAlmostEqual(results[8][7], 0.11180339887498948)

        # Check 5
        generator = ob_snapshot_generator.OBSnapshotGenerator(
            "hitbtc",
            datetime(2019, 5, 1, 1, 11, 0, 0),
            mid_price_range,
            interval,
            session=self.session)
        generator.run()
        all_snapshots = self.session.execute(
            "select timestamp, spread, bids_count, asks_count, bids_volume, asks_volume, bids_price_stddev,\
                                             asks_price_stddev, bids_price_mean, asks_price_mean from order_book_snapshots snap inner join exchanges\
                                             ex on snap.exchange_id = ex.id where ex.name = 'hitbtc' order by timestamp asc"
        )
        results = list(all_snapshots)
        # ten snapshots have been generated - last order is a new ask order which should be included in snapshot
        self.assertEqual(len(results), 10)
        self.assertAlmostEqual(results[9][1], 0.2)
        self.assertEqual(results[9][2], 3)
        self.assertEqual(results[9][3], 5)
        self.assertAlmostEqual(results[9][4], 129.2)
        self.assertAlmostEqual(results[9][5], 297.2)
        self.assertAlmostEqual(results[9][6], 0.08164965809277268)
        self.assertAlmostEqual(results[9][7], 0.10770329614269018)
        self.assertAlmostEqual(results[9][8], 4.3)
        self.assertAlmostEqual(results[9][9], 4.77)

        # Check 6: mid price +- 20%
        mid_price_range = 0.2
        generator = ob_snapshot_generator.OBSnapshotGenerator(
            "hitbtc",
            datetime(2019, 5, 1, 1, 11, 0, 0),
            mid_price_range,
            interval,
            session=self.session)
        generator.run()
        all_snapshots = self.session.execute(
            "select timestamp, spread, bids_count, asks_count, bids_volume, asks_volume, bids_price_stddev,\
                                             asks_price_stddev, bids_price_mean, asks_price_mean from order_book_snapshots snap inner join exchanges\
                                             ex on snap.exchange_id = ex.id where ex.name = 'hitbtc' order by timestamp asc"
        )
        results = list(all_snapshots)
        # ten snapshots have been generated - mid price range is changed to +-20%
        # total of 20 snapshots in db: 10 @ range +-10% and 10 @ range +-20%
        self.assertEqual(len(results), 20)
        self.assertAlmostEqual(results[19][1], 0.2)
        self.assertEqual(results[19][2], 6)
        self.assertEqual(results[19][3], 6)
        self.assertAlmostEqual(results[19][4], 201.8)
        self.assertAlmostEqual(results[19][5], 318.0)
        self.assertAlmostEqual(results[19][6], 0.25603819159562036)
        self.assertAlmostEqual(results[19][7], 0.18800856954464143)
        self.assertAlmostEqual(results[19][8], 4.066666666666666)
        self.assertAlmostEqual(results[19][9], 4.841666666666667)