forked from CSjiade/Stock_DashBoard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinancial.py
More file actions
185 lines (154 loc) · 5.84 KB
/
Copy pathfinancial.py
File metadata and controls
185 lines (154 loc) · 5.84 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import pandas as pd
import numpy as np
import streamlit as st
from yahoofinancials import YahooFinancials
def financeStatement_setup(stock_ticker):
yhfin = YahooFinancials(stock_ticker)
annual_fin = yhfin.get_financial_stmts('annual', 'income')
date = []
revenue = []
cogs = []
gross_profit = []
gross_profit_margin = []
ebit = []
ebit_margin = []
int_exp = []
income = []
income_margin = []
int_coverage = []
million = 1000000
for x in annual_fin['incomeStatementHistory'][stock_ticker]:
data = list(x.items())
date.append(data[0][0])
rev_data = data[0][1]['totalRevenue']
if rev_data is None or rev_data ==0:
revenue.append(np.nan)
else:
revenue.append(rev_data/million)
cogs_data = data[0][1]['costOfRevenue']
if cogs_data is None or cogs_data ==0:
cogs.append(np.nan)
else:
cogs.append(cogs_data/million)
gross_profit_data = data[0][1]['grossProfit']
if gross_profit_data is None or gross_profit_data ==0:
gross_profit.append(np.nan)
gross_profit_margin.append(np.nan)
else:
gross_profit.append(gross_profit_data/million)
gp_margin_data = 100*data[0][1]['grossProfit']/data[0][1]['totalRevenue']
if gp_margin_data < 0:
gp_margin_data = np.nan
gross_profit_margin.append(gp_margin_data)
ebit_data = data[0][1]['ebit']
if ebit_data is None or ebit_data ==0:
ebit.append(np.nan)
ebit_margin.append(np.nan)
else:
ebit.append(ebit_data/million)
ebit_margin_data = 100*data[0][1]['ebit']/data[0][1]['totalRevenue']
if ebit_margin_data < 0:
ebit_margin_data = np.nan
ebit_margin.append(ebit_margin_data)
int_exp_data = data[0][1]['interestExpense']
if int_exp_data is None or int_exp_data==0:
int_exp_data = np.nan
int_exp.append(int_exp_data)
int_coverage.append(int_exp_data)
else:
int_exp.append(int_exp_data/million)
if ebit_data < 0:
int_coverage.append(np.nan)
else:
int_coverage.append(data[0][1]['ebit']/int_exp_data)
income_data = data[0][1]['netIncome']
if income_data is None or income_data==0:
income.append(np.nan)
income_margin.append(np.nan)
else:
income.append(income_data/million)
income_margin_data = 100*data[0][1]['netIncome']/data[0][1]['totalRevenue']
if income_margin_data < 0:
income_margin_data = np.nan
income_margin.append(income_margin_data)
annual_cf = yhfin.get_financial_stmts('annual', 'cash')
da = []
capex = []
divd = []
CFO = []
CFF = []
CFI = []
net_debt = []
stock_issuance = []
for x in annual_cf['cashflowStatementHistory'][stock_ticker]:
data = list(x.items())
try:
da.append(data[0][1]['depreciation']/million)
except KeyError:
da.append(np.nan)
try:
capex.append(data[0][1]['capitalExpenditures']/million)
except KeyError:
capex.append(np.nan)
try:
divd.append(data[0][1]['dividendsPaid']/million)
except KeyError:
divd.append(np.nan)
try:
CFO.append(data[0][1]['totalCashFromOperatingActivities']/million)
except KeyError:
CFO.append(np.nan)
try:
CFF.append(data[0][1]['totalCashFromFinancingActivities']/million)
except KeyError:
CFF.append(np.nan)
try:
CFI.append(data[0][1]['totalCashflowsFromInvestingActivities']/million)
except KeyError:
CFI.append(np.nan)
try:
stock_issuance.append(data[0][1]['issuanceOfStock']/million +
data[0][1]['repurchaseOfStock']/million)
except KeyError:
stock_issuance.append(np.nan)
try:
net_debt.append(data[0][1]['netBorrowings']/million)
except KeyError:
net_debt.append(np.nan)
ebitda = np.add(ebit,da)
ebitda_filtered = []
for ebitda_value in ebitda:
if ebitda_value < 0 or ebitda_value == np.nan:
ebitda_filtered.append(np.nan)
else:
ebitda_filtered.append(ebitda_value)
ebitda_margin = map(lambda x : x*100, np.divide(ebitda_filtered,revenue))
FCF = np.add(CFO,capex)
FCFE = np.add(FCF,net_debt)
df_cash = {'CF Operations': CFO,'CF Finance': CFF,
'CF Investment': CFI,'CAPEX': capex,
'Free Cash Flow': FCF,
'Dividend': divd,
'Stock Issue': stock_issuance,
'Free Cash Flow to Equity': FCFE
}
index = [date]
df_income = {'Revenue': revenue,'COGS': np.multiply(cogs,-1), 'Gross Profit': gross_profit,'EBITDA': ebitda,
'D&A': np.multiply(da,-1),'EBIT': ebit, 'Interest Exp': int_exp,
'Profit': income
}
df_income = pd.DataFrame(data=df_income,index=index)
df_ratios = { 'GP Margin (%)': gross_profit_margin,
'EBITDA Margin (%)': ebitda_margin,
'EBIT Margin (%)': ebit_margin,
'Int Coverage (x)': np.multiply(int_coverage,-1),
'Profit Margin (%)': income_margin
}
df_ratios = pd.DataFrame(data=df_ratios,index=index)
df_cash = pd.DataFrame(data=df_cash,index=index)
st.subheader("Income Data in Millions")
st.table(df_income.transpose().style.format("{:.2f}"))
st.subheader("Key Ratios")
st.table(df_ratios.transpose().style.format("{:.2f}"))
st.subheader("Cash Flow Data in Millions")
st.table(df_cash.transpose().style.format("{:.2f}"))