Provide symbol norming via a classmethod + global table

kraken_ws_orders
Tyler Goodlet 2022-07-10 19:07:54 -04:00
parent 57f2478dc7
commit 22f9b2552c
1 changed files with 31 additions and 25 deletions

View File

@ -141,6 +141,9 @@ class InvalidKey(ValueError):
class Client: class Client:
# global symbol normalization table
_ntable: dict[str, str] = {}
def __init__( def __init__(
self, self,
name: str = '', name: str = '',
@ -302,7 +305,9 @@ class Client:
async def symbol_info( async def symbol_info(
self, self,
pair: Optional[str] = None, pair: Optional[str] = None,
):
) -> dict[str, dict[str, str]]:
if pair is not None: if pair is not None:
pairs = {'pair': pair} pairs = {'pair': pair}
else: else:
@ -328,6 +333,12 @@ class Client:
if not self._pairs: if not self._pairs:
self._pairs = await self.symbol_info() self._pairs = await self.symbol_info()
ntable = {}
for restapikey, info in self._pairs.items():
ntable[restapikey] = ntable['wsname'] = info['altname']
self._ntable.update(ntable)
return self._pairs return self._pairs
async def search_symbols( async def search_symbols(
@ -425,6 +436,25 @@ class Client:
else: else:
raise BrokerError(errmsg) raise BrokerError(errmsg)
@classmethod
def normalize_symbol(
cls,
ticker: str
) -> str:
'''
Normalize symbol names to to a 3x3 pair from the global
definition map which we build out from the data retreived from
the 'AssetPairs' endpoint, see methods above.
'''
symlen = len(ticker)
if symlen != 6:
ticker = cls._ntable[ticker]
else:
raise ValueError(f'Unhandled symbol: {ticker}')
return ticker.lower()
@acm @acm
async def get_client() -> Client: async def get_client() -> Client:
@ -443,27 +473,3 @@ async def get_client() -> Client:
await client.cache_symbols() await client.cache_symbols()
yield client yield client
def normalize_symbol(
ticker: str
) -> str:
'''
Normalize symbol names to to a 3x3 pair.
'''
remap = {
'XXBTZEUR': 'XBTEUR',
'XXMRZEUR': 'XMREUR',
# ws versions? pretty weird..
'XBT/EUR': 'XBTEUR',
'XMR/EUR': 'XMREUR',
}
symlen = len(ticker)
if symlen != 6:
ticker = remap[ticker]
else:
raise ValueError(f'Unhandled symbol: {ticker}')
return ticker.lower()