def test_tickers(self):
        exchange_population = 3
        asset_population = 15

        # exchanges = select_random_exchanges(
        #     exchange_population,
        #     features=['fetchTickers'],
        # )  # Type: list[Exchange]
        exchanges = list(get_exchanges(['bitfinex']).values())
        for exchange in exchanges:
            exchange.init()

            if exchange.assets and len(exchange.assets) >= asset_population:
                assets = select_random_assets(
                    exchange.assets, asset_population
                )
                tickers = exchange.tickers(assets)

                assert len(tickers) == asset_population

            else:
                print(
                    'skipping exchange without assets {}'.format(exchange.name)
                )
                exchange_population -= 1
        pass
    def test_tickers(self):
        exchange_population = 3
        asset_population = 15

        # exchanges = select_random_exchanges(
        #     exchange_population,
        #     features=['fetchTickers'],
        # )  # Type: list[Exchange]
        exchanges = list(get_exchanges(['binance']).values())
        for exchange in exchanges:
            exchange.init()

            if exchange.assets and len(exchange.assets) >= asset_population:
                assets = select_random_assets(
                    exchange.assets, asset_population
                )
                tickers = exchange.tickers(assets)

                assert len(tickers) == asset_population

            else:
                print(
                    'skipping exchange without assets {}'.format(exchange.name)
                )
                exchange_population -= 1
        pass
Beispiel #3
0
    def test_orders(self):
        population = 3
        quote_currency = 'eth'
        order_amount = 0.1

        exchanges = select_random_exchanges(
            population=population,
            features=['fetchOrder'],
            is_authenticated=True,
            base_currency=quote_currency,
        )  # Type: list[Exchange]

        log_catcher = TestHandler()
        with log_catcher:
            for exchange in exchanges:
                exchange.init()

                assets = exchange.get_assets(quote_currency=quote_currency)
                asset = select_random_assets(assets, 1)[0]
                self.assertIsInstance(asset, TradingPair)

                tickers = exchange.tickers([asset])
                price = tickers[asset]['last_price']

                amount = order_amount / price

                limit_price = price * 0.8
                style = ExchangeLimitOrder(limit_price=limit_price)

                order = exchange.order(
                    asset=asset,
                    amount=amount,
                    style=style,
                )
                sleep(1)

                open_order, _ = exchange.get_order(order.id, asset)
                self.assertEqual(0, open_order.status)

                exchange.cancel_order(open_order, asset)
                sleep(1)

                canceled_order, _ = exchange.get_order(open_order.id, asset)
                warnings = [
                    record for record in log_catcher.records
                    if record.level == WARNING
                ]

                self.assertEqual(0, len(warnings))
                self.assertEqual(2, canceled_order.status)
                print('tested {exchange} / {symbol}, order: {order}'.format(
                    exchange=exchange.name,
                    symbol=asset.symbol,
                    order=order.id,
                ))
        pass
