Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,6 @@ import pandas_datareader.data as web
|
|
| 3 |
import yfinance as yf
|
| 4 |
import datetime
|
| 5 |
import plotly.graph_objs as go
|
| 6 |
-
import pandas as pd
|
| 7 |
|
| 8 |
# Set up the layout
|
| 9 |
st.set_page_config(layout="wide")
|
|
@@ -14,12 +13,11 @@ st.markdown(
|
|
| 14 |
<style>
|
| 15 |
/* Increase the width of the sidebar */
|
| 16 |
.css-1d391kg {
|
| 17 |
-
width: 350px;
|
| 18 |
}
|
| 19 |
-
|
| 20 |
/* Ensure main content is still centered properly */
|
| 21 |
.css-1d391kg ~ .css-1d391kg {
|
| 22 |
-
left: 350px;
|
| 23 |
}
|
| 24 |
</style>
|
| 25 |
""",
|
|
@@ -49,8 +47,8 @@ st.sidebar.header("Select Indicators")
|
|
| 49 |
with st.sidebar.expander("Indicators", expanded=True):
|
| 50 |
indicators = {
|
| 51 |
'Sahm Recession Indicator': 'SAHMREALTIME',
|
| 52 |
-
'U.S. Recession Probabilities': '
|
| 53 |
-
'Yield Spread (10Y - 2Y)': 'Yield_Spread',
|
| 54 |
'Stock Market (S&P 500)': 'SP500',
|
| 55 |
'VIX': 'VIX',
|
| 56 |
'Treasury Rates': ('GS10', 'DGS2', 'DGS1MO', 'TB3MS'),
|
|
@@ -95,9 +93,9 @@ crash_periods = {
|
|
| 95 |
if st.sidebar.button('Run Analysis'):
|
| 96 |
# Initialize combined_data as an empty DataFrame
|
| 97 |
combined_data = pd.DataFrame()
|
| 98 |
-
|
| 99 |
-
# Fetch FRED data with error handling
|
| 100 |
fred_data = {}
|
|
|
|
|
|
|
| 101 |
for key, column in indicators.items():
|
| 102 |
if selected_indicators[key]:
|
| 103 |
if isinstance(column, tuple):
|
|
@@ -108,7 +106,7 @@ if st.sidebar.button('Run Analysis'):
|
|
| 108 |
except Exception as e:
|
| 109 |
st.warning(f"Failed to fetch {col} from FRED: {e}")
|
| 110 |
fred_data[col] = pd.DataFrame()
|
| 111 |
-
|
| 112 |
try:
|
| 113 |
fred_data[column] = web.DataReader(column, 'fred', start_date, end_date)
|
| 114 |
except Exception as e:
|
|
@@ -138,11 +136,6 @@ if st.sidebar.button('Run Analysis'):
|
|
| 138 |
st.warning(f"Failed to fetch VIX data: {e}")
|
| 139 |
fred_data['VIX'] = pd.DataFrame()
|
| 140 |
|
| 141 |
-
# Rename recession probabilities for clarity
|
| 142 |
-
if 'RECPROUSM156N' in fred_data:
|
| 143 |
-
fred_data['RECPROUSM156N'].columns = ['Recession_Probabilities']
|
| 144 |
-
fred_data['Recession_Probabilities'] = fred_data.pop('RECPROUSM156N')
|
| 145 |
-
|
| 146 |
# Combine all FRED and Yahoo Finance data
|
| 147 |
if fred_data:
|
| 148 |
combined_data = pd.concat(fred_data.values(), axis=1, join='outer')
|
|
@@ -190,7 +183,6 @@ if st.sidebar.button('Run Analysis'):
|
|
| 190 |
|
| 191 |
if isinstance(column, tuple):
|
| 192 |
if column == ('INDPRO', 'INDPRO_PCT') and 'INDPRO' in combined_data:
|
| 193 |
-
# Plot industrial production and its percentage change on dual y-axes
|
| 194 |
fig.add_trace(go.Scatter(x=combined_data.index, y=combined_data['INDPRO'], mode='lines', name='Industrial Production'))
|
| 195 |
if 'INDPRO_PCT' in combined_data:
|
| 196 |
fig.add_trace(go.Scatter(x=combined_data.index, y=combined_data['INDPRO_PCT'], mode='lines', name='Industrial Production % Change', yaxis='y2'))
|
|
@@ -209,7 +201,6 @@ if st.sidebar.button('Run Analysis'):
|
|
| 209 |
)
|
| 210 |
)
|
| 211 |
elif column == ('CPIAUCSL', 'CPIAUCSL_PCT') and 'CPIAUCSL' in combined_data:
|
| 212 |
-
# Plot inflation and its percentage change on dual y-axes
|
| 213 |
fig.add_trace(go.Scatter(x=combined_data.index, y=combined_data['CPIAUCSL'], mode='lines', name='Inflation (CPI)'))
|
| 214 |
if 'CPIAUCSL_PCT' in combined_data:
|
| 215 |
fig.add_trace(go.Scatter(x=combined_data.index, y=combined_data['CPIAUCSL_PCT'], mode='lines', name='Inflation % Change', yaxis='y2'))
|
|
@@ -228,7 +219,6 @@ if st.sidebar.button('Run Analysis'):
|
|
| 228 |
)
|
| 229 |
)
|
| 230 |
elif column == ('GS10', 'DGS2', 'DGS1MO', 'TB3MS'):
|
| 231 |
-
# Plot multiple Treasury rates in the same subplot
|
| 232 |
for col in column:
|
| 233 |
if col in combined_data:
|
| 234 |
fig.add_trace(go.Scatter(x=combined_data.index, y=combined_data[col], mode='lines', name=col))
|
|
@@ -244,7 +234,6 @@ if st.sidebar.button('Run Analysis'):
|
|
| 244 |
else:
|
| 245 |
if column in combined_data:
|
| 246 |
fig.add_trace(go.Scatter(x=combined_data.index, y=combined_data[column], mode='lines', name=key))
|
| 247 |
-
# Add horizontal threshold line for Sahm Recession Indicator
|
| 248 |
if column == 'SAHMREALTIME':
|
| 249 |
fig.add_hline(y=0.5, line=dict(color="red", dash="dash"), annotation_text="Recession Threshold", annotation_position="bottom right")
|
| 250 |
|
|
|
|
| 3 |
import yfinance as yf
|
| 4 |
import datetime
|
| 5 |
import plotly.graph_objs as go
|
|
|
|
| 6 |
|
| 7 |
# Set up the layout
|
| 8 |
st.set_page_config(layout="wide")
|
|
|
|
| 13 |
<style>
|
| 14 |
/* Increase the width of the sidebar */
|
| 15 |
.css-1d391kg {
|
| 16 |
+
width: 350px;
|
| 17 |
}
|
|
|
|
| 18 |
/* Ensure main content is still centered properly */
|
| 19 |
.css-1d391kg ~ .css-1d391kg {
|
| 20 |
+
left: 350px;
|
| 21 |
}
|
| 22 |
</style>
|
| 23 |
""",
|
|
|
|
| 47 |
with st.sidebar.expander("Indicators", expanded=True):
|
| 48 |
indicators = {
|
| 49 |
'Sahm Recession Indicator': 'SAHMREALTIME',
|
| 50 |
+
'U.S. Recession Probabilities': 'RECPROUSM156N', # Corrected to actual FRED series ID
|
| 51 |
+
'Yield Spread (10Y - 2Y)': 'Yield_Spread', # Kept as placeholder, calculated later
|
| 52 |
'Stock Market (S&P 500)': 'SP500',
|
| 53 |
'VIX': 'VIX',
|
| 54 |
'Treasury Rates': ('GS10', 'DGS2', 'DGS1MO', 'TB3MS'),
|
|
|
|
| 93 |
if st.sidebar.button('Run Analysis'):
|
| 94 |
# Initialize combined_data as an empty DataFrame
|
| 95 |
combined_data = pd.DataFrame()
|
|
|
|
|
|
|
| 96 |
fred_data = {}
|
| 97 |
+
|
| 98 |
+
# Fetch FRED data with error handling, excluding derived columns
|
| 99 |
for key, column in indicators.items():
|
| 100 |
if selected_indicators[key]:
|
| 101 |
if isinstance(column, tuple):
|
|
|
|
| 106 |
except Exception as e:
|
| 107 |
st.warning(f"Failed to fetch {col} from FRED: {e}")
|
| 108 |
fred_data[col] = pd.DataFrame()
|
| 109 |
+
elif column != 'Yield_Spread': # Skip Yield_Spread as it's calculated
|
| 110 |
try:
|
| 111 |
fred_data[column] = web.DataReader(column, 'fred', start_date, end_date)
|
| 112 |
except Exception as e:
|
|
|
|
| 136 |
st.warning(f"Failed to fetch VIX data: {e}")
|
| 137 |
fred_data['VIX'] = pd.DataFrame()
|
| 138 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
# Combine all FRED and Yahoo Finance data
|
| 140 |
if fred_data:
|
| 141 |
combined_data = pd.concat(fred_data.values(), axis=1, join='outer')
|
|
|
|
| 183 |
|
| 184 |
if isinstance(column, tuple):
|
| 185 |
if column == ('INDPRO', 'INDPRO_PCT') and 'INDPRO' in combined_data:
|
|
|
|
| 186 |
fig.add_trace(go.Scatter(x=combined_data.index, y=combined_data['INDPRO'], mode='lines', name='Industrial Production'))
|
| 187 |
if 'INDPRO_PCT' in combined_data:
|
| 188 |
fig.add_trace(go.Scatter(x=combined_data.index, y=combined_data['INDPRO_PCT'], mode='lines', name='Industrial Production % Change', yaxis='y2'))
|
|
|
|
| 201 |
)
|
| 202 |
)
|
| 203 |
elif column == ('CPIAUCSL', 'CPIAUCSL_PCT') and 'CPIAUCSL' in combined_data:
|
|
|
|
| 204 |
fig.add_trace(go.Scatter(x=combined_data.index, y=combined_data['CPIAUCSL'], mode='lines', name='Inflation (CPI)'))
|
| 205 |
if 'CPIAUCSL_PCT' in combined_data:
|
| 206 |
fig.add_trace(go.Scatter(x=combined_data.index, y=combined_data['CPIAUCSL_PCT'], mode='lines', name='Inflation % Change', yaxis='y2'))
|
|
|
|
| 219 |
)
|
| 220 |
)
|
| 221 |
elif column == ('GS10', 'DGS2', 'DGS1MO', 'TB3MS'):
|
|
|
|
| 222 |
for col in column:
|
| 223 |
if col in combined_data:
|
| 224 |
fig.add_trace(go.Scatter(x=combined_data.index, y=combined_data[col], mode='lines', name=col))
|
|
|
|
| 234 |
else:
|
| 235 |
if column in combined_data:
|
| 236 |
fig.add_trace(go.Scatter(x=combined_data.index, y=combined_data[column], mode='lines', name=key))
|
|
|
|
| 237 |
if column == 'SAHMREALTIME':
|
| 238 |
fig.add_hline(y=0.5, line=dict(color="red", dash="dash"), annotation_text="Recession Threshold", annotation_position="bottom right")
|
| 239 |
|