Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# QuantCLI Environment Configuration
# Copy this file to .env and fill in your values

# Required: OpenAI API Key for LLM generation
OPENAI_API_KEY=your_openai_api_key_here

# Optional: OpenAI Model to use (default: gpt-4o-2024-11-20)
OPENAI_MODEL=gpt-4o-2024-11-20

# Optional: Maximum attempts to refine generated code (default: 6)
MAX_REFINE_ATTEMPTS=6

# Optional: Email for Unpaywall API (for downloading PDFs)
UNPAYWALL_EMAIL=your.email@example.com

# Optional: File paths and directories
ARTICLES_FILE=articles.json
DOWNLOADS_DIR=downloads
GENERATED_CODE_DIR=generated_code
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ output.*

# Packaging metadata
*.egg-info/
.venv/
89 changes: 89 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Changelog

All notable changes to QuantCoder will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.0.0] - 2025-11-11

### 🚀 Major Refactoring - OpenAI SDK Migration

#### Added
- **New LLMClient abstraction layer** (`quantcli/llm_client.py`)
- Unified interface for all LLM interactions
- Support for OpenAI SDK v1.x+
- Standardized response format with `LLMResponse` dataclass
- Improved error handling and logging
- Token usage tracking

- **Test infrastructure**
- pytest configuration
- Unit tests for LLMClient
- Coverage reporting (pytest-cov)
- Test markers for unit/integration/slow tests

- **Modern dependencies**
- OpenAI SDK >= 1.0.0
- Rich terminal output library
- Type checking with mypy
- Code quality with ruff

#### Changed
- **Breaking**: Migrated from OpenAI SDK 0.28 to 1.x+
- Replaced deprecated `openai.ChatCompletion.create()` calls
- Updated all LLM interactions in `processor.py` to use LLMClient
- Removed global `openai.api_key` configuration

- **Improved dependency management**
- Bumped Python requirement to >= 3.9
- Pin minimum versions for all dependencies
- Created clean `requirements.txt` (removed legacy freeze)

- **Enhanced setup.py**
- Version bumped to 1.0.0
- Updated classifiers for PyPI
- Improved project description
- Added support for Python 3.9-3.12

#### Removed
- Direct `openai` module imports from utils.py
- Hardcoded global API key setting
- Legacy OpenAI 0.28 compatibility code

### Migration Guide

**For existing users upgrading from 0.3:**

1. Update your environment:
```bash
pip install --upgrade openai>=1.0.0
pip install -e .
```

2. No code changes required - the LLMClient abstraction handles SDK differences internally

3. Ensure `OPENAI_API_KEY` is set in your environment or `.env` file

### Technical Debt Addressed
- ✅ OpenAI SDK obsolescence (0.28 → 1.x+)
- ✅ Missing test coverage
- ✅ Lack of structured logging for LLM calls
- ✅ Token usage visibility

### Known Issues
- GUI module (gui.py) not yet updated - marked for deprecation
- End-to-end integration tests pending
- Documentation needs refresh for v1.0.0

---

## [0.3] - 2024-10-01

### Legacy Version
- Original CLI implementation
- OpenAI SDK 0.28
- Basic PDF processing and code generation
- CrossRef article search
- Interactive mode

