Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,52 +6,87 @@ import pandas as pd
|
|
| 6 |
from geo_macro import UnifiedMarketDataDownloader
|
| 7 |
from feature_engineering import IntegratedTheoryFeatures
|
| 8 |
|
| 9 |
-
#
|
|
|
|
|
|
|
| 10 |
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
try:
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
df
|
|
|
|
| 21 |
|
| 22 |
-
# Run feature
|
| 23 |
engine = IntegratedTheoryFeatures(df)
|
| 24 |
features = engine.build_all_features()
|
| 25 |
|
| 26 |
-
# Extract latest
|
| 27 |
-
latest = features.iloc[-1]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
output = {
|
| 29 |
-
"Regime": latest["regime"],
|
| 30 |
-
"Dalio Composite":
|
| 31 |
-
"Stevenson Inequality":
|
| 32 |
-
"Thiel Monopoly":
|
| 33 |
-
"Gundlach Reckoning":
|
| 34 |
-
"Prob Credit Collapse":
|
| 35 |
-
"Prob Stagflation":
|
| 36 |
-
"Prob Tech Boom":
|
| 37 |
}
|
| 38 |
-
|
| 39 |
return output
|
| 40 |
-
|
| 41 |
except Exception as e:
|
| 42 |
return {"Error": str(e)}
|
| 43 |
|
|
|
|
| 44 |
# Gradio UI
|
| 45 |
-
with gr.Blocks(title="Integrated Market Theory Dashboard") as demo:
|
| 46 |
gr.Markdown("# 🌍 Integrated Market Theory Dashboard")
|
| 47 |
gr.Markdown("Real-time macro regime detection using Dalio, Stevenson, Thiel & Gundlach frameworks")
|
| 48 |
|
| 49 |
with gr.Row():
|
| 50 |
-
days = gr.Slider(365, 2500, value=1825, label="Lookback Window (days)")
|
| 51 |
-
run_btn = gr.Button("🔄 Update Analysis")
|
| 52 |
|
| 53 |
output = gr.JSON(label="Current Market Regime & Probabilities")
|
| 54 |
|
| 55 |
run_btn.click(run_pipeline, inputs=days, outputs=output)
|
| 56 |
|
|
|
|
| 57 |
demo.launch()
|
|
|
|
| 6 |
from geo_macro import UnifiedMarketDataDownloader
|
| 7 |
from feature_engineering import IntegratedTheoryFeatures
|
| 8 |
|
| 9 |
+
# Cache to avoid re-downloading data on every click
|
| 10 |
+
_cached_df = None
|
| 11 |
+
_cached_dates = (None, None)
|
| 12 |
|
| 13 |
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def get_data(start_date: str, end_date: str):
|
| 17 |
+
"""Download or return cached market data."""
|
| 18 |
+
global _cached_df, _cached_dates
|
| 19 |
+
if _cached_df is not None and _cached_dates == (start_date, end_date):
|
| 20 |
+
return _cached_df.copy()
|
| 21 |
+
|
| 22 |
+
print(f"📥 Downloading data from {start_date} to {end_date}...")
|
| 23 |
+
downloader = UnifiedMarketDataDownloader()
|
| 24 |
+
df = downloader.download_all_data(start_date=start_date, end_date=end_date)
|
| 25 |
+
|
| 26 |
+
_cached_df = df.copy()
|
| 27 |
+
_cached_dates = (start_date, end_date)
|
| 28 |
+
return df
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def run_pipeline(days_back: int = 1825):
|
| 32 |
try:
|
| 33 |
+
# Define date range
|
| 34 |
+
today = pd.Timestamp.today()
|
| 35 |
+
start_date = (today - pd.Timedelta(days=days_back)).strftime('%Y-%m-%d')
|
| 36 |
+
end_date = today.strftime('%Y-%m-%d')
|
| 37 |
+
|
| 38 |
+
# Get data (cached if possible)
|
| 39 |
+
df = get_data(start_date, end_date)
|
| 40 |
|
| 41 |
+
# Validate data
|
| 42 |
+
if len(df) < 300:
|
| 43 |
+
return {"Error": "Insufficient data. Try increasing lookback window."}
|
| 44 |
|
| 45 |
+
# Run feature pipeline
|
| 46 |
engine = IntegratedTheoryFeatures(df)
|
| 47 |
features = engine.build_all_features()
|
| 48 |
|
| 49 |
+
# Extract latest non-NaN row
|
| 50 |
+
latest = features.dropna(subset=['regime']).iloc[-1]
|
| 51 |
+
|
| 52 |
+
def fmt(x):
|
| 53 |
+
if pd.isna(x):
|
| 54 |
+
return "N/A"
|
| 55 |
+
if isinstance(x, float) and -1 <= x <= 1:
|
| 56 |
+
return f"{x:.3f}"
|
| 57 |
+
elif isinstance(x, float) and 0 <= x <= 1:
|
| 58 |
+
return f"{x:.1%}"
|
| 59 |
+
else:
|
| 60 |
+
return str(x)
|
| 61 |
+
|
| 62 |
output = {
|
| 63 |
+
"Regime": str(latest["regime"]),
|
| 64 |
+
"Dalio Composite": fmt(latest['dalio_composite_norm']),
|
| 65 |
+
"Stevenson Inequality": fmt(latest['stevenson_inequality_norm']),
|
| 66 |
+
"Thiel Monopoly": fmt(latest['thiel_monopoly_norm']),
|
| 67 |
+
"Gundlach Reckoning": fmt(latest['gundlach_reckoning_norm']),
|
| 68 |
+
"Prob Credit Collapse": fmt(latest['prob_credit_collapse']),
|
| 69 |
+
"Prob Stagflation": fmt(latest['prob_stagflation']),
|
| 70 |
+
"Prob Tech Boom": fmt(latest['prob_tech_boom']),
|
| 71 |
}
|
|
|
|
| 72 |
return output
|
| 73 |
+
|
| 74 |
except Exception as e:
|
| 75 |
return {"Error": str(e)}
|
| 76 |
|
| 77 |
+
|
| 78 |
# Gradio UI
|
| 79 |
+
with gr.Blocks(title="🌍 Integrated Market Theory Dashboard") as demo:
|
| 80 |
gr.Markdown("# 🌍 Integrated Market Theory Dashboard")
|
| 81 |
gr.Markdown("Real-time macro regime detection using Dalio, Stevenson, Thiel & Gundlach frameworks")
|
| 82 |
|
| 83 |
with gr.Row():
|
| 84 |
+
days = gr.Slider(365, 2500, value=1825, step=90, label="Lookback Window (days)")
|
| 85 |
+
run_btn = gr.Button("🔄 Update Analysis", variant="primary")
|
| 86 |
|
| 87 |
output = gr.JSON(label="Current Market Regime & Probabilities")
|
| 88 |
|
| 89 |
run_btn.click(run_pipeline, inputs=days, outputs=output)
|
| 90 |
|
| 91 |
+
# Launch (HF Spaces uses this)
|
| 92 |
demo.launch()
|