Change get_ticker_history format to ccxt format
This commit is contained in:
parent
4141ffa67b
commit
62a48c741b
@ -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]:
|
def get_ticker_history(pair: str, tick_interval: str) -> List[Dict]:
|
||||||
if 'fetchOHLCV' not in _API.has or not _API.has['fetchOHLCV']:
|
if 'fetchOHLCV' not in _API.has or not _API.has['fetchOHLCV']:
|
||||||
raise OperationalException(
|
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:
|
try:
|
||||||
history = _API.fetch_ohlcv(pair, timeframe=tick_interval)
|
return _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 []
|
|
||||||
except ccxt.NetworkError as e:
|
except ccxt.NetworkError as e:
|
||||||
raise NetworkException(
|
raise NetworkException(
|
||||||
'Could not load ticker history due to networking error. Message: {}'.format(e)
|
'Could not load ticker history due to networking error. Message: {}'.format(e)
|
||||||
|
@ -307,24 +307,23 @@ def test_get_ticker_history(default_conf, mocker):
|
|||||||
5, # volume (in quote currency)
|
5, # volume (in quote currency)
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
has = PropertyMock(return_value={'fetchOHLCV': True})
|
type(api_mock).has = PropertyMock(return_value={'fetchOHLCV': True})
|
||||||
type(api_mock).has = has
|
|
||||||
api_mock.fetch_ohlcv = MagicMock(return_value=tick)
|
api_mock.fetch_ohlcv = MagicMock(return_value=tick)
|
||||||
mocker.patch('freqtrade.exchange._API', api_mock)
|
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
|
# retrieve original ticker
|
||||||
ticks = get_ticker_history('ETH/BTC', default_conf['ticker_interval'])
|
ticks = get_ticker_history('ETH/BTC', default_conf['ticker_interval'])
|
||||||
assert ticks[0]['O'] == 1
|
assert ticks[0][0] == 1511686200000
|
||||||
assert ticks[0]['H'] == 2
|
assert ticks[0][1] == 1
|
||||||
assert ticks[0]['L'] == 3
|
assert ticks[0][2] == 2
|
||||||
assert ticks[0]['C'] == 4
|
assert ticks[0][3] == 3
|
||||||
assert ticks[0]['V'] == 5
|
assert ticks[0][4] == 4
|
||||||
|
assert ticks[0][5] == 5
|
||||||
|
|
||||||
# change the ticker
|
# change ticker and ensure tick changes
|
||||||
new_tick = [
|
new_tick = [
|
||||||
[
|
[
|
||||||
1511686200000, # unix timestamp ms
|
1511686210000, # unix timestamp ms
|
||||||
6, # open
|
6, # open
|
||||||
7, # high
|
7, # high
|
||||||
8, # low
|
8, # low
|
||||||
@ -332,16 +331,16 @@ def test_get_ticker_history(default_conf, mocker):
|
|||||||
10, # volume (in quote currency)
|
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)
|
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'])
|
ticks = get_ticker_history('ETH/BTC', default_conf['ticker_interval'])
|
||||||
assert ticks[0]['O'] == 1
|
assert ticks[0][0] == 1511686210000
|
||||||
assert ticks[0]['H'] == 2
|
assert ticks[0][1] == 6
|
||||||
assert ticks[0]['L'] == 3
|
assert ticks[0][2] == 7
|
||||||
assert ticks[0]['C'] == 4
|
assert ticks[0][3] == 8
|
||||||
assert ticks[0]['V'] == 5
|
assert ticks[0][4] == 9
|
||||||
|
assert ticks[0][5] == 10
|
||||||
|
|
||||||
with pytest.raises(OperationalException): # test retrier
|
with pytest.raises(OperationalException): # test retrier
|
||||||
api_mock.fetch_ohlcv = MagicMock(side_effect=ccxt.NetworkError)
|
api_mock.fetch_ohlcv = MagicMock(side_effect=ccxt.NetworkError)
|
||||||
|
Loading…
Reference in New Issue
Block a user