-
Notifications
You must be signed in to change notification settings - Fork 0
/
stock_sim.py
90 lines (72 loc) · 2.63 KB
/
stock_sim.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
80
81
82
83
84
85
86
87
88
89
90
import requests
from bs4 import BeautifulSoup
import json
from sys import exit
# read stock data from json file
STOCK_FILE_PATH = 'stock.json'
with open(STOCK_FILE_PATH, 'r') as f:
stock_data_raw = f.read()
stock_data = json.loads(stock_data_raw)
total_percentage = sum([stock['percentage'] for stock in stock_data['stocks']])
if total_percentage > 100.0:
print("Sum of portfolio percentages over 100%. Exiting...")
exit(0)
elif total_percentage < 100.0:
print(f"Sum of portfolio percentages is {total_percentage}%. You didn't allocate all the money in your budget.")
def get_stock_gains(**kwargs):
URL = f"""https://www.buyupside.com/alphavantagelive/dollarcostavecomputeav.php?symbol=\
{kwargs['stock_symbol']}\
&amountinitial=\
{kwargs['initial_amount']}\
&amount=\
{kwargs['monthly_amount']}\
&start_month=\
{kwargs['start_month']}\
&start_year=\
{kwargs['start_year']}\
&end_month=\
{kwargs['end_month']}\
&end_year=\
{kwargs['end_year']}\
&submit=Calculate+Returns"""
r = requests.get(URL)
soup = BeautifulSoup(r.content, 'html5lib')
# find table rows
main = soup.find('div', attrs = {'class':'main'})
rows = main.findAll('tr')
# retrieve values from table
invested_raw = rows[6].findAll('td')[1].text
total_raw = rows[9].findAll('td')[1].text
# clean values from redundant chars
invested = float(invested_raw.replace('$', '').replace(',', ''))
total = float(total_raw.replace('$', '').replace(',', ''))
gains = total - invested
return {
'stock_name': kwargs['stock_name'],
'stock_symbol': kwargs['stock_symbol'],
'invested': invested,
'total': total,
'gains': gains,
}
stocks = []
for stock in stock_data['stocks']:
stock_gains = get_stock_gains(
stock_name=stock['name'],
stock_symbol=stock['symbol'],
initial_amount=stock_data.get('initial_amount', 0) * stock['percentage'] / 100.0,
monthly_amount=stock_data['monthly_amount'] * stock['percentage'] / 100.0,
start_month=stock_data['start_month'],
start_year=stock_data['start_year'],
end_month=stock_data['end_month'],
end_year=stock_data['end_year'],
)
stocks.append(stock_gains)
print(stock_gains)
total = sum([stock['total'] for stock in stocks])
invested = sum([stock['invested'] for stock in stocks])
gains = sum([stock['gains'] for stock in stocks])
gains_after_tax = gains * (100.0 - stock_data['tax_percentage']) / 100.0
print(f'Total portfolio worth:{total:.2f}')
print(f'Total invested:{invested:.2f}')
print(f'Total gains:{gains:.2f}')
print(f'Total gains(after tax, if sold):{gains_after_tax:.2f}')