Skip to main content

Privacy-preserving agentic SLM for quant strategy forging

Project description

📈 QuantStratForge

PyPI version License Python 3.12+ Downloads

Privacy-preserving agentic SLM for quant strategy forging

QuantStratForge is the first AI-powered quantitative strategy development platform that combines federated learning with privacy-preserving techniques to generate, backtest, and optimize trading strategies without compromising sensitive financial data.

🌟 Key Features

  • 🤖 AI Strategy Generation: Generate trading strategies using advanced language models
  • 🔒 Privacy-Preserving: Federated learning ensures your data never leaves your machine
  • 📊 Comprehensive Backtesting: Built-in backtesting engine with performance metrics
  • ⚡ Strategy Optimization: Automated parameter optimization for maximum returns
  • 🌐 Multiple Interfaces: CLI, Streamlit web app, and FastAPI REST API
  • 📈 Real-time Data: Integration with Yahoo Finance for live market data
  • 🎯 Risk Management: Built-in risk assessment and portfolio optimization

🚀 Quick Start

Installation

pip install quantstratforge

Basic Usage

from quantstratforge import DataFetcher, StrategyGenerator, Backtester, Optimizer

# 1. Fetch market data
fetcher = DataFetcher()
data = fetcher.get_time_series("AAPL")

# 2. Generate AI strategy
generator = StrategyGenerator()
strategy = generator.generate(f"Market data for AAPL: {data}")

# 3. Backtest strategy
backtester = Backtester(ticker="AAPL")
results = backtester.backtest(strategy["strategy_code"])

# 4. Optimize parameters
optimizer = Optimizer(backtester)
optimized = optimizer.optimize(strategy["strategy_code"], {
    "threshold": [0.01, 0.02, 0.03],
    "period": [10, 15, 20]
})

print(f"Sharpe Ratio: {results['sharpe_ratio']:.3f}")
print(f"Best Parameters: {optimized['best_params']}")

🖥️ Demo Applications

Streamlit Web App

Interactive web interface for strategy development:

# Install demo dependencies
pip install quantstratforge[demo]

# Run Streamlit demo
streamlit run demos/streamlit_demo.py

FastAPI REST API

RESTful API for programmatic access:

# Run FastAPI demo
python demos/fastapi_demo.py

API Documentation: Visit http://localhost:8000/docs for interactive API documentation.

Command Line Interface

# Prepare training data
quantstratforge prepare

# Train model (local or federated)
quantstratforge train --federated

# Generate strategy
quantstratforge generate --ticker AAPL --news "Positive earnings outlook"

# Backtest strategy
quantstratforge backtest --strategy_code "def strategy_func(df): ..."

# Optimize strategy
quantstratforge optimize --strategy_code "..." --params '{"threshold": [0.01, 0.02]}'

🏗️ Architecture

Core Components

  • DataFetcher: Handles market data acquisition and preprocessing
  • StrategyModel: Manages AI model training (local and federated)
  • StrategyGenerator: Generates trading strategies using trained models
  • Backtester: Executes backtesting with performance analysis
  • Optimizer: Optimizes strategy parameters for maximum returns

Federated Learning

QuantStratForge uses Flower (Federated Learning Framework) to enable collaborative model training:

# Federated training with multiple clients
model = StrategyModel()
model.federated_train(
    num_clients=3,
    num_rounds=5,
    data_path="./formatted_data"
)

📊 Performance Metrics

Backtesting Results

  • Sharpe Ratio: Risk-adjusted return metric
  • Cumulative Returns: Total portfolio performance
  • Maximum Drawdown: Largest peak-to-trough decline
  • Win Rate: Percentage of profitable trades
  • Volatility: Standard deviation of returns

Optimization Metrics

  • Parameter Sensitivity: How strategy performance varies with parameters
  • Convergence Speed: Time to find optimal parameters
  • Robustness: Performance across different market conditions

🔧 Advanced Usage

Custom Strategy Development

def custom_strategy(df):
    """Custom trading strategy implementation"""
    # Calculate technical indicators
    df['MA_20'] = df['Close'].rolling(20).mean()
    df['RSI'] = calculate_rsi(df['Close'])
    
    # Generate signals
    signals = (df['Close'] > df['MA_20']) & (df['RSI'] < 70)
    return signals.astype(int)

