A domain-specific language for trading strategy development and backtesting
Reason this release was yanked:
CLI import broken: data module excluded from wheel by gitignore
Project description
๐ณ Canopy Trading Language
A powerful, expressive domain-specific language (DSL) for trading strategy development and backtesting.
โ ๏ธ Alpha release (v0.0.1) โ The core DSL, parser, backtest engine, optimizers (Bayesian/Genetic/Grid), and metrics are working (264 unit tests passing). Data provider adapters (Yahoo Finance, CSV) are not yet implemented in this release โ bring your own
pandas.DataFrameof OHLC data for now. The REST API and web frontend mentioned below are experimental and requirepip install canopy-lang[web]. API may change before 1.0.
โจ Features
- ๐ Intuitive DSL: Write trading strategies in a clear, expressive language
- ๐ Technical Indicators: 15+ built-in indicators (SMA, EMA, RSI, MACD, Bollinger Bands, etc.)
- ๐ Fast Backtesting: Vectorized backtest engine for quick results
- ๐ Performance Metrics: Comprehensive metrics (Sharpe, Sortino, drawdown, win rate, etc.)
- ๐ REST API: FastAPI-powered API for programmatic access
- ๐ป Web Interface: React-based frontend for visual strategy development
- ๐ณ Docker Ready: Complete Docker Compose setup for easy deployment
- ๐งช Production Ready: Built with hexagonal architecture and TDD
- ๐ Rich Examples: 10+ example strategies from basic to advanced
๐ Quick Start
With Docker (Recommended)
# Clone the repository
git clone https://github.com/larancibia/canopy-lang.git
cd canopy
# Start all services
docker-compose up
# Access the services
# API: http://localhost:8000
# API Docs: http://localhost:8000/docs
# Web UI: http://localhost:5173
Local Development
# Run setup script
./scripts/setup.sh
# Start development servers
./scripts/run-dev.sh
Simple Example
Create a file my_strategy.canopy:
strategy "MA Crossover"
# Define moving averages
fast_ma = sma(close, 50)
slow_ma = sma(close, 200)
# Entry: Buy when fast crosses above slow
buy when crossover(fast_ma, slow_ma)
# Exit: Sell when fast crosses below slow
sell when crossunder(fast_ma, slow_ma)
# Visualize
plot(fast_ma, "Fast MA", color=blue)
plot(slow_ma, "Slow MA", color=red)
Run the backtest:
# CLI
canopy backtest my_strategy.canopy --symbol AAPL --start 2022-01-01 --end 2023-12-31
# Python API
from canopy import run_backtest
result = run_backtest(
strategy_file="my_strategy.canopy",
symbol="AAPL",
start_date="2022-01-01",
end_date="2023-12-31"
)
print(f"Total Return: {result.metrics.total_return:.2%}")
print(f"Sharpe Ratio: {result.metrics.sharpe_ratio:.2f}")
๐ Documentation
- Language Reference - Complete language syntax and indicators
- API Reference - REST API documentation
- Architecture - System architecture and design
- Development Guide - Contributing and development
- Deployment Guide - Production deployment
๐ฏ Example Strategies
Basic Strategies
- MA Crossover - Golden/Death cross
- RSI Mean Reversion - Oversold/overbought
- EMA Crossover - Exponential MAs
- Volume Breakout - High volume breaks
Advanced Strategies
- Bollinger Squeeze - Volatility breakout
- MACD Divergence - Momentum trading
- ATR Breakout - Volatility-based
- Multi-Timeframe - Cross-timeframe
Portfolio Strategies
- Pairs Trading - Mean reversion pairs
- Sector Rotation - Relative strength
Python Examples
- Custom Indicators - Build your own indicators
- ML Strategy - Machine learning integration
- Optimization - Parameter optimization
๐๏ธ Architecture
Canopy is built using Hexagonal Architecture (Ports and Adapters):
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ UI Layer โ
โ CLI โ Web API โ Web Frontend โ
โโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Application Layer (Use Cases) โ
โ Run Backtest โ Fetch Data โ ... โ
โโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Domain Layer (Business Logic) โ
โ Strategy โ Indicator โ Signal โ ... โ
โโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Ports (Interfaces) โ
โ IDataProvider โ IBacktestEngine โ
โโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Adapters (Implementations) โ
โ Yahoo โ CSV โ Simple Engine โ ... โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
See ARCHITECTURE.md for details.
๐ ๏ธ Tech Stack
Backend
- Python 3.11+ - Core language
- FastAPI - REST API framework
- Pandas & NumPy - Data processing
- Pydantic - Data validation
- yfinance - Market data
Frontend
- React 18 - UI framework
- Vite - Build tool
- TailwindCSS - Styling
- Recharts - Charting
Infrastructure
- Docker & Docker Compose - Containerization
- PostgreSQL - Database
- Redis - Caching and job queue
- GitHub Actions - CI/CD
๐งช Testing
# Run all tests
./scripts/test-all.sh
# Run unit tests only
poetry run pytest tests/unit -v
# Run integration tests
poetry run pytest tests/integration -v
# Run with coverage
poetry run pytest --cov=src/canopy --cov-report=html
๐ฆ Installation
From Source
# Clone repository
git clone https://github.com/larancibia/canopy-lang.git
cd canopy
# Install with Poetry
poetry install
# Or with pip (when published)
pip install canopy-lang
Docker
# Pull images
docker pull ghcr.io/larancibia/canopy-lang/api:latest
docker pull ghcr.io/larancibia/canopy-lang/web:latest
# Run with Docker Compose
docker-compose up
๐ค Contributing
We welcome contributions! Please see our Development Guide.
# Setup development environment
./scripts/setup.sh
# Create a feature branch
git checkout -b feature/amazing-feature
# Make changes and run tests
./scripts/test-all.sh
# Commit and push
git commit -m "Add amazing feature"
git push origin feature/amazing-feature
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐ Roadmap
MVP (Current)
- Core DSL parser
- Basic indicators
- Simple backtest engine
- CLI interface
- REST API
- Web frontend
- Docker deployment
v0.2.0 (Next)
- Portfolio backtesting (multi-asset)
- Walk-forward optimization
- Real-time data feeds
- More indicators (50+)
- Custom indicator creation UI
v0.3.0 (Future)
- Live trading integration
- Machine learning integration
- Options and futures support
- Social trading features
- Cloud hosting (SaaS)
๐ Support
- Documentation: github.com/larancibia/canopy-lang#readme
- Issues: GitHub Issues
- Discussions: GitHub Discussions
- Email: arancibialuisalejandro@gmail.com
๐ Acknowledgments
- Built with FastAPI
- Inspired by Pine Script
- Data from yfinance
Made with โค๏ธ by Luis Arancibia
โญ Star us on GitHub if you find this project helpful!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file canopy_lang-0.0.2.tar.gz.
File metadata
- Download URL: canopy_lang-0.0.2.tar.gz
- Upload date:
- Size: 77.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8a0871706e5079916d508f1f2ba019b98bfab1476e8c1c27a2548695b5e44399
|
|
| MD5 |
28e2f0b22daf7a3c81680412d95caf80
|
|
| BLAKE2b-256 |
68a5663fef686e9267b800fbbd48e4e7695d1297f59106a83ca651d9b0a3b628
|
File details
Details for the file canopy_lang-0.0.2-py3-none-any.whl.
File metadata
- Download URL: canopy_lang-0.0.2-py3-none-any.whl
- Upload date:
- Size: 109.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
90f29154cc3167477c3cb4c9e3195b46c2479c6cd223c22cff32ee60d45b9c95
|
|
| MD5 |
450fb5b9e112170bb71cbf5b008aab36
|
|
| BLAKE2b-256 |
b22fe12d9c4a9171f6ffe1b0892b94189c738ace718c2e3f9678a7d6a4c95385
|