193 changes: 151 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,91 +1,200 @@
# QuantCoder

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Python 3.9+](https://img.shields.io/badge/python-3.9+-blue.svg)](https://www.python.org/downloads/)
[![OpenAI](https://img.shields.io/badge/OpenAI-1.0+-green.svg)](https://github.com/openai/openai-python)
# QuantCoder (CLI Version)

QuantCoder is a command-line tool that allows users to generate QuantConnect trading algorithms from research articles using natural language processing and large language models (LLMs). It was initiated in November 2023 and based on a cognitive architecture inspired by the article ["Dual Agent Chatbots and Expert Systems Design"](https://towardsdev.com/dual-agent-chatbots-and-expert-systems-design-25e2cba434e9)
> **Transform academic trading research into executable QuantConnect algorithms using AI.**

The initial version successfully coded a blended momentum and mean-reversion strategy as described in ["Outperforming the Market (1000% in 10 years)"](https://medium.com/coinmonks/how-to-outperform-the-market-fe151b944c77?sk=7066045abe12d5cf88c7edc80ec2679c), which received over 10,000 impressions on LinkedIn.
QuantCoder is a command-line tool that converts research papers into production-ready QuantConnect trading algorithms using natural language processing and large language models. Based on a dual-agent cognitive architecture, it extracts trading signals, risk management rules, and generates tested Python code.

## ✨ Key Features
As of November 2025, it is under refactoring with readiness expected in February 2026.

---

## 🚀 First-Time Installation
- 📄 **PDF Processing**: Extract trading strategies from academic papers
- 🔍 **CrossRef Integration**: Search and download financial research articles
- 🤖 **AI-Powered Code Generation**: Uses GPT-4o to generate QuantConnect algorithms
- ✅ **Syntax Validation**: Automatic code validation and refinement
- 🎯 **Dual-Agent Architecture**: Separates strategy extraction from code generation
- 📊 **Rich Terminal UI**: Beautiful, interactive command-line interface

> ✅ Requires **Python 3.8 or later**
## 🚀 Installation

### 🛠 Setup Instructions
### Requirements
- **Python 3.9 or later**
- OpenAI API key

```bash
# Clone the repository and switch to the legacy branch
git clone https://github.com/SL-Mar/QuantCoder.git
cd QuantCoder
git checkout quantcoder-legacy
### Setup

# Create and activate a virtual environment
python -m venv .venv-legacy
```bash
# Clone the repository
git clone https://github.com/SL-Mar/quantcoder-legacy.git
cd quantcoder-legacy

# On Windows:
.\.venv-legacy\Scripts\activate
# On macOS/Linux:
source .venv-legacy/bin/activate
# Create and activate virtual environment
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate

# Install dependencies and the CLI
# Install QuantCoder
pip install -e .

# Download required NLP model
python -m spacy download en_core_web_sm
pip install openai==0.28

# Set your OpenAI API key
echo "OPENAI_API_KEY=your-api-key-here" > .env
```

You may also freeze dependencies:
## 💡 Usage

```bash
pip freeze > requirements-legacy.txt
```
### Interactive Mode

---
🧠 LLM Configuration
By default, this project uses the OpenAI gpt-4o-2024-11-20 model for generating trading code from research articles.
Launch the interactive CLI:

## 💡 Usage
```bash
quantcli interactive
```

To launch the CLI tool in interactive mode:
Or:

```bash
python -m quantcli.cli interactive
```

Or if `quantcli` is recognized as a command:
### Command-Line Interface

**Search for research articles:**

```bash
quantcli interactive
quantcli search "momentum trading strategies" --num 5
```

---
**List previously searched articles:**

## ⚠️ OpenAI SDK Compatibility
```bash
quantcli list
```

This legacy version uses the **OpenAI SDK v0.28**. Newer versions (`>=1.0.0`) are **not supported**.
**Download an article:**

```bash
quantcli download 1
```

If you encounter this error:
**Process a PDF to generate algorithm:**

```bash
quantcli process path/to/research-paper.pdf
```
You tried to access openai.ChatCompletion, but this is no longer supported...

## 📚 Example Workflow

1. **Search for trading research:**
```bash
quantcli search "mean reversion high frequency" --num 3
```

2. **Download an interesting paper:**
```bash
quantcli download 1
```

3. **Generate QuantConnect algorithm:**
```bash
quantcli process downloads/paper.pdf
```

4. **Review generated code in `generated_code/` directory**

5. **Copy to QuantConnect and backtest**

## 🏗️ Architecture

QuantCoder uses a dual-agent system:

1. **Extraction Agent**: Analyzes PDF, identifies trading signals and risk management rules
2. **Generation Agent**: Converts extracted information into QuantConnect Python code
3. **Validation Layer**: Checks syntax and refines code using AST analysis

## 📊 What's New in v1.0.0

### Major Improvements

✅ **Migrated to OpenAI SDK 1.x+** - Modern API with better error handling
✅ **LLMClient abstraction layer** - Easily swap LLM providers
✅ **Token usage tracking** - Monitor API costs
✅ **Test infrastructure** - pytest with coverage reporting
✅ **Improved logging** - Structured logs for debugging
✅ **Type hints** - Better code quality with mypy support

See [CHANGELOG.md](CHANGELOG.md) for full details.

## 🧪 Testing

Run the test suite:

```bash
pytest
```

Fix it by running:
With coverage:

```bash
pip install openai==0.28
pytest --cov=quantcli --cov-report=html
```

---
## 📖 Success Stories

## 📁 Articles and Strategies
- ✅ **10K+ LinkedIn impressions** on first algorithm generated
- ✅ **79 GitHub stars** from quantitative trading community
- ✅ **21 forks** actively used by traders worldwide

The folder 'Strategies and publications' contains articles and trading strategies generated using this CLI tool. These strategies may have been manually refined or enhanced using LLM-based methods. Use them at your own discretion — conduct thorough research and validate before live use.
Original case study: ["Outperforming the Market (1000% in 10 years)"](https://medium.com/coinmonks/how-to-outperform-the-market-fe151b944c77)

---
## 🔧 Configuration

## 📜 License
Create a `.env` file in the project root:

This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
```env
OPENAI_API_KEY=your-openai-api-key
```

Optional configuration:

```env
# Change default model (default: gpt-4o-2024-11-20)
OPENAI_MODEL=gpt-4-turbo-preview
```

## 🤝 Contributing

Contributions welcome! Please:

1. Fork the repository
2. Create a feature branch (`git checkout -b feature/amazing-feature`)
3. Commit your changes (`git commit -m 'Add amazing feature'`)
4. Push to the branch (`git push origin feature/amazing-feature`)
5. Open a Pull Request

## 📝 License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## 🙏 Acknowledgments

- Inspired by ["Dual Agent Chatbots and Expert Systems Design"](https://towardsdev.com/dual-agent-chatbots-and-expert-systems-design-25e2cba434e9)
- Built for the [QuantConnect](https://www.quantconnect.com/) algorithmic trading platform
- Powered by [OpenAI GPT-4](https://openai.com/)

## 📧 Contact

**Author**: SL-MAR
**Email**: smr.laignel@gmail.com
**GitHub**: [@SL-Mar](https://github.com/SL-Mar)

---

⭐ **If QuantCoder helps your trading research, give it a star!** ⭐
Loading