File size: 9,944 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
"""
QuantLib Pro SDK - Enterprise Quantitative Finance Toolkit

This module provides the main SDK interface for QuantLib Pro, offering
a comprehensive suite of quantitative finance tools and utilities.

Usage:
    >>> from quantlib_pro import QuantLibSDK
    >>> sdk = QuantLibSDK()
    >>> 
    >>> # Portfolio analysis
    >>> portfolio = sdk.portfolio.create_portfolio(assets=['AAPL', 'GOOGL'])
    >>> returns = sdk.portfolio.calculate_returns(portfolio)
    >>> 
    >>> # Risk management
    >>> var = sdk.risk.calculate_var(returns, confidence_level=0.05)
    >>> cvar = sdk.risk.calculate_cvar(returns, confidence_level=0.05)
    >>> 
    >>> # Options pricing
    >>> bs_price = sdk.options.black_scholes(S=100, K=105, T=0.25, r=0.05, sigma=0.2)
"""

from typing import Optional, Dict, Any, List
from dataclasses import dataclass, field
import logging
import os
from pathlib import Path
import pandas as pd

# Configure logging for SDK
logger = logging.getLogger(__name__)


@dataclass
class SDKConfig:
    """Configuration class for QuantLib Pro SDK."""
    
    # Data source configuration
    alpha_vantage_key: Optional[str] = None
    fred_api_key: Optional[str] = None
    factset_username: Optional[str] = None
    factset_key: Optional[str] = None
    
    # Database configuration
    database_url: Optional[str] = None
    redis_url: Optional[str] = None
    
    # Performance settings
    enable_caching: bool = True
    cache_ttl: int = 3600
    max_workers: int = 4
    
    # Risk management defaults
    default_confidence_level: float = 0.05
    default_time_horizon: int = 252  # Trading days in a year
    
    # Logging configuration
    log_level: str = "INFO"
    log_file: Optional[str] = None
    
    def __post_init__(self):
        """Load configuration from environment variables."""
        self.alpha_vantage_key = self.alpha_vantage_key or os.getenv("ALPHA_VANTAGE_API_KEY")
        self.fred_api_key = self.fred_api_key or os.getenv("FRED_API_KEY") 
        self.factset_username = self.factset_username or os.getenv("FACTSET_USERNAME")
        self.factset_key = self.factset_key or os.getenv("FACTSET_API_KEY")
        self.database_url = self.database_url or os.getenv("DATABASE_URL", "postgresql://quantlib:quantlib123@localhost:5432/quantlib")
        self.redis_url = self.redis_url or os.getenv("REDIS_URL", "redis://localhost:6379")


