Spaces:
No application file
No application file
Upload 4 files
Browse files- app.py +70 -0
- config.py +27 -0
- data_loader.py +53 -0
- requirements.txt +0 -0
app.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import plotly.express as px
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
st.set_page_config(page_title="Quant Domar Strategy", layout="wide")
|
| 7 |
+
st.title("📈 Global Domar Spread Analysis")
|
| 8 |
+
st.markdown("### Real Sustainability Metrics: Spot $i$ vs. Real $g$")
|
| 9 |
+
|
| 10 |
+
# Force refresh button for data verification
|
| 11 |
+
if st.sidebar.button("Purge Cache & Reload"):
|
| 12 |
+
st.cache_data.clear()
|
| 13 |
+
st.rerun()
|
| 14 |
+
|
| 15 |
+
@st.cache_data
|
| 16 |
+
def load_data():
|
| 17 |
+
if not os.path.exists('data/historical_spreads.csv'):
|
| 18 |
+
return pd.DataFrame()
|
| 19 |
+
df = pd.read_csv('data/historical_spreads.csv', parse_dates=['DATE'])
|
| 20 |
+
return df.sort_values(['Country', 'DATE'])
|
| 21 |
+
|
| 22 |
+
df = load_data()
|
| 23 |
+
|
| 24 |
+
if df.empty:
|
| 25 |
+
st.error("Data file missing. Please run 'python data_loader.py' first.")
|
| 26 |
+
else:
|
| 27 |
+
countries = df['Country'].unique()
|
| 28 |
+
|
| 29 |
+
# --- SECTION 1: SPOT RATES ---
|
| 30 |
+
st.subheader("10Y Benchmark Interest Rates ($i$)")
|
| 31 |
+
cols_i = st.columns(len(countries))
|
| 32 |
+
for idx, country in enumerate(countries):
|
| 33 |
+
latest = df[df['Country'] == country].iloc[-1]
|
| 34 |
+
cols_i[idx].metric(country, f"{latest['rate']:.2f}%")
|
| 35 |
+
cols_i[idx].caption(f"Spot: {latest['DATE'].strftime('%Y-%m-%d')}")
|
| 36 |
+
|
| 37 |
+
# --- SECTION 2: REAL GDP GROWTH ---
|
| 38 |
+
st.subheader("Annual Real GDP Growth Rate ($g$)")
|
| 39 |
+
cols_g = st.columns(len(countries))
|
| 40 |
+
for idx, country in enumerate(countries):
|
| 41 |
+
latest = df[df['Country'] == country].iloc[-1]
|
| 42 |
+
# This should now show ~2.3% for USA
|
| 43 |
+
cols_g[idx].metric(country, f"{latest['g_growth']:.2f}%")
|
| 44 |
+
cols_g[idx].caption(f"Quarter: {latest['DATE'].year}-Q{latest['DATE'].quarter}")
|
| 45 |
+
|
| 46 |
+
# --- SECTION 3: TECHNICAL TABLE ---
|
| 47 |
+
st.divider()
|
| 48 |
+
summary = []
|
| 49 |
+
for country in countries:
|
| 50 |
+
latest = df[df['Country'] == country].iloc[-1]
|
| 51 |
+
summary.append({
|
| 52 |
+
"Region": country,
|
| 53 |
+
"10Y Rate": latest['rate'],
|
| 54 |
+
"Real GDP Growth": latest['g_growth'],
|
| 55 |
+
"Spread (i-g)": latest['domar_spread'],
|
| 56 |
+
"As of": latest['DATE'].date()
|
| 57 |
+
})
|
| 58 |
+
|
| 59 |
+
st.table(pd.DataFrame(summary).style.format({
|
| 60 |
+
"10Y Rate": "{:.2f}%", "Real GDP Growth": "{:.2f}%", "Spread (i-g)": "{:.2f}%"
|
| 61 |
+
}))
|
| 62 |
+
|
| 63 |
+
# --- SECTION 4: CHART ---
|
| 64 |
+
st.divider()
|
| 65 |
+
selected = st.sidebar.multiselect("Regions", options=countries, default=list(countries))
|
| 66 |
+
filtered = df[df['Country'].isin(selected)]
|
| 67 |
+
if not filtered.empty:
|
| 68 |
+
fig = px.line(filtered, x='DATE', y='domar_spread', color='Country', title="Domar Spread Trend")
|
| 69 |
+
fig.add_hline(y=0, line_dash="dash", line_color="red")
|
| 70 |
+
st.plotly_chart(fig, use_container_width=True)
|
config.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# config.py
|
| 2 |
+
START_DATE = '2015-01-01'
|
| 3 |
+
|
| 4 |
+
COUNTRIES = ["USA", "UK", "Eurozone", "Japan", "Australia"]
|
| 5 |
+
|
| 6 |
+
COUNTRY_CONFIG = {
|
| 7 |
+
"USA": {
|
| 8 |
+
"GDP_Real": "GDPC1",
|
| 9 |
+
"Rate_10Y": "DGS10",
|
| 10 |
+
},
|
| 11 |
+
"UK": {
|
| 12 |
+
"GDP_Real": "NGDPRSAXDCGBQ",
|
| 13 |
+
"Rate_10Y": "IRLTLT01GBM156N",
|
| 14 |
+
},
|
| 15 |
+
"Eurozone": {
|
| 16 |
+
"GDP_Real": "CLV10MEURB1GQSCAEA20Q",
|
| 17 |
+
"Rate_10Y": "IRLTLT01EZM156N",
|
| 18 |
+
},
|
| 19 |
+
"Japan": {
|
| 20 |
+
"GDP_Real": "JPNRGDPEXP",
|
| 21 |
+
"Rate_10Y": "IRLTLT01JPM156N",
|
| 22 |
+
},
|
| 23 |
+
"Australia": {
|
| 24 |
+
"GDP_Real": "NAEXKP01AUQ661S",
|
| 25 |
+
"Rate_10Y": "IRLTLT01AUM156N",
|
| 26 |
+
}
|
| 27 |
+
}
|
data_loader.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import pandas_datareader.data as web
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
import config
|
| 6 |
+
|
| 7 |
+
load_dotenv()
|
| 8 |
+
FRED_KEY = os.getenv('FRED_API_KEY')
|
| 9 |
+
|
| 10 |
+
def fetch_country_data(country_name):
|
| 11 |
+
cfg = config.COUNTRY_CONFIG[country_name]
|
| 12 |
+
|
| 13 |
+
try:
|
| 14 |
+
# Fetch Real GDP and 10Y Rate
|
| 15 |
+
df = web.DataReader([cfg["GDP_Real"], cfg['Rate_10Y']], 'fred', config.START_DATE, api_key=FRED_KEY)
|
| 16 |
+
df.columns = ['gdp_raw', 'rate_raw']
|
| 17 |
+
|
| 18 |
+
# 1. Isolate GDP releases to prevent daily 0% change errors
|
| 19 |
+
gdp_series = df['gdp_raw'].dropna().drop_duplicates()
|
| 20 |
+
|
| 21 |
+
# 2. Calculate YoY Growth: (Current Quarter / Quarter 1 Year Ago) - 1
|
| 22 |
+
# This yields the ~2.3% figure for USA
|
| 23 |
+
g_growth = gdp_series.pct_change(periods=4) * 100
|
| 24 |
+
|
| 25 |
+
# 3. Create Daily DataFrame
|
| 26 |
+
daily_df = pd.DataFrame(index=df.index)
|
| 27 |
+
daily_df['rate'] = df['rate_raw'].ffill()
|
| 28 |
+
daily_df['g_growth'] = g_growth.reindex(df.index, method='ffill')
|
| 29 |
+
daily_df['domar_spread'] = daily_df['rate'] - daily_df['g_growth']
|
| 30 |
+
daily_df['Country'] = country_name
|
| 31 |
+
|
| 32 |
+
# 4. Filter for valid data only
|
| 33 |
+
return daily_df.dropna(subset=['g_growth', 'rate']).query("g_growth != 0")
|
| 34 |
+
|
| 35 |
+
except Exception as e:
|
| 36 |
+
print(f"Error for {country_name}: {e}")
|
| 37 |
+
return pd.DataFrame()
|
| 38 |
+
|
| 39 |
+
def main():
|
| 40 |
+
master_list = []
|
| 41 |
+
for country in config.COUNTRIES:
|
| 42 |
+
print(f"Syncing {country} Real GDP Data...")
|
| 43 |
+
df_c = fetch_country_data(country)
|
| 44 |
+
if not df_c.empty:
|
| 45 |
+
master_list.append(df_c)
|
| 46 |
+
|
| 47 |
+
if master_list:
|
| 48 |
+
if not os.path.exists('data'): os.makedirs('data')
|
| 49 |
+
pd.concat(master_list).to_csv('data/historical_spreads.csv')
|
| 50 |
+
print("\n[SUCCESS] CSV generated with Real GDP growth.")
|
| 51 |
+
|
| 52 |
+
if __name__ == "__main__":
|
| 53 |
+
main()
|
requirements.txt
ADDED
|
File without changes
|