Tonic commited on
Commit
3d44e72
·
1 Parent(s): 7a5abd6

adds descriptive docstrings for mcp

Browse files
Files changed (1) hide show
  1. app.py +152 -22
app.py CHANGED
@@ -366,7 +366,7 @@ def calculate_bollinger_bands(prices: pd.Series, period: int = 20, std_dev: int
366
  lower_band = middle_band - (std * std_dev)
367
  return upper_band, middle_band, lower_band
368
 
369
- @spaces.GPU(duration=180)
370
  def make_prediction(symbol: str, timeframe: str = "1d", prediction_days: int = 5, strategy: str = "chronos",
371
  use_ensemble: bool = True, use_regime_detection: bool = True, use_stress_testing: bool = True,
372
  risk_free_rate: float = 0.02, ensemble_weights: Dict = None,
@@ -2079,22 +2079,64 @@ def create_interface():
2079
  """
2080
  Process daily timeframe stock analysis with advanced features.
2081
 
 
 
 
 
2082
  Args:
2083
- s (str): Stock symbol (e.g., "AAPL", "MSFT", "GOOGL")
 
2084
  pd (int): Number of days to predict (1-365)
 
2085
  ld (int): Historical lookback period in days (1-3650)
 
2086
  st (str): Prediction strategy to use ("chronos" or "technical")
 
 
2087
  ue (bool): Use ensemble methods
 
2088
  urd (bool): Use regime detection
 
2089
  ust (bool): Use stress testing
2090
- rfr (float): Risk-free rate
2091
- mi (str): Market index
2092
- cw (float): Chronos weight
2093
- tw (float): Technical weight
2094
- sw (float): Statistical weight
 
 
 
 
 
 
2095
 
2096
  Returns:
2097
- Tuple containing all analysis results
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2098
  """
2099
  return analyze_stock(s, "1d", pd, ld, st, ue, urd, ust, rfr, mi, cw, tw, sw)
2100
 
@@ -2113,22 +2155,65 @@ def create_interface():
2113
  """
2114
  Process hourly timeframe stock analysis with advanced features.
2115
 
 
 
 
 
2116
  Args:
2117
- s (str): Stock symbol (e.g., "AAPL", "MSFT", "GOOGL")
 
2118
  pd (int): Number of days to predict (1-7)
 
2119
  ld (int): Historical lookback period in days (1-60)
 
2120
  st (str): Prediction strategy to use ("chronos" or "technical")
 
 
2121
  ue (bool): Use ensemble methods
 
2122
  urd (bool): Use regime detection
 
2123
  ust (bool): Use stress testing
2124
- rfr (float): Risk-free rate
2125
- mi (str): Market index
2126
- cw (float): Chronos weight
2127
- tw (float): Technical weight
2128
- sw (float): Statistical weight
 
 
 
 
 
 
2129
 
2130
  Returns:
2131
- Tuple containing all analysis results
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2132
  """
2133
  return analyze_stock(s, "1h", pd, ld, st, ue, urd, ust, rfr, mi, cw, tw, sw)
2134
 
@@ -2147,22 +2232,67 @@ def create_interface():
2147
  """
2148
  Process 15-minute timeframe stock analysis with advanced features.
2149
 
 
 
 
 
2150
  Args:
2151
- s (str): Stock symbol (e.g., "AAPL", "MSFT", "GOOGL")
 
2152
  pd (int): Number of days to predict (1-2)
 
2153
  ld (int): Historical lookback period in days (1-7)
 
2154
  st (str): Prediction strategy to use ("chronos" or "technical")
 
 
2155
  ue (bool): Use ensemble methods
 
2156
  urd (bool): Use regime detection
 
2157
  ust (bool): Use stress testing
2158
- rfr (float): Risk-free rate
2159
- mi (str): Market index
2160
- cw (float): Chronos weight
2161
- tw (float): Technical weight
2162
- sw (float): Statistical weight
 
 
 
 
 
 
2163
 
2164
  Returns:
2165
- Tuple containing all analysis results
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2166
  """
2167
  return analyze_stock(s, "15m", pd, ld, st, ue, urd, ust, rfr, mi, cw, tw, sw)
2168
 
 
366
  lower_band = middle_band - (std * std_dev)
367
  return upper_band, middle_band, lower_band
368
 
369
+ @spaces.GPU()
370
  def make_prediction(symbol: str, timeframe: str = "1d", prediction_days: int = 5, strategy: str = "chronos",
371
  use_ensemble: bool = True, use_regime_detection: bool = True, use_stress_testing: bool = True,
372
  risk_free_rate: float = 0.02, ensemble_weights: Dict = None,
 
2079
  """
2080
  Process daily timeframe stock analysis with advanced features.
2081
 
2082
+ This function performs comprehensive stock analysis using daily data with support for
2083
+ multiple prediction strategies, ensemble methods, regime detection, and stress testing.
2084
+ It's designed for medium to long-term investment analysis with up to 365 days of prediction.
2085
+
2086
  Args:
2087
+ s (str): Stock symbol (e.g., "AAPL", "MSFT", "GOOGL", "TSLA")
2088
+ Must be a valid stock symbol available on Yahoo Finance
2089
  pd (int): Number of days to predict (1-365)
2090
+ The forecast horizon for the analysis. Longer periods may have higher uncertainty
2091
  ld (int): Historical lookback period in days (1-3650)
2092
+ Amount of historical data to use for analysis. More data generally improves accuracy
2093
  st (str): Prediction strategy to use ("chronos" or "technical")
2094
+ - "chronos": Uses Amazon's Chronos T5 model for time series forecasting
2095
+ - "technical": Uses traditional technical analysis indicators
2096
  ue (bool): Use ensemble methods
2097
+ When True, combines multiple prediction models for improved accuracy
2098
  urd (bool): Use regime detection
2099
+ When True, detects market regimes (bull/bear/sideways) to adjust predictions
2100
  ust (bool): Use stress testing
2101
+ When True, performs scenario analysis under various market conditions
2102
+ rfr (float): Risk-free rate (0.0-0.1)
2103
+ Annual risk-free rate used for risk-adjusted return calculations
2104
+ mi (str): Market index for correlation analysis
2105
+ Options: "^GSPC" (S&P 500), "^DJI" (Dow Jones), "^IXIC" (NASDAQ), "^RUT" (Russell 2000)
2106
+ cw (float): Chronos weight in ensemble (0.0-1.0)
2107
+ Weight given to Chronos model predictions in ensemble methods
2108
+ tw (float): Technical weight in ensemble (0.0-1.0)
2109
+ Weight given to technical analysis predictions in ensemble methods
2110
+ sw (float): Statistical weight in ensemble (0.0-1.0)
2111
+ Weight given to statistical model predictions in ensemble methods
2112
 
2113
  Returns:
2114
+ Tuple[Dict, go.Figure, Dict, Dict, Dict, Dict, Dict, Dict, Dict]: Analysis results containing:
2115
+ - Dict: Basic trading signals (RSI, MACD, Bollinger Bands, SMA, Overall)
2116
+ - go.Figure: Interactive plot with historical data, predictions, and confidence intervals
2117
+ - Dict: Structured product metrics (Market Cap, P/E ratios, financial ratios)
2118
+ - Dict: Advanced risk metrics (Sharpe ratio, VaR, drawdown, correlation)
2119
+ - Dict: Sector and industry analysis metrics
2120
+ - Dict: Market regime detection results
2121
+ - Dict: Stress testing scenario results
2122
+ - Dict: Ensemble method configuration and results
2123
+ - Dict: Advanced trading signals with confidence levels
2124
+
2125
+ Raises:
2126
+ gr.Error: If data cannot be fetched, insufficient data points, or other analysis errors
2127
+ Common errors include invalid symbols, market closure, or insufficient historical data
2128
+
2129
+ Example:
2130
+ >>> signals, plot, metrics, risk, sector, regime, stress, ensemble, advanced = daily_analysis(
2131
+ ... "AAPL", 30, 365, "chronos", True, True, True, 0.02, "^GSPC", 0.6, 0.2, 0.2
2132
+ ... )
2133
+
2134
+ Notes:
2135
+ - Daily analysis is available 24/7 regardless of market hours
2136
+ - Maximum prediction period is 365 days
2137
+ - Historical data can go back up to 10 years (3650 days)
2138
+ - Ensemble weights should sum to 1.0 for optimal results
2139
+ - Risk-free rate is typically between 0.02-0.05 (2-5% annually)
2140
  """
2141
  return analyze_stock(s, "1d", pd, ld, st, ue, urd, ust, rfr, mi, cw, tw, sw)
2142
 
 
2155
  """
2156
  Process hourly timeframe stock analysis with advanced features.
2157
 
2158
+ This function performs high-frequency stock analysis using hourly data, ideal for
2159
+ short to medium-term trading strategies. It includes intraday volatility analysis,
2160
+ volume-price trends, and momentum indicators optimized for hourly timeframes.
2161
+
2162
  Args:
2163
+ s (str): Stock symbol (e.g., "AAPL", "MSFT", "GOOGL", "TSLA")
2164
+ Must be a valid stock symbol with sufficient liquidity for hourly analysis
2165
  pd (int): Number of days to predict (1-7)
2166
+ Limited to 7 days due to Yahoo Finance hourly data constraints
2167
  ld (int): Historical lookback period in days (1-60)
2168
+ Enhanced to 60 days for hourly data (vs standard 30 days)
2169
  st (str): Prediction strategy to use ("chronos" or "technical")
2170
+ - "chronos": Uses Amazon's Chronos T5 model optimized for hourly data
2171
+ - "technical": Uses technical indicators adjusted for hourly timeframes
2172
  ue (bool): Use ensemble methods
2173
+ Combines multiple models for improved short-term prediction accuracy
2174
  urd (bool): Use regime detection
2175
+ Detects intraday market regimes and volatility patterns
2176
  ust (bool): Use stress testing
2177
+ Performs scenario analysis for short-term market shocks
2178
+ rfr (float): Risk-free rate (0.0-0.1)
2179
+ Annual risk-free rate for risk-adjusted calculations
2180
+ mi (str): Market index for correlation analysis
2181
+ Options: "^GSPC" (S&P 500), "^DJI" (Dow Jones), "^IXIC" (NASDAQ), "^RUT" (Russell 2000)
2182
+ cw (float): Chronos weight in ensemble (0.0-1.0)
2183
+ Weight for Chronos model in ensemble predictions
2184
+ tw (float): Technical weight in ensemble (0.0-1.0)
2185
+ Weight for technical analysis in ensemble predictions
2186
+ sw (float): Statistical weight in ensemble (0.0-1.0)
2187
+ Weight for statistical models in ensemble predictions
2188
 
2189
  Returns:
2190
+ Tuple[Dict, go.Figure, Dict, Dict, Dict, Dict, Dict, Dict, Dict]: Analysis results containing:
2191
+ - Dict: Basic trading signals optimized for hourly timeframes
2192
+ - go.Figure: Interactive plot with hourly data, predictions, and intraday patterns
2193
+ - Dict: Product metrics including intraday volatility and volume analysis
2194
+ - Dict: Risk metrics adjusted for hourly data frequency
2195
+ - Dict: Sector analysis with intraday-specific metrics
2196
+ - Dict: Market regime detection for hourly patterns
2197
+ - Dict: Stress testing results for short-term scenarios
2198
+ - Dict: Ensemble analysis configuration and results
2199
+ - Dict: Advanced signals with intraday-specific indicators
2200
+
2201
+ Raises:
2202
+ gr.Error: If market is closed, insufficient data, or analysis errors
2203
+ Hourly data is only available during market hours (9:30 AM - 4:00 PM ET)
2204
+
2205
+ Example:
2206
+ >>> signals, plot, metrics, risk, sector, regime, stress, ensemble, advanced = hourly_analysis(
2207
+ ... "AAPL", 3, 14, "chronos", True, True, True, 0.02, "^GSPC", 0.6, 0.2, 0.2
2208
+ ... )
2209
+
2210
+ Notes:
2211
+ - Only available during market hours (9:30 AM - 4:00 PM ET, weekdays)
2212
+ - Maximum prediction period is 7 days (168 hours)
2213
+ - Historical data limited to 60 days due to Yahoo Finance constraints
2214
+ - Includes pre/post market data for extended hours analysis
2215
+ - Optimized for day trading and swing trading strategies
2216
+ - Requires high-liquidity stocks for reliable hourly analysis
2217
  """
2218
  return analyze_stock(s, "1h", pd, ld, st, ue, urd, ust, rfr, mi, cw, tw, sw)
2219
 
 
2232
  """
2233
  Process 15-minute timeframe stock analysis with advanced features.
2234
 
2235
+ This function performs ultra-high-frequency stock analysis using 15-minute data,
2236
+ designed for scalping and very short-term trading strategies. It includes specialized
2237
+ indicators for intraday patterns, volume analysis, and momentum detection.
2238
+
2239
  Args:
2240
+ s (str): Stock symbol (e.g., "AAPL", "MSFT", "GOOGL", "TSLA")
2241
+ Must be a highly liquid stock symbol suitable for high-frequency analysis
2242
  pd (int): Number of days to predict (1-2)
2243
+ Limited to 2 days due to 15-minute data granularity and model constraints
2244
  ld (int): Historical lookback period in days (1-7)
2245
+ Enhanced to 7 days for 15-minute data (vs standard 5 days)
2246
  st (str): Prediction strategy to use ("chronos" or "technical")
2247
+ - "chronos": Uses Amazon's Chronos T5 model optimized for 15-minute intervals
2248
+ - "technical": Uses technical indicators specifically tuned for 15-minute timeframes
2249
  ue (bool): Use ensemble methods
2250
+ Combines multiple models for improved ultra-short-term prediction accuracy
2251
  urd (bool): Use regime detection
2252
+ Detects micro-market regimes and volatility clustering patterns
2253
  ust (bool): Use stress testing
2254
+ Performs scenario analysis for intraday market shocks and volatility spikes
2255
+ rfr (float): Risk-free rate (0.0-0.1)
2256
+ Annual risk-free rate for risk-adjusted calculations (less relevant for 15m analysis)
2257
+ mi (str): Market index for correlation analysis
2258
+ Options: "^GSPC" (S&P 500), "^DJI" (Dow Jones), "^IXIC" (NASDAQ), "^RUT" (Russell 2000)
2259
+ cw (float): Chronos weight in ensemble (0.0-1.0)
2260
+ Weight for Chronos model in ensemble predictions
2261
+ tw (float): Technical weight in ensemble (0.0-1.0)
2262
+ Weight for technical analysis in ensemble predictions
2263
+ sw (float): Statistical weight in ensemble (0.0-1.0)
2264
+ Weight for statistical models in ensemble predictions
2265
 
2266
  Returns:
2267
+ Tuple[Dict, go.Figure, Dict, Dict, Dict, Dict, Dict, Dict, Dict]: Analysis results containing:
2268
+ - Dict: Basic trading signals optimized for 15-minute timeframes
2269
+ - go.Figure: Interactive plot with 15-minute data, predictions, and micro-patterns
2270
+ - Dict: Product metrics including high-frequency volatility and volume analysis
2271
+ - Dict: Risk metrics adjusted for 15-minute data frequency
2272
+ - Dict: Sector analysis with ultra-short-term metrics
2273
+ - Dict: Market regime detection for 15-minute patterns
2274
+ - Dict: Stress testing results for intraday scenarios
2275
+ - Dict: Ensemble analysis configuration and results
2276
+ - Dict: Advanced signals with 15-minute-specific indicators
2277
+
2278
+ Raises:
2279
+ gr.Error: If market is closed, insufficient data points, or analysis errors
2280
+ 15-minute data requires at least 64 data points and is only available during market hours
2281
+
2282
+ Example:
2283
+ >>> signals, plot, metrics, risk, sector, regime, stress, ensemble, advanced = min15_analysis(
2284
+ ... "AAPL", 1, 3, "chronos", True, True, True, 0.02, "^GSPC", 0.6, 0.2, 0.2
2285
+ ... )
2286
+
2287
+ Notes:
2288
+ - Only available during market hours (9:30 AM - 4:00 PM ET, weekdays)
2289
+ - Maximum prediction period is 2 days (192 15-minute intervals)
2290
+ - Historical data limited to 7 days due to Yahoo Finance constraints
2291
+ - Requires minimum 64 data points for reliable Chronos predictions
2292
+ - Optimized for scalping and very short-term trading strategies
2293
+ - Includes specialized indicators for intraday momentum and volume analysis
2294
+ - Higher transaction costs and slippage considerations for 15-minute strategies
2295
+ - Best suited for highly liquid large-cap stocks with tight bid-ask spreads
2296
  """
2297
  return analyze_stock(s, "15m", pd, ld, st, ue, urd, ust, rfr, mi, cw, tw, sw)
2298