Evolutionary Quantitative Trading Strategy Development System
This project is a fork of OpenEvolve with modifications and enhancements focused on quantitative trading strategy development.
QuantEvolve is a system that leverages evolutionary algorithms and Large Language Models (LLMs) to automatically develop and optimize quantitative trading strategies. The primary goal is to continuously evolve trading strategy code, written in Python, to achieve optimal risk-adjusted returns when backtested on historical market data. By iteratively refining strategies based on performance metrics, QuantEvolve aims to discover novel and effective approaches to trading.
The system is composed of several key components that work together in a cycle:
┌─────────────────┐ ┌──────────────────┐ ┌─────────────────┐
│ Market Data │────▶│ Strategy │────▶│ Backtester │
│ Collector │ │ Generator │ │ & Evaluator │
│ (Binance API) │ │ (LLM Engine) │ │ │
└─────────────────┘ └──────────────────┘ └─────────────────┘
│ ▲ │
│ │ │
│ ┌────────┴────────┐ │
└─────────────▶│ Strategy │◀───────────────┘
│ Database │
└─────────────────┘
- Market Data Collector: Fetches historical market data (OHLCV) from sources like the Binance API. This data forms the basis for backtesting.
- Strategy Generator (LLM Engine): Utilizes Google Gemini models to generate new trading strategies or modify existing ones. It takes promising strategies and performance feedback to suggest code changes.
- Backtester & Evaluator: Executes the generated trading strategies against historical market data. It calculates various performance metrics (e.g., PnL, Sharpe Ratio, Max Drawdown) to assess the effectiveness of each strategy.
- Strategy Database: Stores all evaluated strategies, their code, and their performance metrics. This database serves as a pool of "genetic material" for the evolutionary process, allowing the system to learn from past successes and failures.
- Automated Strategy Development: Automates the workflow from market data collection to trading strategy generation, backtesting, and iterative refinement.
- Evolutionary Optimization: Employs LLMs to analyze existing strategies and their performance, then intelligently modifies their code or creates entirely new strategies to improve upon them.
- Market Adaptation (Goal): While the current implementation relies on historical backtesting, the long-term vision is for strategies to adapt to changing market conditions, potentially through continuous retraining or online learning mechanisms.
- Python (3.9+ recommended)
- Git
-
Clone the repository:
git clone https://github.com/jhyang0/quantevolve.git
-
Navigate to the directory:
cd quantevolve
-
Create a virtual environment (recommended):
python -m venv venv source venv/bin/activate # On Windows use: venv\Scripts\activate
-
Install dependencies: QuantEvolve is designed to be installed as a package. This command installs the
quantevolve
package itself along with its core dependencies listed insetup.py
.pip install -e .
Additionally, for the specific QuantEvolve functionalities, you need to install:
pip install google-generativeai python-binance pandas numpy
The
google-generativeai
package is required for the Gemini integration.
-
Google Gemini API Key: QuantEvolve uses Google Gemini models for strategy generation. You need to provide a Google API key. You can either:
- Set the
GOOGLE_API_KEY
environment variable:
export GOOGLE_API_KEY="your_google_api_key_here"
- Add it directly to the
quantevolve_config.yaml
file:
llm: api_key: "your_google_api_key_here"
- Set the
-
Binance API for Data Collection: The
quantevolve/data/market_data_collector.py
script fetches historical kline data from Binance. You can provide your Binance API credentials in two ways:- Set environment variables:
export BINANCE_API_KEY="your_binance_api_key_here" export BINANCE_API_SECRET="your_binance_api_secret_here"
- Add them to the
quantevolve_config.yaml
file (recommended):
data_collection: binance_api_key: "your_binance_api_key_here" binance_api_secret: "your_binance_api_secret_here" # You can also customize these default parameters: default_symbol: "BTCUSDT" default_interval: "1h" default_lookback_days: 365
Before running the evolution, you need historical market data.
python -m quantevolve.data.market_data_collector
This script downloads historical OHLCV data based on your configuration settings in quantevolve_config.yaml
(or defaults to BTC/USDT 1-hour interval for the past year). The data is saved to the specified path and is used by the quant_evaluator.py
for backtesting.
Once the market data is available, start the QuantEvolve process:
python -m quantevolve.cli
This command will:
- Use
quantevolve/strategy/initial_strategy.py
as the starting point for evolution. - Use
quantevolve/evaluation/quant_evaluator.py
to backtest and evaluate strategies. - Load its configuration from
configs/quantevolve_config.yaml
. - Save outputs to
quantevolve_output/
(the default output directory).
You can customize the evolution parameters, LLM settings, and other aspects by modifying configs/quantevolve_config.yaml
. For additional command line options, run:
python -m quantevolve.cli --help
QuantEvolve saves all its outputs in the quantevolve_output/
directory (or the directory specified by the --output_dir
argument if used, or configured in the YAML file).
best/
:best_strategy.py
: The Python script of the best overall trading strategy found during the entire run.best_strategy_info.json
: A JSON file containing metadata and detailed performance metrics for thebest_strategy.py
.
checkpoints/
: Contains periodic checkpoints of the evolution state. Each checkpoint includes the current population of strategies and the best strategy found up to that point, allowing you to resume runs or analyze intermediate results.logs/
: Contains detailed logs of the evolutionary process (e.g.,quantevolve_YYYYMMDD_HHMMSS.log
), including LLM interactions, evaluation scores, and errors. This is crucial for monitoring and debugging.
Contributions are welcome! If you have suggestions for improvements, new features, or find any bugs, please feel free to:
- Report issues on the GitHub issue tracker.
- Suggest features or enhancements.
- Submit pull requests with your contributions.
Please see CONTRIBUTING.md
for more details on how to contribute (if the file exists).
QuantEvolve is released under the Apache 2.0 License. See the LICENSE
file for more details.