refactor: implement database-only position tracking with lazy context injection

This commit migrates the system to database-only position storage,
eliminating file-based position.jsonl dependencies and fixing
ContextInjector initialization timing issues.

Key Changes:

1. ContextInjector Lifecycle Refactor:
   - Remove ContextInjector creation from BaseAgent.__init__()
   - Add BaseAgent.set_context() method for post-initialization injection
   - Update ModelDayExecutor to create ContextInjector with correct trading day date
   - Ensures ContextInjector receives actual trading date instead of init_date
   - Includes session_id injection for proper database linking

2. Database Position Functions:
   - Implement get_today_init_position_from_db() for querying previous positions
   - Implement add_no_trade_record_to_db() for no-trade day handling
   - Both functions query SQLite directly (positions + holdings tables)
   - Handle first trading day case with initial cash return
   - Include comprehensive error handling and logging

3. System Integration:
   - Update get_agent_system_prompt() to use database queries
   - Update _handle_trading_result() to write no-trade records to database
   - Remove dependencies on position.jsonl file reading/writing
   - Use deployment_config for automatic prod/dev database resolution

Data Flow:
- ModelDayExecutor creates runtime config and trading session
- Agent initialized without context
- ContextInjector created with (signature, date, job_id, session_id)
- Context injected via set_context()
- System prompt queries database for yesterday's position
- Trade tools write directly to database
- No-trade handler creates database records

Fixes:
- ContextInjector no longer receives None values
- No FileNotFoundError for missing position.jsonl files
- Database is single source of truth for position tracking
- Session linking maintained across all position records

Design: docs/plans/2025-02-11-database-position-tracking-design.md
This commit is contained in:
2025-11-02 22:20:01 -05:00
parent 7a734d265b
commit 027b4bd8e4
4 changed files with 239 additions and 26 deletions

View File

@@ -68,14 +68,24 @@ When you think your task is complete, output
def get_agent_system_prompt(today_date: str, signature: str) -> str:
print(f"signature: {signature}")
print(f"today_date: {today_date}")
# Get job_id from runtime config
job_id = get_config_value("JOB_ID")
if not job_id:
raise ValueError("JOB_ID not found in runtime config")
# Query database for yesterday's position
from tools.price_tools import get_today_init_position_from_db
today_init_position = get_today_init_position_from_db(today_date, signature, job_id)
# Get yesterday's buy and sell prices
yesterday_buy_prices, yesterday_sell_prices = get_yesterday_open_and_close_price(today_date, all_nasdaq_100_symbols)
today_buy_price = get_open_prices(today_date, all_nasdaq_100_symbols)
today_init_position = get_today_init_position(today_date, signature)
yesterday_profit = get_yesterday_profit(today_date, yesterday_buy_prices, yesterday_sell_prices, today_init_position)
return agent_system_prompt.format(
date=today_date,
positions=today_init_position,
date=today_date,
positions=today_init_position,
STOP_SIGNAL=STOP_SIGNAL,
yesterday_close_price=yesterday_sell_prices,
today_buy_price=today_buy_price,