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:
+3
-40
@@ -1,42 +1,5 @@
|
||||
package exchange
|
||||
|
||||
import "github.com/gorilla/websocket"
|
||||
|
||||
// These are needed for compilation of the exchange package.
|
||||
// PriceConnector is defined in connector.go.
|
||||
var _ = websocket.ErrCloseSent // keep gorilla/websocket import
|
||||
|
||||
// CalcNetProfit calculates net profit % for a complete round trip (entry + exit) between two exchanges.
|
||||
// buyPrice: price on the buy exchange
|
||||
// sellPrice: price on the sell exchange
|
||||
// buyFee: fee rate on buy exchange (e.g. 0.03 for 0.03%)
|
||||
// sellFee: fee rate on sell exchange
|
||||
// buyFee2: buy fee on the other exchange
|
||||
// sellFee2: sell fee on the other exchange
|
||||
// Returns net profit in percentage.
|
||||
func CalcNetProfit(price1, price2, fee1Buy, fee1Sell, fee2Buy, fee2Sell float64) float64 {
|
||||
// price1 = Bitget, price2 = HyperLiquid
|
||||
// Try: buy cheap (min), sell expensive (max)
|
||||
buyPrice := price1
|
||||
sellPrice := price2
|
||||
buyFee := fee1Buy
|
||||
sellFee := fee2Sell
|
||||
|
||||
if price2 < price1 {
|
||||
buyPrice = price2
|
||||
sellPrice = price1
|
||||
buyFee = fee2Buy
|
||||
sellFee = fee1Sell
|
||||
}
|
||||
|
||||
// Entry: buy at buyPrice (pay buyFee), sell short at sellPrice (pay sellFee)
|
||||
if buyPrice <= 0 || sellPrice <= 0 {
|
||||
return 0
|
||||
}
|
||||
cost := buyPrice * (1 + buyFee/100)
|
||||
revenue := sellPrice * (1 - sellFee/100)
|
||||
|
||||
// Exit: sell long (pay sellFee), buy back short (pay buyFee)
|
||||
// Total fees = 2 * (buyFee + sellFee), first round already in formula above
|
||||
return (revenue/cost-1)*100 - (buyFee + sellFee)
|
||||
}
|
||||
// Package-level helpers for the exchange package.
|
||||
// connector.go imports gorilla/websocket, so this file needs no imports
|
||||
// for that dependency. CalcNetProfit was removed (see netProfit in scanner.go).
|
||||
|
||||
Reference in New Issue
Block a user