os1187 commited on
Commit
4e9c027
·
verified ·
1 Parent(s): 9c66447

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -13
app.py CHANGED
@@ -77,6 +77,19 @@ def compare_to_index(stock_ratios, index_averages):
77
  return comparison, combined_score
78
 
79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
 
81
 
82
  # Load S&P 500 averages
@@ -88,19 +101,19 @@ st.title('S&P 500 Stock Comparison Tool')
88
  # Selection of stock from predefined list
89
  ticker_symbol = st.selectbox('Select a stock', options=stocks)
90
 
91
- if ticker_symbol:
92
- with st.spinner(f'Fetching data for {ticker_symbol}...'):
93
- stock_data = fetch_stock_data(ticker_symbol)
94
- comparison, combined_score = compare_to_index(stock_data, sp500_averages)
95
-
96
- st.write(f"Valuation Comparison for {ticker_symbol}:")
97
- for ratio, result in comparison.items():
98
- if ratio != 'Combined Score': # Avoid repeating the combined score in the loop
99
- st.write(f"{ratio}: {result}")
100
-
101
- # Display the combined score with interpretation
102
- score_interpretation = "Undervalued" if combined_score > 0 else "Overvalued" if combined_score < 0 else "Neutral"
103
- st.metric(label="Combined Valuation Score", value=f"{combined_score} ({score_interpretation})")
104
 
105
 
106
 
 
77
  return comparison, combined_score
78
 
79
 
80
+ def calculate_combined_scores_for_stocks(stocks, index_averages):
81
+ scores = []
82
+
83
+ for ticker_symbol in stocks:
84
+ try:
85
+ stock_data = fetch_stock_data(ticker_symbol)
86
+ _, combined_score = compare_to_index(stock_data, index_averages)
87
+ scores.append([ticker_symbol, combined_score])
88
+ except Exception as e:
89
+ print(f"Error fetching data for {ticker_symbol}: {e}")
90
+
91
+ scores_df = pd.DataFrame(scores, columns=['Stock', 'Combined Score'])
92
+ return scores_df
93
 
94
 
95
  # Load S&P 500 averages
 
101
  # Selection of stock from predefined list
102
  ticker_symbol = st.selectbox('Select a stock', options=stocks)
103
 
104
+ # Assuming 'stocks' is your list of stock ticker symbols
105
+ if st.button('Calculate Combined Scores'):
106
+ scores_df = calculate_combined_scores_for_stocks(stocks, sp500_averages)
107
+
108
+ # Sort by 'Combined Score' to prioritize viewing
109
+ scores_df.sort_values(by='Combined Score', ascending=False, inplace=True)
110
+
111
+ # Display each stock and its combined score with color coding
112
+ for _, row in scores_df.iterrows():
113
+ color = "green" if row['Combined Score'] > 0 else "red" if row['Combined Score'] < 0 else "black"
114
+ score_msg = f"**{row['Stock']}**: <span style='color: {color};'>{row['Combined Score']}</span>"
115
+ st.markdown(score_msg, unsafe_allow_html=True)
116
+
117
 
118
 
119