backtest-consumer¶
Consumes SQS messages containing backtest requests and orchestrates ECS Fargate task execution.
Overview¶
| Property | Value |
|---|---|
| Trigger | SQS Event Source Mapping |
| Runtime | Python 3.11 |
| Timeout | 300 seconds |
| Memory | 256 MB |
Input Schema¶
# SQS Message Body (JSON)
{
"job_id": "uuid-string", # Optional, auto-generated if not provided
"config": { # Nested format (preferred)
"strategy": "MyStrategy", # Required
"timeframe": "1h", # Default: "1h"
"start_date": "2024-01-01", # YYYY-MM-DD format
"end_date": "2024-03-01", # YYYY-MM-DD format
"symbols": ["BTC/USDT:USDT"], # Default: ["BTC/USDT:USDT"]
"stoploss": -0.05, # Optional
"stake_amount": 1000.0 # Default: 1000.0
},
# OR legacy flat format:
"strategy_name": "MyStrategy",
"experiment_name": "backtest-exp", # Optional, for MLflow tracking
"priority": 0, # Default: 0
"submitted_at": "2024-01-01T00:00:00Z"
}
Output¶
# SQS Batch Response
{
"batchItemFailures": [
{"itemIdentifier": "message-id"} # For partial failure handling
]
}
Environment Variables¶
| Variable | Required | Default | Description |
|---|---|---|---|
ECS_CLUSTER | Yes | - | ECS cluster name or ARN |
ECS_TASK_DEFINITION_ARN | Yes | - | Full task definition ARN |
ECS_SUBNETS | Yes | - | Comma-separated subnet IDs |
ECS_SECURITY_GROUPS | Yes | - | Comma-separated security group IDs |
WORKFLOW_STATE_TABLE | Yes | - | DynamoDB backtest state table |
ENVIRONMENT | No | "dev" | Environment name |
AWS Services Used¶
- ECS - Launches Fargate tasks for backtesting
- DynamoDB - Records task launch state
- CloudWatch - Publishes metrics
CloudWatch Metrics¶
| Metric | Description |
|---|---|
BacktestRequestsReceived | Number of SQS messages processed |
BacktestTasksLaunched | Number of ECS tasks launched |
BacktestRequestsFailed | Number of failed requests |
Key Features¶
- Supports both nested and legacy flat message formats with auto-normalization
- Enforces concurrent task limits (
max_concurrent_tasks: 10) - Uses
FARGATE_SPOTfor cost optimization - Records state in DynamoDB for monitoring
- Supports partial batch failure handling for SQS
Example Usage¶
Submit Backtest via SQS¶
import boto3
import json
sqs = boto3.client('sqs')
message = {
"config": {
"strategy": "TrendFollowingStrategy",
"timeframe": "1h",
"start_date": "2024-01-01",
"end_date": "2024-03-01",
"symbols": ["BTC/USDT:USDT", "ETH/USDT:USDT"]
}
}
sqs.send_message(
QueueUrl="https://sqs.us-east-1.amazonaws.com/123456789/backtest-queue",
MessageBody=json.dumps(message)
)
Error Handling¶
| Error | Cause | Resolution |
|---|---|---|
ConcurrentTaskLimitReached | Max tasks running | Wait for tasks to complete |
InvalidConfig | Missing required fields | Check config schema |
ECSLaunchError | Task launch failed | Check ECS permissions and VPC config |
See Also¶
Related Lambdas:
- sqs-consumer - Similar pattern for retraining tasks
- cleanup-resources - Handles failed task cleanup
- notify-completion - Sends completion notifications
Architecture:
- Step Functions Workflows - Backtest workflow orchestration
- Architecture Overview - Workflow diagram
Services:
- Backend Service - API for submitting backtests
- Strategy Service - Strategy configuration
CLI:
- CLI Reference -
tradai backtestcommands