Skip to main content

A domain-specific language for trading strategy development and backtesting

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.DataFrame of OHLC data for now. The REST API and web frontend mentioned below are experimental and require pip install canopy-lang[web]. API may change before 1.0.

Tests Docker Build codecov Python 3.11+ License: MIT

โœจ 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

๐ŸŽฏ Example Strategies

Basic Strategies

Advanced Strategies

Portfolio Strategies

Python Examples

๐Ÿ—๏ธ 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

๐Ÿ™ Acknowledgments


Made with โค๏ธ by Luis Arancibia

โญ Star us on GitHub if you find this project helpful!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

canopy_lang-0.0.3.tar.gz (77.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

canopy_lang-0.0.3-py3-none-any.whl (110.6 kB view details)

Uploaded Python 3

File details

Details for the file canopy_lang-0.0.3.tar.gz.

File metadata

  • Download URL: canopy_lang-0.0.3.tar.gz
  • Upload date:
  • Size: 77.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for canopy_lang-0.0.3.tar.gz
Algorithm Hash digest
SHA256 1f9cbee9ff60f3382f8281edcf44c58de6f84afb8d7f3193209c875e0aef9c90
MD5 8448c4b124e7d1844d949be22b0b4a0f
BLAKE2b-256 9e4bfc0756418df16241233b56eef69832733b21b0a9371aa12fc74c7c554e1a

See more details on using hashes here.

File details

Details for the file canopy_lang-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: canopy_lang-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 110.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for canopy_lang-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 58088300d9c23bf51c77ad5ea217b21cafc2a6755b0ce315f24a5fd5707f9d60
MD5 3d58bfb264ae7b0bb9bdd7afe767720b
BLAKE2b-256 1953bfe04ce874f975339bdd26d5f003ac353f4b8be62ad11c8f92a98002b041

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page