Spaces:
Sleeping
Sleeping
Robert Castagna commited on
Commit ·
8aec326
1
Parent(s): 5bbccf3
added new metrics to Value strat
Browse files- pages/1_Fundamentals.py +23 -30
pages/1_Fundamentals.py
CHANGED
|
@@ -7,6 +7,7 @@ import streamlit as st
|
|
| 7 |
import yfinance as yf
|
| 8 |
from datetime import timedelta
|
| 9 |
from dotenv import load_dotenv
|
|
|
|
| 10 |
|
| 11 |
load_dotenv()
|
| 12 |
|
|
@@ -73,17 +74,19 @@ def get_equity_gains(ticker, period):
|
|
| 73 |
sp_hist = sp.history(period=f'{period}d')[['Close','Dividends']]
|
| 74 |
sp500 = sp_hist.reset_index()
|
| 75 |
|
| 76 |
-
todays_close = sp500.
|
| 77 |
-
|
|
|
|
|
|
|
| 78 |
|
| 79 |
val = {}
|
| 80 |
|
| 81 |
-
if
|
| 82 |
-
delta = todays_close
|
| 83 |
-
val[f'5Y_change'] = (delta /
|
| 84 |
else:
|
| 85 |
-
delta = todays_close
|
| 86 |
-
val[f'5Y_change'] = (delta /
|
| 87 |
|
| 88 |
|
| 89 |
# add in dividend
|
|
@@ -131,53 +134,43 @@ with st.form(key="selecting columns"):
|
|
| 131 |
hash_map = {}
|
| 132 |
|
| 133 |
for ticker in symbols:
|
| 134 |
-
st.spinner(text=f"Loading {ticker} Data...")
|
| 135 |
# make all the API calls and capture return json
|
| 136 |
basic_info = get_industry(ticker)
|
| 137 |
metric_data, annual_series_data, quarterly_series_data = get_company_metrics(ticker)
|
| 138 |
-
|
| 139 |
-
|
| 140 |
# reformat all JSON returns to be flattened dictionaries
|
| 141 |
roe_dict = {'roe': annual_series_data['roe'][0]['v'] if 'roe' in annual_series_data else 0}
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
# merge all dictionary keys per ticker
|
| 149 |
combined_info = basic_info.copy() # Make a copy of the basic info
|
| 150 |
-
combined_info = combined_info | metric_data | roe_dict |
|
| 151 |
|
| 152 |
hash_map[ticker] = combined_info
|
| 153 |
|
| 154 |
-
# beta calculations
|
| 155 |
-
df_b = calc_beta(ticker)
|
| 156 |
-
beta_dfs.append(df_b)
|
| 157 |
-
|
| 158 |
# equity gains
|
| 159 |
_, div, close_price = get_equity_gains(ticker=ticker, period=1810)
|
| 160 |
gains_data[ticker] = [div, close_price]
|
| 161 |
|
| 162 |
|
| 163 |
# Now, create a DataFrame from the hash_map
|
| 164 |
-
df_1 = pd.DataFrame.from_dict(hash_map, orient='index')[['finnhubIndustry','
|
| 165 |
-
# Create beta df
|
| 166 |
-
beta_df = pd.concat(beta_dfs)
|
| 167 |
df_2 = pd.DataFrame.from_dict(gains_data, orient='index', columns=['Recent Dividend','Price'])
|
| 168 |
|
| 169 |
-
|
| 170 |
-
df_final = df_apis.join(df_2)
|
| 171 |
|
| 172 |
# calculate additional columns
|
| 173 |
df_final['5Y_SP500_growth'], _, _ = get_equity_gains(ticker= '^GSPC', period=1810)
|
| 174 |
df_final['90_day_tbill'] = 4.06
|
| 175 |
-
df_final['P/E Ratio'] = df_final['Price'] / df_final['eps']
|
| 176 |
df_final['dividendGrowthRate5Y'] = df_final['dividendGrowthRate5Y']/100
|
| 177 |
-
df_final['CAPM'] = df_final['90_day_tbill']/100 + df_final['
|
| 178 |
df_final['DDM'] = (df_final['Recent Dividend'] * (1+df_final['dividendGrowthRate5Y'])) / (df_final['CAPM'] - df_final['dividendGrowthRate5Y'])
|
| 179 |
-
df_final = df_final[['finnhubIndustry','Price','eps','roe','
|
| 180 |
-
df_final.rename({'finnhubIndustry':'Industry', 'eps':'EPS', 'roe':'ROE'}, inplace=True, axis=1)
|
| 181 |
st.write(df_final)
|
| 182 |
|
| 183 |
if submit_button and symbols and strategy_selection == 'Growth':
|
|
|
|
| 7 |
import yfinance as yf
|
| 8 |
from datetime import timedelta
|
| 9 |
from dotenv import load_dotenv
|
| 10 |
+
from pandas import to_datetime
|
| 11 |
|
| 12 |
load_dotenv()
|
| 13 |
|
|
|
|
| 74 |
sp_hist = sp.history(period=f'{period}d')[['Close','Dividends']]
|
| 75 |
sp500 = sp_hist.reset_index()
|
| 76 |
|
| 77 |
+
todays_close = sp500.iloc[-1]
|
| 78 |
+
todays_close_date = todays_close.Date.tz_localize(None)
|
| 79 |
+
sp500.set_index('Date', inplace=True)
|
| 80 |
+
sp500.index = sp500.index.tz_localize(None)
|
| 81 |
|
| 82 |
val = {}
|
| 83 |
|
| 84 |
+
if todays_close_date - timedelta(days=period) in sp500.index:
|
| 85 |
+
delta = todays_close.Close - sp500.loc[todays_close_date - timedelta(days=period)]['Close']
|
| 86 |
+
val[f'5Y_change'] = (delta / sp500.loc[todays_close_date - timedelta(days=period)]['Close'])
|
| 87 |
else:
|
| 88 |
+
delta = todays_close.Close - sp500.loc[todays_close_date - timedelta(days=period-4)]['Close']
|
| 89 |
+
val[f'5Y_change'] = (delta / sp500.loc[todays_close_date - timedelta(days=period-4)]['Close'])
|
| 90 |
|
| 91 |
|
| 92 |
# add in dividend
|
|
|
|
| 134 |
hash_map = {}
|
| 135 |
|
| 136 |
for ticker in symbols:
|
|
|
|
| 137 |
# make all the API calls and capture return json
|
| 138 |
basic_info = get_industry(ticker)
|
| 139 |
metric_data, annual_series_data, quarterly_series_data = get_company_metrics(ticker)
|
| 140 |
+
|
|
|
|
| 141 |
# reformat all JSON returns to be flattened dictionaries
|
| 142 |
roe_dict = {'roe': annual_series_data['roe'][0]['v'] if 'roe' in annual_series_data else 0}
|
| 143 |
+
eps_dict = {'eps' :annual_series_data['eps'][0]['v'] if 'eps' in annual_series_data else 0}
|
| 144 |
+
pe_dict = {'pe': annual_series_data['pe'][0]['v'] if 'pe' in annual_series_data else 0}
|
| 145 |
+
ps_dict = {'ps': annual_series_data['ps'][0]['v'] if 0 in annual_series_data['ps'] else 0}
|
| 146 |
+
pb_dict = {'pb': annual_series_data['pb'][0]['v'] if 'pb' in annual_series_data else 0}
|
| 147 |
+
pfcf_dict = {'pfcf': annual_series_data['pfcf'][0]['v'] if 'pfcf' in annual_series_data else 0}
|
| 148 |
+
|
| 149 |
# merge all dictionary keys per ticker
|
| 150 |
combined_info = basic_info.copy() # Make a copy of the basic info
|
| 151 |
+
combined_info = combined_info | metric_data | roe_dict | eps_dict | pe_dict | ps_dict | pb_dict | pfcf_dict
|
| 152 |
|
| 153 |
hash_map[ticker] = combined_info
|
| 154 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
# equity gains
|
| 156 |
_, div, close_price = get_equity_gains(ticker=ticker, period=1810)
|
| 157 |
gains_data[ticker] = [div, close_price]
|
| 158 |
|
| 159 |
|
| 160 |
# Now, create a DataFrame from the hash_map
|
| 161 |
+
df_1 = pd.DataFrame.from_dict(hash_map, orient='index')[['finnhubIndustry','beta','pe','ps','pb','pfcf','eps','roe','roeTTM','dividendGrowthRate5Y','epsGrowth5Y','payoutRatioAnnual','payoutRatioTTM']]
|
|
|
|
|
|
|
| 162 |
df_2 = pd.DataFrame.from_dict(gains_data, orient='index', columns=['Recent Dividend','Price'])
|
| 163 |
|
| 164 |
+
df_final = df_1.join(df_2)
|
|
|
|
| 165 |
|
| 166 |
# calculate additional columns
|
| 167 |
df_final['5Y_SP500_growth'], _, _ = get_equity_gains(ticker= '^GSPC', period=1810)
|
| 168 |
df_final['90_day_tbill'] = 4.06
|
|
|
|
| 169 |
df_final['dividendGrowthRate5Y'] = df_final['dividendGrowthRate5Y']/100
|
| 170 |
+
df_final['CAPM'] = df_final['90_day_tbill']/100 + df_final['beta']*(df_final['5Y_SP500_growth'] - df_final['90_day_tbill']/100)
|
| 171 |
df_final['DDM'] = (df_final['Recent Dividend'] * (1+df_final['dividendGrowthRate5Y'])) / (df_final['CAPM'] - df_final['dividendGrowthRate5Y'])
|
| 172 |
+
df_final = df_final[['finnhubIndustry','Price','eps','roe','roeTTM','pe','ps','pb','pfcf','epsGrowth5Y','payoutRatioAnnual','payoutRatioTTM','beta','Recent Dividend','90_day_tbill','5Y_SP500_growth','dividendGrowthRate5Y','CAPM','DDM']]
|
| 173 |
+
df_final.rename({'finnhubIndustry':'Industry', 'eps':'EPS', 'roe':'ROE','pe':'P/E','ps':'P/S', 'pb':'P/B','pfcf':'P/FCF','beta':'Beta'}, inplace=True, axis=1)
|
| 174 |
st.write(df_final)
|
| 175 |
|
| 176 |
if submit_button and symbols and strategy_selection == 'Growth':
|