address latest comments, refactor the pack position function

kraken_orders
Konstantine Tsafatinos 2022-03-23 10:34:53 -04:00
parent 1525c645ce
commit cb8e97a142
1 changed files with 15 additions and 26 deletions

View File

@ -306,7 +306,6 @@ class Client:
price: float, price: float,
action: str, action: str,
size: float, size: float,
# account: str,
reqid: int = None, reqid: int = None,
) -> int: ) -> int:
''' '''
@ -474,36 +473,26 @@ def pack_positions(
for trade in trades.values(): for trade in trades.values():
sign = -1 if trade['type'] == 'sell' else 1 sign = -1 if trade['type'] == 'sell' else 1
# This catch is for populating the dict with new values
# as the plus assigment will fail if there no value
# tied to the key
pair = trade['pair'] pair = trade['pair']
vol = float(trade['vol']) vol = float(trade['vol'])
# This is for the initial addition of a pair so the vols[pair] = vols.get(pair, 0) + sign * vol
# += operation does not fail. costs[pair] = costs.get(pair, 0) + sign * float(trade['cost'])
vols[pair] = vols.setdefault(pair, 0) positions[pair] = costs[pair] / vols[pair] if vols[pair] else 0
costs[pair] = costs.setdefault(pair, 0)
positions[pair] = positions.setdefault(pair, 0)
vols[pair] += sign * vol
costs[pair] += sign * float(trade['cost'])
if vols[pair] != 0:
positions[pair] = costs[pair] / vols[pair]
else:
positions[pair] = 0
for ticker, pos in positions.items(): for ticker, pos in positions.items():
norm_sym = normalize_symbol(ticker)
vol = float(vols[ticker]) vol = float(vols[ticker])
if vol != 0: if not vol:
msg = BrokerdPosition( continue
broker='kraken', norm_sym = normalize_symbol(ticker)
account=acc, msg = BrokerdPosition(
symbol=norm_sym, broker='kraken',
currency=norm_sym[-3:], account=acc,
size=vol, symbol=norm_sym,
avg_price=float(pos), currency=norm_sym[-3:],
) size=vol,
position_msgs.append(msg.dict()) avg_price=float(pos),
)
position_msgs.append(msg.dict())
return position_msgs return position_msgs