mirror of
https://github.com/Xe138/AI-Trader.git
synced 2026-04-01 17:17:24 -04:00
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
This commit is contained in:
@@ -660,8 +660,6 @@ Summary:"""
|
|||||||
|
|
||||||
async def _handle_trading_result(self, today_date: str) -> None:
|
async def _handle_trading_result(self, today_date: str) -> None:
|
||||||
"""Handle trading results with database writes."""
|
"""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_trade = get_config_value("IF_TRADE")
|
||||||
|
|
||||||
if if_trade:
|
if if_trade:
|
||||||
@@ -669,24 +667,11 @@ Summary:"""
|
|||||||
print("✅ Trading completed")
|
print("✅ Trading completed")
|
||||||
else:
|
else:
|
||||||
print("📊 No trading, maintaining positions")
|
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)
|
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:
|
def register_agent(self) -> None:
|
||||||
"""Register new agent, create initial positions"""
|
"""Register new agent, create initial positions"""
|
||||||
# Check if position.jsonl file already exists
|
# Check if position.jsonl file already exists
|
||||||
|
|||||||
@@ -337,12 +337,12 @@ def get_today_init_position_from_db(
|
|||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Get most recent position before today
|
# Get most recent trading day before today
|
||||||
cursor.execute("""
|
cursor.execute("""
|
||||||
SELECT p.id, p.cash
|
SELECT id, ending_cash
|
||||||
FROM positions p
|
FROM trading_days
|
||||||
WHERE p.job_id = ? AND p.model = ? AND p.date < ?
|
WHERE job_id = ? AND model = ? AND date < ?
|
||||||
ORDER BY p.date DESC, p.action_id DESC
|
ORDER BY date DESC
|
||||||
LIMIT 1
|
LIMIT 1
|
||||||
""", (job_id, modelname, today_date))
|
""", (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")
|
logger.info(f"No previous position found for {modelname}, returning initial cash")
|
||||||
return {"CASH": 10000.0}
|
return {"CASH": 10000.0}
|
||||||
|
|
||||||
position_id, cash = row
|
trading_day_id, cash = row
|
||||||
position_dict = {"CASH": cash}
|
position_dict = {"CASH": cash}
|
||||||
|
|
||||||
# Get holdings for this position
|
# Get holdings for this trading day
|
||||||
cursor.execute("""
|
cursor.execute("""
|
||||||
SELECT symbol, quantity
|
SELECT symbol, quantity
|
||||||
FROM holdings
|
FROM holdings
|
||||||
WHERE position_id = ?
|
WHERE trading_day_id = ?
|
||||||
""", (position_id,))
|
""", (trading_day_id,))
|
||||||
|
|
||||||
for symbol, quantity in cursor.fetchall():
|
for symbol, quantity in cursor.fetchall():
|
||||||
position_dict[symbol] = quantity
|
position_dict[symbol] = quantity
|
||||||
|
|||||||
Reference in New Issue
Block a user