mirror of
https://github.com/Xe138/AI-Trader.git
synced 2026-04-01 17:17:24 -04:00
fix: enable cross-job portfolio continuity in get_previous_trading_day
Remove job_id filter from get_previous_trading_day() SQL query to enable portfolio continuity across jobs. Previously, new jobs would reset to initial $10,000 cash instead of continuing from previous job's ending position. Root cause: get_previous_trading_day() filtered by job_id, while get_current_position_from_db() correctly queries across all jobs. This inconsistency caused starting_cash to default to initial_cash when no previous day was found within the same job. Changes: - api/database.py: Remove job_id filter from SQL WHERE clause - tests/unit/test_database_helpers.py: Add test for cross-job continuity Fixes position tracking bug where subsequent jobs on consecutive dates would not recognize previous day's holdings from different job.
This commit is contained in:
@@ -611,6 +611,10 @@ class Database:
|
|||||||
|
|
||||||
Handles weekends/holidays by finding actual previous trading day.
|
Handles weekends/holidays by finding actual previous trading day.
|
||||||
|
|
||||||
|
NOTE: Queries across ALL jobs for the given model to enable portfolio
|
||||||
|
continuity even when new jobs are created with overlapping date ranges.
|
||||||
|
The job_id parameter is kept for API compatibility but not used in the query.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
dict with keys: id, date, ending_cash, ending_portfolio_value
|
dict with keys: id, date, ending_cash, ending_portfolio_value
|
||||||
or None if no previous day exists
|
or None if no previous day exists
|
||||||
@@ -619,11 +623,11 @@ class Database:
|
|||||||
"""
|
"""
|
||||||
SELECT id, date, ending_cash, ending_portfolio_value
|
SELECT id, date, ending_cash, ending_portfolio_value
|
||||||
FROM trading_days
|
FROM trading_days
|
||||||
WHERE job_id = ? AND model = ? AND date < ?
|
WHERE model = ? AND date < ?
|
||||||
ORDER BY date DESC
|
ORDER BY date DESC
|
||||||
LIMIT 1
|
LIMIT 1
|
||||||
""",
|
""",
|
||||||
(job_id, model, current_date)
|
(model, current_date)
|
||||||
)
|
)
|
||||||
|
|
||||||
row = cursor.fetchone()
|
row = cursor.fetchone()
|
||||||
|
|||||||
@@ -130,6 +130,44 @@ class TestDatabaseHelpers:
|
|||||||
assert previous is not None
|
assert previous is not None
|
||||||
assert previous["date"] == "2025-01-17"
|
assert previous["date"] == "2025-01-17"
|
||||||
|
|
||||||
|
def test_get_previous_trading_day_across_jobs(self, db):
|
||||||
|
"""Test retrieving previous trading day from different job (cross-job continuity)."""
|
||||||
|
# Setup: Create two jobs
|
||||||
|
db.connection.execute(
|
||||||
|
"INSERT INTO jobs (job_id, status, config_path, date_range, models, created_at) VALUES (?, ?, ?, ?, ?, ?)",
|
||||||
|
("job-1", "completed", "config.json", "2025-10-07,2025-10-07", "deepseek-chat-v3.1", "2025-11-07T00:00:00Z")
|
||||||
|
)
|
||||||
|
db.connection.execute(
|
||||||
|
"INSERT INTO jobs (job_id, status, config_path, date_range, models, created_at) VALUES (?, ?, ?, ?, ?, ?)",
|
||||||
|
("job-2", "running", "config.json", "2025-10-08,2025-10-08", "deepseek-chat-v3.1", "2025-11-07T01:00:00Z")
|
||||||
|
)
|
||||||
|
|
||||||
|
# Day 1 in job-1
|
||||||
|
db.create_trading_day(
|
||||||
|
job_id="job-1",
|
||||||
|
model="deepseek-chat-v3.1",
|
||||||
|
date="2025-10-07",
|
||||||
|
starting_cash=10000.0,
|
||||||
|
starting_portfolio_value=10000.0,
|
||||||
|
daily_profit=214.58,
|
||||||
|
daily_return_pct=2.15,
|
||||||
|
ending_cash=123.59,
|
||||||
|
ending_portfolio_value=10214.58
|
||||||
|
)
|
||||||
|
|
||||||
|
# Test: Get previous day from job-2 on next date
|
||||||
|
# Should find job-1's record (cross-job continuity)
|
||||||
|
previous = db.get_previous_trading_day(
|
||||||
|
job_id="job-2",
|
||||||
|
model="deepseek-chat-v3.1",
|
||||||
|
current_date="2025-10-08"
|
||||||
|
)
|
||||||
|
|
||||||
|
assert previous is not None
|
||||||
|
assert previous["date"] == "2025-10-07"
|
||||||
|
assert previous["ending_cash"] == 123.59
|
||||||
|
assert previous["ending_portfolio_value"] == 10214.58
|
||||||
|
|
||||||
def test_get_ending_holdings(self, db):
|
def test_get_ending_holdings(self, db):
|
||||||
"""Test retrieving ending holdings for a trading day."""
|
"""Test retrieving ending holdings for a trading day."""
|
||||||
db.connection.execute(
|
db.connection.execute(
|
||||||
|
|||||||
Reference in New Issue
Block a user