-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswtsx_market_cap.py
executable file
·63 lines (49 loc) · 1.67 KB
/
swtsx_market_cap.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
#!/usr/bin/env python3
"""Get market caps of SWTSX."""
import math
import pandas as pd
import common
CAP_MAP = {
"US_LARGE_CAP": ["> $70,000 M", "$15,000-$70,000 M"],
"US_SMALL_CAP": ["<$1,000 M", "$1,000-$3,000 M", "$3,000-$15,000 M"],
}
class ToleranceError(Exception):
"""Percents do not add up close to 100."""
def get_market_cap(page):
"""Get market cap data from Schwab."""
table_dict = {}
common.schwab_browser_page(page)
page.get_by_role("heading", name="Portfolio", exact=True).click()
for _, keys in CAP_MAP.items():
for cap in keys:
table_dict[cap] = float(
page.locator(f'tr:has-text("{cap}")')
.inner_text()
.split("\t\n")[1]
.strip("%")
)
return table_dict
def save_market_cap():
"""Writes SWTSX market cap weightings to swtsx_market_cap DB table."""
with common.run_with_browser_page(
"https://www.schwabassetmanagement.com/products/swtsx"
) as page:
table_dict = get_market_cap(page)
if not math.isclose(sum(table_dict.values()), 100, rel_tol=0.01):
raise ToleranceError()
market_cap_dict = {}
# For determining large vs small cap, compare:
# https://www.schwabassetmanagement.com/products/scha
# https://www.schwabassetmanagement.com/products/swtsx
for cap, keys in CAP_MAP.items():
market_cap_dict[cap] = sum(table_dict[x] for x in keys)
market_cap_df = pd.DataFrame(
market_cap_dict,
index=[pd.Timestamp.now()],
)
common.to_sql(market_cap_df, "swtsx_market_cap")
def main():
"""Main."""
save_market_cap()
if __name__ == "__main__":
main()