From bd754b740d3717a9a32cacb02d9ecb675b5794eb Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Mon, 12 Jul 2021 11:18:43 -0400 Subject: [PATCH] Only re-calc avg pp price on pp size increases --- piker/clearing/_paper_engine.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/piker/clearing/_paper_engine.py b/piker/clearing/_paper_engine.py index 5b746ebb..84e681dd 100644 --- a/piker/clearing/_paper_engine.py +++ b/piker/clearing/_paper_engine.py @@ -187,6 +187,7 @@ class PaperBoi: # remaining lots to fill order_complete: bool = True, remaining: float = 0, + ) -> None: """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 # to do this stuff from a sequence of cleared orders to enable # so called "contextual positions". - new_size = size + pp_msg.size - if new_size != 0: - pp_msg.avg_price = (size*price + pp_msg.avg_price) / new_size - else: + # old size minus the new size gives us size differential with + # +ve -> increase in pp size + # -ve -> decrease in pp size + size_diff = abs(new_size) - abs(pp_msg.size) + + if new_size == 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 await self.ems_trades_stream.send(pp_msg.dict())