analyze_pattern
Analyze stock chart patterns to predict forward returns. Search historical embeddings for similar setups by symbol and date, retrieve 1/3/5/10-day performance statistics, outcome distributions, and AI-generated summaries.
Instructions
Complete pattern analysis in one call: search + follow-through + AI summary.
This is the recommended tool for most use cases. It combines search_charts,
get_follow_through, and get_pattern_summary into a single call.
Returns matching patterns, forward return statistics (1/3/5/10 day),
outcome distribution, and an AI-written summary.
Args:
query: Symbol + date, e.g. 'AAPL 2024-06-15' or 'TSLA 6/15/24 3d'
timeframe: Session: rth (regular hours), premarket, rth_3d, rth_5d, or auto
top_n: Number of results (1-50)
include_summary: Whether to include AI-generated summary (default True)Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | ||
| timeframe | No | auto | |
| top_n | No | ||
| include_summary | No |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- mcp_server.py:271-297 (handler)Main handler function for the analyze_pattern tool. Decorated with @mcp.tool(), it accepts query, timeframe, top_n, and include_summary parameters. It routes to either HTTP API or direct Python implementation based on configuration.
@mcp.tool() async def analyze_pattern(query: str, timeframe: str = "auto", top_n: int = 10, include_summary: bool = True) -> str: """Complete pattern analysis in one call: search + follow-through + AI summary. This is the recommended tool for most use cases. It combines search_charts, get_follow_through, and get_pattern_summary into a single call. Returns matching patterns, forward return statistics (1/3/5/10 day), outcome distribution, and an AI-written summary. Args: query: Symbol + date, e.g. 'AAPL 2024-06-15' or 'TSLA 6/15/24 3d' timeframe: Session: rth (regular hours), premarket, rth_3d, rth_5d, or auto top_n: Number of results (1-50) include_summary: Whether to include AI-generated summary (default True) """ try: if _use_http(): result = _http_post("/api/v1/analyze", { "query": query, "timeframe": timeframe, "top_n": top_n, "include_summary": include_summary, }) else: result = _direct_analyze(query, timeframe, top_n, include_summary) return json.dumps(result, default=str, indent=2) except Exception as e: return json.dumps({"error": str(e)}) - mcp_server.py:133-180 (helper)Helper function _direct_analyze that implements the core logic for analyze_pattern when using direct Python imports (non-HTTP mode). It performs search, computes follow-through statistics, generates outcome distribution, and optionally gets AI summary.
def _direct_analyze(query: str, timeframe: str = "auto", top_n: int = 10, include_summary: bool = True) -> dict: """Run combined analysis directly via Python imports.""" from dotenv import load_dotenv load_dotenv() # Search search_result = _direct_search(query, timeframe, top_n) if "error" in search_result: return search_result results = search_result.get("results", []) if not results: return {**search_result, "follow_through": None, "outcome_distribution": None, "summary": None} # Follow-through ft = _direct_follow_through(results) # Outcome distribution rets_5d = ft.get("horizon_returns", {}).get(5, []) outcome_dist = None if rets_5d: clean = [r for r in rets_5d if r is not None] if clean: up = sum(1 for r in clean if r > 0) outcome_dist = { "up_count": up, "down_count": len(clean) - up, "total": len(clean), "median_return": round(sorted(clean)[len(clean) // 2], 2), } # Summary summary_text = None if include_summary: try: q = search_result["query"] label = f"{q['symbol']} {q['date']}" summary_result = _direct_summary(label, len(results), ft.get("horizon_returns", {})) summary_text = summary_result.get("summary") except Exception: pass return { **search_result, "follow_through": ft, "outcome_distribution": outcome_dist, "summary": summary_text, }