Fix 8 bugs from code review

B#1 — sigCh shared across goroutines, SIGINT unreliable
  → context.WithCancel: main loop cancels ctx on SIGINT,
    4 WS goroutines select on ctx.Done() instead of shared sigCh

B#3 — restoreOpenPositions missing LastScaleAt
  → Set LastScaleAt = tr.OpenedAt on restore so scale-in cooldown works

B#4 — dYdX heartbeat goroutine leaks on reconnect
  → Added stopHeartbeat chan + heartbeatMu mutex; close old channel
    before spawning new heartbeat goroutine

B#5 — GetBitgetSize fmt.Sprintf rounds up, may exceed amountUSD
  → Added math.Floor(sz*multiplier)/multiplier before format to round
    DOWN to nearest valid step size for every coin

B#6 — netProfit and CalcNetProfit duplicate formula
  → scanner.go netProfit now delegates to exchange.CalcNetProfit

B#7 — Aevo Run callback only 2 params, incompatible with startExchange
  → Changed to 4-arg callback func(coin, price, bid, ask) with bid=ask=0

B#8 — parseFloat uses fmt.Sscanf (slow, locale-sensitive)
  → Replaced with strconv.ParseFloat

B#9 — dYdX receives hlSymbols instead of its own symbol list
  → Added dydxSymbols var, built from c.HL like other exchanges
This commit is contained in:
jackyu66git
2026-05-03 17:48:06 +08:00
parent c2489614a7
commit eb74495470
7 changed files with 45 additions and 19 deletions
+10 -3
View File
@@ -2,6 +2,7 @@ package main
import (
"bytes"
"context"
"fmt"
"io"
"log"
@@ -61,16 +62,21 @@ func main() {
log.Printf("[Trader] Automated trading DISABLED (set TRADE_ENABLED=1 or TEST_MODE=true in .env)")
}
// Context for graceful shutdown — replaces shared sigCh (B#1)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt, syscall.SIGUSR1)
// Collect symbols
var bnSymbols, bgSymbols, hlSymbols []string
var bnSymbols, bgSymbols, hlSymbols, dydxSymbols []string
var aevoSymbols []exchange.TrackedSymbol
for _, c := range TrackedCoins {
bnSymbols = append(bnSymbols, c.BN)
bgSymbols = append(bgSymbols, c.BG)
hlSymbols = append(hlSymbols, c.HL)
dydxSymbols = append(dydxSymbols, c.HL)
aevoSymbols = append(aevoSymbols, exchange.TrackedSymbol{
Coin: c.Name,
InstrumentID: c.Name + "-PERP",
@@ -87,7 +93,7 @@ func main() {
})
log.Printf("[%s] WS error: %v (reconnecting...)", name, err)
select {
case <-sigCh:
case <-ctx.Done():
return
case <-time.After(3 * time.Second):
}
@@ -98,7 +104,7 @@ func main() {
startExchange("Binance", exchange.NewBinanceWS(bnSymbols).Run)
startExchange("HyperLiquid", exchange.NewHyperLiquidWS(hlSymbols).Run)
startExchange("Bitget", exchange.NewBitgetWS(bgSymbols).Run)
startExchange("dYdX", exchange.NewDydxWS(hlSymbols).Run)
startExchange("dYdX", exchange.NewDydxWS(dydxSymbols).Run) // B#9: use dedicated symbol list
log.Println("[Monitor] Waiting for initial data...")
time.Sleep(10 * time.Second)
@@ -131,6 +137,7 @@ func main() {
continue
}
log.Println("[Monitor] Shutting down...")
cancel() // B#1: cancel context to stop all WS goroutines
runLoop = false
case <-statusTick.C: