From 80ce43e0c50dba8ae2cc1fa0c6745a8efbfac151 Mon Sep 17 00:00:00 2001 From: Jacob Stultz Date: Thu, 7 Nov 2013 10:11:09 -0800 Subject: [PATCH] Calculate change percentage correctly Measuring percent change based on the current price instead of the original price is silly; instead use change / old price --- plugins/stock.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/plugins/stock.py b/plugins/stock.py index 101cd5e..388bcbe 100644 --- a/plugins/stock.py +++ b/plugins/stock.py @@ -19,16 +19,18 @@ def stock(inp): if quote['Change'] is None: return "unknown ticker symbol %s" % inp - if quote['Change'][0] == '-': + change = float(quote['Change']) + price = float(quote['LastTradePriceOnly']) + + if change < 0: quote['color'] = "5" else: quote['color'] = "3" - quote['Percent_Change'] = (100 * float(quote['Change']) / - float(quote['LastTradePriceOnly'])) + quote['PercentChange'] = 100 * change / (price - change) ret = "%(Name)s - %(LastTradePriceOnly)s " \ - "\x03%(color)s%(Change)s (%(Percent_Change).2f%%)\x03 " \ + "\x03%(color)s%(Change)s (%(PercentChange).2f%%)\x03 " \ "Day Range: %(DaysRange)s " \ "MCAP: %(MarketCapitalization)s" % quote