Commit Graph

7 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 40c0995bfe fix(prompts): correct risk_usd formula - remove duplicate leverage multiplication
## Problem (Issue #592)
risk_usd formula incorrectly multiplies leverage twice:
- Incorrect: risk_usd = |Entry - Stop| × Position Size × Leverage 
This causes AI to calculate risk as 10x (or leverage倍) higher than actual.
## Root Cause
Position Size already includes leverage effect:
- Position Size (coins) = position_size_usd / price
- position_size_usd = margin × leverage
- Therefore: Position Size = (margin × leverage) / price
Multiplying leverage again amplifies risk calculation by "leverage" times.
## Example
Scenario: $100 margin, 10x leverage, 0.02 BTC position, $500 stop distance
**Correct calculation:**
risk_usd = $500 × 0.02 = $10 
Risk % = 10% of margin (reasonable)
**Incorrect calculation (current):**
risk_usd = $500 × 0.02 × 10 = $100 
Risk % = 100% of margin (completely wrong!)
## Impact
- AI miscalculates risk as "leverage" times higher
- May refuse valid trades thinking risk is too high
- Risk control logic becomes ineffective
- Potential for position sizing errors
## Solution
Correct formula: risk_usd = |Entry - Stop| × Position Size (coins)
Added warnings:
- CN: ⚠️ 不要再乘杠杆:仓位数量已包含杠杆效应
- EN: ⚠️ Do NOT multiply by leverage: Position Size already includes leverage effect
## Modified Files
- prompts/adaptive.txt (line 404)
- prompts/nof1.txt (line 104)
Closes #592
2025-11-07 08:46:37 +08:00
0xYYBB | ZYY | Bobo 366a7fd5f5 fix(prompts): correct confidence scale from 0-1 to 0-100 to match backend schema (#564)
## Problem
The prompts specified confidence range as 0-1 (float), but the backend code
expects 0-100 (integer). This causes JSON parsing errors when AI outputs
values like 0.85:
```
Error: json: cannot unmarshal number 0.85 into Go struct field Decision.confidence of type int
Result: confidence defaults to 0
```
## Root Cause
**Backend Definition** (decision/engine.go:103):
```go
Confidence int `json:"confidence,omitempty"` // 信心度 (0-100)
```
**Prompts (before fix)**:
- adaptive.txt: "confidence (信心度 0-1)"
- nof1.txt: "confidence (float, 0-1)"
**buildHardSystemPrompt** (decision/engine.go:336):
```go
sb.WriteString("- `confidence`: 0-100(开仓建议≥75)\n")
```
The dynamic system prompt was correct, but the base prompts contradicted it.
## Solution
Update prompt files to use consistent 0-100 integer scale:
### adaptive.txt
- `confidence (信心度 0-1)` → `confidence (信心度 0-100)`
- `<0.85` → `<85`
- `0.85-0.90` → `85-90`
- etc.
### nof1.txt
- `confidence (float, 0-1)` → `confidence (int, 0-100)`
- `0.0-0.3` → `0-30`
- `0.3-0.6` → `30-60`
- etc.
## Impact
-  Fixes JSON parsing errors when AI outputs float values
-  Aligns prompts with backend schema
-  Consistent with buildHardSystemPrompt() output format
-  No breaking changes (backend already expects 0-100)
## Testing
```bash
# Verify backend expects 0-100
grep "Confidence int" decision/engine.go
# Output: Confidence int `json:"confidence,omitempty"` // 信心度 (0-100)
# Verify buildHardSystemPrompt uses 0-100
grep "confidence.*0-100" decision/engine.go
# Output: sb.WriteString("- `confidence`: 0-100(开仓建议≥75)\n")
# Build test
go build ./decision/...  #  PASS
```
## Related
- Addresses schema mismatch mentioned in Issue #557
- Note: confidence field is currently not validated by backend (validateDecision
  does not check confidence value), but correct schema prevents parsing errors
---
Co-authored-by: tinkle-community <tinklefund@gmail.com>
2025-11-06 00:35:53 +08:00
Icyoung 89f95d2bef Merge pull request #437 from zhouyongyou/fix/margin-calculation
fix(margin): correct position sizing formula to prevent insufficient margin errors
2025-11-05 16:13:37 +08:00
ZhouYongyou f99052e780 fix(prompts): rename actions to match backend implementation
## Problem
Backend code expects these action names:
- `open_long`, `open_short`, `close_long`, `close_short`
But prompts use outdated names:
- `buy_to_enter`, `sell_to_enter`, `close`
This causes all trading decisions to fail with unknown action errors.
## Solution
Minimal changes to fix action name compatibility:
### prompts/nof1.txt
-  `buy_to_enter` → `open_long`
-  `sell_to_enter` → `open_short`
-  `close` → `close_long` / `close_short`
-  Explicitly list `wait` action
- +18 lines, -6 lines (only action definitions section)
### prompts/adaptive.txt
-  `buy_to_enter` → `open_long`
-  `sell_to_enter` → `open_short`
-  `close` → `close_long` / `close_short`
- +15 lines, -6 lines (only action definitions section)
## Impact
-  Trading decisions now execute successfully
-  Maintains all existing functionality
-  No new features added (minimal diff)
## Verification
```bash
# Backend expects these actions:
grep 'Action string' decision/engine.go
# "open_long", "open_short", "close_long", "close_short", ...
# Old names removed:
grep -r "buy_to_enter\|sell_to_enter" prompts/
# (no results)
```
Co-Authored-By: tinkle-community <tinklefund@gmail.com>
2025-11-04 19:41:23 +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