From f72074876dfa410d08b2f6f7725c1601a946088c Mon Sep 17 00:00:00 2001 From: fredfortier Date: Fri, 20 Oct 2017 13:17:02 -0400 Subject: [PATCH 1/3] Misc small fixes --- catalyst/data/loader.py | 4 ++-- catalyst/examples/buy_low_sell_high_live.py | 2 +- catalyst/exchange/bitfinex/bitfinex.py | 5 +++-- catalyst/exchange/bittrex/bittrex.py | 8 ++++++-- catalyst/exchange/poloniex/poloniex.py | 2 +- catalyst/sources/benchmark_source.py | 8 +++++++- catalyst/utils/run_algo.py | 2 +- tests/exchange/test_bundle.py | 19 +++++++++++++++++++ 8 files changed, 40 insertions(+), 10 deletions(-) diff --git a/catalyst/data/loader.py b/catalyst/data/loader.py index ce293e9b..7a539bfb 100644 --- a/catalyst/data/loader.py +++ b/catalyst/data/loader.py @@ -105,8 +105,8 @@ def load_crypto_market_data(trading_day=None, trading_days=None, # if trading_days is None: # trading_days = get_calendar('OPEN').schedule - if start_dt is None: - start_dt = get_calendar('OPEN').first_trading_session + # if start_dt is None: + start_dt = get_calendar('OPEN').first_trading_session if end_dt is None: end_dt = pd.Timestamp.utcnow() diff --git a/catalyst/examples/buy_low_sell_high_live.py b/catalyst/examples/buy_low_sell_high_live.py index 6742896f..bfd44a2f 100644 --- a/catalyst/examples/buy_low_sell_high_live.py +++ b/catalyst/examples/buy_low_sell_high_live.py @@ -21,7 +21,7 @@ def initialize(context): context.ASSET_NAME = 'etc_btc' context.asset = symbol(context.ASSET_NAME) - context.TARGET_POSITIONS = 3 + context.TARGET_POSITIONS = 30 context.PROFIT_TARGET = 0.1 context.SLIPPAGE_ALLOWED = 0.02 diff --git a/catalyst/exchange/bitfinex/bitfinex.py b/catalyst/exchange/bitfinex/bitfinex.py index 1817ea2d..da141e6f 100644 --- a/catalyst/exchange/bitfinex/bitfinex.py +++ b/catalyst/exchange/bitfinex/bitfinex.py @@ -56,7 +56,7 @@ class Bitfinex(Exchange): # Max is 90 but playing it safe # https://www.bitfinex.com/posts/188 - self.max_requests_per_minute = 20 + self.max_requests_per_minute = 80 self.request_cpt = dict() self.bundle = ExchangeBundle(self) @@ -665,10 +665,11 @@ class Bitfinex(Exchange): return time.strftime('%Y-%m-%d', time.gmtime(int(response.json()[-1][0] / 1000))) - def get_orderbook(self, asset, order_type='all'): + def get_orderbook(self, asset, order_type='all', limit=100): exchange_symbol = asset.exchange_symbol try: self.ask_request() + # TODO: implement limit response = self._request( 'book/{}'.format(exchange_symbol), None) data = response.json() diff --git a/catalyst/exchange/bittrex/bittrex.py b/catalyst/exchange/bittrex/bittrex.py index dcc9b943..94df439f 100644 --- a/catalyst/exchange/bittrex/bittrex.py +++ b/catalyst/exchange/bittrex/bittrex.py @@ -358,7 +358,7 @@ class Bittrex(Exchange): json.dump(symbol_map, f, sort_keys=True, indent=2, separators=(',', ':')) - def get_orderbook(self, asset, order_type='all'): + def get_orderbook(self, asset, order_type='all', limit=100): if order_type == 'all': order_type = 'both' elif order_type == 'bid': @@ -369,7 +369,11 @@ class Bittrex(Exchange): raise ValueError('invalid type') exchange_symbol = asset.exchange_symbol - data = self.api.getorderbook(market=exchange_symbol, type=order_type) + data = self.api.getorderbook( + market=exchange_symbol, + type=order_type, + depth=100 + ) result = dict() for exchange_type in data: diff --git a/catalyst/exchange/poloniex/poloniex.py b/catalyst/exchange/poloniex/poloniex.py index e24acc75..099e64a2 100644 --- a/catalyst/exchange/poloniex/poloniex.py +++ b/catalyst/exchange/poloniex/poloniex.py @@ -49,7 +49,7 @@ class Poloniex(Exchange): self.transactions = defaultdict(list) self.num_candles_limit = 2000 - self.max_requests_per_minute = 20 + self.max_requests_per_minute = 60 self.request_cpt = dict() self.bundle = ExchangeBundle(self) diff --git a/catalyst/sources/benchmark_source.py b/catalyst/sources/benchmark_source.py index 05d5c601..a20bf15f 100644 --- a/catalyst/sources/benchmark_source.py +++ b/catalyst/sources/benchmark_source.py @@ -72,7 +72,13 @@ class BenchmarkSource(object): "benchmark_returns.") def get_value(self, dt): - return self._precalculated_series.loc[dt] + try: + series = self._precalculated_series + value = series.loc[dt] + return value + except Exception: + # TODO: workaround, find permanent fix + return 0 def get_range(self, start_dt, end_dt): return self._precalculated_series.loc[start_dt:end_dt] diff --git a/catalyst/utils/run_algo.py b/catalyst/utils/run_algo.py index 3a318748..cadf8d3d 100644 --- a/catalyst/utils/run_algo.py +++ b/catalyst/utils/run_algo.py @@ -268,7 +268,7 @@ def _run(handle_data, ) # TODO: use the constructor instead - # sim_params._arena = 'live' + sim_params._arena = 'live' algorithm_class = partial( ExchangeTradingAlgorithmLive, diff --git a/tests/exchange/test_bundle.py b/tests/exchange/test_bundle.py index 301f71bb..d7ef9ed7 100644 --- a/tests/exchange/test_bundle.py +++ b/tests/exchange/test_bundle.py @@ -17,6 +17,25 @@ log = Logger('test_exchange_bundle') class ExchangeBundleTestCase: + def test_spot_value(self): + data_frequency = 'daily' + exchange_name = 'poloniex' + + exchange = get_exchange(exchange_name) + exchange_bundle = ExchangeBundle(exchange) + assets = [ + exchange.get_asset('btc_usdt') + ] + dt = pd.to_datetime('2017-9-29 23:59', utc=True) + + values = exchange_bundle.get_spot_values( + assets=assets, + field='close', + dt=dt, + data_frequency=data_frequency + ) + pass + def test_ingest_minute(self): data_frequency = 'minute' exchange_name = 'bitfinex' From f918fc97bcfc1364865d5ec057d1e32a6e860f75 Mon Sep 17 00:00:00 2001 From: fredfortier Date: Fri, 20 Oct 2017 13:36:39 -0400 Subject: [PATCH 2/3] Fix an issue with data.history() in backtest mode --- catalyst/examples/buy_low_sell_high_live.py | 14 +++++++------- catalyst/exchange/exchange_bundle.py | 2 +- tests/exchange/test_bundle.py | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/catalyst/examples/buy_low_sell_high_live.py b/catalyst/examples/buy_low_sell_high_live.py index bfd44a2f..a1fd326c 100644 --- a/catalyst/examples/buy_low_sell_high_live.py +++ b/catalyst/examples/buy_low_sell_high_live.py @@ -18,7 +18,7 @@ log = Logger('buy low sell high') def initialize(context): log.info('initializing algo') - context.ASSET_NAME = 'etc_btc' + context.ASSET_NAME = 'btc_usdt' context.asset = symbol(context.ASSET_NAME) context.TARGET_POSITIONS = 30 @@ -41,7 +41,7 @@ def _handle_data(context, data): context.asset, fields='price', bar_count=20, - frequency='15m' + frequency='1d' ) rsi = talib.RSI(prices.values, timeperiod=14)[-1] log.info('got rsi: {}'.format(rsi)) @@ -54,7 +54,7 @@ def _handle_data(context, data): elif rsi <= 70: buy_increment = 0.2 else: - buy_increment = None + buy_increment = 0.1 cash = context.portfolio.cash log.info('base currency available: {cash}'.format(cash=cash)) @@ -147,14 +147,14 @@ def analyze(context, stats): run_algorithm( - capital_base=1, + capital_base=100000, initialize=initialize, handle_data=handle_data, analyze=analyze, - exchange_name='bitfinex', + exchange_name='poloniex', start=pd.to_datetime('2017-5-01', utc=True), - end=pd.to_datetime('2017-10-01', utc=True), - base_currency='btc', + end=pd.to_datetime('2017-10-16', utc=True), + base_currency='usdt', data_frequency='daily' ) # run_algorithm( diff --git a/catalyst/exchange/exchange_bundle.py b/catalyst/exchange/exchange_bundle.py index ad014b8d..8152aac4 100644 --- a/catalyst/exchange/exchange_bundle.py +++ b/catalyst/exchange/exchange_bundle.py @@ -597,7 +597,7 @@ class ExchangeBundle: for asset_index, asset in enumerate(assets): asset_values = arrays[asset_index] - value_series = pd.Series(asset_values[0], index=periods) + value_series = pd.Series(asset_values.flatten(), index=periods) series[asset] = value_series return series diff --git a/tests/exchange/test_bundle.py b/tests/exchange/test_bundle.py index d7ef9ed7..c4575fe8 100644 --- a/tests/exchange/test_bundle.py +++ b/tests/exchange/test_bundle.py @@ -26,7 +26,7 @@ class ExchangeBundleTestCase: assets = [ exchange.get_asset('btc_usdt') ] - dt = pd.to_datetime('2017-9-29 23:59', utc=True) + dt = pd.to_datetime('2017-10-14', utc=True) values = exchange_bundle.get_spot_values( assets=assets, From 493fc95a205800ced09c758013c8ee66857c8a20 Mon Sep 17 00:00:00 2001 From: fredfortier Date: Fri, 20 Oct 2017 15:17:29 -0400 Subject: [PATCH 3/3] Fixed an issue with historical data in live mode --- catalyst/exchange/exchange.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/catalyst/exchange/exchange.py b/catalyst/exchange/exchange.py index f8c902a8..22c06439 100644 --- a/catalyst/exchange/exchange.py +++ b/catalyst/exchange/exchange.py @@ -472,7 +472,7 @@ class Exchange: # Adding bars too recent to be contained in the consolidated # exchanges bundles. We go directly against the exchange # to retrieve the candles. - + start_dt = get_start_dt(end_dt, adj_bar_count, data_frequency) trailing_dt = \ series[asset].index[-1] + get_delta(1, data_frequency) \ if asset in series else start_dt