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
+16 -2
View File
@@ -3,13 +3,16 @@ package exchange
import (
"encoding/json"
"log"
"sync"
"time"
)
// DydxWS connects to dYdX v4 WebSocket for market data (oracle prices).
type DydxWS struct {
Conn *PriceConnector
Tracked []string // coin names like ["BTC", "ETH", ...]
Conn *PriceConnector
Tracked []string // coin names like ["BTC", "ETH", ...]
stopHeartbeat chan struct{}
heartbeatMu sync.Mutex
}
type dydxSubscribeMsg struct {
@@ -53,6 +56,15 @@ func (d *DydxWS) Run(updateFn func(coin string, price, bid, ask float64)) error
log.Printf("[dYdX WS] Subscribe error: %v", err)
}
// B#4: Stop any previous heartbeat goroutine before starting a new one
d.heartbeatMu.Lock()
if d.stopHeartbeat != nil {
close(d.stopHeartbeat)
}
d.stopHeartbeat = make(chan struct{})
hbStop := d.stopHeartbeat
d.heartbeatMu.Unlock()
// dYdX requires JSON {"type":"ping"} every ~30s
go func() {
heartbeat := time.NewTicker(15 * time.Second)
@@ -65,6 +77,8 @@ func (d *DydxWS) Run(updateFn func(coin string, price, bid, ask float64)) error
if err := d.Conn.SendJSON(map[string]string{"type": "ping"}); err != nil {
log.Printf("[dYdX WS] Heartbeat send error: %v", err)
}
case <-hbStop:
return
case <-d.Conn.Done():
return
}