-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
91 lines (69 loc) · 3.45 KB
/
data.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
91
import yfinance as yf
import pandas as pd
import webbrowser
# Open config file and read each parameter into a list
config_file = open("config.txt", "r")
config = config_file.readlines()
# Get ticker from user, put it to uppercase, defaults to config[0]
ticker = input("Ticker: ").strip().upper() or config[0].strip().upper()
stock = yf.Ticker(ticker)
# Get income statement, balance sheet, and cashflow
income = stock.income_stmt
balance = stock.balance_sheet
cashflow = stock.cashflow
# Get history metadata for exchange name
meta = stock.history_metadata
# Get revenue growth estimates from analysis
growth = stock.revenue_estimate
# Get path from config[1], make file called data.xlsx
path_in = config[1].strip()
path = path_in + "data.xlsx"
# Ask if user wants to open websites automatically, or default to config[2]
open_browser = input("Open websites? Y or N: ").strip().upper() or config[2].strip().upper()
# Make ExcelWriter
writer = pd.ExcelWriter(path)
# Put the three tables together, transpose, drop empty years
result = pd.concat([income, balance, cashflow])
resultT = result.transpose()
resultT = resultT[resultT["Total Revenue"].notna()]
# Drop empty growth estimates, drop unneeded revenue stats, and transpose
growth = growth[growth["growth"].notna()]
growth = growth["growth"]
growthT = growth.transpose()
# List of stats of interest
stat_list = ["Total Revenue", "Cost Of Revenue", "Reconciled Depreciation", "Capital Expenditure",
"Working Capital", "Cash Cash Equivalents And Short Term Investments", "Cash Cash Equivalents And Federal Funds Sold",
"Research And Development", "Selling General And Administration", "Other Operating Expenses",
"Loss Adjustment Expense", "Occupancy And Equipment", "Other Income Expense", "Other Non Interest Expense",
"Professional Expense And Contract Services Expense", "Other Taxes", "Current Debt And Capital Lease Obligation",
"Long Term Debt And Capital Lease Obligation", "Special Income Charges", "Other Special Charges"]
# Clean up results with stat_list, transpose again (while avoiding an unhelpful pandas FutureWarning), and sort by year
with pd.option_context("future.no_silent_downcasting", True):
clean = resultT.reindex(columns=stat_list).fillna(0).infer_objects(copy=False)
cleanT = clean.transpose()
cleanT = cleanT.sort_index(axis=1, ascending=True)
# Write the main dataset to a sheet in data.xlsx
cleanT.to_excel(writer, sheet_name = "data")
main_sheet = writer.sheets["data"]
# Write the growth estimate data to a separate sheet in data.xlsx
growthT.to_excel(writer, sheet_name = "growth")
growth_sheet = writer.sheets["growth"]
# Put main data into a currency format in case user wants to read data.xlsx directly
fmt_currency = writer.book.add_format({"num_format" : "$#,##0" ,"bold" :False})
main_sheet.set_column("A:A", 30)
main_sheet.set_column("B:E", 20, fmt_currency)
# Put growth estimate data into a percentage format
fmt_percent = writer.book.add_format({"num_format": "0.0%"})
growth_sheet.set_column("B:B", 10, fmt_percent)
# Close ExcelWriter
writer.close()
# Get exchange name from history metadata
exchange = meta["fullExchangeName"].strip().upper()
# Concatenate URLs for websites to be opened
WACC_link = "https://finbox.com/" + exchange + ":" + ticker + "/models/wacc/"
# If user wants to open websites, open them
if open_browser == "Y":
webbrowser.open(WACC_link)
# config[3] allows for errors to be read after run complete
if config[3].strip().upper() == "Y":
input("...")