File size: 10,848 Bytes
72fc0ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
"""
FastAPI Router for Macro Economic Data (FRED Integration)

Provides real economic indicators from the Federal Reserve Economic Data (FRED) API.
Endpoints for GDP, unemployment, inflation, interest rates, and macro regime detection.
"""

import logging
from datetime import datetime, timedelta
from typing import Dict, List, Optional

from fastapi import APIRouter, HTTPException, Query
from pydantic import BaseModel, Field

from quantlib_pro.data.fred_provider import FREDProvider

logger = logging.getLogger(__name__)

macro_router = APIRouter(
    prefix="/macro", 
    tags=["Federal Reserve Economic Data (FRED)"],
    responses={
        429: {"description": "Rate limit exceeded"},
        500: {"description": "Internal server error"},
        503: {"description": "FRED service unavailable"}
    }
)

# Global FRED provider (reuse connection)
_fred_provider: Optional[FREDProvider] = None


def get_fred_provider() -> FREDProvider:
    """Get or create FRED provider."""
    global _fred_provider
    if _fred_provider is None:
        try:
            # Use the project's FRED API key
            _fred_provider = FREDProvider(api_key="5f5dcf2ef53c496228fa2935b71d9d40")
            logger.info("FRED provider initialized successfully")
        except Exception as e:
            logger.error(f"Failed to initialize FRED provider: {e}")
            raise HTTPException(
                status_code=503, 
                detail=f"FRED service unavailable: {e}"
            )
    return _fred_provider


# Request/Response Models
class EconomicIndicatorsRequest(BaseModel):
    indicators: List[str] = Field(
        default=["GDP_GROWTH", "UNEMPLOYMENT", "CPI", "FED_RATE"],
        description="Economic indicators to fetch"
    )
    periods: int = Field(default=60, description="Number of periods to retrieve")


class MacroRegimeResponse(BaseModel):
    regime: str
    confidence: float
    expansion_probability: float
    recession_probability: float
    indicators: Dict[str, float]
    last_updated: str


class YieldCurveResponse(BaseModel):
    rates: Dict[str, float]
    last_updated: str


# Endpoints
@macro_router.post("/indicators")
async def get_economic_indicators(request: EconomicIndicatorsRequest):
    """
    Get real economic indicators from FRED.
    
    Fetches current and historical data for key economic indicators including:
    - GDP Growth Rate
    - Unemployment Rate  
    - Consumer Price Index (CPI)
    - Federal Funds Rate
    - Manufacturing PMI
    - Consumer Sentiment
    """
    try:
        fred = get_fred_provider()
        
        # Fetch economic indicators
        data = fred.get_economic_indicators(
            indicators=request.indicators,
            periods=request.periods
        )
        
        # Get current snapshot
        snapshot = fred.get_macro_regime_data()
        
        response = {
            "indicators": request.indicators,
            "periods_requested": request.periods,
            "periods_returned": len(data) if not data.empty else 0,
            "current_values": snapshot,
            "historical_data": data.to_dict('index') if not data.empty else {},
            "data_source": "Federal Reserve Economic Data (FRED)",
            "last_updated": datetime.now().isoformat(),
        }
        
        return response
        
    except Exception as e:
        logger.error(f"Failed to fetch economic indicators: {e}")
        raise HTTPException(
            status_code=500,
            detail=f"Failed to fetch economic data: {e}"
        )


@macro_router.get(
    "/regime",
    summary="Real-Time Macroeconomic Regime Analysis",
    description="**Live economic regime detection** using Federal Reserve data for institutional investment decision-making.",
)
async def get_macro_regime():
    """
    **Real-Time Macroeconomic Regime Analysis using Federal Reserve Data**
    
    Analyze current economic conditions using live data from the Federal Reserve Economic 
    Database (FRED) to determine market regime for institutional investment strategies.
    
    **Economic Regimes:**
    - **Expansion**: GDP growth >2.5%, unemployment <4%, manufacturing PMI >52  
    - **Recession**: GDP contraction, unemployment >6%, inverted yield curve
    - **Transition**: Mixed economic signals indicating regime change in progress
    
    **Data Sources (Real-Time):**
    - **GDP Growth Rate**: Quarterly annualized rate from Bureau of Economic Analysis
    - **Unemployment Rate**: Monthly non-farm payroll data from Bureau of Labor Statistics  
    - **Federal Funds Rate**: Target rate from Federal Open Market Committee
    - **10-Year Treasury**: Daily yield from U.S. Treasury Department
    - **Manufacturing PMI**: Institute for Supply Management purchasing managers index
    - **Yield Curve**: 2Y/10Y spread for recession probability assessment
    
    **Institutional Applications:**
    - **Asset Allocation**: Regime-based portfolio rebalancing strategies
    - **Risk Management**: Recession probability for stress testing scenarios
    - **Hedge Fund Strategies**: Macro trend following and contrarian positioning
    - **Fixed Income**: Duration and credit allocation based on regime outlook
    
    **Data Reliability:**
    - **Source**: Federal Reserve Economic Data (FRED) API
    - **Update Frequency**: Real-time with automatic cache refresh
    - **Historical Coverage**: 70+ years of economic data for backtesting
    - **Enterprise SLA**: 99.9% data availability guarantee
    
    Returns:
        Current regime assessment, confidence levels, economic indicators, and recession probability
    """
    try:
        fred = get_fred_provider()
        regime_analysis = fred.assess_macro_regime()
        
        return MacroRegimeResponse(**regime_analysis)
        
    except Exception as e:
        logger.error(f"Failed to analyze macro regime: {e}")
        raise HTTPException(
            status_code=500,
            detail=f"Macro regime analysis failed: {e}"
        )


