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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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
"""
Streamlit Caching Strategies

Week 13: Performance optimization through strategic caching.
"""

import streamlit as st
import pandas as pd
import numpy as np
from functools import wraps
from typing import Any, Callable
import hashlib
import json


def cache_data_with_ttl(ttl: int = 3600):
    """
    Decorator for caching data with time-to-live.
    
    Args:
        ttl: Time to live in seconds (default: 1 hour)
    
    Usage:
        @cache_data_with_ttl(ttl=300)  # 5 minutes
        def expensive_calculation():
            ...
    """
    return st.cache_data(ttl=ttl, show_spinner=False)


def cache_resource_singleton():
    """
    Decorator for caching resources (connections, models, etc).
    These are shared across all users and sessions.
    
    Usage:
        @cache_resource_singleton()
        def get_database_connection():
            ...
    """
    return st.cache_resource(show_spinner=False)


@cache_data_with_ttl(ttl=3600)
def cached_market_data(ticker: str, start_date: str, end_date: str) -> pd.DataFrame:
    """
    Fetch and cache market data for a ticker.
    
    Args:
        ticker: Stock ticker symbol
        start_date: Start date (YYYY-MM-DD)
        end_date: End date (YYYY-MM-DD)
    
    Returns:
        DataFrame with price data
    """
    from quantlib_pro.data.market_data import MarketDataProvider
    
    provider = MarketDataProvider()
    df = provider.get_stock_data(
        ticker=ticker,
        start_date=start_date,
        end_date=end_date
    )
    
    if df.empty:
        raise ValueError(f"No data available for {ticker}")
    
    # Calculate returns
    returns = df['Close'].pct_change().dropna()
    
    return pd.DataFrame({
        "date": df.index,
        "close": df['Close'].values,
        "returns": returns.values if len(returns) > 0 else [],
    })


@cache_data_with_ttl(ttl=1800)
def cached_portfolio_optimization(
    tickers: tuple,  # Must be hashable (tuple, not list)
    weights: tuple,
    risk_free_rate: float,
    num_points: int,
) -> dict:
    """
    Cache portfolio optimization results.
    
    Args:
        tickers: Tuple of ticker symbols
        weights: Tuple of portfolio weights
        risk_free_rate: Risk-free rate
        num_points: Number of efficient frontier points
    
    Returns:
        Dict with optimization results
    """
    from quantlib_pro.portfolio.optimizer import PortfolioOptimizer
    from quantlib_pro.data.market_data import MarketDataProvider
    import pandas as pd
    from datetime import datetime, timedelta
    
    # Fetch real market data
    provider = MarketDataProvider()
    end_date = datetime.now().strftime('%Y-%m-%d')
    start_date = (datetime.now() - timedelta(days=365)).strftime('%Y-%m-%d')
    
    returns_data = {}
    for ticker in tickers:
        df = provider.get_stock_data(ticker, start_date, end_date)
        if not df.empty:
            returns_data[ticker] = df['Close'].pct_change().dropna()
    
    if not returns_data:
        raise ValueError("No data available for any ticker")
    
    # Align returns
    returns_df = pd.DataFrame(returns_data)
    expected_returns = returns_df.mean() * 252
    cov_matrix = returns_df.cov() * 252
    
    # Run optimization
    optimizer = PortfolioOptimizer(
        expected_returns=expected_returns,
        cov_matrix=cov_matrix,
        risk_free_rate=risk_free_rate,
        tickers=list(tickers)
    )
    
    frontier = optimizer.efficient_frontier(num_points=num_points)
    
    return {
        "returns": frontier['returns'],
        "volatilities": frontier['volatilities'],
        "sharpe_ratios": frontier['sharpe_ratios'],
        "optimal_idx": frontier.get('optimal_idx', 0),
    }


@cache_data_with_ttl(ttl=1800)
def cached_option_pricing(
    spot: float,
    strike: float,
    time_to_expiry: float,
    volatility: float,
    risk_free_rate: float,
    option_type: str,
) -> dict:
    """
    Cache option pricing calculations.
    
    Args:
        spot: Spot price
        strike: Strike price
        time_to_expiry: Time to expiration (years)
        volatility: Implied volatility
        risk_free_rate: Risk-free rate
        option_type: 'call' or 'put'
    
    Returns:
        Dict with option price and Greeks
    """
    from quantlib_pro.options.black_scholes import price_with_greeks
    
    return price_with_greeks(
        spot=spot,
        strike=strike,
        T=time_to_expiry,
        r=risk_free_rate,
        sigma=volatility,
        option_type=option_type,
    )


@cache_data_with_ttl(ttl=600)
def cached_var_calculation(
    returns_hash: str,  # Hash of returns array
    confidence_level: float,
    method: str,
) -> dict:
    """
    Cache VaR calculations.
    
    Note: We hash the returns array because numpy arrays aren't hashable.
    
    Args:
        returns_hash: MD5 hash of returns array
        confidence_level: Confidence level (0.95, 0.99, etc)
        method: VaR method (historical, parametric, monte_carlo)
    
    Returns:
        Dict with VaR results
    """
    # This is a placeholder - actual implementation would unhash returns
    # and call calculate_var
    return {
        "var": -0.02,
        "cvar": -0.03,
        "confidence_level": confidence_level,
        "method": method,
    }


