-
Notifications
You must be signed in to change notification settings - Fork 0
/
testing.py
57 lines (48 loc) · 1.63 KB
/
testing.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
import yaml
from iexfinance import Stock, get_historical_data
import arrow
import csv
import json
from datetime import datetime, timedelta
with open("config.yaml", 'r') as configfile:
cfg = yaml.load(configfile)
symbols = []
for k,v in cfg['positions'].items():
symbols.append(k)
def get_price(sym):
symbol = Stock(sym)
price = symbol.get_price()
return price
#print(get_price('CARA'))
def fill_historicalvalues(symbols, months=1):
end = datetime.now()
start = datetime.now() - timedelta(days=(months*30))
data = {}
mytable = {}
for symbol in symbols:
try:
data[symbol] = get_historical_data(symbol, start=start, end=end, output_format='json')
except:
pass
for k, v in data.items():
for date, values in v[k].items():
profit = 0
pull = cfg['positions'].get(k, None)
if pull:
shares = pull['shares']
basis = pull['basis']
dayvalue = shares * values['close']
costvalue = shares * basis
profit = dayvalue - costvalue
print(f'Date={date} Sym={k} Profit={profit}')
if str(date) in mytable:
mytable[str(date)] += profit
else:
mytable[str(date)] = profit
for k, v in mytable.items():
row = [k, v]
with open('backfill.csv', 'a', newline='') as csvFile:
writer = csv.writer(csvFile)
writer.writerow(row)
csvFile.close()
#fill_historicalvalues(symbols, months=1)