From 62a48c741b42a095d2924a5b95c70df72a77a1bb Mon Sep 17 00:00:00 2001 From: enenn Date: Wed, 28 Mar 2018 21:59:05 +0200 Subject: [PATCH] Change get_ticker_history format to ccxt format --- freqtrade/exchange/__init__.py | 18 ++---------- freqtrade/tests/exchange/test_exchange.py | 35 +++++++++++------------ 2 files changed, 19 insertions(+), 34 deletions(-) diff --git a/freqtrade/exchange/__init__.py b/freqtrade/exchange/__init__.py index 34a3bbab3..cfb88ea37 100644 --- a/freqtrade/exchange/__init__.py +++ b/freqtrade/exchange/__init__.py @@ -235,25 +235,11 @@ def get_ticker(pair: str, refresh: Optional[bool] = True) -> dict: def get_ticker_history(pair: str, tick_interval: str) -> List[Dict]: if 'fetchOHLCV' not in _API.has or not _API.has['fetchOHLCV']: raise OperationalException( - 'Exhange {} does not support fetching historical candlestick data.'.format(_API.name) + 'Exchange {} does not support fetching historical candlestick data.'.format(_API.name) ) try: - history = _API.fetch_ohlcv(pair, timeframe=tick_interval) - history_json = [] - for candlestick in history: - history_json.append({ - 'T': arrow.get(candlestick[0]/1000.0).strftime('%Y-%m-%dT%H:%M:%S.%f'), - 'O': candlestick[1], - 'H': candlestick[2], - 'L': candlestick[3], - 'C': candlestick[4], - 'V': candlestick[5], - }) - return history_json - except IndexError as e: - logger.warning('Empty ticker history. Msg %s', str(e)) - return [] + return _API.fetch_ohlcv(pair, timeframe=tick_interval) except ccxt.NetworkError as e: raise NetworkException( 'Could not load ticker history due to networking error. Message: {}'.format(e) diff --git a/freqtrade/tests/exchange/test_exchange.py b/freqtrade/tests/exchange/test_exchange.py index 1d53b33e5..f4a0a98db 100644 --- a/freqtrade/tests/exchange/test_exchange.py +++ b/freqtrade/tests/exchange/test_exchange.py @@ -307,24 +307,23 @@ def test_get_ticker_history(default_conf, mocker): 5, # volume (in quote currency) ] ] - has = PropertyMock(return_value={'fetchOHLCV': True}) - type(api_mock).has = has + type(api_mock).has = PropertyMock(return_value={'fetchOHLCV': True}) api_mock.fetch_ohlcv = MagicMock(return_value=tick) mocker.patch('freqtrade.exchange._API', api_mock) - mocker.patch('freqtrade.exchange._API.has', {'fetchOHLCV': True}) - mocker.patch('freqtrade.exchange._API.fetch_ohlcv', return_value=tick) + # retrieve original ticker ticks = get_ticker_history('ETH/BTC', default_conf['ticker_interval']) - assert ticks[0]['O'] == 1 - assert ticks[0]['H'] == 2 - assert ticks[0]['L'] == 3 - assert ticks[0]['C'] == 4 - assert ticks[0]['V'] == 5 + assert ticks[0][0] == 1511686200000 + assert ticks[0][1] == 1 + assert ticks[0][2] == 2 + assert ticks[0][3] == 3 + assert ticks[0][4] == 4 + assert ticks[0][5] == 5 - # change the ticker + # change ticker and ensure tick changes new_tick = [ [ - 1511686200000, # unix timestamp ms + 1511686210000, # unix timestamp ms 6, # open 7, # high 8, # low @@ -332,16 +331,16 @@ def test_get_ticker_history(default_conf, mocker): 10, # volume (in quote currency) ] ] - api_mock.get_ticker_history = MagicMock(return_value=new_tick) + api_mock.fetch_ohlcv = MagicMock(return_value=new_tick) mocker.patch('freqtrade.exchange._API', api_mock) - # ensure caching will still return the original ticker ticks = get_ticker_history('ETH/BTC', default_conf['ticker_interval']) - assert ticks[0]['O'] == 1 - assert ticks[0]['H'] == 2 - assert ticks[0]['L'] == 3 - assert ticks[0]['C'] == 4 - assert ticks[0]['V'] == 5 + assert ticks[0][0] == 1511686210000 + assert ticks[0][1] == 6 + assert ticks[0][2] == 7 + assert ticks[0][3] == 8 + assert ticks[0][4] == 9 + assert ticks[0][5] == 10 with pytest.raises(OperationalException): # test retrier api_mock.fetch_ohlcv = MagicMock(side_effect=ccxt.NetworkError)