mirror of
https://github.com/Xe138/AI-Trader.git
synced 2026-04-09 12:17:24 -04:00
Compare commits
9 Commits
v0.3.0-alp
...
v0.3.0-alp
| Author | SHA1 | Date | |
|---|---|---|---|
| fcfdf36c1c | |||
| 019c84fca8 | |||
| 8521f685c7 | |||
| bf12e981fe | |||
| a16bac5d08 | |||
| 81b92e293a | |||
| b1b486dcc4 | |||
| 1bdfefae35 | |||
| dbd8f0141c |
@@ -35,7 +35,7 @@ MAX_SIMULATION_DAYS=30
|
|||||||
AUTO_DOWNLOAD_PRICE_DATA=true
|
AUTO_DOWNLOAD_PRICE_DATA=true
|
||||||
|
|
||||||
# Data Volume Configuration
|
# Data Volume Configuration
|
||||||
# Base directory for all persistent data (will contain data/, logs/, configs/ subdirectories)
|
# Base directory for all persistent data (will contain data/ and configs/ subdirectories)
|
||||||
# Use relative paths (./volumes) or absolute paths (/home/user/ai-trader-volumes)
|
# Use relative paths (./volumes) or absolute paths (/home/user/ai-trader-volumes)
|
||||||
# Defaults to current directory (.) if not set
|
# Defaults to current directory (.) if not set
|
||||||
VOLUME_PATH=.
|
VOLUME_PATH=.
|
||||||
|
|||||||
30
DOCKER.md
30
DOCKER.md
@@ -154,10 +154,9 @@ docker-compose up
|
|||||||
|
|
||||||
### Volume Mounts
|
### Volume Mounts
|
||||||
|
|
||||||
Docker Compose mounts three volumes for persistent data. By default, these are stored in the project directory:
|
Docker Compose mounts two volumes for persistent data. By default, these are stored in the project directory:
|
||||||
|
|
||||||
- `./data:/app/data` - Price data and trading records
|
- `./data:/app/data` - Price data and trading records
|
||||||
- `./logs:/app/logs` - MCP service logs
|
|
||||||
- `./configs:/app/configs` - Configuration files (allows editing configs without rebuilding)
|
- `./configs:/app/configs` - Configuration files (allows editing configs without rebuilding)
|
||||||
|
|
||||||
### Custom Volume Location
|
### Custom Volume Location
|
||||||
@@ -174,7 +173,6 @@ VOLUME_PATH=./volumes
|
|||||||
|
|
||||||
This will store data in:
|
This will store data in:
|
||||||
- `/home/user/trading-data/data/`
|
- `/home/user/trading-data/data/`
|
||||||
- `/home/user/trading-data/logs/`
|
|
||||||
- `/home/user/trading-data/configs/`
|
- `/home/user/trading-data/configs/`
|
||||||
|
|
||||||
**Note:** The directory structure is automatically created. You'll need to copy your existing configs:
|
**Note:** The directory structure is automatically created. You'll need to copy your existing configs:
|
||||||
@@ -190,7 +188,7 @@ To reset all trading data:
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
docker-compose down
|
docker-compose down
|
||||||
rm -rf ${VOLUME_PATH:-.}/data/agent_data/* ${VOLUME_PATH:-.}/logs/*
|
rm -rf ${VOLUME_PATH:-.}/data/agent_data/*
|
||||||
docker-compose up
|
docker-compose up
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -217,8 +215,7 @@ docker pull ghcr.io/xe138/ai-trader-server:latest
|
|||||||
```bash
|
```bash
|
||||||
docker run --env-file .env \
|
docker run --env-file .env \
|
||||||
-v $(pwd)/data:/app/data \
|
-v $(pwd)/data:/app/data \
|
||||||
-v $(pwd)/logs:/app/logs \
|
-p 8080:8080 \
|
||||||
-p 8000-8003:8000-8003 \
|
|
||||||
ghcr.io/xe138/ai-trader-server:latest
|
ghcr.io/xe138/ai-trader-server:latest
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -234,9 +231,9 @@ docker pull ghcr.io/xe138/ai-trader-server:v1.0.0
|
|||||||
**Symptom:** Container exits immediately or errors about ports
|
**Symptom:** Container exits immediately or errors about ports
|
||||||
|
|
||||||
**Solutions:**
|
**Solutions:**
|
||||||
- Check ports 8000-8003 not already in use: `lsof -i :8000-8003`
|
|
||||||
- View container logs: `docker-compose logs`
|
- View container logs: `docker-compose logs`
|
||||||
- Check MCP service logs: `cat logs/math.log`
|
- Check if API port 8080 is already in use: `lsof -i :8080`
|
||||||
|
- Verify MCP services started by checking Docker logs for service startup messages
|
||||||
|
|
||||||
### Missing API Keys
|
### Missing API Keys
|
||||||
|
|
||||||
@@ -258,12 +255,12 @@ docker pull ghcr.io/xe138/ai-trader-server:v1.0.0
|
|||||||
|
|
||||||
### Permission Issues
|
### Permission Issues
|
||||||
|
|
||||||
**Symptom:** Cannot write to data or logs directories
|
**Symptom:** Cannot write to data directory
|
||||||
|
|
||||||
**Solutions:**
|
**Solutions:**
|
||||||
- Ensure directories writable: `chmod -R 755 data logs`
|
- Ensure data directory is writable: `chmod -R 755 data`
|
||||||
- Check volume mount permissions
|
- Check volume mount permissions
|
||||||
- May need to create directories first: `mkdir -p data logs`
|
- May need to create directory first: `mkdir -p data`
|
||||||
|
|
||||||
### Container Keeps Restarting
|
### Container Keeps Restarting
|
||||||
|
|
||||||
@@ -298,13 +295,12 @@ docker buildx build --platform linux/amd64,linux/arm64 -t ai-trader-server .
|
|||||||
docker stats ai-trader-server
|
docker stats ai-trader-server
|
||||||
```
|
```
|
||||||
|
|
||||||
### Access MCP Services Directly
|
### Access API Directly
|
||||||
|
|
||||||
Services exposed on host:
|
API server exposed on host:
|
||||||
- Math: http://localhost:8000
|
- REST API: http://localhost:8080
|
||||||
- Search: http://localhost:8001
|
|
||||||
- Trade: http://localhost:8002
|
MCP services run internally and are not exposed to the host.
|
||||||
- Price: http://localhost:8003
|
|
||||||
|
|
||||||
## Development Workflow
|
## Development Workflow
|
||||||
|
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ WORKDIR /app
|
|||||||
COPY . .
|
COPY . .
|
||||||
|
|
||||||
# Create necessary directories
|
# Create necessary directories
|
||||||
RUN mkdir -p data logs data/agent_data
|
RUN mkdir -p data data/agent_data
|
||||||
|
|
||||||
# Make entrypoint executable
|
# Make entrypoint executable
|
||||||
RUN chmod +x entrypoint.sh
|
RUN chmod +x entrypoint.sh
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ from tools.deployment_config import (
|
|||||||
log_api_key_warning,
|
log_api_key_warning,
|
||||||
get_deployment_mode
|
get_deployment_mode
|
||||||
)
|
)
|
||||||
|
from agent.context_injector import ContextInjector
|
||||||
|
|
||||||
# Load environment variables
|
# Load environment variables
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
@@ -125,6 +126,9 @@ class BaseAgent:
|
|||||||
self.model: Optional[ChatOpenAI] = None
|
self.model: Optional[ChatOpenAI] = None
|
||||||
self.agent: Optional[Any] = None
|
self.agent: Optional[Any] = None
|
||||||
|
|
||||||
|
# Context injector for MCP tools
|
||||||
|
self.context_injector: Optional[ContextInjector] = None
|
||||||
|
|
||||||
# Data paths
|
# Data paths
|
||||||
self.data_path = os.path.join(self.base_log_path, self.signature)
|
self.data_path = os.path.join(self.base_log_path, self.signature)
|
||||||
self.position_file = os.path.join(self.data_path, "position", "position.jsonl")
|
self.position_file = os.path.join(self.data_path, "position", "position.jsonl")
|
||||||
@@ -169,16 +173,32 @@ class BaseAgent:
|
|||||||
print("⚠️ OpenAI base URL not set, using default")
|
print("⚠️ OpenAI base URL not set, using default")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Create MCP client
|
# Get job_id from runtime config if available (API mode)
|
||||||
self.client = MultiServerMCPClient(self.mcp_config)
|
from tools.general_tools import get_config_value
|
||||||
|
job_id = get_config_value("JOB_ID") # Returns None if not in API mode
|
||||||
|
|
||||||
|
# Create context injector for injecting signature and today_date into tool calls
|
||||||
|
self.context_injector = ContextInjector(
|
||||||
|
signature=self.signature,
|
||||||
|
today_date=self.init_date, # Will be updated per trading session
|
||||||
|
job_id=job_id # Will be None in standalone mode, populated in API mode
|
||||||
|
)
|
||||||
|
|
||||||
|
# Create MCP client with interceptor
|
||||||
|
self.client = MultiServerMCPClient(
|
||||||
|
self.mcp_config,
|
||||||
|
tool_interceptors=[self.context_injector]
|
||||||
|
)
|
||||||
|
|
||||||
# Get tools
|
# Get tools
|
||||||
self.tools = await self.client.get_tools()
|
raw_tools = await self.client.get_tools()
|
||||||
if not self.tools:
|
if not raw_tools:
|
||||||
print("⚠️ Warning: No MCP tools loaded. MCP services may not be running.")
|
print("⚠️ Warning: No MCP tools loaded. MCP services may not be running.")
|
||||||
print(f" MCP configuration: {self.mcp_config}")
|
print(f" MCP configuration: {self.mcp_config}")
|
||||||
|
self.tools = []
|
||||||
else:
|
else:
|
||||||
print(f"✅ Loaded {len(self.tools)} MCP tools")
|
print(f"✅ Loaded {len(raw_tools)} MCP tools")
|
||||||
|
self.tools = raw_tools
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise RuntimeError(
|
raise RuntimeError(
|
||||||
f"❌ Failed to initialize MCP client: {e}\n"
|
f"❌ Failed to initialize MCP client: {e}\n"
|
||||||
@@ -336,6 +356,10 @@ Summary:"""
|
|||||||
"""
|
"""
|
||||||
print(f"📈 Starting trading session: {today_date}")
|
print(f"📈 Starting trading session: {today_date}")
|
||||||
|
|
||||||
|
# Update context injector with current trading date
|
||||||
|
if self.context_injector:
|
||||||
|
self.context_injector.today_date = today_date
|
||||||
|
|
||||||
# Clear conversation history for new trading day
|
# Clear conversation history for new trading day
|
||||||
self.clear_conversation_history()
|
self.clear_conversation_history()
|
||||||
|
|
||||||
|
|||||||
66
agent/context_injector.py
Normal file
66
agent/context_injector.py
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
"""
|
||||||
|
Tool interceptor for injecting runtime context into MCP tool calls.
|
||||||
|
|
||||||
|
This interceptor automatically injects `signature` and `today_date` parameters
|
||||||
|
into buy/sell tool calls to support concurrent multi-model simulations.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from typing import Any, Callable, Awaitable
|
||||||
|
|
||||||
|
|
||||||
|
class ContextInjector:
|
||||||
|
"""
|
||||||
|
Intercepts tool calls to inject runtime context (signature, today_date).
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
interceptor = ContextInjector(signature="gpt-5", today_date="2025-10-01")
|
||||||
|
client = MultiServerMCPClient(config, tool_interceptors=[interceptor])
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, signature: str, today_date: str, job_id: str = None, session_id: int = None):
|
||||||
|
"""
|
||||||
|
Initialize context injector.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
signature: Model signature to inject
|
||||||
|
today_date: Trading date to inject
|
||||||
|
job_id: Job UUID to inject (optional)
|
||||||
|
session_id: Trading session ID to inject (optional, updated during execution)
|
||||||
|
"""
|
||||||
|
self.signature = signature
|
||||||
|
self.today_date = today_date
|
||||||
|
self.job_id = job_id
|
||||||
|
self.session_id = session_id
|
||||||
|
|
||||||
|
async def __call__(
|
||||||
|
self,
|
||||||
|
request: Any, # MCPToolCallRequest
|
||||||
|
handler: Callable[[Any], Awaitable[Any]]
|
||||||
|
) -> Any: # MCPToolCallResult
|
||||||
|
"""
|
||||||
|
Intercept tool call and inject context parameters.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
request: Tool call request containing name and arguments
|
||||||
|
handler: Async callable to execute the actual tool
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Result from handler after injecting context
|
||||||
|
"""
|
||||||
|
# Inject context parameters for trade tools
|
||||||
|
if request.name in ["buy", "sell"]:
|
||||||
|
# Add signature and today_date to args if not present
|
||||||
|
if "signature" not in request.args:
|
||||||
|
request.args["signature"] = self.signature
|
||||||
|
if "today_date" not in request.args:
|
||||||
|
request.args["today_date"] = self.today_date
|
||||||
|
if "job_id" not in request.args and self.job_id:
|
||||||
|
request.args["job_id"] = self.job_id
|
||||||
|
if "session_id" not in request.args and self.session_id:
|
||||||
|
request.args["session_id"] = self.session_id
|
||||||
|
|
||||||
|
# Debug logging
|
||||||
|
print(f"[ContextInjector] Tool: {request.name}, Args after injection: {request.args}")
|
||||||
|
|
||||||
|
# Call the actual tool handler
|
||||||
|
return await handler(request)
|
||||||
@@ -52,10 +52,6 @@ class MCPServiceManager:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# Create logs directory
|
|
||||||
self.log_dir = Path('logs')
|
|
||||||
self.log_dir.mkdir(exist_ok=True)
|
|
||||||
|
|
||||||
# Set signal handlers
|
# Set signal handlers
|
||||||
signal.signal(signal.SIGINT, self.signal_handler)
|
signal.signal(signal.SIGINT, self.signal_handler)
|
||||||
signal.signal(signal.SIGTERM, self.signal_handler)
|
signal.signal(signal.SIGTERM, self.signal_handler)
|
||||||
@@ -77,27 +73,23 @@ class MCPServiceManager:
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Start service process
|
|
||||||
log_file = self.log_dir / f"{service_id}.log"
|
|
||||||
|
|
||||||
# Set PYTHONPATH to /app so services can import from tools module
|
# Set PYTHONPATH to /app so services can import from tools module
|
||||||
env = os.environ.copy()
|
env = os.environ.copy()
|
||||||
env['PYTHONPATH'] = str(Path.cwd())
|
env['PYTHONPATH'] = str(Path.cwd())
|
||||||
|
|
||||||
with open(log_file, 'w') as f:
|
# Start service process (output goes to Docker logs)
|
||||||
process = subprocess.Popen(
|
process = subprocess.Popen(
|
||||||
[sys.executable, str(script_path)],
|
[sys.executable, str(script_path)],
|
||||||
stdout=f,
|
stdout=subprocess.DEVNULL,
|
||||||
stderr=subprocess.STDOUT,
|
stderr=subprocess.DEVNULL,
|
||||||
cwd=Path.cwd(), # Use current working directory (/app)
|
cwd=Path.cwd(), # Use current working directory (/app)
|
||||||
env=env # Pass environment with PYTHONPATH
|
env=env # Pass environment with PYTHONPATH
|
||||||
)
|
)
|
||||||
|
|
||||||
self.services[service_id] = {
|
self.services[service_id] = {
|
||||||
'process': process,
|
'process': process,
|
||||||
'name': service_name,
|
'name': service_name,
|
||||||
'port': port,
|
'port': port
|
||||||
'log_file': log_file
|
|
||||||
}
|
}
|
||||||
|
|
||||||
print(f"✅ {service_name} service started (PID: {process.pid}, Port: {port})")
|
print(f"✅ {service_name} service started (PID: {process.pid}, Port: {port})")
|
||||||
@@ -167,7 +159,7 @@ class MCPServiceManager:
|
|||||||
print(f"✅ {service['name']} service running normally")
|
print(f"✅ {service['name']} service running normally")
|
||||||
else:
|
else:
|
||||||
print(f"❌ {service['name']} service failed to start")
|
print(f"❌ {service['name']} service failed to start")
|
||||||
print(f" Please check logs: {service['log_file']}")
|
print(f" Check Docker logs for details: docker logs ai-trader-server")
|
||||||
|
|
||||||
def print_service_info(self):
|
def print_service_info(self):
|
||||||
"""Print service information"""
|
"""Print service information"""
|
||||||
@@ -175,7 +167,6 @@ class MCPServiceManager:
|
|||||||
for service_id, service in self.services.items():
|
for service_id, service in self.services.items():
|
||||||
print(f" - {service['name']}: http://localhost:{service['port']} (PID: {service['process'].pid})")
|
print(f" - {service['name']}: http://localhost:{service['port']} (PID: {service['process'].pid})")
|
||||||
|
|
||||||
print(f"\n📁 Log files location: {self.log_dir.absolute()}")
|
|
||||||
print("\n🛑 Press Ctrl+C to stop all services")
|
print("\n🛑 Press Ctrl+C to stop all services")
|
||||||
|
|
||||||
def keep_alive(self):
|
def keep_alive(self):
|
||||||
|
|||||||
@@ -1,197 +1,333 @@
|
|||||||
from fastmcp import FastMCP
|
from fastmcp import FastMCP
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
from typing import Dict, List, Optional, Any
|
from typing import Dict, List, Optional, Any, Tuple
|
||||||
# Add project root directory to Python path
|
# Add project root directory to Python path
|
||||||
project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||||
sys.path.insert(0, project_root)
|
sys.path.insert(0, project_root)
|
||||||
from tools.price_tools import get_yesterday_date, get_open_prices, get_yesterday_open_and_close_price, get_latest_position, get_yesterday_profit
|
from tools.price_tools import get_open_prices
|
||||||
import json
|
import json
|
||||||
from tools.general_tools import get_config_value,write_config_value
|
from tools.deployment_config import get_db_path
|
||||||
|
from api.database import get_db_connection
|
||||||
|
from datetime import datetime
|
||||||
mcp = FastMCP("TradeTools")
|
mcp = FastMCP("TradeTools")
|
||||||
|
|
||||||
|
|
||||||
|
def get_current_position_from_db(job_id: str, model: str, date: str) -> Tuple[Dict[str, float], int]:
|
||||||
@mcp.tool()
|
|
||||||
def buy(symbol: str, amount: int) -> Dict[str, Any]:
|
|
||||||
"""
|
"""
|
||||||
Buy stock function
|
Query current position from SQLite database.
|
||||||
|
|
||||||
This function simulates stock buying operations, including the following steps:
|
|
||||||
1. Get current position and operation ID
|
|
||||||
2. Get stock opening price for the day
|
|
||||||
3. Validate buy conditions (sufficient cash)
|
|
||||||
4. Update position (increase stock quantity, decrease cash)
|
|
||||||
5. Record transaction to position.jsonl file
|
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
symbol: Stock symbol, such as "AAPL", "MSFT", etc.
|
job_id: Job UUID
|
||||||
amount: Buy quantity, must be a positive integer, indicating how many shares to buy
|
model: Model signature
|
||||||
|
date: Trading date (YYYY-MM-DD)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Tuple of (position_dict, next_action_id)
|
||||||
|
- position_dict: {symbol: quantity, "CASH": amount}
|
||||||
|
- next_action_id: Next available action_id for this job+model
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
Exception: If database query fails
|
||||||
|
"""
|
||||||
|
db_path = get_db_path()
|
||||||
|
conn = get_db_connection(db_path)
|
||||||
|
cursor = conn.cursor()
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Get most recent position on or before this date
|
||||||
|
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
|
||||||
|
LIMIT 1
|
||||||
|
""", (job_id, model, date))
|
||||||
|
|
||||||
|
position_row = cursor.fetchone()
|
||||||
|
|
||||||
|
if not position_row:
|
||||||
|
# No position found - this shouldn't happen if ModelDayExecutor initializes properly
|
||||||
|
raise Exception(f"No position found for job_id={job_id}, model={model}, date={date}")
|
||||||
|
|
||||||
|
position_id = position_row[0]
|
||||||
|
cash = position_row[1]
|
||||||
|
|
||||||
|
# Build position dict starting with CASH
|
||||||
|
position_dict = {"CASH": cash}
|
||||||
|
|
||||||
|
# Get holdings for this position
|
||||||
|
cursor.execute("""
|
||||||
|
SELECT symbol, quantity
|
||||||
|
FROM holdings
|
||||||
|
WHERE position_id = ?
|
||||||
|
""", (position_id,))
|
||||||
|
|
||||||
|
for row in cursor.fetchall():
|
||||||
|
symbol = row[0]
|
||||||
|
quantity = row[1]
|
||||||
|
position_dict[symbol] = quantity
|
||||||
|
|
||||||
|
# Get next action_id
|
||||||
|
cursor.execute("""
|
||||||
|
SELECT COALESCE(MAX(action_id), -1) + 1 as next_action_id
|
||||||
|
FROM positions
|
||||||
|
WHERE job_id = ? AND model = ?
|
||||||
|
""", (job_id, model))
|
||||||
|
|
||||||
|
next_action_id = cursor.fetchone()[0]
|
||||||
|
|
||||||
|
return position_dict, next_action_id
|
||||||
|
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
|
||||||
|
@mcp.tool()
|
||||||
|
def buy(symbol: str, amount: int, signature: str = None, today_date: str = None,
|
||||||
|
job_id: str = None, session_id: int = None) -> Dict[str, Any]:
|
||||||
|
"""
|
||||||
|
Buy stock function - writes to SQLite database.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
symbol: Stock symbol (e.g., "AAPL", "MSFT")
|
||||||
|
amount: Number of shares to buy (positive integer)
|
||||||
|
signature: Model signature (injected by ContextInjector)
|
||||||
|
today_date: Trading date YYYY-MM-DD (injected by ContextInjector)
|
||||||
|
job_id: Job UUID (injected by ContextInjector)
|
||||||
|
session_id: Trading session ID (injected by ContextInjector)
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Dict[str, Any]:
|
Dict[str, Any]:
|
||||||
- Success: Returns new position dictionary (containing stock quantity and cash balance)
|
- Success: {"CASH": amount, symbol: quantity, ...}
|
||||||
- Failure: Returns {"error": error message, ...} dictionary
|
- Failure: {"error": message, ...}
|
||||||
|
|
||||||
Raises:
|
|
||||||
ValueError: Raised when SIGNATURE environment variable is not set
|
|
||||||
|
|
||||||
Example:
|
|
||||||
>>> result = buy("AAPL", 10)
|
|
||||||
>>> print(result) # {"AAPL": 110, "MSFT": 5, "CASH": 5000.0, ...}
|
|
||||||
"""
|
"""
|
||||||
# Step 1: Get environment variables and basic information
|
# Validate required parameters
|
||||||
# Get signature (model name) from environment variable, used to determine data storage path
|
if not job_id:
|
||||||
signature = get_config_value("SIGNATURE")
|
return {"error": "Missing required parameter: job_id"}
|
||||||
if signature is None:
|
if not signature:
|
||||||
raise ValueError("SIGNATURE environment variable is not set")
|
return {"error": "Missing required parameter: signature"}
|
||||||
|
if not today_date:
|
||||||
|
return {"error": "Missing required parameter: today_date"}
|
||||||
|
|
||||||
# Get current trading date from environment variable
|
db_path = get_db_path()
|
||||||
today_date = get_config_value("TODAY_DATE")
|
conn = get_db_connection(db_path)
|
||||||
|
cursor = conn.cursor()
|
||||||
|
|
||||||
# Step 2: Get current latest position and operation ID
|
|
||||||
# get_latest_position returns two values: position dictionary and current maximum operation ID
|
|
||||||
# This ID is used to ensure each operation has a unique identifier
|
|
||||||
try:
|
try:
|
||||||
current_position, current_action_id = get_latest_position(today_date, signature)
|
# Step 1: Get current position
|
||||||
except Exception as e:
|
current_position, next_action_id = get_current_position_from_db(job_id, signature, today_date)
|
||||||
print(e)
|
|
||||||
print(current_position, current_action_id)
|
|
||||||
print(today_date, signature)
|
|
||||||
# Step 3: Get stock opening price for the day
|
|
||||||
# Use get_open_prices function to get the opening price of specified stock for the day
|
|
||||||
# If stock symbol does not exist or price data is missing, KeyError exception will be raised
|
|
||||||
try:
|
|
||||||
this_symbol_price = get_open_prices(today_date, [symbol])[f'{symbol}_price']
|
|
||||||
except KeyError:
|
|
||||||
# Stock symbol does not exist or price data is missing, return error message
|
|
||||||
return {"error": f"Symbol {symbol} not found! This action will not be allowed.", "symbol": symbol, "date": today_date}
|
|
||||||
|
|
||||||
# Step 4: Validate buy conditions
|
# Step 2: Get stock price
|
||||||
# Calculate cash required for purchase: stock price × buy quantity
|
try:
|
||||||
try:
|
this_symbol_price = get_open_prices(today_date, [symbol])[f'{symbol}_price']
|
||||||
cash_left = current_position["CASH"] - this_symbol_price * amount
|
except KeyError:
|
||||||
except Exception as e:
|
return {"error": f"Symbol {symbol} not found on {today_date}", "symbol": symbol, "date": today_date}
|
||||||
print(current_position, "CASH", this_symbol_price, amount)
|
|
||||||
|
|
||||||
# Check if cash balance is sufficient for purchase
|
# Step 3: Validate sufficient cash
|
||||||
if cash_left < 0:
|
cash_required = this_symbol_price * amount
|
||||||
# Insufficient cash, return error message
|
cash_available = current_position.get("CASH", 0)
|
||||||
return {"error": "Insufficient cash! This action will not be allowed.", "required_cash": this_symbol_price * amount, "cash_available": current_position.get("CASH", 0), "symbol": symbol, "date": today_date}
|
cash_left = cash_available - cash_required
|
||||||
else:
|
|
||||||
# Step 5: Execute buy operation, update position
|
if cash_left < 0:
|
||||||
# Create a copy of current position to avoid directly modifying original data
|
return {
|
||||||
|
"error": "Insufficient cash",
|
||||||
|
"required_cash": cash_required,
|
||||||
|
"cash_available": cash_available,
|
||||||
|
"symbol": symbol,
|
||||||
|
"date": today_date
|
||||||
|
}
|
||||||
|
|
||||||
|
# Step 4: Calculate new position
|
||||||
new_position = current_position.copy()
|
new_position = current_position.copy()
|
||||||
|
|
||||||
# Decrease cash balance
|
|
||||||
new_position["CASH"] = cash_left
|
new_position["CASH"] = cash_left
|
||||||
|
new_position[symbol] = new_position.get(symbol, 0) + amount
|
||||||
|
|
||||||
# Increase stock position quantity
|
# Step 5: Calculate portfolio value and P&L
|
||||||
new_position[symbol] += amount
|
portfolio_value = cash_left
|
||||||
|
for sym, qty in new_position.items():
|
||||||
|
if sym != "CASH":
|
||||||
|
try:
|
||||||
|
price = get_open_prices(today_date, [sym])[f'{sym}_price']
|
||||||
|
portfolio_value += qty * price
|
||||||
|
except KeyError:
|
||||||
|
pass # Symbol price not available, skip
|
||||||
|
|
||||||
# Step 6: Record transaction to position.jsonl file
|
# Get previous portfolio value for P&L calculation
|
||||||
# Build file path: {project_root}/data/agent_data/{signature}/position/position.jsonl
|
cursor.execute("""
|
||||||
# Use append mode ("a") to write new transaction record
|
SELECT portfolio_value
|
||||||
# Each operation ID increments by 1, ensuring uniqueness of operation sequence
|
FROM positions
|
||||||
position_file_path = os.path.join(project_root, "data", "agent_data", signature, "position", "position.jsonl")
|
WHERE job_id = ? AND model = ? AND date < ?
|
||||||
with open(position_file_path, "a") as f:
|
ORDER BY date DESC, action_id DESC
|
||||||
# Write JSON format transaction record, containing date, operation ID, transaction details and updated position
|
LIMIT 1
|
||||||
print(f"Writing to position.jsonl: {json.dumps({'date': today_date, 'id': current_action_id + 1, 'this_action':{'action':'buy','symbol':symbol,'amount':amount},'positions': new_position})}")
|
""", (job_id, signature, today_date))
|
||||||
f.write(json.dumps({"date": today_date, "id": current_action_id + 1, "this_action":{"action":"buy","symbol":symbol,"amount":amount},"positions": new_position}) + "\n")
|
|
||||||
# Step 7: Return updated position
|
row = cursor.fetchone()
|
||||||
write_config_value("IF_TRADE", True)
|
previous_value = row[0] if row else 10000.0 # Default initial value
|
||||||
print("IF_TRADE", get_config_value("IF_TRADE"))
|
|
||||||
|
daily_profit = portfolio_value - previous_value
|
||||||
|
daily_return_pct = (daily_profit / previous_value * 100) if previous_value > 0 else 0
|
||||||
|
|
||||||
|
# Step 6: Write to positions table
|
||||||
|
created_at = datetime.utcnow().isoformat() + "Z"
|
||||||
|
|
||||||
|
cursor.execute("""
|
||||||
|
INSERT INTO positions (
|
||||||
|
job_id, date, model, action_id, action_type, symbol,
|
||||||
|
amount, price, cash, portfolio_value, daily_profit,
|
||||||
|
daily_return_pct, session_id, created_at
|
||||||
|
)
|
||||||
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
|
""", (
|
||||||
|
job_id, today_date, signature, next_action_id, "buy", symbol,
|
||||||
|
amount, this_symbol_price, cash_left, portfolio_value, daily_profit,
|
||||||
|
daily_return_pct, session_id, created_at
|
||||||
|
))
|
||||||
|
|
||||||
|
position_id = cursor.lastrowid
|
||||||
|
|
||||||
|
# Step 7: Write to holdings table
|
||||||
|
for sym, qty in new_position.items():
|
||||||
|
if sym != "CASH":
|
||||||
|
cursor.execute("""
|
||||||
|
INSERT INTO holdings (position_id, symbol, quantity)
|
||||||
|
VALUES (?, ?, ?)
|
||||||
|
""", (position_id, sym, qty))
|
||||||
|
|
||||||
|
conn.commit()
|
||||||
|
print(f"[buy] {signature} bought {amount} shares of {symbol} at ${this_symbol_price}")
|
||||||
return new_position
|
return new_position
|
||||||
|
|
||||||
@mcp.tool()
|
except Exception as e:
|
||||||
def sell(symbol: str, amount: int) -> Dict[str, Any]:
|
conn.rollback()
|
||||||
"""
|
return {"error": f"Trade failed: {str(e)}", "symbol": symbol, "date": today_date}
|
||||||
Sell stock function
|
|
||||||
|
|
||||||
This function simulates stock selling operations, including the following steps:
|
finally:
|
||||||
1. Get current position and operation ID
|
conn.close()
|
||||||
2. Get stock opening price for the day
|
|
||||||
3. Validate sell conditions (position exists, sufficient quantity)
|
|
||||||
4. Update position (decrease stock quantity, increase cash)
|
@mcp.tool()
|
||||||
5. Record transaction to position.jsonl file
|
def sell(symbol: str, amount: int, signature: str = None, today_date: str = None,
|
||||||
|
job_id: str = None, session_id: int = None) -> Dict[str, Any]:
|
||||||
|
"""
|
||||||
|
Sell stock function - writes to SQLite database.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
symbol: Stock symbol, such as "AAPL", "MSFT", etc.
|
symbol: Stock symbol (e.g., "AAPL", "MSFT")
|
||||||
amount: Sell quantity, must be a positive integer, indicating how many shares to sell
|
amount: Number of shares to sell (positive integer)
|
||||||
|
signature: Model signature (injected by ContextInjector)
|
||||||
|
today_date: Trading date YYYY-MM-DD (injected by ContextInjector)
|
||||||
|
job_id: Job UUID (injected by ContextInjector)
|
||||||
|
session_id: Trading session ID (injected by ContextInjector)
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Dict[str, Any]:
|
Dict[str, Any]:
|
||||||
- Success: Returns new position dictionary (containing stock quantity and cash balance)
|
- Success: {"CASH": amount, symbol: quantity, ...}
|
||||||
- Failure: Returns {"error": error message, ...} dictionary
|
- Failure: {"error": message, ...}
|
||||||
|
|
||||||
Raises:
|
|
||||||
ValueError: Raised when SIGNATURE environment variable is not set
|
|
||||||
|
|
||||||
Example:
|
|
||||||
>>> result = sell("AAPL", 10)
|
|
||||||
>>> print(result) # {"AAPL": 90, "MSFT": 5, "CASH": 15000.0, ...}
|
|
||||||
"""
|
"""
|
||||||
# Step 1: Get environment variables and basic information
|
# Validate required parameters
|
||||||
# Get signature (model name) from environment variable, used to determine data storage path
|
if not job_id:
|
||||||
signature = get_config_value("SIGNATURE")
|
return {"error": "Missing required parameter: job_id"}
|
||||||
if signature is None:
|
if not signature:
|
||||||
raise ValueError("SIGNATURE environment variable is not set")
|
return {"error": "Missing required parameter: signature"}
|
||||||
|
if not today_date:
|
||||||
|
return {"error": "Missing required parameter: today_date"}
|
||||||
|
|
||||||
# Get current trading date from environment variable
|
db_path = get_db_path()
|
||||||
today_date = get_config_value("TODAY_DATE")
|
conn = get_db_connection(db_path)
|
||||||
|
cursor = conn.cursor()
|
||||||
|
|
||||||
# Step 2: Get current latest position and operation ID
|
|
||||||
# get_latest_position returns two values: position dictionary and current maximum operation ID
|
|
||||||
# This ID is used to ensure each operation has a unique identifier
|
|
||||||
current_position, current_action_id = get_latest_position(today_date, signature)
|
|
||||||
|
|
||||||
# Step 3: Get stock opening price for the day
|
|
||||||
# Use get_open_prices function to get the opening price of specified stock for the day
|
|
||||||
# If stock symbol does not exist or price data is missing, KeyError exception will be raised
|
|
||||||
try:
|
try:
|
||||||
this_symbol_price = get_open_prices(today_date, [symbol])[f'{symbol}_price']
|
# Step 1: Get current position
|
||||||
except KeyError:
|
current_position, next_action_id = get_current_position_from_db(job_id, signature, today_date)
|
||||||
# Stock symbol does not exist or price data is missing, return error message
|
|
||||||
return {"error": f"Symbol {symbol} not found! This action will not be allowed.", "symbol": symbol, "date": today_date}
|
|
||||||
|
|
||||||
# Step 4: Validate sell conditions
|
# Step 2: Validate position exists
|
||||||
# Check if holding this stock
|
if symbol not in current_position:
|
||||||
if symbol not in current_position:
|
return {"error": f"No position for {symbol}", "symbol": symbol, "date": today_date}
|
||||||
return {"error": f"No position for {symbol}! This action will not be allowed.", "symbol": symbol, "date": today_date}
|
|
||||||
|
|
||||||
# Check if position quantity is sufficient for selling
|
if current_position[symbol] < amount:
|
||||||
if current_position[symbol] < amount:
|
return {
|
||||||
return {"error": "Insufficient shares! This action will not be allowed.", "have": current_position.get(symbol, 0), "want_to_sell": amount, "symbol": symbol, "date": today_date}
|
"error": "Insufficient shares",
|
||||||
|
"have": current_position[symbol],
|
||||||
|
"want_to_sell": amount,
|
||||||
|
"symbol": symbol,
|
||||||
|
"date": today_date
|
||||||
|
}
|
||||||
|
|
||||||
# Step 5: Execute sell operation, update position
|
# Step 3: Get stock price
|
||||||
# Create a copy of current position to avoid directly modifying original data
|
try:
|
||||||
new_position = current_position.copy()
|
this_symbol_price = get_open_prices(today_date, [symbol])[f'{symbol}_price']
|
||||||
|
except KeyError:
|
||||||
|
return {"error": f"Symbol {symbol} not found on {today_date}", "symbol": symbol, "date": today_date}
|
||||||
|
|
||||||
# Decrease stock position quantity
|
# Step 4: Calculate new position
|
||||||
new_position[symbol] -= amount
|
new_position = current_position.copy()
|
||||||
|
new_position[symbol] -= amount
|
||||||
|
new_position["CASH"] = new_position.get("CASH", 0) + (this_symbol_price * amount)
|
||||||
|
|
||||||
# Increase cash balance: sell price × sell quantity
|
# Step 5: Calculate portfolio value and P&L
|
||||||
# Use get method to ensure CASH field exists, default to 0 if not present
|
portfolio_value = new_position["CASH"]
|
||||||
new_position["CASH"] = new_position.get("CASH", 0) + this_symbol_price * amount
|
for sym, qty in new_position.items():
|
||||||
|
if sym != "CASH":
|
||||||
|
try:
|
||||||
|
price = get_open_prices(today_date, [sym])[f'{sym}_price']
|
||||||
|
portfolio_value += qty * price
|
||||||
|
except KeyError:
|
||||||
|
pass
|
||||||
|
|
||||||
# Step 6: Record transaction to position.jsonl file
|
# Get previous portfolio value
|
||||||
# Build file path: {project_root}/data/agent_data/{signature}/position/position.jsonl
|
cursor.execute("""
|
||||||
# Use append mode ("a") to write new transaction record
|
SELECT portfolio_value
|
||||||
# Each operation ID increments by 1, ensuring uniqueness of operation sequence
|
FROM positions
|
||||||
position_file_path = os.path.join(project_root, "data", "agent_data", signature, "position", "position.jsonl")
|
WHERE job_id = ? AND model = ? AND date < ?
|
||||||
with open(position_file_path, "a") as f:
|
ORDER BY date DESC, action_id DESC
|
||||||
# Write JSON format transaction record, containing date, operation ID and updated position
|
LIMIT 1
|
||||||
print(f"Writing to position.jsonl: {json.dumps({'date': today_date, 'id': current_action_id + 1, 'this_action':{'action':'sell','symbol':symbol,'amount':amount},'positions': new_position})}")
|
""", (job_id, signature, today_date))
|
||||||
f.write(json.dumps({"date": today_date, "id": current_action_id + 1, "this_action":{"action":"sell","symbol":symbol,"amount":amount},"positions": new_position}) + "\n")
|
|
||||||
|
row = cursor.fetchone()
|
||||||
|
previous_value = row[0] if row else 10000.0
|
||||||
|
|
||||||
|
daily_profit = portfolio_value - previous_value
|
||||||
|
daily_return_pct = (daily_profit / previous_value * 100) if previous_value > 0 else 0
|
||||||
|
|
||||||
|
# Step 6: Write to positions table
|
||||||
|
created_at = datetime.utcnow().isoformat() + "Z"
|
||||||
|
|
||||||
|
cursor.execute("""
|
||||||
|
INSERT INTO positions (
|
||||||
|
job_id, date, model, action_id, action_type, symbol,
|
||||||
|
amount, price, cash, portfolio_value, daily_profit,
|
||||||
|
daily_return_pct, session_id, created_at
|
||||||
|
)
|
||||||
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
|
""", (
|
||||||
|
job_id, today_date, signature, next_action_id, "sell", symbol,
|
||||||
|
amount, this_symbol_price, new_position["CASH"], portfolio_value, daily_profit,
|
||||||
|
daily_return_pct, session_id, created_at
|
||||||
|
))
|
||||||
|
|
||||||
|
position_id = cursor.lastrowid
|
||||||
|
|
||||||
|
# Step 7: Write to holdings table
|
||||||
|
for sym, qty in new_position.items():
|
||||||
|
if sym != "CASH":
|
||||||
|
cursor.execute("""
|
||||||
|
INSERT INTO holdings (position_id, symbol, quantity)
|
||||||
|
VALUES (?, ?, ?)
|
||||||
|
""", (position_id, sym, qty))
|
||||||
|
|
||||||
|
conn.commit()
|
||||||
|
print(f"[sell] {signature} sold {amount} shares of {symbol} at ${this_symbol_price}")
|
||||||
|
return new_position
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
conn.rollback()
|
||||||
|
return {"error": f"Trade failed: {str(e)}", "symbol": symbol, "date": today_date}
|
||||||
|
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
|
||||||
# Step 7: Return updated position
|
|
||||||
write_config_value("IF_TRADE", True)
|
|
||||||
return new_position
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
# new_result = buy("AAPL", 1)
|
|
||||||
# print(new_result)
|
|
||||||
# new_result = sell("AAPL", 1)
|
|
||||||
# print(new_result)
|
|
||||||
port = int(os.getenv("TRADE_HTTP_PORT", "8002"))
|
port = int(os.getenv("TRADE_HTTP_PORT", "8002"))
|
||||||
mcp.run(transport="streamable-http", port=port)
|
mcp.run(transport="streamable-http", port=port)
|
||||||
|
|||||||
@@ -122,11 +122,19 @@ class ModelDayExecutor:
|
|||||||
session_id = self._create_trading_session(cursor)
|
session_id = self._create_trading_session(cursor)
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|
||||||
|
# Initialize starting position if this is first day
|
||||||
|
self._initialize_starting_position(cursor, session_id)
|
||||||
|
conn.commit()
|
||||||
|
|
||||||
# Set environment variable for agent to use isolated config
|
# Set environment variable for agent to use isolated config
|
||||||
os.environ["RUNTIME_ENV_PATH"] = self.runtime_config_path
|
os.environ["RUNTIME_ENV_PATH"] = self.runtime_config_path
|
||||||
|
|
||||||
# Initialize agent
|
# Initialize agent
|
||||||
agent = self._initialize_agent()
|
agent = await self._initialize_agent()
|
||||||
|
|
||||||
|
# Update context injector with session_id
|
||||||
|
if hasattr(agent, 'context_injector') and agent.context_injector:
|
||||||
|
agent.context_injector.session_id = session_id
|
||||||
|
|
||||||
# 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}")
|
||||||
@@ -209,7 +217,7 @@ class ModelDayExecutor:
|
|||||||
"""Execute model-day simulation (sync entry point)."""
|
"""Execute model-day simulation (sync entry point)."""
|
||||||
return self.execute_sync()
|
return self.execute_sync()
|
||||||
|
|
||||||
def _initialize_agent(self):
|
async def _initialize_agent(self):
|
||||||
"""
|
"""
|
||||||
Initialize trading agent with config.
|
Initialize trading agent with config.
|
||||||
|
|
||||||
@@ -259,6 +267,9 @@ class ModelDayExecutor:
|
|||||||
# - Database initialization is handled by JobManager
|
# - Database initialization is handled by JobManager
|
||||||
# - File-based position tracking is only for standalone/CLI mode
|
# - File-based position tracking is only for standalone/CLI mode
|
||||||
|
|
||||||
|
# Initialize MCP client and AI model
|
||||||
|
await agent.initialize()
|
||||||
|
|
||||||
return agent
|
return agent
|
||||||
|
|
||||||
def _create_trading_session(self, cursor) -> int:
|
def _create_trading_session(self, cursor) -> int:
|
||||||
@@ -284,6 +295,51 @@ class ModelDayExecutor:
|
|||||||
|
|
||||||
return cursor.lastrowid
|
return cursor.lastrowid
|
||||||
|
|
||||||
|
def _initialize_starting_position(self, cursor, session_id: int) -> None:
|
||||||
|
"""
|
||||||
|
Initialize starting position if no prior positions exist for this job+model.
|
||||||
|
|
||||||
|
Creates action_id=0 position with initial_cash and zero stock holdings.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
cursor: Database cursor
|
||||||
|
session_id: Trading session ID
|
||||||
|
"""
|
||||||
|
# Check if any positions exist for this job+model
|
||||||
|
cursor.execute("""
|
||||||
|
SELECT COUNT(*) FROM positions
|
||||||
|
WHERE job_id = ? AND model = ?
|
||||||
|
""", (self.job_id, self.model_sig))
|
||||||
|
|
||||||
|
if cursor.fetchone()[0] > 0:
|
||||||
|
# Positions already exist, no initialization needed
|
||||||
|
return
|
||||||
|
|
||||||
|
# Load config to get initial_cash
|
||||||
|
import json
|
||||||
|
with open(self.config_path, 'r') as f:
|
||||||
|
config = json.load(f)
|
||||||
|
|
||||||
|
agent_config = config.get("agent_config", {})
|
||||||
|
initial_cash = agent_config.get("initial_cash", 10000.0)
|
||||||
|
|
||||||
|
# Create initial position record
|
||||||
|
from datetime import datetime
|
||||||
|
created_at = datetime.utcnow().isoformat() + "Z"
|
||||||
|
|
||||||
|
cursor.execute("""
|
||||||
|
INSERT INTO positions (
|
||||||
|
job_id, date, model, action_id, action_type,
|
||||||
|
cash, portfolio_value, session_id, created_at
|
||||||
|
)
|
||||||
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
|
""", (
|
||||||
|
self.job_id, self.date, self.model_sig, 0, "no_trade",
|
||||||
|
initial_cash, initial_cash, session_id, created_at
|
||||||
|
))
|
||||||
|
|
||||||
|
logger.info(f"Initialized starting position for {self.model_sig} with ${initial_cash}")
|
||||||
|
|
||||||
async def _store_reasoning_logs(
|
async def _store_reasoning_logs(
|
||||||
self,
|
self,
|
||||||
cursor,
|
cursor,
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ services:
|
|||||||
container_name: ai-trader-server
|
container_name: ai-trader-server
|
||||||
volumes:
|
volumes:
|
||||||
- ${VOLUME_PATH:-.}/data:/app/data
|
- ${VOLUME_PATH:-.}/data:/app/data
|
||||||
- ${VOLUME_PATH:-.}/logs:/app/logs
|
|
||||||
# User configs mounted to /app/user-configs (default config baked into image)
|
# User configs mounted to /app/user-configs (default config baked into image)
|
||||||
- ${VOLUME_PATH:-.}/configs:/app/user-configs
|
- ${VOLUME_PATH:-.}/configs:/app/user-configs
|
||||||
environment:
|
environment:
|
||||||
|
|||||||
@@ -112,10 +112,9 @@ docker-compose up
|
|||||||
|
|
||||||
### Volume Mounts
|
### Volume Mounts
|
||||||
|
|
||||||
Docker Compose mounts three volumes for persistent data. By default, these are stored in the project directory:
|
Docker Compose mounts two volumes for persistent data. By default, these are stored in the project directory:
|
||||||
|
|
||||||
- `./data:/app/data` - Price data and trading records
|
- `./data:/app/data` - Price data and trading records
|
||||||
- `./logs:/app/logs` - MCP service logs
|
|
||||||
- `./configs:/app/configs` - Configuration files (allows editing configs without rebuilding)
|
- `./configs:/app/configs` - Configuration files (allows editing configs without rebuilding)
|
||||||
|
|
||||||
### Custom Volume Location
|
### Custom Volume Location
|
||||||
@@ -132,7 +131,6 @@ VOLUME_PATH=./volumes
|
|||||||
|
|
||||||
This will store data in:
|
This will store data in:
|
||||||
- `/home/user/trading-data/data/`
|
- `/home/user/trading-data/data/`
|
||||||
- `/home/user/trading-data/logs/`
|
|
||||||
- `/home/user/trading-data/configs/`
|
- `/home/user/trading-data/configs/`
|
||||||
|
|
||||||
**Note:** The directory structure is automatically created. You'll need to copy your existing configs:
|
**Note:** The directory structure is automatically created. You'll need to copy your existing configs:
|
||||||
@@ -148,7 +146,7 @@ To reset all trading data:
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
docker-compose down
|
docker-compose down
|
||||||
rm -rf ${VOLUME_PATH:-.}/data/agent_data/* ${VOLUME_PATH:-.}/logs/*
|
rm -rf ${VOLUME_PATH:-.}/data/agent_data/*
|
||||||
docker-compose up
|
docker-compose up
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -175,8 +173,7 @@ docker pull ghcr.io/xe138/ai-trader-server:latest
|
|||||||
```bash
|
```bash
|
||||||
docker run --env-file .env \
|
docker run --env-file .env \
|
||||||
-v $(pwd)/data:/app/data \
|
-v $(pwd)/data:/app/data \
|
||||||
-v $(pwd)/logs:/app/logs \
|
-p 8080:8080 \
|
||||||
-p 8000-8003:8000-8003 \
|
|
||||||
ghcr.io/xe138/ai-trader-server:latest
|
ghcr.io/xe138/ai-trader-server:latest
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -192,9 +189,9 @@ docker pull ghcr.io/xe138/ai-trader-server:v1.0.0
|
|||||||
**Symptom:** Container exits immediately or errors about ports
|
**Symptom:** Container exits immediately or errors about ports
|
||||||
|
|
||||||
**Solutions:**
|
**Solutions:**
|
||||||
- Check ports 8000-8003 not already in use: `lsof -i :8000-8003`
|
|
||||||
- View container logs: `docker-compose logs`
|
- View container logs: `docker-compose logs`
|
||||||
- Check MCP service logs: `cat logs/math.log`
|
- Check if API port 8080 is already in use: `lsof -i :8080`
|
||||||
|
- Verify MCP services started by checking Docker logs for service startup messages
|
||||||
|
|
||||||
### Missing API Keys
|
### Missing API Keys
|
||||||
|
|
||||||
@@ -216,12 +213,12 @@ docker pull ghcr.io/xe138/ai-trader-server:v1.0.0
|
|||||||
|
|
||||||
### Permission Issues
|
### Permission Issues
|
||||||
|
|
||||||
**Symptom:** Cannot write to data or logs directories
|
**Symptom:** Cannot write to data directory
|
||||||
|
|
||||||
**Solutions:**
|
**Solutions:**
|
||||||
- Ensure directories writable: `chmod -R 755 data logs`
|
- Ensure data directory is writable: `chmod -R 755 data`
|
||||||
- Check volume mount permissions
|
- Check volume mount permissions
|
||||||
- May need to create directories first: `mkdir -p data logs`
|
- May need to create directory first: `mkdir -p data`
|
||||||
|
|
||||||
### Container Keeps Restarting
|
### Container Keeps Restarting
|
||||||
|
|
||||||
@@ -256,13 +253,12 @@ docker buildx build --platform linux/amd64,linux/arm64 -t ai-trader-server .
|
|||||||
docker stats ai-trader-server
|
docker stats ai-trader-server
|
||||||
```
|
```
|
||||||
|
|
||||||
### Access MCP Services Directly
|
### Access API Directly
|
||||||
|
|
||||||
Services exposed on host:
|
API server exposed on host:
|
||||||
- Math: http://localhost:8000
|
- REST API: http://localhost:8080
|
||||||
- Search: http://localhost:8001
|
|
||||||
- Trade: http://localhost:8002
|
MCP services run internally and are not exposed to the host.
|
||||||
- Price: http://localhost:8003
|
|
||||||
|
|
||||||
## Development Workflow
|
## Development Workflow
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user