-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymbols.py
37 lines (26 loc) · 949 Bytes
/
symbols.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
from typing import List
import pandas as pd
def save_as_txt(symbols: List[str], filename: str) -> None:
with open(filename, "w") as f:
for symbol in symbols:
f.write(symbol + "\n")
def load_symbols_from_txt(filename: str) -> List[str]:
with open(filename, "r") as f:
symbols = f.read().splitlines()
return symbols
def get_sp500_stocks() -> List[str]:
try:
symbols = load_symbols_from_txt("symbols.txt")
except FileNotFoundError:
url = "https://en.wikipedia.org/wiki/List_of_S%26P_500_companies"
table = pd.read_html(url, header=0)[0]
symbols = table["Symbol"].tolist()
save_as_txt(symbols, "symbols.txt")
return symbols
def get_invalid_symbols() -> List[str]:
try:
return load_symbols_from_txt("invalid_symbols.txt")
except FileNotFoundError:
return []
SYMBOLS = get_sp500_stocks()
INVALID_SYMBOLS = get_invalid_symbols()