address latest comments, refactor the pack position function
parent
1525c645ce
commit
cb8e97a142
|
@ -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,27 +473,17 @@ 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:
|
||||||
|
continue
|
||||||
|
norm_sym = normalize_symbol(ticker)
|
||||||
msg = BrokerdPosition(
|
msg = BrokerdPosition(
|
||||||
broker='kraken',
|
broker='kraken',
|
||||||
account=acc,
|
account=acc,
|
||||||
|
|
Loading…
Reference in New Issue