mirror of
https://github.com/NoFxAiOS/nofx.git
synced 2026-07-14 08:16:56 +08:00
fix: isStockSymbol misclassifies crypto symbols (BTC/ETH/SOL) as stock tickers
Critical bug: isStockSymbol('BTC') returned true because 'BTC' is 3
uppercase letters and the suffix check only catches 'BTCUSDT'-style pairs.
This caused crypto trade commands to be routed to Alpaca (stock trader)
instead of crypto exchanges.
Fix: add knownCryptoSymbols map with 60+ common crypto base symbols.
Check this map first before the heuristic letter-count check.
Also includes:
- Unit tests for isStockSymbol (crypto vs stock classification)
- Alpaca support in handleSyncBalance/handleClosePosition API handlers
- Cancel orphaned SL/TP orders before manual position close
- Clear peak PnL cache on position close (prevent stale data)
- Lighter FormatQuantity uses actual market precision instead of hardcoded 4
This commit is contained in:
65
agent/tools_test.go
Normal file
65
agent/tools_test.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package agent
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestIsStockSymbol(t *testing.T) {
|
||||
tests := []struct {
|
||||
sym string
|
||||
want bool
|
||||
}{
|
||||
// Known crypto base symbols — must NOT be detected as stock
|
||||
{"BTC", false},
|
||||
{"ETH", false},
|
||||
{"SOL", false},
|
||||
{"BNB", false},
|
||||
{"XRP", false},
|
||||
{"DOGE", false},
|
||||
{"ADA", false},
|
||||
{"AVAX", false},
|
||||
{"DOT", false},
|
||||
{"LINK", false},
|
||||
{"PEPE", false},
|
||||
{"SHIB", false},
|
||||
{"TRUMP", false},
|
||||
{"USDT", false},
|
||||
{"USDC", false},
|
||||
{"W", false}, // single letter crypto
|
||||
|
||||
// Crypto pairs — must NOT be stock
|
||||
{"BTCUSDT", false},
|
||||
{"ETHUSDT", false},
|
||||
{"SOLUSDT", false},
|
||||
{"DOGEUSDT", false},
|
||||
|
||||
// Real stock tickers — must be detected as stock
|
||||
{"AAPL", true},
|
||||
{"TSLA", true},
|
||||
{"NVDA", true},
|
||||
{"MSFT", true},
|
||||
{"GOOGL", true},
|
||||
{"AMZN", true},
|
||||
{"META", true},
|
||||
{"AMD", true},
|
||||
{"PLTR", true},
|
||||
{"BA", true},
|
||||
{"F", true}, // Ford — 1 letter
|
||||
{"GM", true}, // 2 letters
|
||||
{"JPM", true}, // 3 letters
|
||||
|
||||
// Mixed / edge cases
|
||||
{"btc", false}, // lowercase crypto
|
||||
{"aapl", true}, // lowercase stock (uppercased internally)
|
||||
{"BTC123", false}, // not pure letters
|
||||
{"123456", false}, // digits
|
||||
{"", false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.sym, func(t *testing.T) {
|
||||
got := isStockSymbol(tt.sym)
|
||||
if got != tt.want {
|
||||
t.Errorf("isStockSymbol(%q) = %v, want %v", tt.sym, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user