os1187 commited on
Commit
602a247
1 Parent(s): 94126ed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -11
app.py CHANGED
@@ -2,7 +2,7 @@ import streamlit as st
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,12 +14,12 @@ 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') if info.get('priceToBook') else None
23
  }
24
  return financials, info
25
 
@@ -51,8 +51,11 @@ def color_combined_score(value):
51
  return f'background-color: {color};'
52
 
53
  def filter_incomplete_stocks(df, required_columns):
54
- df = df.dropna(subset=required_columns)
55
- return df
 
 
 
56
 
57
  st.title('S&P 500 Stock Comparison Tool')
58
 
@@ -62,7 +65,6 @@ sp500_averages = load_sp500_averages('sp500_averages.csv')
62
  scores_df = calculate_combined_scores_for_stocks(sp500_list, sp500_averages)
63
  required_columns = ['P/E Ratio', 'P/B Ratio', 'P/S Ratio', 'Debt to Equity Ratio', 'Return on Equity', 'Book-to-Market Ratio']
64
 
65
- # Ensure filtering is done correctly with complete data
66
  scores_df_filtered = filter_incomplete_stocks(scores_df, required_columns)
67
  scores_df_sorted = scores_df_filtered.sort_values(by='Combined Score', ascending=False)
68
 
@@ -85,10 +87,10 @@ with col2:
85
  st.write(f"**{info.get('longName', 'N/A')}** ({ticker_symbol})")
86
  st.write(info.get('longBusinessSummary', 'N/A'))
87
 
88
- # Iterate over required_columns instead of required_ratios
89
  for ratio in required_columns:
90
  value = stock_data.get(ratio, 'N/A')
91
  average = sp500_averages.loc[ratio, 'Average'] if ratio in sp500_averages.index else 'N/A'
92
  status = comparison.get(ratio, 'N/A')
93
  st.write(f"{ratio}: {value} (Your Ratio) | {average} (S&P 500 Avg) - {status}")
94
 
 
 
2
  import yfinance as yf
3
  import pandas as pd
4
 
5
+ @st.cache_resource
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', None),
18
+ 'P/B Ratio': info.get('priceToBook', None),
19
+ 'P/S Ratio': info.get('priceToSalesTrailing12Months', None),
20
+ 'Debt to Equity Ratio': info.get('debtToEquity', None),
21
+ 'Return on Equity': info.get('returnOnEquity', None),
22
+ 'Book-to-Market Ratio': 1 / info.get('priceToBook', float('nan')) if info.get('priceToBook') else None
23
  }
24
  return financials, info
25
 
 
51
  return f'background-color: {color};'
52
 
53
  def filter_incomplete_stocks(df, required_columns):
54
+ # Ensure all required columns exist in the DataFrame
55
+ for column in required_columns:
56
+ if column not in df.columns:
57
+ df[column] = pd.NA
58
+ return df.dropna(subset=required_columns)
59
 
60
  st.title('S&P 500 Stock Comparison Tool')
61
 
 
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
  scores_df_filtered = filter_incomplete_stocks(scores_df, required_columns)
69
  scores_df_sorted = scores_df_filtered.sort_values(by='Combined Score', ascending=False)
70
 
 
87
  st.write(f"**{info.get('longName', 'N/A')}** ({ticker_symbol})")
88
  st.write(info.get('longBusinessSummary', 'N/A'))
89
 
 
90
  for ratio in required_columns:
91
  value = stock_data.get(ratio, 'N/A')
92
  average = sp500_averages.loc[ratio, 'Average'] if ratio in sp500_averages.index else 'N/A'
93
  status = comparison.get(ratio, 'N/A')
94
  st.write(f"{ratio}: {value} (Your Ratio) | {average} (S&P 500 Avg) - {status}")
95
 
96
+