# Backtest custom strategy
backtester = Backtester(ticker="AAPL")
results = backtester.backtest(custom_strategy)

Federated Learning Setup

# Configure federated learning
from quantstratforge.model import StrategyModel

model = StrategyModel(
    model_name="mistralai/Mistral-7B-Instruct-v0.2",
    lora_r=16
)

# Start federated training
model.federated_train(
    num_clients=5,
    num_rounds=10,
    client_resources={"num_cpus": 2, "num_gpus": 0.5}
)

API Integration

import requests

# Generate strategy via API
response = requests.post("http://localhost:8000/api/generate-strategy", json={
    "ticker": "AAPL",
    "risk_level": "medium",
    "news_sentiment": "Positive market sentiment"
})

strategy = response.json()
print(strategy["strategy_code"])

📈 Use Cases

Individual Traders

  • Generate personalized trading strategies
  • Backtest ideas before live trading
  • Optimize parameters for maximum returns
  • Learn quantitative trading concepts

Financial Institutions

  • Develop proprietary trading algorithms
  • Collaborate on model training without data sharing
  • Scale strategy development across teams
  • Maintain regulatory compliance

Research Organizations

  • Study market behavior patterns
  • Develop new quantitative methods
  • Publish research with reproducible results
  • Collaborate with industry partners

🔒 Privacy & Security

Data Protection

  • Local Processing: All sensitive data stays on your machine
  • Federated Learning: Models trained without sharing raw data
  • Encryption: All communications encrypted in transit
  • Audit Trail: Complete logging of all operations

Compliance

  • GDPR: Full compliance with European data protection regulations
  • SOX: Meets Sarbanes-Oxley requirements for financial data
  • PCI DSS: Secure handling of financial information
  • Custom: Configurable for specific regulatory requirements

🧪 Testing

Run the comprehensive test suite:

# Install development dependencies
pip install quantstratforge[dev]

# Run tests
pytest tests/ -v

# Run with coverage
pytest tests/ --cov=quantstratforge --cov-report=html

📚 Documentation

🤝 Contributing

We welcome contributions! Please see our Contributing Guidelines for details.

Development Setup

# Clone repository
git clone https://github.com/your-username/quantstratforge.git
cd quantstratforge

# Install in development mode
pip install -e ".[dev,demo]"

# Run tests
pytest

# Format code
black .
ruff check . --fix

📄 License

This project is licensed under the Apache License 2.0. See LICENSE for details.

🆘 Support

🌟 Acknowledgments

📊 Statistics

GitHub stars GitHub forks GitHub issues GitHub pull requests


Made with ❤️ for the quantitative finance community

QuantStratForge - Where AI meets Privacy in Quantitative Trading

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

quantstratforge-0.2.1.tar.gz (15.7 kB view details)

Uploaded Source

Built Distribution

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

quantstratforge-0.2.1-py3-none-any.whl (16.0 kB view details)

Uploaded Python 3

File details

Details for the file quantstratforge-0.2.1.tar.gz.

File metadata

  • Download URL: quantstratforge-0.2.1.tar.gz
  • Upload date:
  • Size: 15.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.12.3 Linux/6.14.0-33-generic

File hashes

Hashes for quantstratforge-0.2.1.tar.gz
Algorithm Hash digest
SHA256 7064b80458278a4d706a29f13e82e2b252cd2e24ae293ddced2a590ad7c0fc0e
MD5 8391bf136c33c0407a0220c47f211328
BLAKE2b-256 021b4e773f3155223306856a48cd7d8c14f5e0c70cc874467a4bb5986278af45

See more details on using hashes here.

File details

Details for the file quantstratforge-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: quantstratforge-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 16.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.12.3 Linux/6.14.0-33-generic

File hashes

Hashes for quantstratforge-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 93fea63793a0c88730c79be15ae83a188a41e3f9b17c906fa30ca31286bb2f03
MD5 59a61f808b966a966d595abc2c3b1d96
BLAKE2b-256 a6df0c776e848d4655d73ef384742b1a7337a26d6cdc337332691d5f2d095831

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