'`kraken`: fix pos loading using `digits_to_dec()` to pair info

Our issue was not having the correct value set on each
`Symbol.lot_tick_size`.. and then doing PPU calcs with the default set
for legacy mkts..

Also,
- actually write `pps.toml` on broker mode exit.
- drop `get_likely_pair()` and import from pp module.
rekt_pps
Tyler Goodlet 2023-03-10 16:08:25 -05:00
parent badc30baae
commit d01fdbf981
2 changed files with 36 additions and 39 deletions

View File

@ -20,6 +20,7 @@ Kraken web API wrapping.
''' '''
from contextlib import asynccontextmanager as acm from contextlib import asynccontextmanager as acm
from datetime import datetime from datetime import datetime
from decimal import Decimal
import itertools import itertools
from typing import ( from typing import (
Any, Any,
@ -248,6 +249,9 @@ class Client:
{}, {},
) )
by_bsuid = resp['result'] by_bsuid = resp['result']
# TODO: we need to pull out the "asset" decimals
# data and return a `decimal.Decimal` instead here!
return { return {
self._atable[sym].lower(): float(bal) self._atable[sym].lower(): float(bal)
for sym, bal in by_bsuid.items() for sym, bal in by_bsuid.items()

View File

@ -21,7 +21,6 @@ Order api and machinery
from collections import ChainMap, defaultdict from collections import ChainMap, defaultdict
from contextlib import ( from contextlib import (
asynccontextmanager as acm, asynccontextmanager as acm,
contextmanager as cm,
) )
from functools import partial from functools import partial
from itertools import count from itertools import count
@ -47,8 +46,12 @@ from piker.pp import (
Transaction, Transaction,
open_trade_ledger, open_trade_ledger,
open_pps, open_pps,
get_likely_pair,
)
from piker.data._source import (
Symbol,
digits_to_dec,
) )
from piker.data._source import Symbol
from piker.clearing._messages import ( from piker.clearing._messages import (
Order, Order,
Status, Status,
@ -470,12 +473,14 @@ async def trades_dialogue(
with ( with (
open_pps( open_pps(
'kraken', 'kraken',
acctid acctid,
write_on_exit=True,
) as table, ) as table,
open_trade_ledger( open_trade_ledger(
'kraken', 'kraken',
acctid acctid,
) as ledger_dict, ) as ledger_dict,
): ):
# transaction-ify the ledger entries # transaction-ify the ledger entries
@ -494,7 +499,10 @@ async def trades_dialogue(
# what amount of trades-transactions need # what amount of trades-transactions need
# to be reloaded. # to be reloaded.
balances = await client.get_balances() balances = await client.get_balances()
# await tractor.breakpoint()
for dst, size in balances.items(): for dst, size in balances.items():
# we don't care about tracking positions # we don't care about tracking positions
# in the user's source fiat currency. # in the user's source fiat currency.
if ( if (
@ -508,45 +516,20 @@ async def trades_dialogue(
) )
continue continue
def get_likely_pair(
dst: str,
bsuid: str,
src_fiat: str = src_fiat
) -> str:
'''
Attempt to get the likely trading pair masting
a given destination asset `dst: str`.
'''
try:
src_name_start = bsuid.rindex(src_fiat)
except (
ValueError, # substr not found
):
# TODO: handle nested positions..(i.e.
# positions where the src fiat was used to
# buy some other dst which was furhter used
# to buy another dst..)
log.warning(
f'No src fiat {src_fiat} found in {bsuid}?'
)
return
likely_dst = bsuid[:src_name_start]
if likely_dst == dst:
return bsuid
def has_pp( def has_pp(
dst: str, dst: str,
size: float, size: float,
) -> Position | bool: ) -> Position | None:
src2dst: dict[str, str] = {} src2dst: dict[str, str] = {}
for bsuid in table.pps: for bsuid in table.pps:
likely_pair = get_likely_pair(dst, bsuid) likely_pair = get_likely_pair(
src_fiat,
dst,
bsuid,
)
if likely_pair: if likely_pair:
src2dst[src_fiat] = dst src2dst[src_fiat] = dst
@ -574,7 +557,7 @@ async def trades_dialogue(
) )
return pp return pp
return False return None # signal no entry
pos = has_pp(dst, size) pos = has_pp(dst, size)
if not pos: if not pos:
@ -602,7 +585,11 @@ async def trades_dialogue(
# yet and thus this likely pair grabber will # yet and thus this likely pair grabber will
# likely fail. # likely fail.
for bsuid in table.pps: for bsuid in table.pps:
likely_pair = get_likely_pair(dst, bsuid) likely_pair = get_likely_pair(
src_fiat,
dst,
bsuid,
)
if likely_pair: if likely_pair:
break break
else: else:
@ -724,8 +711,8 @@ async def handle_order_updates(
''' '''
Main msg handling loop for all things order management. Main msg handling loop for all things order management.
This code is broken out to make the context explicit and state variables This code is broken out to make the context explicit and state
defined in the signature clear to the reader. variables defined in the signature clear to the reader.
''' '''
async for msg in ws_stream: async for msg in ws_stream:
@ -1204,7 +1191,13 @@ def norm_trade_records(
fqsn, fqsn,
info={ info={
'lot_size_digits': pair_info.lot_decimals, 'lot_size_digits': pair_info.lot_decimals,
'lot_tick_size': digits_to_dec(
pair_info.lot_decimals,
),
'tick_size_digits': pair_info.pair_decimals, 'tick_size_digits': pair_info.pair_decimals,
'price_tick_size': digits_to_dec(
pair_info.pair_decimals,
),
'asset_type': 'crypto', 'asset_type': 'crypto',
}, },
) )