mirror of
https://github.com/Xe138/AI-Trader.git
synced 2026-04-08 11:47:24 -04:00
refactor: migrate trade tools from file-based to SQLite position storage
Complete rewrite of position management in MCP trade tools: **Trade Tools (agent_tools/tool_trade.py)** - Replace file-based position.jsonl reads with SQLite queries - Add get_current_position_from_db() to query positions and holdings tables - Rewrite buy() and sell() to write directly to database - Calculate portfolio value and P&L metrics in tools - Accept job_id and session_id parameters via ContextInjector - Return errors with proper context for debugging - Use deployment-aware database path resolution **Context Injection (agent/context_injector.py)** - Add job_id and session_id to constructor - Inject job_id and session_id into buy/sell tool calls - Support optional parameters (None in standalone mode) **BaseAgent (agent/base_agent/base_agent.py)** - Read JOB_ID from runtime config - Pass job_id to ContextInjector during initialization - Enable automatic context injection for API mode **ModelDayExecutor (api/model_day_executor.py)** - Add _initialize_starting_position() method - Create initial position record before agent runs - Load initial_cash from config - Update context_injector.session_id after session creation - Link positions to sessions automatically **Architecture Changes:** - Eliminates file-based position tracking entirely - Single source of truth: SQLite database - Positions automatically linked to trading sessions - Concurrent execution safe (no file system conflicts) - Deployment mode aware (prod vs dev databases) This completes the migration to database-only position storage. File-based position.jsonl is no longer used or created. Fixes context injection errors in concurrent simulations.
This commit is contained in:
@@ -173,10 +173,15 @@ class BaseAgent:
|
||||
print("⚠️ OpenAI base URL not set, using default")
|
||||
|
||||
try:
|
||||
# Get job_id from runtime config if available (API mode)
|
||||
from tools.general_tools import get_config_value
|
||||
job_id = get_config_value("JOB_ID") # Returns None if not in API mode
|
||||
|
||||
# Create context injector for injecting signature and today_date into tool calls
|
||||
self.context_injector = ContextInjector(
|
||||
signature=self.signature,
|
||||
today_date=self.init_date # Will be updated per trading session
|
||||
today_date=self.init_date, # Will be updated per trading session
|
||||
job_id=job_id # Will be None in standalone mode, populated in API mode
|
||||
)
|
||||
|
||||
# Create MCP client with interceptor
|
||||
|
||||
Reference in New Issue
Block a user