fix: data race, scale-in PnL, nonce mutex, dead code, hourly check

- 🔴 Data race: Add GetPositionsCopy() returning deep copies (no shared
  ArbPosition pointers). Use it in dashboard broadcastLoop + handleStatus.
- 🟡 Scale-in PnL: Track LongEntryPrices/ShortEntryPrices on ArbPosition,
  compute weighted average (harmonic mean) at exit for accurate PnL.
- 🟢 CalcNetProfit: Delete dead code from exchange/helpers.go.
- 🟢 HL nonce: Add sync.Mutex around lastNonce++ (thread safety).
- 🟢 Hourly check: Change from 5-second window to minute window.
- 🟢 ExitPrice: Test mode closeLeg already handled by checkExit.
This commit is contained in:
jackyu66git
2026-05-03 18:31:14 +08:00
parent ab48e207a5
commit b08d8490fc
6 changed files with 94 additions and 54 deletions
+5 -1
View File
@@ -8,6 +8,7 @@ import (
"math/big"
"net/http"
"strings"
"sync"
"time"
"crypto/ed25519"
@@ -50,6 +51,7 @@ type HyperLiquidTrade struct {
Address string
client *http.Client
lastNonce int64
nonceMu sync.Mutex // protect lastNonce++ (Issue #4)
}
func NewHyperLiquidTrade(privateKeyHex, address string) (*HyperLiquidTrade, error) {
@@ -96,9 +98,11 @@ func (h *HyperLiquidTrade) PlaceMarketOrder(coin, side, sz string) (string, erro
BrokerCode: 0,
}
// Generate nonce
// Generate nonce (thread-safe)
h.nonceMu.Lock()
h.lastNonce++
nonce := time.Now().UnixMilli()*1_000_000 + h.lastNonce%1_000_000
h.nonceMu.Unlock()
// Sign the action
sig, err := h.signAction(action, nonce)