import pandas as pd from catalyst.exchange.exchange_data_portal import DataPortalExchangeBacktest, \ DataPortalExchangeLive from logbook import Logger from test_utils import rnd_history_date_days, rnd_bar_count from catalyst import get_calendar from catalyst.exchange.asset_finder_exchange import AssetFinderExchange from catalyst.exchange.bitfinex.bitfinex import Bitfinex from catalyst.exchange.bittrex.bittrex import Bittrex from catalyst.exchange.exchange_utils import get_exchange_auth, \ get_common_assets from catalyst.exchange.factory import get_exchange, get_exchanges log = Logger('test_bitfinex') class TestExchangeDataPortal: @classmethod def setup(self): log.info('creating bitfinex exchange') exchanges = get_exchanges(['bitfinex', 'bittrex', 'poloniex']) open_calendar = get_calendar('OPEN') asset_finder = AssetFinderExchange() self.data_portal_live = DataPortalExchangeLive( exchanges=exchanges, asset_finder=asset_finder, trading_calendar=open_calendar, first_trading_day=pd.to_datetime('today', utc=True) ) self.data_portal_backtest = DataPortalExchangeBacktest( exchanges=exchanges, asset_finder=asset_finder, trading_calendar=open_calendar, first_trading_day=None # will set dynamically based on assets ) def test_get_history_window_live(self): asset_finder = self.data_portal_live.asset_finder assets = [ asset_finder.lookup_symbol('eth_btc', self.bitfinex), asset_finder.lookup_symbol('eth_btc', self.bittrex) ] now = pd.Timestamp.utcnow() data = self.data_portal_live.get_history_window( assets, now, 10, '1m', 'price') pass def test_get_spot_value_live(self): asset_finder = self.data_portal_live.asset_finder assets = [ asset_finder.lookup_symbol('eth_btc', self.bitfinex), asset_finder.lookup_symbol('eth_btc', self.bittrex) ] now = pd.Timestamp.utcnow() value = self.data_portal_live.get_spot_value( assets, 'price', now, '1m') pass def test_get_history_window_backtest(self): asset_finder = self.data_portal_live.asset_finder assets = [ asset_finder.lookup_symbol('neo_btc', self.bitfinex), ] date = pd.to_datetime('2017-09-10', utc=True) data = self.data_portal_backtest.get_history_window( assets, date, 10, '1m', 'close', 'minute') log.info('found history window: {}'.format(data)) pass def test_get_spot_value_backtest(self): asset_finder = self.data_portal_backtest.asset_finder assets = [ asset_finder.lookup_symbol('neo_btc', self.bitfinex), ] date = pd.to_datetime('2017-09-10', utc=True) value = self.data_portal_backtest.get_spot_value( assets, 'close', date, 'minute') log.info('found spot value {}'.format(value)) pass def test_history_compare_exchanges(self): exchanges = get_exchanges(['bittrex', 'bitfinex', 'poloniex']) assets = get_common_assets(exchanges) date = rnd_history_date_days() bar_count = rnd_bar_count() data = self.data_portal_backtest.get_history_window( assets=assets, end_dt=date, bar_count=bar_count, frequency='1d', field='close', data_frequency='daily' ) log.info('found history window: {}'.format(data)) def test_validate_resample(self): symbol = ['eth_btc'] exchange_name = 'poloniex' exchange = get_exchange(exchange_name, base_currency=symbol) assets = exchange.get_assets(symbols=symbol) date = rnd_history_date_days( max_days=10, last_dt=pd.to_datetime('2017-11-1', utc=True) ) bar_count = rnd_bar_count(max_bars=10) sample_minutes = 15 sample_data = self.data_portal_backtest.get_history_window( assets=assets, end_dt=date, bar_count=bar_count, frequency='{}T'.format(sample_minutes), field='close', data_frequency='daily' ) minute_data = self.data_portal_backtest.get_history_window( assets=assets, end_dt=date, bar_count=bar_count * sample_minutes, frequency='1T', field='close', data_frequency='daily' ) resampled_minute_data = minute_data.resample( '{}T'.format(sample_minutes)) print(sample_data.tail(10)) print(resampled_minute_data.tail(10)) print(minute_data.tail(10)) pass