mirror of
https://github.com/Xe138/AI-Trader.git
synced 2026-04-09 12:17:24 -04:00
Compare commits
6 Commits
v0.3.0-alp
...
v0.3.0-alp
| Author | SHA1 | Date | |
|---|---|---|---|
| 396a2747d3 | |||
| 71ec53db45 | |||
| b6bd949d23 | |||
| b5f18ac0f3 | |||
| 90b6ad400d | |||
| 6e4b2a4cc5 |
@@ -244,35 +244,24 @@ def initialize_dev_database(db_path: str = "data/trading_dev.db") -> None:
|
|||||||
Args:
|
Args:
|
||||||
db_path: Path to dev database file
|
db_path: Path to dev database file
|
||||||
"""
|
"""
|
||||||
print(f"🔍 DIAGNOSTIC: initialize_dev_database() CALLED with db_path={db_path}")
|
|
||||||
from tools.deployment_config import should_preserve_dev_data
|
from tools.deployment_config import should_preserve_dev_data
|
||||||
|
|
||||||
preserve = should_preserve_dev_data()
|
if should_preserve_dev_data():
|
||||||
print(f"🔍 DIAGNOSTIC: should_preserve_dev_data() returned: {preserve}")
|
|
||||||
|
|
||||||
if preserve:
|
|
||||||
print(f"ℹ️ PRESERVE_DEV_DATA=true, keeping existing dev database: {db_path}")
|
print(f"ℹ️ PRESERVE_DEV_DATA=true, keeping existing dev database: {db_path}")
|
||||||
# Ensure schema exists even if preserving data
|
# Ensure schema exists even if preserving data
|
||||||
db_exists = Path(db_path).exists()
|
if not Path(db_path).exists():
|
||||||
print(f"🔍 DIAGNOSTIC: Database exists check: {db_exists}")
|
|
||||||
if not db_exists:
|
|
||||||
print(f"📁 Dev database doesn't exist, creating: {db_path}")
|
print(f"📁 Dev database doesn't exist, creating: {db_path}")
|
||||||
initialize_database(db_path)
|
initialize_database(db_path)
|
||||||
print(f"🔍 DIAGNOSTIC: initialize_dev_database() RETURNING (preserve mode)")
|
|
||||||
return
|
return
|
||||||
|
|
||||||
# Delete existing dev database
|
# Delete existing dev database
|
||||||
db_exists = Path(db_path).exists()
|
if Path(db_path).exists():
|
||||||
print(f"🔍 DIAGNOSTIC: Database exists (before deletion): {db_exists}")
|
|
||||||
if db_exists:
|
|
||||||
print(f"🗑️ Removing existing dev database: {db_path}")
|
print(f"🗑️ Removing existing dev database: {db_path}")
|
||||||
Path(db_path).unlink()
|
Path(db_path).unlink()
|
||||||
print(f"🔍 DIAGNOSTIC: Database deleted successfully")
|
|
||||||
|
|
||||||
# Create fresh dev database
|
# Create fresh dev database
|
||||||
print(f"📁 Creating fresh dev database: {db_path}")
|
print(f"📁 Creating fresh dev database: {db_path}")
|
||||||
initialize_database(db_path)
|
initialize_database(db_path)
|
||||||
print(f"🔍 DIAGNOSTIC: initialize_dev_database() COMPLETED successfully")
|
|
||||||
|
|
||||||
|
|
||||||
def cleanup_dev_database(db_path: str = "data/trading_dev.db", data_path: str = "./data/dev_agent_data") -> None:
|
def cleanup_dev_database(db_path: str = "data/trading_dev.db", data_path: str = "./data/dev_agent_data") -> None:
|
||||||
|
|||||||
43
api/main.py
43
api/main.py
@@ -116,7 +116,7 @@ class HealthResponse(BaseModel):
|
|||||||
|
|
||||||
def create_app(
|
def create_app(
|
||||||
db_path: str = "data/jobs.db",
|
db_path: str = "data/jobs.db",
|
||||||
config_path: str = "configs/default_config.json"
|
config_path: str = "/tmp/runtime_config.json" if Path("/tmp/runtime_config.json").exists() else "configs/default_config.json"
|
||||||
) -> FastAPI:
|
) -> FastAPI:
|
||||||
"""
|
"""
|
||||||
Create FastAPI application instance.
|
Create FastAPI application instance.
|
||||||
@@ -131,49 +131,31 @@ def create_app(
|
|||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def lifespan(app: FastAPI):
|
async def lifespan(app: FastAPI):
|
||||||
"""Initialize database on startup, cleanup on shutdown if needed"""
|
"""Initialize database on startup, cleanup on shutdown if needed"""
|
||||||
print("=" * 80)
|
|
||||||
print("🔍 DIAGNOSTIC: LIFESPAN FUNCTION CALLED!")
|
|
||||||
print("=" * 80)
|
|
||||||
|
|
||||||
from tools.deployment_config import is_dev_mode, get_db_path
|
from tools.deployment_config import is_dev_mode, get_db_path
|
||||||
from api.database import initialize_dev_database, initialize_database
|
from api.database import initialize_dev_database, initialize_database
|
||||||
|
|
||||||
# Startup - use closure to access db_path from create_app scope
|
# Startup - use closure to access db_path from create_app scope
|
||||||
logger.info("🚀 FastAPI application starting...")
|
logger.info("🚀 FastAPI application starting...")
|
||||||
logger.info("📊 Initializing database...")
|
logger.info("📊 Initializing database...")
|
||||||
print(f"🔍 DIAGNOSTIC: Lifespan - db_path from closure: {db_path}")
|
|
||||||
|
|
||||||
deployment_mode = is_dev_mode()
|
if is_dev_mode():
|
||||||
print(f"🔍 DIAGNOSTIC: Lifespan - is_dev_mode() returned: {deployment_mode}")
|
|
||||||
|
|
||||||
if deployment_mode:
|
|
||||||
# Initialize dev database (reset unless PRESERVE_DEV_DATA=true)
|
# Initialize dev database (reset unless PRESERVE_DEV_DATA=true)
|
||||||
logger.info(" 🔧 DEV mode detected - initializing dev database")
|
logger.info(" 🔧 DEV mode detected - initializing dev database")
|
||||||
print("🔍 DIAGNOSTIC: Lifespan - DEV mode detected")
|
|
||||||
dev_db_path = get_db_path(db_path)
|
dev_db_path = get_db_path(db_path)
|
||||||
print(f"🔍 DIAGNOSTIC: Lifespan - Resolved dev database path: {dev_db_path}")
|
|
||||||
print(f"🔍 DIAGNOSTIC: Lifespan - About to call initialize_dev_database({dev_db_path})")
|
|
||||||
initialize_dev_database(dev_db_path)
|
initialize_dev_database(dev_db_path)
|
||||||
print(f"🔍 DIAGNOSTIC: Lifespan - initialize_dev_database() completed")
|
|
||||||
log_dev_mode_startup_warning()
|
log_dev_mode_startup_warning()
|
||||||
else:
|
else:
|
||||||
# Ensure production database schema exists
|
# Ensure production database schema exists
|
||||||
logger.info(" 🏭 PROD mode - ensuring database schema exists")
|
logger.info(" 🏭 PROD mode - ensuring database schema exists")
|
||||||
print("🔍 DIAGNOSTIC: Lifespan - PROD mode detected")
|
|
||||||
print(f"🔍 DIAGNOSTIC: Lifespan - About to call initialize_database({db_path})")
|
|
||||||
initialize_database(db_path)
|
initialize_database(db_path)
|
||||||
print(f"🔍 DIAGNOSTIC: Lifespan - initialize_database() completed")
|
|
||||||
|
|
||||||
logger.info("✅ Database initialized")
|
logger.info("✅ Database initialized")
|
||||||
logger.info("🌐 API server ready to accept requests")
|
logger.info("🌐 API server ready to accept requests")
|
||||||
print("🔍 DIAGNOSTIC: Lifespan - Startup complete, yielding control")
|
|
||||||
print("=" * 80)
|
|
||||||
|
|
||||||
yield
|
yield
|
||||||
|
|
||||||
# Shutdown (if needed in future)
|
# Shutdown (if needed in future)
|
||||||
logger.info("🛑 FastAPI application shutting down...")
|
logger.info("🛑 FastAPI application shutting down...")
|
||||||
print("🔍 DIAGNOSTIC: LIFESPAN SHUTDOWN CALLED")
|
|
||||||
|
|
||||||
app = FastAPI(
|
app = FastAPI(
|
||||||
title="AI-Trader Simulation API",
|
title="AI-Trader Simulation API",
|
||||||
@@ -538,45 +520,26 @@ def create_app(
|
|||||||
|
|
||||||
|
|
||||||
# Create default app instance
|
# Create default app instance
|
||||||
print("=" * 80)
|
|
||||||
print("🔍 DIAGNOSTIC: Module api.main is being imported/executed")
|
|
||||||
print("=" * 80)
|
|
||||||
|
|
||||||
app = create_app()
|
app = create_app()
|
||||||
print(f"🔍 DIAGNOSTIC: create_app() completed, app object created: {app}")
|
|
||||||
|
|
||||||
# Ensure database is initialized when module is loaded
|
# Ensure database is initialized when module is loaded
|
||||||
# This handles cases where lifespan might not be triggered properly
|
# This handles cases where lifespan might not be triggered properly
|
||||||
print("🔍 DIAGNOSTIC: Starting module-level database initialization check...")
|
|
||||||
logger.info("🔧 Module-level database initialization check...")
|
logger.info("🔧 Module-level database initialization check...")
|
||||||
|
|
||||||
from tools.deployment_config import is_dev_mode, get_db_path
|
from tools.deployment_config import is_dev_mode, get_db_path
|
||||||
from api.database import initialize_dev_database, initialize_database
|
from api.database import initialize_dev_database, initialize_database
|
||||||
|
|
||||||
_db_path = app.state.db_path
|
_db_path = app.state.db_path
|
||||||
print(f"🔍 DIAGNOSTIC: app.state.db_path = {_db_path}")
|
|
||||||
|
|
||||||
deployment_mode = is_dev_mode()
|
if is_dev_mode():
|
||||||
print(f"🔍 DIAGNOSTIC: is_dev_mode() returned: {deployment_mode}")
|
|
||||||
|
|
||||||
if deployment_mode:
|
|
||||||
print("🔍 DIAGNOSTIC: DEV mode detected - initializing dev database at module load")
|
|
||||||
logger.info(" 🔧 DEV mode - initializing dev database at module load")
|
logger.info(" 🔧 DEV mode - initializing dev database at module load")
|
||||||
_dev_db_path = get_db_path(_db_path)
|
_dev_db_path = get_db_path(_db_path)
|
||||||
print(f"🔍 DIAGNOSTIC: Resolved dev database path: {_dev_db_path}")
|
|
||||||
print(f"🔍 DIAGNOSTIC: About to call initialize_dev_database({_dev_db_path})")
|
|
||||||
initialize_dev_database(_dev_db_path)
|
initialize_dev_database(_dev_db_path)
|
||||||
print(f"🔍 DIAGNOSTIC: initialize_dev_database() completed successfully")
|
|
||||||
else:
|
else:
|
||||||
print("🔍 DIAGNOSTIC: PROD mode - ensuring database exists at module load")
|
|
||||||
logger.info(" 🏭 PROD mode - ensuring database exists at module load")
|
logger.info(" 🏭 PROD mode - ensuring database exists at module load")
|
||||||
print(f"🔍 DIAGNOSTIC: About to call initialize_database({_db_path})")
|
|
||||||
initialize_database(_db_path)
|
initialize_database(_db_path)
|
||||||
print(f"🔍 DIAGNOSTIC: initialize_database() completed successfully")
|
|
||||||
|
|
||||||
print("🔍 DIAGNOSTIC: Module-level database initialization complete")
|
|
||||||
logger.info("✅ Module-level database initialization complete")
|
logger.info("✅ Module-level database initialization complete")
|
||||||
print("=" * 80)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ This module provides:
|
|||||||
|
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
import asyncio
|
||||||
from typing import Dict, Any, Optional, List, TYPE_CHECKING
|
from typing import Dict, Any, Optional, List, TYPE_CHECKING
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
@@ -118,7 +119,7 @@ class ModelDayExecutor:
|
|||||||
|
|
||||||
# Run trading session
|
# Run trading session
|
||||||
logger.info(f"Running trading session for {self.model_sig} on {self.date}")
|
logger.info(f"Running trading session for {self.model_sig} on {self.date}")
|
||||||
session_result = agent.run_trading_session(self.date)
|
session_result = asyncio.run(agent.run_trading_session(self.date))
|
||||||
|
|
||||||
# Persist results to SQLite
|
# Persist results to SQLite
|
||||||
self._write_results_to_db(agent, session_result)
|
self._write_results_to_db(agent, session_result)
|
||||||
@@ -211,8 +212,10 @@ class ModelDayExecutor:
|
|||||||
init_date=config.get("date_range", {}).get("init_date", "2025-10-13")
|
init_date=config.get("date_range", {}).get("init_date", "2025-10-13")
|
||||||
)
|
)
|
||||||
|
|
||||||
# Register agent (creates initial position if needed)
|
# Note: In API mode, we don't call register_agent() because:
|
||||||
agent.register_agent()
|
# - Position data is stored in SQLite database, not files
|
||||||
|
# - Database initialization is handled by JobManager
|
||||||
|
# - File-based position tracking is only for standalone/CLI mode
|
||||||
|
|
||||||
return agent
|
return agent
|
||||||
|
|
||||||
|
|||||||
@@ -6,30 +6,28 @@
|
|||||||
},
|
},
|
||||||
"models": [
|
"models": [
|
||||||
{
|
{
|
||||||
"name": "claude-3.7-sonnet",
|
"name": "claude-sonnet-4-5",
|
||||||
"basemodel": "anthropic/claude-3.7-sonnet",
|
"basemodel": "anthropic/claude-sonnet-4.5",
|
||||||
"signature": "claude-3.7-sonnet",
|
"signature": "claude-sonnet-4.5",
|
||||||
"enabled": false,
|
"enabled": true
|
||||||
"openai_base_url": "Optional: YOUR_OPENAI_BASE_URL,you can write them in .env file",
|
|
||||||
"openai_api_key": "Optional: YOUR_OPENAI_API_KEY,you can write them in .env file"
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "deepseek-chat-v3.1",
|
"name": "deepseek-chat-v3.1",
|
||||||
"basemodel": "deepseek/deepseek-chat-v3.1",
|
"basemodel": "deepseek/deepseek-chat-v3.1",
|
||||||
"signature": "deepseek-chat-v3.1",
|
"signature": "deepseek-chat-v3.1",
|
||||||
"enabled": false
|
"enabled": true
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "qwen3-max",
|
"name": "qwen3-max",
|
||||||
"basemodel": "qwen/qwen3-max",
|
"basemodel": "qwen/qwen3-max",
|
||||||
"signature": "qwen3-max",
|
"signature": "qwen3-max",
|
||||||
"enabled": false
|
"enabled": true
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "gemini-2.5-flash",
|
"name": "gemini-2.5-flash",
|
||||||
"basemodel": "google/gemini-2.5-flash",
|
"basemodel": "google/gemini-2.5-flash",
|
||||||
"signature": "gemini-2.5-flash",
|
"signature": "gemini-2.5-flash",
|
||||||
"enabled": false
|
"enabled": true
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "gpt-5",
|
"name": "gpt-5",
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ Tests verify:
|
|||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
import json
|
import json
|
||||||
from unittest.mock import Mock, patch, MagicMock
|
from unittest.mock import Mock, patch, MagicMock, AsyncMock
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
|
||||||
@@ -29,7 +29,8 @@ def create_mock_agent(positions=None, last_trade=None, current_prices=None,
|
|||||||
mock_agent.get_current_prices.return_value = current_prices or {}
|
mock_agent.get_current_prices.return_value = current_prices or {}
|
||||||
mock_agent.get_reasoning_steps.return_value = reasoning_steps or []
|
mock_agent.get_reasoning_steps.return_value = reasoning_steps or []
|
||||||
mock_agent.get_tool_usage.return_value = tool_usage or {}
|
mock_agent.get_tool_usage.return_value = tool_usage or {}
|
||||||
mock_agent.run_trading_session.return_value = session_result or {"success": True}
|
# run_trading_session is async, so use AsyncMock
|
||||||
|
mock_agent.run_trading_session = AsyncMock(return_value=session_result or {"success": True})
|
||||||
|
|
||||||
return mock_agent
|
return mock_agent
|
||||||
|
|
||||||
|
|||||||
@@ -69,8 +69,16 @@ def get_db_path(base_db_path: str) -> str:
|
|||||||
Example:
|
Example:
|
||||||
PROD: "data/trading.db" -> "data/trading.db"
|
PROD: "data/trading.db" -> "data/trading.db"
|
||||||
DEV: "data/trading.db" -> "data/trading_dev.db"
|
DEV: "data/trading.db" -> "data/trading_dev.db"
|
||||||
|
|
||||||
|
Note:
|
||||||
|
This function is idempotent - calling it multiple times on the same
|
||||||
|
path will not add multiple _dev suffixes.
|
||||||
"""
|
"""
|
||||||
if is_dev_mode():
|
if is_dev_mode():
|
||||||
|
# Check if already has _dev suffix (idempotent)
|
||||||
|
if "_dev.db" in base_db_path:
|
||||||
|
return base_db_path
|
||||||
|
|
||||||
# Insert _dev before .db extension
|
# Insert _dev before .db extension
|
||||||
if base_db_path.endswith(".db"):
|
if base_db_path.endswith(".db"):
|
||||||
return base_db_path[:-3] + "_dev.db"
|
return base_db_path[:-3] + "_dev.db"
|
||||||
|
|||||||
Reference in New Issue
Block a user