A multi-agent collaborative portfolio trading bot built with Rust, using DeepSeek LLM for decision making, and running on Binance testnet for automated cryptocurrency trading.
- Multi-Agent Architecture: Market Analyst → Strategy Researcher → Risk Manager → Trade Executor → Portfolio Coordinator
- Portfolio Management: Support for multiple symbols with intelligent fund allocation
- Professional Role Division: Each agent specializes in specific domains, collaborative decision making
- Contract Trading: Support for long/short positions, automatic position switching
- Technical Indicators: SMA(5/20/50/100), Price Change Rate, ATR, Volume Ratio
- Risk Control: Position monitoring, P&L tracking, fund allocation, error retry
flowchart TD
A[main.rs<br/>Main Program] --> B[Phase 1: Parallel Market Analysis]
A --> C[Phase 2: Portfolio Fund Allocation]
A --> D[Phase 3: Sequential Trade Execution]
B --> B1[Market Analyst<br/>market.rs]
B --> B2[Market Analyst<br/>market.rs]
B --> B3[Market Analyst<br/>market.rs]
B1 --> E[MarketReport]
B2 --> E
B3 --> E
E --> F[Portfolio Coordinator]
F --> G[PortfolioAllocation]
G --> H[Strategy Researcher]
H --> I[StrategySuggestion]
I --> J[Risk Manager]
J --> K[RiskAssessment]
K --> L[Trade Executor<br/>executor.rs]
L --> M[TradingDecision]
L --> N[TradeResult]
M --> O[state.rs<br/>Logging]
N --> O
subgraph "Parallel Execution"
B1
B2
B3
end
subgraph "Sequential Execution"
H
J
L
end
style A fill:#e1f5fe
style B1 fill:#f3e5f5
style B2 fill:#f3e5f5
style B3 fill:#f3e5f5
style F fill:#e8f5e8
style H fill:#fff3e0
style J fill:#ffebee
style L fill:#e8eaf6
style O fill:#f5f5f5
-
Market Analyst (
market.rs)- Fetch K-line data, calculate technical indicators
- Analyze market trends, strength, phases
- Output:
MarketReport
-
Strategy Researcher
- Develop trading strategies based on market analysis
- Suggest target positions, stop-loss, take-profit
- Output:
StrategySuggestion
-
Risk Manager
- Assess trading risks, approve strategies
- Calculate suggested trade quantities
- Output:
RiskAssessment
-
Trade Executor (
executor.rs)- Make final decisions based on all inputs
- Execute specific trading operations
- Output:
TradingDecision
-
Portfolio Coordinator
- Manage fund allocation across multiple symbols
- Develop portfolio strategies
- Output:
PortfolioAllocation
Copy environment template:
cp .env.example .envEdit .env file with your information:
# Binance Testnet API (from https://testnet.binancefuture.com)
BINANCE_API_KEY=your_testnet_api_key
BINANCE_SECRET=your_testnet_secret
BINANCE_TESTNET=true
# DeepSeek API (from https://platform.deepseek.com)
DEEPSEEK_API_KEY=your_deepseek_key
# Trading Configuration
TRADE_SYMBOLS=BTCUSDT,ETHUSDT # Multiple symbols, comma separated
MIN_TRADE_AMOUNT=0.001 # AI decision minimum trade quantity
MAX_TRADE_AMOUNT=0.003 # AI decision maximum trade quantity
TRADE_INTERVAL=1m # Options: 1m, 15m, 30m, 1h
LEVERAGE=10 # Leverage: 1-125
MAX_POSITION=0.005 # Maximum position per symbol
PORTFOLIO_MODE=balanced # Portfolio mode: balanced | aggressive | conservativecargo build --release
cargo run --releaseThe program automatically creates logs/ directory:
logs/trades.jsonl- Trade records (open/close positions, prices, P&L)logs/decisions.jsonl- Decision records (including multi-agent analysis process)logs/performance.json- Performance tracking data
Each line is a JSON object, view with jq:
# View recent 10 trades
tail -10 logs/trades.jsonl | jq
# View all decisions
cat logs/decisions.jsonl | jq -c '{symbol: .symbol, signal: .decision.signal, reason: .decision.reason}'
# View performance data
cat logs/performance.json | jqEach symbol executes in parallel:
- Fetch K-line Data: 120 K-lines, calculate technical indicators
- Market Analyst Decision: Analyze trends, strength, market phases
- Position Query: Get current position status
- Portfolio Coordinator Allocation: Allocate funds based on symbol analysis results
- Strategy Development: Choose portfolio strategy based on configuration
balanced: Balanced allocation, moderate riskaggressive: Concentrate on best symbols, pursue high returnsconservative: Diversified investment, control risk
Execute sequentially for each symbol:
- Strategy Researcher Suggestion: Develop specific strategies based on market conditions
- Risk Manager Assessment: Approve strategies, calculate suggested quantities
- Trade Executor Execution: Make comprehensive decisions, execute trades
System calculates the following technical indicators:
- Moving Averages: SMA5, SMA20, SMA50, SMA100
- Price Change Rate: 1-period, 3-period, 6-period, 12-period
- ATR Indicator: 14-period Average True Range
- Volume Ratio: Current volume / Average volume
- Agent Collaboration: Each decision reviewed by multiple professional roles
- Fund Allocation: Portfolio coordinator allocates funds reasonably
- Trading Constraints: Adhere to exchange minimum order quantity, minimum notional value rules
- Position Limits: Maximum position control per symbol
- Error Handling: Single failure doesn't affect overall operation
- Balanced Mode: Equally allocate based on analysis quality
- Aggressive Mode: Concentrate funds on most promising symbols
- Conservative Mode: Strictly control single symbol risk exposure
- For Learning and Research Only: This project is for technical validation, not investment advice
- Testnet First: Strongly recommend running on Binance testnet for several weeks first
- Fund Management: Even on testnet, set reasonable trading parameters
- Monitor Operation: Regularly check logs, observe decision quality
- LLM Limitations: Large language models are not crystal balls, cannot predict the future
Important Update: Binance testnet has migrated to Demo platform
- Visit https://demo.binance.com
- Login with GitHub or Google account (or register new account)
- Go to Futures trading page
- Click top-right avatar → API Management
- Create new API Key (automatically get test funds)
- Copy API Key and Secret to
.envfile
Note:
- Testnet REST API address:
https://testnet.binancefuture.com - Demo platform automatically provides virtual USDT for testing
- Testnet data syncs with mainnet in real-time, but trades don't affect real funds
- Visit https://platform.deepseek.com
- Register account and top up (first time gets free credits)
- Create API key in API Keys page
Check if .env file exists and is correctly configured.
- Check network connection
- Confirm Binance testnet is accessible
- Check if rate limited (retry later)
Edit TRADE_INTERVAL in .env file:
1m- 1 minute15m- 15 minutes30m- 30 minutes1h- 1 hour
Edit TRADE_SYMBOLS in .env file, separate multiple pairs with commas:
TRADE_SYMBOLS=BTCUSDT,ETHUSDT,SOLUSDT,ADAUSDTTotal: 1000+ lines
├── main.rs 300+ lines (Main program + Portfolio coordination)
├── executor.rs 267 lines (Trade execution + HMAC signing)
├── multi_agent.rs 200+ lines (Multi-agent collaboration logic)
├── llm.rs 179 lines (DeepSeek API + Prompts)
├── market.rs 118 lines (Binance API + Indicator calculation)
├── types.rs 81 lines (Data structure definitions)
├── state.rs 55 lines (Logging)
├── logging.rs 40 lines (Logging system)
└── performance.rs 30 lines (Performance tracking)
- Rust 1.83+ (edition 2021)
- async-openai - DeepSeek API client
- reqwest - HTTP requests
- tokio - Async runtime
- serde/serde_json - JSON serialization
- hmac/sha2 - Binance API signing
- anyhow - Error handling
- chrono - Time handling
- futures - Async programming
- log/flexi_logger - Logging system
MIT