From 55212a5977c6e640634c5be583fc832461aaec2f Mon Sep 17 00:00:00 2001 From: dave Date: Fri, 14 Feb 2020 09:36:12 -0800 Subject: [PATCH] correct historical buy price calculation --- pyircbot/modules/StockPlay.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/pyircbot/modules/StockPlay.py b/pyircbot/modules/StockPlay.py index d96bd98..085dd5b 100644 --- a/pyircbot/modules/StockPlay.py +++ b/pyircbot/modules/StockPlay.py @@ -163,16 +163,22 @@ class StockPlay(ModuleBase): target_count = self.get_holding(nick, symbol) # backtrack until we hit this many shares spent = 0 count = 0 + buys = 0 with closing(self.sql.getCursor()) as c: for row in c.execute("SELECT * FROM stockplay_trades WHERE nick=? AND symbol=? ORDER BY time DESC", (nick, symbol)).fetchall(): - count += row["count"] * (1 if row["type"] == "buy" else -1) - spent += row["price"] * (1 if row["type"] == "buy" else -1) + if row["type"] == "buy": + count += row["count"] + spent += row["price"] + buys += row["count"] + else: + count -= row["count"] + if count == target_count: # at this point in history the user held 0 of the symbol, stop backtracking break if not count: return Decimal(0) - return Decimal(spent) / 100 / Decimal(count) + return Decimal(spent) / 100 / Decimal(buys) def price_updater(self): """