From 4db8131397e223be84b5dd3c05423f0b64d80f5a Mon Sep 17 00:00:00 2001 From: fredfortier Date: Fri, 1 Dec 2017 00:06:11 -0500 Subject: [PATCH] BLD: paper trading adjustments --- catalyst/examples/simple_loop.py | 34 ++++++++++++------------- catalyst/exchange/ccxt/ccxt_exchange.py | 15 +++++++---- catalyst/exchange/exchange_algorithm.py | 1 + catalyst/exchange/exchange_utils.py | 13 ++++++---- catalyst/utils/run_algo.py | 21 ++++++++------- tests/exchange/test_ccxt.py | 18 ++++--------- 6 files changed, 51 insertions(+), 51 deletions(-) diff --git a/catalyst/examples/simple_loop.py b/catalyst/examples/simple_loop.py index 75f55092..b0dbd929 100644 --- a/catalyst/examples/simple_loop.py +++ b/catalyst/examples/simple_loop.py @@ -107,25 +107,25 @@ def analyze(context, perf): pass -# run_algorithm( -# capital_base=250, -# start=pd.to_datetime('2017-11-1 0:00', utc=True), -# end=pd.to_datetime('2017-11-10 23:59', utc=True), -# data_frequency='daily', -# initialize=initialize, -# handle_data=handle_data, -# analyze=analyze, -# exchange_name='bitfinex', -# algo_namespace='simple_loop', -# base_currency='usd' -# ) run_algorithm( + capital_base=250, + start=pd.to_datetime('2017-11-1 0:00', utc=True), + end=pd.to_datetime('2017-11-10 23:59', utc=True), + data_frequency='daily', initialize=initialize, handle_data=handle_data, - analyze=None, - exchange_name='binance', - live=True, + analyze=analyze, + exchange_name='bitfinex', algo_namespace='simple_loop', - base_currency='eth', - live_graph=False, + base_currency='usd' ) +# run_algorithm( +# initialize=initialize, +# handle_data=handle_data, +# analyze=None, +# exchange_name='binance', +# live=True, +# algo_namespace='simple_loop', +# base_currency='eth', +# live_graph=False, +# ) diff --git a/catalyst/exchange/ccxt/ccxt_exchange.py b/catalyst/exchange/ccxt/ccxt_exchange.py index d04c346f..d2c735cc 100644 --- a/catalyst/exchange/ccxt/ccxt_exchange.py +++ b/catalyst/exchange/ccxt/ccxt_exchange.py @@ -28,6 +28,8 @@ SUPPORTED_EXCHANGES = dict( bitfinex=ccxt.bitfinex, bittrex=ccxt.bittrex, poloniex=ccxt.poloniex, + bitmex=ccxt.bitmex, + gdax=ccxt.gdax, ) @@ -88,12 +90,15 @@ class CCXT(Exchange): return '{}/{}'.format(parts[0].upper(), parts[1].upper()) def get_catalyst_symbol(self, market_or_symbol): - symbol = market_or_symbol if isinstance( - market_or_symbol, string_types - ) else market_or_symbol['symbol'] + if isinstance(market_or_symbol, string_types): + parts = market_or_symbol.split('/') + return '{}_{}'.format(parts[0].lower(), parts[1].lower()) - parts = symbol.split('/') - return '{}_{}'.format(parts[0].lower(), parts[1].lower()) + else: + return '{}_{}'.format( + market_or_symbol['base'].lower(), + market_or_symbol['quote'].lower(), + ) def get_timeframe(self, freq): freq_match = re.match(r'([0-9].*)?(m|M|d|D|h|H|T)', freq, re.M | re.I) diff --git a/catalyst/exchange/exchange_algorithm.py b/catalyst/exchange/exchange_algorithm.py index bd7c84a1..c2102015 100644 --- a/catalyst/exchange/exchange_algorithm.py +++ b/catalyst/exchange/exchange_algorithm.py @@ -289,6 +289,7 @@ class ExchangeTradingAlgorithmLive(ExchangeTradingAlgorithmBase): def __init__(self, *args, **kwargs): self.algo_namespace = kwargs.pop('algo_namespace', None) self.live_graph = kwargs.pop('live_graph', None) + self.simulate_orders = kwargs.pop('simulate_orders', None) self._clock = None self.frame_stats = deque(maxlen=60) diff --git a/catalyst/exchange/exchange_utils.py b/catalyst/exchange/exchange_utils.py index ee0ce44e..0ca18c95 100644 --- a/catalyst/exchange/exchange_utils.py +++ b/catalyst/exchange/exchange_utils.py @@ -594,12 +594,15 @@ def mixin_market_params(exchange_name, params, market): params['maker'] = 0.001 params['taker'] = 0.002 - else: - if 'maker' in market: - params['maker'] = market['maker'] + elif 'maker' in market and 'taker' in market \ + and market['maker'] is not None and market['taker'] is not None: + params['maker'] = market['maker'] + params['taker'] = market['taker'] - if 'taker' in market: - params['taker'] = market['taker'] + else: + # TODO: default commission, make configurable + params['maker'] = 0.0015 + params['taker'] = 0.0025 info = market['info'] if 'info' in market else None if info: diff --git a/catalyst/utils/run_algo.py b/catalyst/utils/run_algo.py index 404b21c0..a20490bc 100644 --- a/catalyst/utils/run_algo.py +++ b/catalyst/utils/run_algo.py @@ -11,10 +11,7 @@ import pandas as pd from catalyst.data.bundles import load from catalyst.data.data_portal import DataPortal -from catalyst.exchange.bittrex.bittrex import Bittrex -from catalyst.exchange.bitfinex.bitfinex import Bitfinex from catalyst.exchange.factory import get_exchange -from catalyst.exchange.poloniex.poloniex import Poloniex try: from pygments import highlight @@ -40,11 +37,9 @@ from catalyst.exchange.exchange_data_portal import DataPortalExchangeLive, \ from catalyst.exchange.asset_finder_exchange import AssetFinderExchange from catalyst.exchange.exchange_portfolio import ExchangePortfolio from catalyst.exchange.exchange_errors import ( - ExchangeRequestError, ExchangeAuthEmpty, - ExchangeRequestErrorTooManyAttempts, - BaseCurrencyNotFoundError, ExchangeNotFoundError) -from catalyst.exchange.exchange_utils import get_exchange_auth, \ - get_algo_object, get_exchange_folder + ExchangeRequestError, ExchangeRequestErrorTooManyAttempts, + BaseCurrencyNotFoundError) +from catalyst.exchange.exchange_utils import get_algo_object from logbook import Logger from catalyst.constants import LOG_LEVEL @@ -95,7 +90,8 @@ def _run(handle_data, exchange, algo_namespace, base_currency, - live_graph): + live_graph, + simulate_orders): """Run a backtest for the given algorithm. This is shared between the cli and :func:`catalyst.run_algo`. @@ -282,7 +278,8 @@ def _run(handle_data, ExchangeTradingAlgorithmLive, exchanges=exchanges, algo_namespace=algo_namespace, - live_graph=live_graph + live_graph=live_graph, + simulate_orders=simulate_orders ) elif exchanges: # Removed the existing Poloniex fork to keep things simple @@ -444,6 +441,7 @@ def run_algorithm(initialize, base_currency=None, algo_namespace=None, live_graph=False, + simulate_orders=True, output=os.devnull): """Run a trading algorithm. @@ -565,5 +563,6 @@ def run_algorithm(initialize, exchange=exchange_name, algo_namespace=algo_namespace, base_currency=base_currency, - live_graph=live_graph + live_graph=live_graph, + simulate_orders=simulate_orders ) diff --git a/tests/exchange/test_ccxt.py b/tests/exchange/test_ccxt.py index eccdd18f..b1df6a19 100644 --- a/tests/exchange/test_ccxt.py +++ b/tests/exchange/test_ccxt.py @@ -15,7 +15,7 @@ log = Logger('test_ccxt') class TestCCXT(BaseExchangeTestCase): @classmethod def setup(self): - exchange_name = 'binance' + exchange_name = 'gdax' auth = get_exchange_auth(exchange_name) self.exchange = CCXT( exchange_name=exchange_name, @@ -64,25 +64,17 @@ class TestCCXT(BaseExchangeTestCase): start_dt=pd.to_datetime('2017-01-01', utc=True) ) - df = pd.DataFrame(candles) - df.set_index('last_traded', drop=True, inplace=True) - - folder = os.path.join( - tempfile.gettempdir(), 'catalyst', self.exchange.name, 'eth_btc' - ) - ensure_directory(folder) - - path = os.path.join(folder, 'output.csv') - df.to_csv(path) + for asset in candles: + df = pd.DataFrame(candles[asset]) + df.set_index('last_traded', drop=True, inplace=True) pass def test_tickers(self): log.info('retrieving tickers') tickers = self.exchange.tickers([ self.exchange.get_asset('eth_btc'), - self.exchange.get_asset('etc_btc') ]) - assert len(tickers) == 2 + assert len(tickers) == 1 pass def test_get_balances(self):