@macro_router.get("/yield-curve")
async def get_yield_curve():
    """
    Get current US Treasury yield curve.
    
    Returns rates for:
    - 3 Month Treasury
    - 2 Year Treasury  
    - 5 Year Treasury
    - 10 Year Treasury
    - 30 Year Treasury
    
    Yield curve shape indicates economic expectations and recession risk.
    """
    try:
        fred = get_fred_provider()
        rates = fred.get_yield_curve_data()
        
        return YieldCurveResponse(
            rates=rates,
            last_updated=datetime.now().isoformat()
        )
        
    except Exception as e:
        logger.error(f"Failed to fetch yield curve: {e}")
        raise HTTPException(
            status_code=500,
            detail=f"Yield curve data unavailable: {e}"
        )


@macro_router.get("/recession-probability")  
async def get_recession_probability():
    """
    Calculate recession probability using economic indicators.
    
    Uses the Sahm Rule and other recession indicators:
    - Unemployment rate changes
    - Inverted yield curve (10Y-2Y spread)
    - Manufacturing PMI below 50
    - Consumer sentiment decline
    
    Returns probability score and contributing factors.
    """
    try:
        fred = get_fred_provider()
        indicators = fred.get_recession_indicators()
        
        return {
            "recession_probability": indicators.get("recession_probability", 0.0),
            "sahm_indicator": indicators.get("sahm_indicator", 0.0),
            "yield_spread_10y_2y": indicators.get("yield_spread_10y_2y", 200),
            "contributing_factors": {
                "unemployment_rate": indicators.get("unemployment_rate", 4.0),
                "pmi": indicators.get("pmi", 52.0),
                "consumer_sentiment": indicators.get("consumer_sentiment", 100.0),
            },
            "interpretation": {
                "low_risk": "< 20% probability",
                "moderate_risk": "20-50% probability", 
                "high_risk": "> 50% probability",
            },
            "data_source": "Federal Reserve Economic Data (FRED)",
            "last_updated": datetime.now().isoformat(),
        }
        
    except Exception as e:
        logger.error(f"Failed to calculate recession probability: {e}")
        raise HTTPException(
            status_code=500,
            detail=f"Recession analysis failed: {e}"
        )


@macro_router.get("/series/{series_name}")
async def get_economic_series(
    series_name: str,
    periods: int = Query(default=60, description="Number of periods"),
    start_date: Optional[str] = Query(default=None, description="Start date YYYY-MM-DD")
):
    """
    Get historical time series for specific economic indicator.
    
    Available series:
    - GDP, GDP_GROWTH
    - UNEMPLOYMENT  
    - INFLATION, CPI
    - FED_FUNDS, TREASURY_10Y, TREASURY_2Y
    - PMI
    """
    try:
        fred = get_fred_provider()
        
        data = fred.get_historical_series(
            series=series_name,
            start_date=start_date,
            periods=periods
        )
        
        if data.empty:
            raise HTTPException(
                status_code=404,
                detail=f"No data found for series: {series_name}"
            )
        
        return {
            "series": series_name,
            "periods_returned": len(data),
            "start_date": data.index.min().strftime('%Y-%m-%d'),
            "end_date": data.index.max().strftime('%Y-%m-%d'),
            "data": data.to_dict('index'),
            "data_source": "Federal Reserve Economic Data (FRED)",
            "last_updated": datetime.now().isoformat(),
        }
        
    except ValueError as e:
        raise HTTPException(status_code=400, detail=str(e))
    except Exception as e:
        logger.error(f"Failed to fetch series {series_name}: {e}")
        raise HTTPException(
            status_code=500,
            detail=f"Failed to fetch economic series: {e}"
        )


@macro_router.get("/health")
async def macro_health():
    """Check FRED service health and connectivity."""
    try:
        fred = get_fred_provider()
        
        # Test connection by fetching one indicator
        test_data = fred.fred.get_latest_value('GS10')
        
        return {
            "status": "healthy",
            "fred_connection": "active",
            "test_indicator": "10Y Treasury Rate",
            "test_value": test_data[0],
            "test_date": test_data[1],
            "last_checked": datetime.now().isoformat(),
        }
        
    except Exception as e:
        logger.error(f"FRED health check failed: {e}")
        return {
            "status": "unhealthy", 
            "error": str(e),
            "fred_connection": "failed",
            "last_checked": datetime.now().isoformat(),
        }