Skip to content

A comprehensive algorithmic trading system monorepo integrating QuantConnect Lean engine with AI-powered strategy generation and rebate optimization tools.

Notifications You must be signed in to change notification settings

Astralchemist/Trading-System-CI

Repository files navigation

Trading System CI - Monorepo

A comprehensive algorithmic trading system monorepo integrating QuantConnect Lean engine with AI-powered strategy generation and rebate optimization tools.

πŸ—οΈ Architecture

Trading-System-CI/
β”œβ”€β”€ docker-compose.yml          # Orchestrates all services
β”œβ”€β”€ lean-core/                  # QuantConnect Lean engine
β”‚   β”œβ”€β”€ Dockerfile
β”‚   └── Lean/                   # Lean engine submodule
β”œβ”€β”€ services/
β”‚   β”œβ”€β”€ rebate/                 # Rebate optimization sandbox
β”‚   β”‚   β”œβ”€β”€ Dockerfile
β”‚   β”‚   β”œβ”€β”€ app.py             # Streamlit UI with Optuna
β”‚   β”‚   └── requirements.txt
β”‚   └── strategy/               # AI strategy generator
β”‚       β”œβ”€β”€ Dockerfile
β”‚       β”œβ”€β”€ generate.py        # AI code generation & validation
β”‚       └── requirements.txt
└── data/                       # Shared data directory

πŸš€ Services

1. lean-core - QuantConnect Lean Engine

The algorithmic trading engine that executes strategies.

  • Technology: C# / .NET, Python support
  • Purpose: Backtesting and live trading execution
  • Base Image: quantconnect/lean:foundation

2. rebate - Rebate Optimization Sandbox

Interactive UI for optimizing trading rebate parameters.

  • Technology: Streamlit + Optuna
  • Port: 8501
  • Features:
    • Hyperparameter optimization for rebate strategies
    • Visual analysis of optimization results
    • Parameter importance analysis
    • Export optimization data

3. strategy - AI Strategy Generator

Generates and validates trading strategies using AI.

  • Technology: Python + Anthropic Claude API
  • Features:
    • Natural language to trading strategy code
    • Support for Python and C# strategies
    • Automatic code validation
    • QuantConnect Lean compatibility checks

πŸ“¦ Quick Start

Prerequisites

  • Docker & Docker Compose
  • Git (with submodules initialized)
  • (Optional) Anthropic API key for AI strategy generation

1. Clone Repository

git clone <your-repo-url>
cd Trading-System-CI

# Initialize Lean submodule if needed
git submodule update --init --recursive

2. Configure Environment

Create a .env file in the root:

# Optional: For AI strategy generation
ANTHROPIC_API_KEY=your_api_key_here

# Lean engine configuration
LEAN_DATA_DIRECTORY=/Data

3. Build and Run

# Build all services
docker-compose build

# Start all services
docker-compose up

# Or run specific service
docker-compose up rebate

4. Access Services

  • Rebate Optimization UI: http://localhost:8501
  • Lean Engine: Running in background (check logs: docker-compose logs lean)
  • Strategy Generator: Run via docker-compose run strategy

5. Running Backtests (Using Utility Scripts)

The repository includes helpful utility scripts in the scripts/ directory:

# Interactive menu to select and run algorithms
./scripts/backtest_menu.sh

# Or run a specific algorithm directly
./scripts/run_backtest.sh algorithms/SimpleBuyAndHold.py

# View backtest results
./scripts/view_results.sh

# Download sample market data
./scripts/download_sample_data.sh

These scripts handle Docker exec commands and result parsing automatically.

πŸ”§ Development

Running Individual Services

Rebate Optimization:

cd services/rebate
pip install -r requirements.txt
streamlit run app.py

Strategy Generator:

cd services/strategy
pip install -r requirements.txt
python generate.py

Lean Engine:

docker-compose up lean

Generating Strategies

# Run the strategy generator
docker-compose run strategy

# Generated strategies saved to: services/strategy/generated/

Customizing Optimization

Edit parameters in the Streamlit UI:

  1. Go to http://localhost:8501
  2. Adjust parameters in the sidebar
  3. Click "Run Optimization"
  4. Download results as CSV

πŸ“Š Usage Examples

Example 1: Generate a Trading Strategy

from generate import StrategyGenerator

generator = StrategyGenerator(api_key="your_key")

strategy = generator.generate_strategy(
    prompt="Create a momentum strategy using RSI with 30/70 thresholds",
    language="python"
)

validation = generator.validate_strategy(strategy["code"], "python")
if validation["valid"]:
    filepath = generator.save_strategy(strategy, "my_rsi_strategy")
    print(f"Strategy saved to: {filepath}")

