quant-flow
quant-flow copied to clipboard
AI-powered crypto perpetual futures trading bot for Hyperliquid DEX.
Quant Flow
AI-powered crypto perpetual futures trading bot for Hyperliquid DEX
π Full Documentation Β· δΈζ
β οΈ Disclaimer: This project is for educational and research purposes only. Leveraged trading carries substantial risk of loss. Always test on testnet before using real funds.
What is Quant Flow?
Quant Flow is an AI-powered automated trading system for Hyperliquid DEX, built on LangChain/LangGraph. It supports two independent trading strategies:
| Strategy | Entry Point | Description |
|---|---|---|
| Perpetual Agent | main.py |
Multi-agent architecture with one independent decision context per trading pair |
| Grid Flow | grid_main.py |
AI-driven grid market making β LLM judges direction & width, math engine calculates params |
Key Features
Core Capabilities
- π€ Multi-Agent Architecture β Independent decision-making per trading pair
- π Multi-LLM Support β OpenAI, NVIDIA, Google, Cloudflare, LiteLLM
- π Grid Flow Strategy β AI-driven dynamic grid market making
- π Kelly Formula Position Sizing β Dynamic optimal position calculation
- π‘οΈ ATR Dynamic Stop-Loss/Take-Profit β Volatility-adaptive risk management
- π Account Protection β Plugin-based: max drawdown / daily loss / consecutive loss / position timeout, each independently togglable
- π Decision Validation β Multi-timeframe trend resonance, signal quality
- π Backtesting β
single/gridstrategies with checkpoint resume - π API Fallback β LLM and Hyperliquid API fallback mechanisms
AI Decision Enhancements (Research-Backed)
| Feature | Paper | Config | Description |
|---|---|---|---|
| FinCoT Reasoning | arXiv:2506.16123 | prompt.set: nof1-improved |
6-step forced reasoning chain, +17% accuracy, -8.9x token cost |
| Bull/Bear Debate | arXiv:2412.20138 | debate.enabled |
Two agents debate bull/bear to eliminate confirmation bias |
| CEX Signals + On-chain | MDPI Mathematics 14(2):346 | enhanced_analysis.enabled |
Binance funding rate, Fear&Greed, MVRV/SOPR signals |
| Regime Adaptive | Springer Digital Finance | regime_adaptive.enabled |
Dynamic params for trending/ranging/volatile market states |
| Market Monitor | β | market_monitor.enabled |
Independent thread triggers decisions on volatility spikes |
All enhancements are controlled by independent config flags and are off by default.
Quick Start
Docker (Recommended)
# 1. Initialize (auto-configure UID/GID, create directories)
bash init-deployment.sh
# 2. Configure
cp config.yaml.example config.yaml
cp config.grid.yaml.example config.grid.yaml # optional, for grid mode
vim .env # API keys and private key
vim config.yaml # trading parameters
# 3. Start (main strategy by default)
docker compose up -d
# Run grid strategy only
RUN_MODE=grid docker compose up -d
# Run both strategies simultaneously
RUN_MODE=all docker compose up -d
# View logs
docker compose logs -f
Local Development
# Install uv (if not installed)
curl -LsSf https://astral.sh/uv/install.sh | sh
# Install dependencies (Python 3.11+ required)
uv sync
# Configure
cp .env.example .env
cp config.yaml.example config.yaml
# Run main strategy
uv run python main.py
# Run grid strategy
uv run python grid_main.py --config config.grid.yaml --env-file .env
Configuration
Environment Variables (.env)
# LLM API (configure based on client_type in config.yaml)
NVIDIA_API_KEY=xxx
OPENAI_API_KEY=xxx
OPENAI_API_BASE=xxx
# Hyperliquid
HYPERLIQUID_PRIVATE_KEY=0x... # wallet private key
HYPERLIQUID_TESTNET=true # true=testnet, false=mainnet
Wallet modes: Single wallet (fill
HYPERLIQUID_PRIVATE_KEYonly) or API wallet proxy (also fillHYPERLIQUID_ACCOUNT_ADDRESS, requires authorization on the main wallet webpage).
Trading Config (config.yaml)
llm:
client_type: langchain_nvidia # openai / cloudflare / google / litellm / nvidia
model: qwen/qwen3.5-122b-a10b # pick any model your provider supports
temperature: 0.2
trading:
symbols: [BTC, ETH]
max_trade_amount: 100
max_leverage: 10
prompt:
set: nof1-improved # recommended: integrates FinCoT 6-step reasoning
enhanced_analysis:
enabled: true
debate:
enabled: false # +2 LLM calls per decision
regime_adaptive:
enabled: false # requires enhanced_analysis: true
# Plugin-based protection (new format). Empty list = no risk control.
# The legacy `account_protection: { enabled: true, ... }` block is still
# auto-migrated for backward compatibility.
protections:
- name: max_drawdown
max_drawdown_pct: 0.10
pause_hours: 4
- name: daily_loss
max_daily_loss_pct: 0.05
pause_hours: 4
- name: consecutive_loss
max_consecutive_losses: 5
per_symbol: true # true = lock only the losing symbol
pause_hours: 4
- name: position_timeout
max_position_hours: 48
market_monitor:
enabled: false
alert_threshold_pct: 3.0
See config.yaml.example for the full reference.
Backtesting
# Single agent backtest
uv run python backtest.py --symbol BTC --strategy single \
--start-date 2024-01-01 --end-date 2024-12-01
# Grid strategy backtest
uv run python backtest.py --symbol BTC --strategy grid \
--start-date 2024-01-01 --end-date 2024-12-01
# Resume from checkpoint
uv run python backtest.py --resume-from backtest_results/backtest_BTC_xxx/live_report.json
# Deterministic replay: record once, replay deterministically (single strategy only)
uv run python backtest.py --symbol BTC --strategy single \
--start-date 2024-01-01 --end-date 2024-03-01 \
--record-decisions decisions.jsonl
uv run python backtest.py --symbol BTC --strategy single \
--start-date 2024-01-01 --end-date 2024-03-01 \
--replay-decisions decisions.jsonl # skips LLM, runs in seconds
# A/B comparison (test effect of specific features)
uv run python backtest_comparison.py --symbol BTC --compare all
uv run python backtest_comparison.py --symbol BTC --compare fincot
See BACKTEST_README.md for full backtest documentation.
Testing
uv run pytest tests/
uv run pytest tests/test_decision_validator.py -v
uv run pytest tests/ --cov=src
Project Structure
quant-flow/
βββ main.py # Main strategy entry
βββ grid_main.py # Grid strategy entry
βββ backtest.py # Backtest runner
βββ backtest_comparison.py # A/B comparison tool
βββ src/
β βββ agent/ # Agent implementations
β βββ trading/ # Trading core (client, orders, grid manager)
β βββ plugins/protections/ # Plugin-based risk control (drawdown, daily loss, etc.)
β βββ data/ # Market data, indicators, enricher, candle align
β βββ llm/ # LLM client wrappers
β βββ backtest/ # Backtest engine + decision recorder/replayer
β βββ notification/ # Notification module
βββ prompts/ # 8 prompt strategy sets
βββ website/ # Docusaurus documentation site
βββ tests/ # Test suite
Docker Management
docker compose up -d # Start
docker compose down # Stop
docker compose logs -f # Logs
docker compose ps # Status
# Update
git pull && docker compose build && docker compose up -d
Troubleshooting
| Error | Solution |
|---|---|
PermissionError: /app/logs/... |
Run bash init-deployment.sh |
open interest is at cap |
Asset hit OI cap, use a different trading pair |
Leverage exceeds maximum allowed |
Lower max_leverage in config |
| API wallet can't trade | Authorize the API wallet on the main wallet webpage |
Links
- π Full Documentation
- π¦ Hyperliquid
- π° Testnet Faucet
- βοΈ LangChain
π¨π³ δΈζθ―΄ζ README.zh-Hans.md