def hash_numpy_array(arr: np.ndarray) -> str:
    """
    Create a hash of a numpy array for caching.
    
    Args:
        arr: Numpy array
    
    Returns:
        MD5 hash string
    """
    return hashlib.md5(arr.tobytes()).hexdigest()


@cache_resource_singleton()
def get_hmm_model():
    """
    Load and cache HMM model.
    This is expensive to initialize, so cache it.
    
    Returns:
        Fitted HMM model
    """
    from hmmlearn import hmm
    
    # Initialize model (would be pre-trained in production)
    model = hmm.GaussianHMM(n_components=3, covariance_type="full", n_iter=100)
    
    return model


@cache_data_with_ttl(ttl=3600)
def cached_correlation_matrix(tickers: tuple, start_date: str, end_date: str) -> pd.DataFrame:
    """
    Cache correlation matrix calculation.
    
    Args:
        tickers: Tuple of ticker symbols
        start_date: Start date
        end_date: End date
    
    Returns:
        Correlation matrix DataFrame
    """
    from quantlib_pro.data.market_data import MarketDataProvider
    
    provider = MarketDataProvider()
    returns_dict = {}
    
    for ticker in tickers:
        try:
            df = provider.get_stock_data(ticker, start_date, end_date)
            if not df.empty:
                returns_dict[ticker] = df['Close'].pct_change().dropna()
        except Exception as e:
            # Skip tickers that fail to load
            continue
    
    if not returns_dict:
        raise ValueError("No data available for any ticker")
    
    returns_df = pd.DataFrame(returns_dict)
    return returns_df.corr()


@cache_data_with_ttl(ttl=300)
def cached_volatility_surface(
    spot: float,
    strikes: tuple,
    maturities: tuple,
    atm_vol: float,
) -> np.ndarray:
    """
    Cache volatility surface construction.
    
    Args:
        spot: Spot price
        strikes: Tuple of strike prices
        maturities: Tuple of maturities (days)
        atm_vol: At-the-money volatility
    
    Returns:
        2D array of implied volatilities
    """
    surface = np.zeros((len(strikes), len(maturities)))
    
    for i, strike in enumerate(strikes):
        for j, maturity in enumerate(maturities):
            moneyness = np.log(strike / spot)
            time_years = maturity / 365.0
            
            # Simple parametric model
            term_factor = 1.0 + 0.3 * np.exp(-time_years * 0.5)
            skew_factor = moneyness * (1 - 0.5 * moneyness)
            convexity_factor = 0.3 * moneyness ** 2
            
            vol = atm_vol * term_factor + skew_factor + convexity_factor
            surface[i, j] = max(vol, 0.05)
    
    return surface


def clear_all_caches():
    """Clear all Streamlit caches."""
    st.cache_data.clear()
    st.cache_resource.clear()


def get_cache_stats() -> dict:
    """
    Get statistics about cache usage.
    
    Returns:
        Dict with cache statistics
    """
    # Streamlit doesn't expose cache stats directly
    # This is a placeholder for monitoring
    return {
        "data_cache_enabled": True,
        "resource_cache_enabled": True,
        "ttl_default": 3600,
    }


# Session state helpers for caching across page navigations

def get_session_state(key: str, default: Any = None) -> Any:
    """
    Get value from session state with default.
    
    Args:
        key: Session state key
        default: Default value if key not found
    
    Returns:
        Value from session state or default
    """
    return st.session_state.get(key, default)


def set_session_state(key: str, value: Any):
    """
    Set value in session state.
    
    Args:
        key: Session state key
        value: Value to store
    """
    st.session_state[key] = value


def memoize_in_session(key: str, default: Any = None):
    """
    Decorator to memoize function results in session state.
    
    Args:
        key: Session state key
        default: Default value
    
    Usage:
        @memoize_in_session("expensive_calculation")
        def expensive_calculation():
            ...
    """
    def decorator(func: Callable) -> Callable:
        @wraps(func)
        def wrapper(*args, **kwargs):
            if key not in st.session_state:
                st.session_state[key] = func(*args, **kwargs)
            return st.session_state[key]
        return wrapper
    return decorator


# Caching strategy guidelines

CACHING_GUIDELINES = """
# Streamlit Caching Best Practices

## Use @st.cache_data for:
- DataFrame transformations
- API calls
- Database queries
- Computationally expensive calculations
- Data loading/processing

## Use @st.cache_resource for:
- Database connections
- ML model loading
- API clients
- Thread pools
- Any object that should be shared across all users

## Use session_state for:
- User-specific temporary data
- Form inputs
- Page navigation state
- Calculated results that don't need to persist

## TTL (Time-to-Live) Guidelines:
- Real-time data: 60-300 seconds
- Intraday data: 300-900 seconds (5-15 minutes)
- Daily data: 1800-3600 seconds (30-60 minutes)
- Static/reference data: 7200+ seconds (2+ hours)

## Cache Invalidation:
- Automatic: Based on TTL
- Manual: Call st.cache_data.clear() or st.cache_resource.clear()
- Parameter-based: Cache is invalidated when function arguments change

## Performance Tips:
1. Cache at the highest level possible
2. Use tuples instead of lists for hashable arguments
3. Hash large numpy arrays before passing to cached functions
4. Clear caches during deployments
5. Monitor memory usage with large caches
"""