Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -43,15 +43,23 @@ def fetch_stock_data(ticker_symbol):
|
|
43 |
def compare_to_index(stock_ratios, index_averages):
|
44 |
comparison = {}
|
45 |
for ratio, value in stock_ratios.items():
|
46 |
-
#
|
47 |
-
if ratio in index_averages.index:
|
48 |
average = index_averages.loc[ratio]['Average']
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
else:
|
51 |
-
comparison[ratio] = 'N/A'
|
52 |
return comparison
|
53 |
|
54 |
|
|
|
55 |
# Load S&P 500 averages
|
56 |
sp500_averages = load_sp500_averages(sp500_averages_path)
|
57 |
|
@@ -66,10 +74,10 @@ if ticker_symbol:
|
|
66 |
stock_data = fetch_stock_data(ticker_symbol)
|
67 |
comparison = compare_to_index(stock_data, sp500_averages)
|
68 |
|
69 |
-
st.write(f"
|
70 |
-
for ratio,
|
71 |
-
|
72 |
-
|
73 |
|
74 |
|
75 |
|
|
|
43 |
def compare_to_index(stock_ratios, index_averages):
|
44 |
comparison = {}
|
45 |
for ratio, value in stock_ratios.items():
|
46 |
+
# Ensure the ratio exists in the DataFrame
|
47 |
+
if ratio in index_averages.index and value is not None:
|
48 |
average = index_averages.loc[ratio]['Average']
|
49 |
+
# Default interpretation for most ratios (higher = overvalued)
|
50 |
+
interpretation = 'Overvalued' if value > average else 'Undervalued'
|
51 |
+
|
52 |
+
# Invert interpretation for specific ratios like P/B Ratio, Book-to-Market Ratio
|
53 |
+
if ratio in ['Book-to-Market Ratio']: # Add any other ratios where higher means undervalued
|
54 |
+
interpretation = 'Undervalued' if value > average else 'Overvalued'
|
55 |
+
|
56 |
+
comparison[ratio] = f"{interpretation} (Your Ratio: {value}, S&P 500 Avg: {average})"
|
57 |
else:
|
58 |
+
comparison[ratio] = 'N/A'
|
59 |
return comparison
|
60 |
|
61 |
|
62 |
+
|
63 |
# Load S&P 500 averages
|
64 |
sp500_averages = load_sp500_averages(sp500_averages_path)
|
65 |
|
|
|
74 |
stock_data = fetch_stock_data(ticker_symbol)
|
75 |
comparison = compare_to_index(stock_data, sp500_averages)
|
76 |
|
77 |
+
st.write(f"Valuation Comparison for {ticker_symbol}:")
|
78 |
+
for ratio, result in comparison.items():
|
79 |
+
st.write(f"{ratio}: {result}")
|
80 |
+
|
81 |
|
82 |
|
83 |
|