Add simple caching to get_ticker and re-enable caching test
This commit is contained in:
parent
478c3e299e
commit
8e52598da9
@ -17,6 +17,9 @@ logger = logging.getLogger(__name__)
|
|||||||
_API: ccxt.Exchange = None
|
_API: ccxt.Exchange = None
|
||||||
_CONF: dict = {}
|
_CONF: dict = {}
|
||||||
|
|
||||||
|
# Cache for ticker data
|
||||||
|
_TICKER_CACHE: dict = {}
|
||||||
|
|
||||||
# Holds all open sell orders for dry_run
|
# Holds all open sell orders for dry_run
|
||||||
_DRY_RUN_OPEN_ORDERS: Dict[str, Any] = {}
|
_DRY_RUN_OPEN_ORDERS: Dict[str, Any] = {}
|
||||||
|
|
||||||
@ -190,9 +193,13 @@ def get_balances() -> dict:
|
|||||||
|
|
||||||
|
|
||||||
def get_ticker(pair: str, refresh: Optional[bool] = True) -> dict:
|
def get_ticker(pair: str, refresh: Optional[bool] = True) -> dict:
|
||||||
|
global _TICKER_CACHE
|
||||||
try:
|
try:
|
||||||
# TODO: add caching
|
if not refresh:
|
||||||
return _API.fetch_ticker(pair)
|
if _TICKER_CACHE:
|
||||||
|
return _TICKER_CACHE
|
||||||
|
_TICKER_CACHE = _API.fetch_ticker(pair)
|
||||||
|
return _TICKER_CACHE
|
||||||
except ccxt.NetworkError as e:
|
except ccxt.NetworkError as e:
|
||||||
raise NetworkException(
|
raise NetworkException(
|
||||||
'Could not load tickers due to networking error. Message: {}'.format(e)
|
'Could not load tickers due to networking error. Message: {}'.format(e)
|
||||||
|
@ -224,9 +224,9 @@ def test_get_ticker(default_conf, mocker):
|
|||||||
|
|
||||||
# if not caching the result we should get the same ticker
|
# if not caching the result we should get the same ticker
|
||||||
# if not fetching a new result we should get the cached ticker
|
# if not fetching a new result we should get the cached ticker
|
||||||
# ticker = get_ticker(pair='ETH/BTC', refresh=False)
|
ticker = get_ticker(pair='ETH/BTC', refresh=False)
|
||||||
# assert ticker['bid'] == 0.00001098
|
assert ticker['bid'] == 0.00001098
|
||||||
# assert ticker['ask'] == 0.00001099
|
assert ticker['ask'] == 0.00001099
|
||||||
|
|
||||||
# force ticker refresh
|
# force ticker refresh
|
||||||
ticker = get_ticker(pair='ETH/BTC', refresh=True)
|
ticker = get_ticker(pair='ETH/BTC', refresh=True)
|
||||||
@ -259,13 +259,26 @@ def test_get_ticker_history(default_conf, mocker):
|
|||||||
assert ticks[0]['V'] == 5
|
assert ticks[0]['V'] == 5
|
||||||
|
|
||||||
# change the ticker
|
# change the ticker
|
||||||
# tick = 999
|
new_tick = [
|
||||||
# api_mock.get_ticker_history = MagicMock(return_value=tick)
|
[
|
||||||
# mocker.patch('freqtrade.exchange._API', api_mock)
|
1511686200000, # unix timestamp ms
|
||||||
|
6, # open
|
||||||
|
7, # high
|
||||||
|
8, # low
|
||||||
|
9, # close
|
||||||
|
10, # volume (in quote currency)
|
||||||
|
]
|
||||||
|
]
|
||||||
|
api_mock.get_ticker_history = MagicMock(return_value=new_tick)
|
||||||
|
mocker.patch('freqtrade.exchange._API', api_mock)
|
||||||
|
|
||||||
# ensure caching will still return the original ticker
|
# 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 == 123
|
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
|
||||||
|
|
||||||
|
|
||||||
def test_cancel_order_dry_run(default_conf, mocker):
|
def test_cancel_order_dry_run(default_conf, mocker):
|
||||||
|
Loading…
Reference in New Issue
Block a user