-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
78 lines (55 loc) · 1.61 KB
/
database.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from pathlib import Path
import pandas as pd
import datetime as dt
import matplotlib.pyplot as plt
def is_db_file(f):
return f.name.endswith("csv")
def get_files(p):
f = []
for x in p.iterdir():
if is_db_file(x):
f.append(x)
return f
def get_date_str_from_file_name(f):
return f.name.split('.')[0]
def get_date_from_file_name(f):
date_str = get_date_str_from_file_name(f)
return get_date_from_str(date_str)
def get_date_from_str(date_string):
return dt.datetime.strptime(date_string, '%Y-%m-%d %H:%M:%S')
def get_price_for_ticker_from_file(f, my_stock):
xx = pd.read_csv(f)
for xo in xx.iterrows():
series = xo[1]
ticker = series[0]
if ticker == my_stock:
current_price = series[4]
return current_price
class StockPoint:
def __init__(self, date, price):
self.date = date
self.price = price
def to_string(self):
return '{0} @ {1}'.format(self.date, self.price)
def get_prices_for_ticker(files, my_stock):
pp = []
for f in files:
date = get_date_from_file_name(f)
price = get_price_for_ticker_from_file(f, my_stock)
pp.append(StockPoint(date, price))
pp.sort(key=lambda x: x.date, reverse=False)
return pp
def plot_prices(price_list):
global p
yy = []
xx = []
for p in price_list:
yy.append(p.price)
xx.append(p.date)
plt.step(xx, yy)
plt.ylabel('some numbers')
plt.show()
prices = get_prices_for_ticker(get_files(Path('./records')), "EEEE")
for p in prices:
print(p.to_string())
plot_prices(prices)