Beispiel #4
0
    def test_validate_bundles(self):
        # exchange_population = 3
        asset_population = 3
        data_frequency = random.choice(['minute'])

        # bundle = 'dailyBundle' if data_frequency
        #  == 'daily' else 'minuteBundle'
        # exchanges = select_random_exchanges(
        #     population=exchange_population,
        #     features=[bundle],
        # )  # Type: list[Exchange]
        exchanges = [get_exchange('poloniex', skip_init=True)]

        data_portal = TestSuiteBundle.get_data_portal(exchanges)
        for exchange in exchanges:
            exchange.init()

            frequencies = exchange.get_candle_frequencies(data_frequency)
            freq = random.sample(frequencies, 1)[0]
            rnd = random.SystemRandom()
            # field = rnd.choice(['open', 'high', 'low', 'close', 'volume'])
            field = rnd.choice(['volume'])

            bar_count = random.randint(3, 6)

            assets = select_random_assets(
                exchange.assets, asset_population
            )
            end_dt = None
            for asset in assets:
                attribute = 'end_{}'.format(data_frequency)
                asset_end_dt = getattr(asset, attribute)

                if end_dt is None or asset_end_dt < end_dt:
                    end_dt = asset_end_dt

            end_dt = end_dt + timedelta(minutes=3)
            dt_range = pd.date_range(
                end=end_dt, periods=bar_count, freq=freq
            )
            self.compare_bundle_with_exchange(
                exchange=exchange,
                assets=assets,
                end_dt=dt_range[-1],
                bar_count=bar_count,
                freq=freq,
                data_frequency=data_frequency,
                data_portal=data_portal,
                field=field,
            )
        pass
    def test_validate_bundles(self):
        # exchange_population = 3
        asset_population = 3
        data_frequency = random.choice(['minute'])

        # bundle = 'dailyBundle' if data_frequency
        #  == 'daily' else 'minuteBundle'
        # exchanges = select_random_exchanges(
        #     population=exchange_population,
        #     features=[bundle],
        # )  # Type: list[Exchange]
        exchanges = [get_exchange('poloniex', skip_init=True)]

        data_portal = TestSuiteBundle.get_data_portal(exchanges)
        for exchange in exchanges:
            exchange.init()

            frequencies = exchange.get_candle_frequencies(data_frequency)
            freq = random.sample(frequencies, 1)[0]

            bar_count = random.randint(1, 10)

            assets = select_random_assets(
                exchange.assets, asset_population
            )
            end_dt = None
            for asset in assets:
                attribute = 'end_{}'.format(data_frequency)
                asset_end_dt = getattr(asset, attribute)

                if end_dt is None or asset_end_dt < end_dt:
                    end_dt = asset_end_dt

            end_dt = end_dt + timedelta(minutes=3)
            dt_range = pd.date_range(
                end=end_dt, periods=bar_count, freq=freq
            )
            self.compare_bundle_with_exchange(
                exchange=exchange,
                assets=assets,
                end_dt=dt_range[-1],
                bar_count=bar_count,
                freq=freq,
                data_frequency=data_frequency,
                data_portal=data_portal,
            )
        pass
    def test_validate_bundles(self):
        # exchange_population = 3
        asset_population = 3
        data_frequency = random.choice(['minute', 'daily'])

        # bundle = 'dailyBundle' if data_frequency
        #  == 'daily' else 'minuteBundle'
        # exchanges = select_random_exchanges(
        #     population=exchange_population,
        #     features=[bundle],
        # )  # Type: list[Exchange]
        exchanges = [get_exchange('bitfinex', skip_init=True)]

        data_portal = TestSuiteBundle.get_data_portal(
            [exchange.name for exchange in exchanges])
        for exchange in exchanges:
            exchange.init()

            frequencies = exchange.get_candle_frequencies(data_frequency)
            freq = random.sample(frequencies, 1)[0]

            bar_count = random.randint(1, 10)

            assets = select_random_assets(exchange.assets, asset_population)
            end_dt = None
            for asset in assets:
                attribute = 'end_{}'.format(data_frequency)
                asset_end_dt = getattr(asset, attribute)

                if end_dt is None or asset_end_dt < end_dt:
                    end_dt = asset_end_dt

            dt_range = pd.date_range(end=end_dt, periods=bar_count, freq=freq)
            self.compare_bundle_with_exchange(
                exchange=exchange,
                assets=assets,
                end_dt=dt_range[-1],
                bar_count=bar_count,
                freq=freq,
                data_frequency=data_frequency,
                data_portal=data_portal,
            )
        pass
    def test_orders(self):
        population = 3
        quote_currency = 'eth'
        order_amount = 0.1

        exchanges = select_random_exchanges(
            population=population,
            features=['fetchOrder'],
            is_authenticated=True,
            base_currency=quote_currency,
        )  # Type: list[Exchange]

        for exchange in exchanges:
            exchange.init()

            assets = exchange.get_assets(quote_currency=quote_currency)
            asset = select_random_assets(assets, 1)[0]
            assert asset

            tickers = exchange.tickers([asset])
            price = tickers[asset]['last_price']

            amount = order_amount / price

            limit_price = price * 0.8
            style = ExchangeLimitOrder(limit_price=limit_price)

            order = exchange.order(
                asset=asset,
                amount=amount,
                style=style,
            )
            sleep(1)

            open_order, _ = exchange.get_order(order.id, asset)
            assert open_order.status == 0

            exchange.cancel_order(open_order, asset)
            sleep(1)

            canceled_order, _ = exchange.get_order(open_order.id, asset)
            assert canceled_order.status == 2
        pass
