get_ticker can return a cached value

This commit is contained in:
Jean-Baptiste LE STANG
2018-01-02 10:56:42 +01:00
parent a57707071c
commit 7d21015b52
4 changed files with 33 additions and 28 deletions

View File

@@ -134,8 +134,8 @@ def get_balances():
return _API.get_balances()
def get_ticker(pair: str) -> dict:
return _API.get_ticker(pair)
def get_ticker(pair: str, refresh: Optional[bool] = True) -> dict:
return _API.get_ticker(pair, refresh)
@cached(TTLCache(maxsize=100, ttl=30))

View File

@@ -1,5 +1,5 @@
import logging
from typing import List, Dict
from typing import List, Dict, Optional
from bittrex.bittrex import Bittrex as _Bittrex, API_V2_0, API_V1_1
from requests.exceptions import ContentDecodingError
@@ -38,6 +38,7 @@ class Bittrex(Exchange):
calls_per_second=1,
api_version=API_V2_0,
)
self.cached_ticker = {}
@staticmethod
def _validate_response(response) -> None:
@@ -95,26 +96,29 @@ class Bittrex(Exchange):
raise OperationalException('{message}'.format(message=data['message']))
return data['result']
def get_ticker(self, pair: str) -> dict:
data = _API.get_ticker(pair.replace('_', '-'))
if not data['success']:
Bittrex._validate_response(data)
raise OperationalException('{message} params=({pair})'.format(
message=data['message'],
pair=pair))
def get_ticker(self, pair: str, refresh: Optional[bool] = True) -> dict:
data = _API.get_ticker(pair.replace('_', '-'), refresh)
if refresh:
if not data['success']:
Bittrex._validate_response(data)
raise OperationalException('{message} params=({pair})'.format(
message=data['message'],
pair=pair))
if not data.get('result') \
or not data['result'].get('Bid') \
or not data['result'].get('Ask') \
or not data['result'].get('Last'):
raise ContentDecodingError('{message} params=({pair})'.format(
message='Got invalid response from bittrex',
pair=pair))
return {
'bid': float(data['result']['Bid']),
'ask': float(data['result']['Ask']),
'last': float(data['result']['Last']),
}
if not data.get('result') \
or not data['result'].get('Bid') \
or not data['result'].get('Ask') \
or not data['result'].get('Last'):
raise ContentDecodingError('{message} params=({pair})'.format(
message='Got invalid response from bittrex',
pair=pair))
# Update the pair
self.cached_ticker[pair] = {
'bid': float(data['result']['Bid']),
'ask': float(data['result']['Ask']),
'last': float(data['result']['Last']),
}
return self.cached_ticker[pair]
def get_ticker_history(self, pair: str, tick_interval: int) -> List[Dict]:
if tick_interval == 1:

View File

@@ -1,5 +1,5 @@
from abc import ABC, abstractmethod
from typing import List, Dict
from typing import List, Dict, Optional
class Exchange(ABC):
@@ -62,10 +62,11 @@ class Exchange(ABC):
"""
@abstractmethod
def get_ticker(self, pair: str) -> dict:
def get_ticker(self, pair: str, refresh: Optional[bool] = True) -> dict:
"""
Gets ticker for given pair.
:param pair: Pair as str, format: BTC_ETC
:param refresh: Shall we query a new value or a cached value is enought
:return: dict, format: {
'bid': float,
'ask': float,