-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
79 lines (59 loc) · 1.96 KB
/
main.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
79
from ast import arg
import json
import getPrices
import datetime
import locale
from mail import sendEmail
from jinja2 import Environment, FileSystemLoader
import argparse
locale.setlocale(locale.LC_ALL, '' )# Set currency location
def updateHoldings(holdings):
for holding in holdings:
print("getting price for stock")
latestPrice = getPrices.getPrice(f"{holding['ticker']}.AX")
holding['recent-price'] = latestPrice
holding['history'].append({
"time":datetime.datetime.now().isoformat(),
"price":latestPrice
})
return holdings
def generateHTML(person):
print('Doing calculations for email...')
date = datetime.datetime.now()
date = date.strftime("%-d %B %Y")
print("Generating email...")
file_loader = FileSystemLoader("templates")
env = Environment(loader=file_loader)
template = env.get_template('email.html')
return template.render(
date=date,
netPurchase=round(sum(i['purchase-price'] for i in person['holdings']),2),
netRecent=round(sum(i['recent-price'] for i in person['holdings']),2),
netLastWeek=round(sum(i['history'][-2]['price'] for i in person['holdings']),2),
person=person,
calcPercent=calcPercent
)
def calcPercent(x, y):
try:
return round(((x/y) - 1)*100,2)
except ZeroDivisionError:
return 0
def main():
parser = argparse.ArgumentParser(description='Manage portfolio tracking')
parser.add_argument('-a', help="Add new holding", action="store_true")
args = vars(parser.parse_args())
data = open('data.json').read()
persons = json.loads(data)
if args['a'] == False:
updatedPersons = []
for person in persons:
print("Getting info for", person['name'])
#Update stock prices
person['holdings'] = updateHoldings(person['holdings'])
content = generateHTML(person)
sendEmail(subject="Weekly Portfolio Update",recipient=person['email'], content=content)
updatedPersons.append(person)
with open('data.json','w') as f:
json.dump(updatedPersons,fp=f, indent=2)
if __name__ == "__main__":
main()