Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,7 @@ import streamlit as st
|
|
| 2 |
import yfinance as yf
|
| 3 |
import pandas as pd
|
| 4 |
|
| 5 |
-
@st.
|
| 6 |
def get_sp500_list():
|
| 7 |
table = pd.read_html('https://en.wikipedia.org/wiki/List_of_S%26P_500_companies')
|
| 8 |
return table[0]['Symbol'].tolist()
|
|
@@ -14,13 +14,15 @@ def fetch_stock_data(ticker_symbol):
|
|
| 14 |
ticker = yf.Ticker(ticker_symbol)
|
| 15 |
info = ticker.info
|
| 16 |
financials = {
|
| 17 |
-
'P/E Ratio': info.get('forwardPE'
|
| 18 |
-
'P/B Ratio': info.get('priceToBook'
|
| 19 |
-
'P/S Ratio': info.get('priceToSalesTrailing12Months'
|
| 20 |
-
'Debt to Equity Ratio': info.get('debtToEquity'
|
| 21 |
-
'Return on Equity': info.get('returnOnEquity'
|
| 22 |
-
'Book-to-Market Ratio': 1 / info.get('priceToBook'
|
| 23 |
}
|
|
|
|
|
|
|
| 24 |
return financials, info
|
| 25 |
|
| 26 |
def compare_to_index(stock_ratios, index_averages):
|
|
@@ -63,34 +65,47 @@ sp500_list = get_sp500_list()
|
|
| 63 |
sp500_averages = load_sp500_averages('sp500_averages.csv')
|
| 64 |
|
| 65 |
scores_df = calculate_combined_scores_for_stocks(sp500_list, sp500_averages)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
required_columns = ['P/E Ratio', 'P/B Ratio', 'P/S Ratio', 'Debt to Equity Ratio', 'Return on Equity', 'Book-to-Market Ratio']
|
| 67 |
|
| 68 |
-
|
|
|
|
|
|
|
| 69 |
scores_df_sorted = scores_df_filtered.sort_values(by='Combined Score', ascending=False)
|
| 70 |
|
|
|
|
|
|
|
|
|
|
| 71 |
col1, col2 = st.columns([3, 5])
|
| 72 |
|
| 73 |
with col1:
|
| 74 |
st.subheader("Stock Overview")
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
with col2:
|
| 79 |
st.subheader("Stock Details")
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
| 2 |
import yfinance as yf
|
| 3 |
import pandas as pd
|
| 4 |
|
| 5 |
+
@st.experimental_singleton
|
| 6 |
def get_sp500_list():
|
| 7 |
table = pd.read_html('https://en.wikipedia.org/wiki/List_of_S%26P_500_companies')
|
| 8 |
return table[0]['Symbol'].tolist()
|
|
|
|
| 14 |
ticker = yf.Ticker(ticker_symbol)
|
| 15 |
info = ticker.info
|
| 16 |
financials = {
|
| 17 |
+
'P/E Ratio': info.get('forwardPE'),
|
| 18 |
+
'P/B Ratio': info.get('priceToBook'),
|
| 19 |
+
'P/S Ratio': info.get('priceToSalesTrailing12Months'),
|
| 20 |
+
'Debt to Equity Ratio': info.get('debtToEquity'),
|
| 21 |
+
'Return on Equity': info.get('returnOnEquity'),
|
| 22 |
+
'Book-to-Market Ratio': 1 / info.get('priceToBook') if info.get('priceToBook') else None
|
| 23 |
}
|
| 24 |
+
# Debug: Print to see if financials are fetched correctly
|
| 25 |
+
print(f"Financials for {ticker_symbol}: {financials}")
|
| 26 |
return financials, info
|
| 27 |
|
| 28 |
def compare_to_index(stock_ratios, index_averages):
|
|
|
|
| 65 |
sp500_averages = load_sp500_averages('sp500_averages.csv')
|
| 66 |
|
| 67 |
scores_df = calculate_combined_scores_for_stocks(sp500_list, sp500_averages)
|
| 68 |
+
|
| 69 |
+
# Debug: Print the DataFrame before filtering to see its content
|
| 70 |
+
print("Scores DataFrame before filtering:", scores_df.head())
|
| 71 |
+
|
| 72 |
required_columns = ['P/E Ratio', 'P/B Ratio', 'P/S Ratio', 'Debt to Equity Ratio', 'Return on Equity', 'Book-to-Market Ratio']
|
| 73 |
|
| 74 |
+
# Attempt a lenient filtering approach or skip filtering to debug
|
| 75 |
+
# scores_df_filtered = filter_incomplete_stocks(scores_df, required_columns)
|
| 76 |
+
scores_df_filtered = scores_df # Temporarily bypass filtering to debug
|
| 77 |
scores_df_sorted = scores_df_filtered.sort_values(by='Combined Score', ascending=False)
|
| 78 |
|
| 79 |
+
# Debug: Print the DataFrame after sorting to see if it's empty
|
| 80 |
+
print("Scores DataFrame after sorting:", scores_df_sorted.head())
|
| 81 |
+
|
| 82 |
col1, col2 = st.columns([3, 5])
|
| 83 |
|
| 84 |
with col1:
|
| 85 |
st.subheader("Stock Overview")
|
| 86 |
+
if not scores_df_sorted.empty:
|
| 87 |
+
styled_scores_df = scores_df_sorted.style.applymap(color_combined_score, subset=['Combined Score'])
|
| 88 |
+
st.dataframe(styled_scores_df)
|
| 89 |
+
else:
|
| 90 |
+
st.write("No data available after filtering.")
|
| 91 |
|
| 92 |
with col2:
|
| 93 |
st.subheader("Stock Details")
|
| 94 |
+
if not scores_df_sorted.empty:
|
| 95 |
+
sorted_tickers = scores_df_sorted['Stock'].tolist()
|
| 96 |
+
ticker_symbol = st.selectbox('Select a stock for details', options=sorted_tickers)
|
| 97 |
+
if ticker_symbol:
|
| 98 |
+
with st.spinner(f'Fetching data for {ticker_symbol}...'):
|
| 99 |
+
stock_data, info = fetch_stock_data(ticker_symbol)
|
| 100 |
+
comparison, _ = compare_to_index(stock_data, sp500_averages)
|
| 101 |
+
|
| 102 |
+
st.write(f"**{info.get('longName', 'N/A')}** ({ticker_symbol})")
|
| 103 |
+
st.write(info.get('longBusinessSummary', 'N/A'))
|
| 104 |
+
|
| 105 |
+
for ratio in required_columns:
|
| 106 |
+
value = stock_data.get(ratio, 'N/A')
|
| 107 |
+
average = sp500_averages.loc[ratio, 'Average'] if ratio in sp500_averages.index else 'N/A'
|
| 108 |
+
status = comparison.get(ratio, 'N/A')
|
| 109 |
+
st.write(f"{ratio}: {value} (Your Ratio) | {average} (S&P 500 Avg) - {status}")
|
| 110 |
+
else:
|
| 111 |
+
st.write("No stocks to display.")
|