Mercurial > lbo > hg > stex
changeset 12:f092f8324486
Add graphical indicators for average buy price and current update
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Fri, 01 Mar 2019 17:04:08 +0100 |
parents | 24971c82b835 |
children | a713a417d56f |
files | client/client.py server/server.py |
diffstat | 2 files changed, 44 insertions(+), 12 deletions(-) [+] |
line wrap: on
line diff
--- a/client/client.py Fri Mar 01 16:42:02 2019 +0100 +++ b/client/client.py Fri Mar 01 17:04:08 2019 +0100 @@ -126,11 +126,12 @@ raise AttributeError('stock not found!') stock = self.stock[stocksym] price = stock.current_price - assert price > 0 + if price == 0: + price = 1 if price * num > self.cash: return False self.cash -= price * num - stock.current_num += num + stock.change_hold(num) return True def sell(self, stocksym, num): @@ -138,11 +139,10 @@ raise AttributeError('stock not found!') stock = self.stock[stocksym] price = stock.current_price - assert price > 0 if num > stock.current_num: return False self.cash += price * num - stock.current_num -= num + stock.change_hold(-num) return True def update(self, message): @@ -172,6 +172,7 @@ mydepot = None current_price = -1 + total_buy_price = 0 current_num = 0 MAXHIST = 500 price_history = [] @@ -188,9 +189,21 @@ if upd['split']: self.current_num = self.current_num * 2 + def change_hold(self, diff): + self.current_num += diff + if diff > 0: + self.total_buy_price += diff * self.current_price + if diff < 0: + self.total_buy_price += diff * self.avg_buy_price() + + def avg_buy_price(self): + return self.total_buy_price / (self.current_num or 1) + class StockGraph(chart.QChartView): sym = '' + # Updated by StockWidget + avg_buy_price = 0 MAX_LEN = 500 XAXIS = [i for i in range(0, MAX_LEN)] @@ -198,30 +211,45 @@ current = 0 series = None - min, max = 1e9, -1e9 + avg_buy_series = None + upd_series = None def __init__(self, sym, dim): super().__init__() super().setMinimumSize(300, 200) self.sym = sym self.series = chart.QLineSeries(self) + self.avg_buy_series = chart.QLineSeries(self) + self.upd_series = chart.QLineSeries(self) + for x in self.XAXIS: self.series.append(x, 0) super().chart().setTitle(self.sym) super().chart().legend().hide() super().chart().addSeries(self.series) + super().chart().addSeries(self.avg_buy_series) + super().chart().addSeries(self.upd_series) super().chart().createDefaultAxes() def update_stock(self, value): - if value < self.min: - self.min = value - if value > self.max: - self.max = value + min, max = 1e9, -1e9 + for v in self.series.pointsVector(): + if v.y() < min: + min = v.y() + if v.y() > max: + max = v.y() previous, nxt = (self.current - 1) % self.MAX_LEN, (self.current + 1) % self.MAX_LEN self.series.replace(self.current, self.current, value) - self.series.replace(nxt, nxt, 0) + + self.upd_series.clear() + self.upd_series.append(self.current, 0) + self.upd_series.append(self.current, max) + + self.avg_buy_series.clear() + self.avg_buy_series.append(0, self.avg_buy_price) + self.avg_buy_series.append(self.MAX_LEN - 1, self.avg_buy_price) self.current += 1 if self.current >= self.MAX_LEN: @@ -229,7 +257,10 @@ self.plot() def plot(self): - super().chart().removeSeries(super().chart().series()[0]) + while super().chart().series(): + super().chart().removeSeries(super().chart().series()[0]) + super().chart().addSeries(self.upd_series) + super().chart().addSeries(self.avg_buy_series) super().chart().addSeries(self.series) super().chart().createDefaultAxes() @@ -287,6 +318,7 @@ def update_values(self): val = self.depotstock.current_price / 100 + self.graph.avg_buy_price = self.depotstock.avg_buy_price() / 100 self.current_state.setText('{} pc / {:.2f} ø/pc / {:.2f} ø'.format(self.depotstock.current_num, val, self.depotstock.current_num * val))
--- a/server/server.py Fri Mar 01 16:42:02 2019 +0100 +++ b/server/server.py Fri Mar 01 17:04:08 2019 +0100 @@ -41,7 +41,7 @@ def next_price(self): """Calculates a (random) next price based on the current price and history. Returns a dict suitable for inclusion in a _stockdata object.""" dev = 0.02 * self._current_value or 1 - new_value = int(_random.normalvariate(self._current_value * 1.0001, dev)) + new_value = int(_random.normalvariate(self._current_value * 1.0005, dev)) new_value = abs(new_value) split = False