From fea2ed104e8aaf9796f815be984ef95a04ccbb5f Mon Sep 17 00:00:00 2001 From: lenak25 Date: Thu, 22 Feb 2018 16:50:27 +0200 Subject: [PATCH] BUG: fix issue #236: handle properly empty candles received from exchanges --- catalyst/exchange/exchange.py | 48 ++++++++++++++++++++--------------- catalyst/support/issue_236.py | 32 +++++++++++++++++++++++ 2 files changed, 59 insertions(+), 21 deletions(-) create mode 100644 catalyst/support/issue_236.py diff --git a/catalyst/exchange/exchange.py b/catalyst/exchange/exchange.py index 3b57256b..a0a247fb 100644 --- a/catalyst/exchange/exchange.py +++ b/catalyst/exchange/exchange.py @@ -1,4 +1,5 @@ import abc +import pytz from abc import ABCMeta, abstractmethod, abstractproperty from datetime import timedelta from time import sleep @@ -514,32 +515,37 @@ class Exchange: series = dict() for asset in candles: - first_candle = candles[asset][0] - asset_series = self.get_series_from_candles( - candles=candles[asset], - start_dt=first_candle['last_traded'], - end_dt=end_dt, - data_frequency=frequency, - field=field, - ) - - delta_candle_size = candle_size * 60 if unit == 'H' else candle_size - # Checking to make sure that the dates match - delta = get_delta(delta_candle_size, data_frequency) - adj_end_dt = end_dt - delta - last_traded = asset_series.index[-1] - - if last_traded < adj_end_dt: - raise LastCandleTooEarlyError( - last_traded=last_traded, - end_dt=adj_end_dt, - exchange=self.name, + if candles[asset]: + first_candle = candles[asset][0] + asset_series = self.get_series_from_candles( + candles=candles[asset], + start_dt=first_candle['last_traded'], + end_dt=end_dt, + data_frequency=frequency, + field=field, ) + delta_candle_size = candle_size * 60 if unit == 'H' else candle_size + # Checking to make sure that the dates match + delta = get_delta(delta_candle_size, data_frequency) + adj_end_dt = end_dt - delta + last_traded = asset_series.index[-1] + + if last_traded < adj_end_dt: + raise LastCandleTooEarlyError( + last_traded=last_traded, + end_dt=adj_end_dt, + exchange=self.name, + ) + else: # empty candle received + # because other assets are tz-aware, we need its tz to be set as well + asset_series = pd.Series([], index=pd.DatetimeIndex([], tz=pytz.utc)) + + series[asset] = asset_series df = pd.DataFrame(series) - df.dropna(inplace=True) + #df.dropna(inplace=True) # commented out due to issue 236 return df diff --git a/catalyst/support/issue_236.py b/catalyst/support/issue_236.py new file mode 100644 index 00000000..c3a437a9 --- /dev/null +++ b/catalyst/support/issue_236.py @@ -0,0 +1,32 @@ +from catalyst.api import symbol +from catalyst.utils.run_algo import run_algorithm + +coins = ['dash', 'btc', 'dash', 'etc', 'eth', 'ltc', 'nxt', 'rep', 'str', 'xmr', 'xrp', 'zec'] +symbols = None + + +def initialize(context): + pass + + +def _handle_data(context, data): + global symbols + if symbols is None: symbols = [symbol(c + '_usdt') for c in coins] + + print'getting history for: %s' % [s.symbol for s in symbols] + history = data.history(symbols, + ['close', 'volume'], + bar_count=1, # EXCEPTION, Change to 2 + frequency='5T') + #print 'history: %s' % history.shape + +run_algorithm(initialize=initialize, + handle_data=_handle_data, + analyze=lambda _, results: True, + exchange_name='poloniex', + base_currency='usdt', + algo_namespace='issue-236', + live=True, + data_frequency='minute', + capital_base=3000, + simulate_orders=True) \ No newline at end of file