Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
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 |
|