43 lines
1.4 KiB
Go
43 lines
1.4 KiB
Go
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)
|
|
}
|