Beispiel #8
0
    def test_validate_last_candle(self):
        # exchange_population = 3
        asset_population = 3
        data_frequency = random.choice(['minute'])

        # bundle = 'dailyBundle' if data_frequency
        #  == 'daily' else 'minuteBundle'
        # exchanges = select_random_exchanges(
        #     population=exchange_population,
        #     features=[bundle],
        # )  # Type: list[Exchange]
        exchanges = [get_exchange('poloniex', skip_init=True)]

        data_portal = TestSuiteBundle.get_data_portal(exchanges)
        for exchange in exchanges:
            exchange.init()

            frequencies = exchange.get_candle_frequencies(data_frequency)
            freq = random.sample(frequencies, 1)[0]

            assets = select_random_assets(
                exchange.assets, asset_population
            )
            end_dt = None
            for asset in assets:
                attribute = 'end_{}'.format(data_frequency)
                asset_end_dt = getattr(asset, attribute)

                if end_dt is None or asset_end_dt < end_dt:
                    end_dt = asset_end_dt

            end_dt = end_dt + timedelta(minutes=3)
            self.compare_current_with_last_candle(
                exchange=exchange,
                assets=assets,
                end_dt=end_dt,
                freq=freq,
                data_frequency=data_frequency,
                data_portal=data_portal,
            )
        pass
    def test_candles(self):
        exchange_population = 3
        asset_population = 3

        # exchanges = select_random_exchanges(
        #     population=exchange_population,
        #     features=['fetchOHLCV'],
        # )  # Type: list[Exchange]
        exchanges = list(get_exchanges(['binance']).values())
        for exchange in exchanges:
            exchange.init()

            if exchange.assets and len(exchange.assets) >= asset_population:
                frequencies = exchange.get_candle_frequencies()
                freq = random.sample(frequencies, 1)[0]

                bar_count = random.randint(1, 10)
                end_dt = pd.Timestamp.utcnow().floor('1T')
                dt_range = pd.date_range(
                    end=end_dt, periods=bar_count, freq=freq
                )
                assets = select_random_assets(
                    exchange.assets, asset_population
                )

                candles = exchange.get_candles(
                    freq=freq,
                    assets=assets,
                    bar_count=bar_count,
                    start_dt=dt_range[0],
                )

                assert len(candles) == asset_population

            else:
                print(
                    'skipping exchange without assets {}'.format(exchange.name)
                )
                exchange_population -= 1
        pass
    def test_candles(self):
        exchange_population = 3
        asset_population = 3

        exchanges = select_random_exchanges(
            population=exchange_population,
            features=['fetchOHLCV'],
        )  # Type: list[Exchange]
        for exchange in exchanges:
            exchange.init()

            if exchange.assets and len(exchange.assets) >= asset_population:
                frequencies = exchange.get_candle_frequencies()
                freq = random.sample(frequencies, 1)[0]

                bar_count = random.randint(1, 10)
                end_dt = pd.Timestamp.utcnow().floor('1T')
                dt_range = pd.date_range(
                    end=end_dt, periods=bar_count, freq=freq
                )
                assets = select_random_assets(
                    exchange.assets, asset_population
                )

                candles = exchange.get_candles(
                    freq=freq,
                    assets=assets,
                    bar_count=bar_count,
                    start_dt=dt_range[0],
                    end_dt=dt_range[-1],
                )

                assert len(candles) == asset_population

            else:
                print(
                    'skipping exchange without assets {}'.format(exchange.name)
                )
                exchange_population -= 1
        pass
    def test_orders(self):
        population = 3
        quote_currency = 'eth'
        order_amount = 0.1

        # exchanges = select_random_exchanges(
        #     population=population,
        #     features=['fetchOrder'],
        #     is_authenticated=True,
        #     base_currency=quote_currency,
        # )  # Type: list[Exchange]

        exchanges = [
            get_exchange(
                'binance',
                base_currency=quote_currency,
                must_authenticate=True,
            )
        ]
        log_catcher = TestHandler()
        with log_catcher:
            for exchange in exchanges:
                exchange.init()

                assets = exchange.get_assets(quote_currency=quote_currency)
                asset = select_random_assets(assets, 1)[0]
                self.assertIsInstance(asset, TradingPair)

                tickers = exchange.tickers([asset])
                price = tickers[asset]['last_price']

                amount = order_amount / price

                limit_price = price * 0.8
                style = ExchangeLimitOrder(limit_price=limit_price)

                order = exchange.order(
                    asset=asset,
                    amount=amount,
                    style=style,
                )
                sleep(1)

                open_order = exchange.get_order(order.id, asset)
                self.assertEqual(0, open_order.status)

                exchange.cancel_order(open_order, asset)
                sleep(1)

                canceled_order = exchange.get_order(open_order.id, asset)
                warnings = [record for record in log_catcher.records if
                            record.level == WARNING]

                self.assertEqual(0, len(warnings))
                self.assertEqual(2, canceled_order.status)
                print(
                    'tested {exchange} / {symbol}, order: {order}'.format(
                        exchange=exchange.name,
                        symbol=asset.symbol,
                        order=order.id,
                    )
                )
        pass