Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -61,170 +61,6 @@ crash_periods = {
|
|
| 61 |
'2020-02-01': '2020-04-01'
|
| 62 |
}
|
| 63 |
|
| 64 |
-
# Run button
|
| 65 |
-
if st.sidebar.button('Run Analysis'):
|
| 66 |
-
# Fetch the data from FRED
|
| 67 |
-
sahm_recession_indicator = web.DataReader('SAHMREALTIME', 'fred', start_date, end_date)
|
| 68 |
-
retail_sales = web.DataReader('RSXFS', 'fred', start_date, end_date)
|
| 69 |
-
industrial_production = web.DataReader('INDPRO', 'fred', start_date, end_date)
|
| 70 |
-
unemployment_rate = web.DataReader('UNRATE', 'fred', start_date, end_date)
|
| 71 |
-
inflation = web.DataReader('CPIAUCSL', 'fred', start_date, end_date)
|
| 72 |
-
nonfarm_payrolls = web.DataReader('PAYEMS', 'fred', start_date, end_date)
|
| 73 |
-
jobless_claims = web.DataReader('ICSA', 'fred', start_date, end_date)
|
| 74 |
-
consumer_confidence = web.DataReader('UMCSENT', 'fred', start_date, end_date)
|
| 75 |
-
housing_starts = web.DataReader('HOUST', 'fred', start_date, end_date)
|
| 76 |
-
treasury_rate_10y = web.DataReader('GS10', 'fred', start_date, end_date)
|
| 77 |
-
treasury_rate_2y = web.DataReader('DGS2', 'fred', start_date, end_date)
|
| 78 |
-
treasury_rate_1m = web.DataReader('DGS1MO', 'fred', start_date, end_date)
|
| 79 |
-
treasury_rate_3m = web.DataReader('TB3MS', 'fred', start_date, end_date)
|
| 80 |
-
federal_funds_rate = web.DataReader('FEDFUNDS', 'fred', start_date, end_date)
|
| 81 |
-
recession_probabilities = web.DataReader('RECPROUSM156N', 'fred', start_date, end_date)
|
| 82 |
-
|
| 83 |
-
# Rename columns for clarity
|
| 84 |
-
recession_probabilities.columns = ['Recession_Probabilities']
|
| 85 |
-
|
| 86 |
-
# Fetch the data from Yahoo Finance
|
| 87 |
-
sp500 = yf.download('^GSPC', start=start_date, end=end_date)['Adj Close']
|
| 88 |
-
vix = yf.download('^VIX', start=start_date, end=end_date)['Adj Close']
|
| 89 |
-
|
| 90 |
-
# Calculate percentage changes for relevant indicators
|
| 91 |
-
industrial_production_pct = industrial_production.pct_change() * 100
|
| 92 |
-
inflation_pct = inflation.pct_change() * 100
|
| 93 |
-
|
| 94 |
-
# Rename columns for clarity
|
| 95 |
-
industrial_production_pct = industrial_production_pct.rename(columns={'INDPRO': 'INDPRO_PCT'})
|
| 96 |
-
inflation_pct = inflation_pct.rename(columns={'CPIAUCSL': 'CPIAUCSL_PCT'})
|
| 97 |
-
|
| 98 |
-
# Combine all data into a single DataFrame, aligning by date
|
| 99 |
-
combined_data = sahm_recession_indicator.join([
|
| 100 |
-
retail_sales, industrial_production, industrial_production_pct,
|
| 101 |
-
unemployment_rate, inflation, inflation_pct,
|
| 102 |
-
sp500.rename('SP500'), nonfarm_payrolls, jobless_claims,
|
| 103 |
-
consumer_confidence, housing_starts, treasury_rate_10y, treasury_rate_2y, treasury_rate_1m, treasury_rate_3m, federal_funds_rate, vix.rename('VIX'),
|
| 104 |
-
recession_probabilities
|
| 105 |
-
], how='outer')
|
| 106 |
-
|
| 107 |
-
# Interpolate missing values for alignment
|
| 108 |
-
combined_data = combined_data.interpolate(method='time')
|
| 109 |
-
|
| 110 |
-
# Calculate the yield spread
|
| 111 |
-
combined_data['Yield_Spread'] = combined_data['GS10'] - combined_data['DGS2']
|
| 112 |
-
|
| 113 |
-
# Plot each selected series
|
| 114 |
-
for key, column in indicators.items():
|
| 115 |
-
if selected_indicators[key]:
|
| 116 |
-
fig = go.Figure()
|
| 117 |
-
|
| 118 |
-
# Add recession shading
|
| 119 |
-
for peak, trough in crash_periods.items():
|
| 120 |
-
fig.add_shape(
|
| 121 |
-
type="rect",
|
| 122 |
-
xref="x",
|
| 123 |
-
yref="paper",
|
| 124 |
-
x0=peak,
|
| 125 |
-
y0=0,
|
| 126 |
-
x1=trough,
|
| 127 |
-
y1=1,
|
| 128 |
-
fillcolor="gray",
|
| 129 |
-
opacity=0.3,
|
| 130 |
-
layer="below",
|
| 131 |
-
line_width=0,
|
| 132 |
-
)
|
| 133 |
-
|
| 134 |
-
if isinstance(column, tuple):
|
| 135 |
-
if column == ('INDPRO', 'INDPRO_PCT'):
|
| 136 |
-
# Plot industrial production and its percentage change on dual y-axes
|
| 137 |
-
fig.add_trace(go.Scatter(x=combined_data.index, y=combined_data['INDPRO'], mode='lines', name='Industrial Production'))
|
| 138 |
-
fig.add_trace(go.Scatter(x=combined_data.index, y=combined_data['INDPRO_PCT'], mode='lines', name='Industrial Production % Change', yaxis='y2'))
|
| 139 |
-
|
| 140 |
-
# Create a secondary y-axis and place the legend inside the plot area
|
| 141 |
-
fig.update_layout(
|
| 142 |
-
yaxis2=dict(
|
| 143 |
-
title="Industrial Production % Change",
|
| 144 |
-
overlaying='y',
|
| 145 |
-
side='right'
|
| 146 |
-
),
|
| 147 |
-
legend=dict(
|
| 148 |
-
x=0.02,
|
| 149 |
-
y=0.95,
|
| 150 |
-
traceorder='normal',
|
| 151 |
-
bgcolor='rgba(255, 255, 255, 0.5)',
|
| 152 |
-
bordercolor='rgba(0, 0, 0, 0)'
|
| 153 |
-
),
|
| 154 |
-
autosize=True,
|
| 155 |
-
margin=dict(l=50, r=50, t=50, b=50),
|
| 156 |
-
height=500, # Slightly increased height
|
| 157 |
-
width=900, # Added width for 16:9 ratio
|
| 158 |
-
)
|
| 159 |
-
elif column == ('CPIAUCSL', 'CPIAUCSL_PCT'):
|
| 160 |
-
# Plot inflation and its percentage change on dual y-axes
|
| 161 |
-
fig.add_trace(go.Scatter(x=combined_data.index, y=combined_data['CPIAUCSL'], mode='lines', name='Inflation (CPI)'))
|
| 162 |
-
It seems that the message was cut off, but here’s the complete adjusted code that should address the full-screen vertical stretching issue:
|
| 163 |
-
|
| 164 |
-
```python
|
| 165 |
-
import streamlit as st
|
| 166 |
-
import pandas_datareader.data as web
|
| 167 |
-
import yfinance as yf
|
| 168 |
-
import datetime
|
| 169 |
-
import plotly.graph_objs as go
|
| 170 |
-
|
| 171 |
-
# Set up the layout
|
| 172 |
-
st.set_page_config(layout="wide")
|
| 173 |
-
|
| 174 |
-
# App title and description
|
| 175 |
-
st.title("Key Economic Recession Indicators")
|
| 176 |
-
st.markdown("""
|
| 177 |
-
This tool allows you to visualize and analyze various recession indicators over time.
|
| 178 |
-
The shaded areas in the charts represent historical recession periods.
|
| 179 |
-
Use the checkboxes in the sidebar to choose the indicators you'd like to explore.
|
| 180 |
-
""")
|
| 181 |
-
|
| 182 |
-
# Sidebar for chart selection
|
| 183 |
-
st.sidebar.header("Select Indicators")
|
| 184 |
-
indicators = {
|
| 185 |
-
'Sahm Recession Indicator': 'SAHMREALTIME',
|
| 186 |
-
'U.S. Recession Probabilities': 'Recession_Probabilities',
|
| 187 |
-
'Yield Spread (10Y - 2Y)': 'Yield_Spread',
|
| 188 |
-
'Stock Market (S&P 500)': 'SP500',
|
| 189 |
-
'VIX': 'VIX',
|
| 190 |
-
'Treasury Rates': ('GS10', 'DGS2', 'DGS1MO', 'TB3MS'),
|
| 191 |
-
'Federal Funds Rate': 'FEDFUNDS',
|
| 192 |
-
'Unemployment Rate': 'UNRATE',
|
| 193 |
-
'Nonfarm Payrolls': 'PAYEMS',
|
| 194 |
-
'Jobless Claims': 'ICSA',
|
| 195 |
-
'Retail Sales': 'RSXFS',
|
| 196 |
-
'Industrial Production': ('INDPRO', 'INDPRO_PCT'),
|
| 197 |
-
'Housing Starts': 'HOUST',
|
| 198 |
-
'Consumer Confidence': 'UMCSENT',
|
| 199 |
-
'Inflation (CPI)': ('CPIAUCSL', 'CPIAUCSL_PCT')
|
| 200 |
-
}
|
| 201 |
-
|
| 202 |
-
selected_indicators = {key: st.sidebar.checkbox(key, value=True) for key in indicators.keys()}
|
| 203 |
-
|
| 204 |
-
# Define start and end dates
|
| 205 |
-
start_date = datetime.datetime(1920, 1, 1)
|
| 206 |
-
end_date = datetime.datetime.today()
|
| 207 |
-
extended_end_date = end_date + datetime.timedelta(days=120)
|
| 208 |
-
|
| 209 |
-
# Define recession periods (as before)
|
| 210 |
-
crash_periods = {
|
| 211 |
-
'1929-08-01': '1933-03-01',
|
| 212 |
-
'1937-05-01': '1938-06-01',
|
| 213 |
-
'1945-02-01': '1945-10-01',
|
| 214 |
-
'1948-11-01': '1949-10-01',
|
| 215 |
-
'1953-07-01': '1954-05-01',
|
| 216 |
-
'1957-08-01': '1958-04-01',
|
| 217 |
-
'1960-04-01': '1961-02-01',
|
| 218 |
-
'1969-12-01': '1970-11-01',
|
| 219 |
-
'1973-11-01': '1975-03-01',
|
| 220 |
-
'1980-01-01': '1980-07-01',
|
| 221 |
-
'1981-07-01': '1982-11-01',
|
| 222 |
-
'1990-07-01': '1991-03-01',
|
| 223 |
-
'2001-03-01': '2001-11-01',
|
| 224 |
-
'2007-12-01': '2009-06-01',
|
| 225 |
-
'2020-02-01': '2020-04-01'
|
| 226 |
-
}
|
| 227 |
-
|
| 228 |
# Run button
|
| 229 |
if st.sidebar.button('Run Analysis'):
|
| 230 |
# Fetch the data from FRED
|
|
@@ -342,7 +178,6 @@ if st.sidebar.button('Run Analysis'):
|
|
| 342 |
autosize=True,
|
| 343 |
margin=dict(l=50, r=50, t=50, b=50),
|
| 344 |
height=500, # SlightIt looks like the last part of the code snippet was cut off. Here's the continuation and completion of the code to ensure that the graph does not stretch disproportionately when displayed in full screen:
|
| 345 |
-
|
| 346 |
)
|
| 347 |
elif column == ('GS10', 'DGS2', 'DGS1MO', 'TB3MS'):
|
| 348 |
# Plot multiple Treasury rates in the same subplot
|
|
@@ -420,4 +255,4 @@ hide_streamlit_style = """
|
|
| 420 |
footer {visibility: hidden;}
|
| 421 |
</style>
|
| 422 |
"""
|
| 423 |
-
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
|
|
|
|
| 61 |
'2020-02-01': '2020-04-01'
|
| 62 |
}
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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, b=50),
|
| 180 |
height=500, # SlightIt looks like the last part of the code snippet was cut off. Here's the continuation and completion of the code to ensure that the graph does not stretch disproportionately when displayed in full screen:
|
|
|
|
| 181 |
)
|
| 182 |
elif column == ('GS10', 'DGS2', 'DGS1MO', 'TB3MS'):
|
| 183 |
# Plot multiple Treasury rates in the same subplot
|
|
|
|
| 255 |
footer {visibility: hidden;}
|
| 256 |
</style>
|
| 257 |
"""
|
| 258 |
+
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
|