os1187 commited on
Commit
9c66447
1 Parent(s): a80c9bd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -8
app.py CHANGED
@@ -42,21 +42,40 @@ def fetch_stock_data(ticker_symbol):
42
 
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
 
@@ -72,11 +91,17 @@ ticker_symbol = st.selectbox('Select a stock', options=stocks)
72
  if ticker_symbol:
73
  with st.spinner(f'Fetching data for {ticker_symbol}...'):
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
 
 
42
 
43
  def compare_to_index(stock_ratios, index_averages):
44
  comparison = {}
45
+ undervalued_count = 0
46
+ overvalued_count = 0
47
+
48
  for ratio, value in stock_ratios.items():
 
49
  if ratio in index_averages.index and value is not None:
50
  average = index_averages.loc[ratio]['Average']
51
+ # Interpretation for most ratios (higher = overvalued)
52
+ if value > average:
53
+ interpretation = 'Overvalued'
54
+ overvalued_count += 1
55
+ else:
56
+ interpretation = 'Undervalued'
57
+ undervalued_count += 1
58
 
59
+ # Adjust interpretation for specific ratios
60
+ if ratio in ['Book-to-Market Ratio']: # Example: higher means undervalued
61
  interpretation = 'Undervalued' if value > average else 'Overvalued'
62
+ if interpretation == 'Undervalued':
63
+ undervalued_count += 1 # Correct previous count if needed
64
+ overvalued_count -= 1
65
+ else:
66
+ undervalued_count -= 1
67
+ overvalued_count += 1
68
 
69
  comparison[ratio] = f"{interpretation} (Your Ratio: {value}, S&P 500 Avg: {average})"
70
  else:
71
  comparison[ratio] = 'N/A'
72
+
73
+ # Calculate combined score
74
+ combined_score = undervalued_count - overvalued_count
75
+ comparison['Combined Score'] = combined_score
76
+
77
+ return comparison, combined_score
78
+
79
 
80
 
81
 
 
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
 
107