Compare commits

..

3 Commits

Author SHA1 Message Date
05620facc2 fix: update context_injector with trading_day_id after creation
Changes:
- Update context_injector.trading_day_id after trading_day record is created

Root Cause:
- ContextInjector was created before trading_day record existed
- trading_day_id was None when context_injector was initialized
- Even though trading_day_id was written to runtime config, the
  context_injector's attribute was never updated
- MCP tools use the injected trading_day_id parameter, not runtime config

Flow:
1. ModelDayExecutor creates ContextInjector (trading_day_id=None)
2. Agent.run_trading_session() creates trading_day record
3. NEW: Update context_injector.trading_day_id = trading_day_id
4. MCP tools receive trading_day_id via context injection

Impact:
- Fixes: "Trade failed: trading_day_id not found in runtime config"
- Trading tools (buy/sell) can now record actions properly
- Actions are linked to correct trading_day record

Related: agent/base_agent/base_agent.py:541-543
2025-11-04 23:04:47 -05:00
7c71a047bc fix: update position queries to use new trading_days schema
Changes:
- Update get_today_init_position_from_db to query trading_days table
- Remove obsolete add_no_trade_record_to_db calls from BaseAgent
- Simplify _handle_trading_result (trading_day record handles both scenarios)

Root Cause:
- Code was still querying old positions table after schema migration
- The add_no_trade_record_to_db function is obsolete in new schema

New Schema Behavior:
- trading_day record created at session start (regardless of trading)
- trading_day record updated at session end with final results
- No separate "no-trade" record needed

Impact:
- Fixes: "no such table: positions" error in get_today_init_position_from_db
- Removes unnecessary database writes for no-trade scenarios
- Simplifies codebase by removing obsolete function calls

Related: tools/price_tools.py:340-364, agent/base_agent/base_agent.py:661-673
2025-11-04 22:49:01 -05:00
9da65c2d53 fix: correct Database default path to match system-wide db_path
Changes:
- Change Database.__init__ default from "data/trading.db" to "data/jobs.db"

Root Cause:
- Job creation uses "data/jobs.db" (via JobManager, SimulationWorker)
- BaseAgent's Database() was using "data/trading.db" by default
- This caused jobs table to exist in jobs.db but trading_days INSERT
  tried to reference job_id from trading.db, causing FK constraint failure

Impact:
- Fixes: "FOREIGN KEY constraint failed" when creating trading_day records
- Ensures all components use same database file for referential integrity
- Maintains DEV/PROD mode isolation via get_db_path()

Related: api/database.py:521
2025-11-04 22:39:36 -05:00
3 changed files with 17 additions and 28 deletions

View File

@@ -538,6 +538,10 @@ Summary:"""
from tools.general_tools import write_config_value
write_config_value('TRADING_DAY_ID', trading_day_id)
# Update context_injector with trading_day_id for MCP tools
if self.context_injector:
self.context_injector.trading_day_id = trading_day_id
# 6. Run AI trading session
action_count = 0
@@ -660,8 +664,6 @@ Summary:"""
async def _handle_trading_result(self, today_date: str) -> None:
"""Handle trading results with database writes."""
from tools.price_tools import add_no_trade_record_to_db
if_trade = get_config_value("IF_TRADE")
if if_trade:
@@ -669,23 +671,10 @@ Summary:"""
print("✅ Trading completed")
else:
print("📊 No trading, maintaining positions")
# Get context from runtime config
job_id = get_config_value("JOB_ID")
session_id = self.context_injector.session_id if self.context_injector else None
if not job_id or not session_id:
raise ValueError("Missing JOB_ID or session_id for no-trade record")
# Write no-trade record to database
add_no_trade_record_to_db(
today_date,
self.signature,
job_id,
session_id
)
write_config_value("IF_TRADE", False)
# Note: In new schema, trading_day record is created at session start
# and updated at session end, so no separate no-trade record needed
def register_agent(self) -> None:
"""Register new agent, create initial positions"""

View File

@@ -518,7 +518,7 @@ class Database:
"""
if db_path is None:
from tools.deployment_config import get_db_path
db_path = get_db_path("data/trading.db")
db_path = get_db_path("data/jobs.db")
self.db_path = db_path
self.connection = sqlite3.connect(db_path, check_same_thread=False)

View File

@@ -337,12 +337,12 @@ def get_today_init_position_from_db(
cursor = conn.cursor()
try:
# Get most recent position before today
# Get most recent trading day before today
cursor.execute("""
SELECT p.id, p.cash
FROM positions p
WHERE p.job_id = ? AND p.model = ? AND p.date < ?
ORDER BY p.date DESC, p.action_id DESC
SELECT id, ending_cash
FROM trading_days
WHERE job_id = ? AND model = ? AND date < ?
ORDER BY date DESC
LIMIT 1
""", (job_id, modelname, today_date))
@@ -353,15 +353,15 @@ def get_today_init_position_from_db(
logger.info(f"No previous position found for {modelname}, returning initial cash")
return {"CASH": 10000.0}
position_id, cash = row
trading_day_id, cash = row
position_dict = {"CASH": cash}
# Get holdings for this position
# Get holdings for this trading day
cursor.execute("""
SELECT symbol, quantity
FROM holdings
WHERE position_id = ?
""", (position_id,))
WHERE trading_day_id = ?
""", (trading_day_id,))
for symbol, quantity in cursor.fetchall():
position_dict[symbol] = quantity