class QuantLibSDK:
    """
    Main SDK interface for QuantLib Pro.
    
    This class provides access to all quantitative finance modules
    including portfolio management, risk analysis, options pricing,
    volatility modeling, market regime detection, and more.
    
    Args:
        config: Optional SDKConfig instance for custom configuration
        
    Example:
        >>> sdk = QuantLibSDK()
        >>> portfolio_metrics = sdk.portfolio.analyze(['AAPL', 'GOOGL', 'MSFT'])
        >>> risk_metrics = sdk.risk.portfolio_risk(portfolio_metrics)
    """
    
    def __init__(self, config: Optional[SDKConfig] = None):
        self.config = config or SDKConfig()
        self._setup_logging()
        
        # Lazy-loaded managers
        self._portfolio_manager = None
        self._risk_manager = None
        self._options_manager = None
        self._volatility_manager = None
        self._data_manager = None
        self._macro_manager = None
        self._market_regime_manager = None
        self._analytics_manager = None
        self._execution_manager = None
        
        logger.info("QuantLib Pro SDK initialized")
    
    def _setup_logging(self):
        """Configure logging for the SDK."""
        logging.basicConfig(
            level=getattr(logging, self.config.log_level.upper()),
            format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
            filename=self.config.log_file
        )
    
    @property
    def portfolio(self):
        """Portfolio management and analysis tools."""
        if self._portfolio_manager is None:
            from .portfolio.manager import PortfolioManager
            self._portfolio_manager = PortfolioManager(self.config)
        return self._portfolio_manager
    
    @property
    def risk(self):
        """Risk management and measurement tools."""
        if self._risk_manager is None:
            from .risk.manager import RiskManager
            self._risk_manager = RiskManager(self.config)
        return self._risk_manager
    
    @property 
    def options(self):
        """Options pricing and Greeks calculation tools."""
        if self._options_manager is None:
            from .options.manager import OptionsManager
            self._options_manager = OptionsManager(self.config)
        return self._options_manager
    
    @property
    def volatility(self):
        """Volatility modeling and forecasting tools."""
        if self._volatility_manager is None:
            from .volatility.manager import VolatilityManager
            self._volatility_manager = VolatilityManager(self.config)
        return self._volatility_manager
    
    @property
    def data(self):
        """Market data retrieval and processing tools."""
        if self._data_manager is None:
            from .data.manager import DataManager
            self._data_manager = DataManager(self.config)
        return self._data_manager
    
    @property
    def macro(self):
        """Macroeconomic analysis and indicators."""
        if self._macro_manager is None:
            from .macro.manager import MacroManager
            self._macro_manager = MacroManager(self.config)
        return self._macro_manager
    
    @property
    def market_regime(self):
        """Market regime detection and analysis."""
        if self._market_regime_manager is None:
            from .market_regime.manager import MarketRegimeManager
            self._market_regime_manager = MarketRegimeManager(self.config)
        return self._market_regime_manager
    
    @property
    def analytics(self):
        """Advanced analytics and statistical tools."""
        if self._analytics_manager is None:
            from .analytics.manager import AnalyticsManager
            self._analytics_manager = AnalyticsManager(self.config)
        return self._analytics_manager
    
    @property
    def execution(self):
        """Trade execution and transaction cost analysis."""
        if self._execution_manager is None:
            from .execution.manager import ExecutionManager
            self._execution_manager = ExecutionManager(self.config)
        return self._execution_manager
    
    def health_check(self) -> Dict[str, Any]:
        """
        Perform a health check of all SDK components.
        
        Returns:
            Dict containing status of each component
        """
        status = {
            "sdk_version": "1.0.0",
            "config": self.config.__dict__,
            "components": {}
        }
        
        components = [
            ("portfolio", self.portfolio),
            ("risk", self.risk), 
            ("options", self.options),
            ("volatility", self.volatility),
            ("data", self.data),
            ("macro", self.macro),
            ("market_regime", self.market_regime),
            ("analytics", self.analytics),
            ("execution", self.execution)
        ]
        
        for name, component in components:
            try:
                # Test basic functionality if health_check method exists
                if hasattr(component, 'health_check'):
                    status["components"][name] = component.health_check()
                else:
                    status["components"][name] = {"status": "available"}
            except Exception as e:
                status["components"][name] = {"status": "error", "error": str(e)}
                logger.error(f"Health check failed for {name}: {e}")
        
        return status
    
    def get_supported_assets(self) -> List[str]:
        """Get list of supported asset symbols."""
        return self.data.get_supported_symbols()
    
    def quick_analysis(self, symbols: List[str], period: str = "1y") -> Dict[str, Any]:
        """
        Perform quick analysis on a list of symbols.
        
        Args:
            symbols: List of asset symbols to analyze
            period: Time period for analysis (e.g., "1y", "6m", "3m")
            
        Returns:
            Dict containing portfolio and risk metrics
        """
        try:
            # Get data
            data = self.data.get_price_data(symbols, period=period)
            
            # Portfolio analysis
            portfolio_metrics = self.portfolio.calculate_portfolio_metrics(data)
            
            # Risk analysis
            risk_metrics = self.risk.calculate_portfolio_risk(data)
            
            # Combine results
            return {
                "symbols": symbols,
                "period": period,
                "portfolio": portfolio_metrics,
                "risk": risk_metrics,
                "analysis_timestamp": str(pd.Timestamp.now())
            }
            
        except Exception as e:
            logger.error(f"Quick analysis failed: {e}")
            return {"error": str(e)}


# Convenience function for quick SDK access
def create_sdk(config: Optional[Dict[str, Any]] = None) -> QuantLibSDK:
    """
    Convenience function to create a QuantLibSDK instance.
    
    Args:
        config: Dictionary of configuration parameters
        
    Returns:
        Configured QuantLibSDK instance
    """
    if config:
        sdk_config = SDKConfig(**config)
    else:
        sdk_config = SDKConfig()
    
    return QuantLibSDK(sdk_config)


# Version information
__version__ = "1.0.0"
__author__ = "QuantLib Pro Team"
__email__ = "info@quantlibpro.com"

# Package-level exports
__all__ = [
    "QuantLibSDK",
    "SDKConfig", 
    "create_sdk",
    "__version__",
    "__author__", 
    "__email__"
]