Commit Graph

3 Commits

Author SHA1 Message Date
0xYYBB | ZYY | Bobo
36365a3b30 fix(prompts): reduce margin usage from 95% to 88% for Hyperliquid liquidation buffer (#666)
## Problem
Users with small accounts (<$200) encounter Hyperliquid error:
"Insufficient margin to place order. asset=1"
Real case: $98.89 account failed to open position
## Root Cause
5% reserve insufficient for:
- Trading fees (~0.04%)
- Slippage (0.01-0.1%)
- Liquidation margin buffer (Hyperliquid requirement)
Additionally, undefined "Allocation %" parameter caused confusion.
## Solution
1. Reduce margin usage rate from 95% to 88% (reserve 12%)
2. Remove undefined "Allocation %" parameter
3. Add small account example ($98.89) for clarity
## Example ($98.89 account)
Before: $93.95 margin → $4.75 remaining 
After:  $87.02 margin → $11.87 remaining 
## Modified Files
- prompts/adaptive.txt
- prompts/default.txt
- prompts/nof1.txt
## Testing
Verified with $98.89 account on z-dev branch - successful order placement
Fixes #549
2025-11-07 09:19:20 +08:00
ZhouYongyou
4e6b868531 fix(margin): correct position sizing formula to prevent insufficient margin errors
## Problem
AI was calculating position_size_usd incorrectly, treating it as margin requirement instead of notional value, causing code=-2019 errors (insufficient margin).
## Solution
### 1. Updated AI prompts with correct formula
- **prompts/adaptive.txt**: Added clear position sizing calculation steps
- **prompts/nof1.txt**: Added English version with example
- **prompts/default.txt**: Added Chinese version with example
**Correct formula:**
1. Available Margin = Available Cash × 0.95 × Allocation % (reserve 5% for fees)
2. Notional Value = Available Margin × Leverage
3. position_size_usd = Notional Value (this is the value for JSON)
**Example:** $500 cash, 5x leverage → position_size_usd = $2,375 (not $500)
### 2. Added code-level validation
- **trader/auto_trader.go**: Added margin checks in executeOpenLong/ShortWithRecord
- Validates required margin + fees ≤ available balance before opening position
- Returns clear error message if insufficient
## Impact
- Prevents code=-2019 errors
- AI now understands the difference between notional value and margin requirement
- Double validation: AI prompt + code check
## Testing
-  Compiles successfully
- ⚠️ Requires live trading environment testing
2025-11-04 18:44:07 +08:00
SkywalkerJi
5ad135310f Supports custom system prompts and custom models. 2025-11-01 19:45:54 +08:00