[Fix] bug when calculating spread pos using net pos

This commit is contained in:
vn.py 2019-10-17 16:18:18 +08:00
parent 1a054ae9ae
commit 9f8c0e87b7

View File

@ -172,24 +172,34 @@ class SpreadData:
def calculate_pos(self): def calculate_pos(self):
"""""" """"""
self.net_pos = 0 long_pos = 0
short_pos = 0
for n, leg in enumerate(self.legs.values()): for n, leg in enumerate(self.legs.values()):
leg_long_pos = 0
leg_short_pos = 0
trading_multiplier = self.trading_multipliers[leg.vt_symbol] trading_multiplier = self.trading_multipliers[leg.vt_symbol]
adjusted_net_pos = leg.net_pos / trading_multiplier adjusted_net_pos = leg.net_pos / trading_multiplier
if adjusted_net_pos > 0: if adjusted_net_pos > 0:
adjusted_net_pos = floor(adjusted_net_pos) adjusted_net_pos = floor(adjusted_net_pos)
leg_long_pos = adjusted_net_pos
else: else:
adjusted_net_pos = ceil(adjusted_net_pos) adjusted_net_pos = ceil(adjusted_net_pos)
leg_short_pos = abs(adjusted_net_pos)
if not n: if not n:
self.net_pos = adjusted_net_pos long_pos = leg_long_pos
short_pos = leg_short_pos
else: else:
if adjusted_net_pos > 0: long_pos = min(long_pos, leg_long_pos)
self.net_pos = min(self.net_pos, adjusted_net_pos) short_pos = min(short_pos, leg_short_pos)
if long_pos > 0:
self.net_pos = long_pos
else: else:
self.net_pos = max(self.net_pos, adjusted_net_pos) self.net_pos = short_pos
def clear_price(self): def clear_price(self):
"""""" """"""