correct historical buy price calculation

This commit is contained in:
dave 2020-02-14 09:36:12 -08:00
parent 998d3a5f55
commit 55212a5977
1 changed files with 9 additions and 3 deletions

View File

@ -163,16 +163,22 @@ class StockPlay(ModuleBase):
target_count = self.get_holding(nick, symbol) # backtrack until we hit this many shares target_count = self.get_holding(nick, symbol) # backtrack until we hit this many shares
spent = 0 spent = 0
count = 0 count = 0
buys = 0
with closing(self.sql.getCursor()) as c: with closing(self.sql.getCursor()) as c:
for row in c.execute("SELECT * FROM stockplay_trades WHERE nick=? AND symbol=? ORDER BY time DESC", for row in c.execute("SELECT * FROM stockplay_trades WHERE nick=? AND symbol=? ORDER BY time DESC",
(nick, symbol)).fetchall(): (nick, symbol)).fetchall():
count += row["count"] * (1 if row["type"] == "buy" else -1) if row["type"] == "buy":
spent += row["price"] * (1 if row["type"] == "buy" else -1) 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 if count == target_count: # at this point in history the user held 0 of the symbol, stop backtracking
break break
if not count: if not count:
return Decimal(0) return Decimal(0)
return Decimal(spent) / 100 / Decimal(count) return Decimal(spent) / 100 / Decimal(buys)
def price_updater(self): def price_updater(self):
""" """