Implementation:24mlight A Share investment Agent execute stock analysis
| Knowledge Sources | |
|---|---|
| Domains | Backend_Engineering, Finance |
| Last Updated | 2026-10-10 14:00 GMT |
Overview
Concrete service function for executing stock analysis in a background thread, calling run_hedge_fund within a workflow_run context manager with error handling, provided by the A_Share_investment_Agent repository.
Description
The execute_stock_analysis function runs in a ThreadPoolExecutor thread. It initializes the portfolio from the request, calls run_hedge_fund within a workflow_run(run_id) context manager (which registers and completes the run in api_state), and returns the result. On error, it marks the run as "error" in api_state and re-raises.
Usage
Import when you need to:
- Execute stock analysis asynchronously from the API backend
- Run analysis with run tracking and error handling
Code Reference
Source Location
- Repository: A_Share_investment_Agent
- File: backend/services/analysis.py
- Lines: L20-82
Signature
def execute_stock_analysis(request: StockAnalysisRequest, run_id: str) -> Dict[str, Any]:
"""Execute stock analysis task in background."""
Import
from backend.services import execute_stock_analysis
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| request | StockAnalysisRequest | Yes | Pydantic model with ticker, show_reasoning, num_of_news, initial_capital, initial_position |
| run_id | str | Yes | Unique run identifier |
Outputs
| Name | Type | Description |
|---|---|---|
| return | Dict[str, Any] | Result from run_hedge_fund (last message or full state) |
Usage Examples
from backend.services import execute_stock_analysis
from backend.models.api_models import StockAnalysisRequest
request = StockAnalysisRequest(
ticker="002848",
show_reasoning=True,
num_of_news=5,
initial_capital=100000.0,
initial_position=0,
)
# Runs in thread pool via api_state._executor.submit()
result = execute_stock_analysis(request, run_id="uuid-...")