Only re-calc avg pp price on pp size increases

ordermodepps_backup
Tyler Goodlet 2021-07-12 11:18:43 -04:00
parent 94275c9be8
commit 5a271f9a5e
1 changed files with 15 additions and 4 deletions

View File

@ -187,6 +187,7 @@ class PaperBoi:
# remaining lots to fill # remaining lots to fill
order_complete: bool = True, order_complete: bool = True,
remaining: float = 0, remaining: float = 0,
) -> None: ) -> None:
"""Pretend to fill a broker order @ price and size. """Pretend to fill a broker order @ price and size.
@ -259,14 +260,24 @@ class PaperBoi:
# TODO: eventually it'd be nice to have a small set of routines # TODO: eventually it'd be nice to have a small set of routines
# to do this stuff from a sequence of cleared orders to enable # to do this stuff from a sequence of cleared orders to enable
# so called "contextual positions". # so called "contextual positions".
new_size = size + pp_msg.size new_size = size + pp_msg.size
if new_size != 0: # old size minus the new size gives us size differential with
pp_msg.avg_price = (size*price + pp_msg.avg_price) / new_size # +ve -> increase in pp size
else: # -ve -> decrease in pp size
size_diff = abs(new_size) - abs(pp_msg.size)
if new_size == 0:
pp_msg.avg_price = 0 pp_msg.avg_price = 0
elif size_diff > 0:
# only update the "average position price" when the position
# size increases not when it decreases (i.e. the position is
# being made smaller)
pp_msg.avg_price = (
abs(size) * price + pp_msg.avg_price * abs(pp_msg.size)
) / abs(new_size)
pp_msg.size = new_size pp_msg.size = new_size
await self.ems_trades_stream.send(pp_msg.dict()) await self.ems_trades_stream.send(pp_msg.dict())