Files
catalyst/tests/exchange/test_suite_exchange.py
T

156 lines
4.2 KiB
Python

import json
import os
import random
import unittest
from logging import Logger
from time import sleep
import pandas as pd
from ccxt import AuthenticationError
from catalyst.exchange.exchange_errors import ExchangeRequestError
from catalyst.exchange.exchange_utils import get_exchange_folder
from catalyst.exchange.factory import find_exchanges
from catalyst.utils.paths import data_root
log = Logger('TestSuiteExchange')
def handle_exchange_error(exchange, e):
is_blacklist = False
if isinstance(e, AuthenticationError):
is_blacklist = True
elif isinstance(e, ValueError) or isinstance(e, ExchangeRequestError):
is_blacklist = True
else:
log.warn('unexpected error: {}'.format(e))
is_blacklist = True
if is_blacklist:
try:
message = '{}: {}'.format(
e.__class__, e.message.decode('ascii', 'ignore')
)
except Exception:
message = 'unexpected error'
folder = get_exchange_folder(exchange.name)
filename = os.path.join(folder, 'blacklist.txt')
with open(filename, 'wt') as handle:
handle.write(message)
def select_random_exchanges(population=3, features=None):
all_exchanges = find_exchanges(features)
if population is not None:
exchanges = random.sample(all_exchanges, population)
else:
exchanges = all_exchanges
return exchanges
def select_random_assets(all_assets, population=3):
assets = random.sample(all_assets, population)
return assets
# TODO: convert to Nosetest
class TestSuiteExchange(unittest.TestCase):
def _test_markets_exchange(self, exchange, attempts=0):
assets = None
try:
exchange.init()
# Verify that the assets and markets are populated
if not exchange.markets:
raise ValueError(
'no markets found'
)
if not exchange.assets:
raise ValueError(
'no assets derived from markets'
)
assets = exchange.assets
except ExchangeRequestError as e:
sleep(5)
if attempts > 5:
handle_exchange_error(exchange, e)
else:
print(
're-trying an exchange request {} {}'.format(
exchange.name, attempts
)
)
self._test_markets_exchange(exchange, attempts + 1)
except Exception as e:
handle_exchange_error(exchange, e)
return assets
def test_markets(self):
population = 3
results = dict()
exchanges = select_random_exchanges(population) # Type: list[Exchange]
for exchange in exchanges:
assets = self._test_markets_exchange(exchange)
if assets is not None:
results[exchange.name] = len(assets)
folder = get_exchange_folder(exchange.name)
filename = os.path.join(folder, 'whitelist.json')
symbols = [asset.symbol for asset in assets]
with open(filename, 'wt') as handle:
json.dump(symbols, handle, indent=4)
series = pd.Series(results)
print('the tested markets\n{}'.format(series))
if population is not None:
assert (len(results) == population)
pass
def test_tickers(self):
exchange_population = 3
asset_population = 3
exchanges = select_random_exchanges(
exchange_population
) # Type: list[Exchange]
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_candles(self):
pass
def test_orders(self):
pass