Example 2: Optimize Rebate Parameters

  1. Open http://localhost:8501
  2. Set your base trading volume and frequency
  3. Click "Run Optimization"
  4. Review best parameters and estimated savings
  5. Download trial data for further analysis

πŸ› οΈ Configuration

Docker Compose Services

lean:

  • Build context: ./lean-core
  • Volumes: ./data:/Data, ./lean-core:/Lean
  • Interactive terminal enabled

rebate:

  • Build context: ./services/rebate
  • Port: 8501:8501
  • Volume mount for live code updates

strategy:

  • Build context: ./services/strategy
  • Depends on: lean
  • Volume mount for data sharing

πŸ§ͺ Testing

# Test strategy generation
docker-compose run strategy python -m pytest

# Test rebate service (run in container)
docker-compose run rebate python -m pytest

# View lean logs
docker-compose logs -f lean

πŸ–ΌοΈ Screenshots

Rebate Optimization Sandbox

Rebate Optimization UI

The interactive Streamlit interface allows you to optimize trading rebate parameters using Optuna hyperparameter optimization.

⚠️ Known Issues & Troubleshooting

Docker Errors Example

Common Issues

1. Anthropic API Credit Error (400)

Error code: 400 - {'type': 'error', 'error': {'type': 'invalid_request_error',
'message': 'Your credit balance is too low to access the Anthropic API.'}}

Solution:

  • Add credits to your Anthropic account at https://console.anthropic.com/
  • Or set up a valid API key with available credits in your .env file
  • The strategy generator service requires API credits to function

2. Permission Denied Error on Strategy Generation

PermissionError: [Errno 13] Permission denied: '/app/generated/strategy_1.py'

Solution:

  • This was fixed in the latest Docker configuration
  • The services/strategy/Dockerfile now creates the generated/ directory with proper permissions
  • Rebuild the container: docker-compose build strategy

3. Lean Engine Configuration Errors

UnhandledExceptionEventArgs: Unrecognized command or argument 'tail'
at QuantConnect.Configuration.ApplicationParser.ParseArguments

Solution:

  • These errors occur when the Lean container uses tail -f /dev/null as a keep-alive command
  • This is intentional - the Lean engine is now designed to stay running so you can exec backtests into it
  • To run a backtest, use: docker-compose exec lean dotnet /Lean/Launcher/bin/Release/QuantConnect.Lean.Launcher.dll
  • Or use the provided utility scripts in scripts/ directory

4. Missing Lean DLL or Build Issues

Solution:

  • Ensure the Lean submodule is initialized: git submodule update --init --recursive
  • Rebuild the lean-core container: docker-compose build lean --no-cache
  • The Dockerfile now includes proper build steps with dotnet restore and dotnet build

5. Volume Mount Issues

Solution:

  • The docker-compose.yml now uses selective volume mounts
  • If you need to develop Lean algorithms, uncomment the development volume mounts in docker-compose.yml
  • For production use, keep the default configuration to preserve built DLLs

Getting Help

If you encounter other issues:

  1. Check the logs: docker-compose logs -f [service-name]
  2. Rebuild containers: docker-compose build --no-cache
  3. Check that all environment variables are set in .env
  4. Verify the Lean submodule is properly initialized

πŸ“ Project Status

Completed

  • βœ… Monorepo structure with docker-compose orchestration
  • βœ… Lean engine Docker integration
  • βœ… AI-powered strategy generator with validation
  • βœ… Interactive rebate optimization UI with Optuna
  • βœ… CI/CD pipeline setup (GitHub Actions)

In Progress

  • πŸ”„ Strategy backtesting automation
  • πŸ”„ Integration between strategy generator and Lean engine
  • πŸ”„ Advanced validation and risk metrics

Planned

  • πŸ“‹ Live trading integration
  • πŸ“‹ Real-time performance monitoring dashboard
  • πŸ“‹ Multi-exchange rebate comparison
  • πŸ“‹ Automated strategy deployment

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/my-feature
  3. Commit changes: git commit -am 'Add my feature'
  4. Push to branch: git push origin feature/my-feature
  5. Submit a pull request

πŸ“„ License

[Add your license here]

πŸ”— Resources

πŸ“§ Support

For issues and questions:

  • Open an issue on GitHub
  • Check existing documentation
  • Review QuantConnect forums

Built with: QuantConnect Lean, Python, Docker, Streamlit, Optuna, and Claude AI

About

A comprehensive algorithmic trading system monorepo integrating QuantConnect Lean engine with AI-powered strategy generation and rebate optimization tools.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published