Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wiseplat
GitHub Repository: wiseplat/python-code
Path: blob/master/ invest-robot-contest_investRobot-master/robotlib/vizualization.py
5929 views
1
import matplotlib.pyplot as plt
2
3
4
class Visualizer:
5
def __init__(self, ticker, currency):
6
self.ticker = ticker
7
self.currency = currency
8
self.fig = plt.figure()
9
self.buys = []
10
self.sells = []
11
self.prices = {}
12
13
def add_price(self, time, price: float):
14
self.prices[time] = price
15
16
def add_buy(self, time):
17
self.buys.append(time)
18
19
def add_sell(self, time):
20
self.sells.append(time)
21
22
def update_plot(self):
23
self.fig.clear()
24
25
x = list(self.prices.keys())[-50:]
26
y = list(self.prices.values())[-50:]
27
28
minx = min(x)
29
buys = [buy for buy in self.buys if buy >= minx]
30
sells = [sell for sell in self.sells if sell >= minx]
31
32
plt.title(self.ticker)
33
plt.xlabel('time')
34
plt.ylabel(f'price ({self.currency})')
35
plt.plot(x, y)
36
plt.vlines(buys, ymin=min(y), ymax=max(y), color='g')
37
plt.vlines(sells, ymin=min(y), ymax=max(y), color='r')
38
plt.draw()
39
plt.pause(0.2)
40
41