Path: blob/master/ invest-robot-contest_investRobot-master/robotlib/vizualization.py
5929 views
import matplotlib.pyplot as plt123class Visualizer:4def __init__(self, ticker, currency):5self.ticker = ticker6self.currency = currency7self.fig = plt.figure()8self.buys = []9self.sells = []10self.prices = {}1112def add_price(self, time, price: float):13self.prices[time] = price1415def add_buy(self, time):16self.buys.append(time)1718def add_sell(self, time):19self.sells.append(time)2021def update_plot(self):22self.fig.clear()2324x = list(self.prices.keys())[-50:]25y = list(self.prices.values())[-50:]2627minx = min(x)28buys = [buy for buy in self.buys if buy >= minx]29sells = [sell for sell in self.sells if sell >= minx]3031plt.title(self.ticker)32plt.xlabel('time')33plt.ylabel(f'price ({self.currency})')34plt.plot(x, y)35plt.vlines(buys, ymin=min(y), ymax=max(y), color='g')36plt.vlines(sells, ymin=min(y), ymax=max(y), color='r')37plt.draw()38plt.pause(0.2)394041