feat: actual exchange fees for entry orders

- BitgetTrade: added GetTradeFee() queries /mix/order/fills
- HyperLiquidTrade: added GetTradeFee() parses MarketOpen response
- trader.placeOrder() now returns (errMsg, actualFeeUSD)
- executeEntry uses actual fee from exchange, falls back to estimate
This commit is contained in:
jackyu66git
2026-05-04 18:35:33 +08:00
parent 0b2bfd0b02
commit 6baae85eaa
3 changed files with 132 additions and 28 deletions
+51 -1
View File
@@ -87,6 +87,57 @@ func (b *BitgetTrade) PlaceMarketOrder(side, symbol, size, tradeSide string) (st
return result.Data.OrderID, nil
}
// GetTradeFee queries the fills endpoint for actual fee charged.
func (b *BitgetTrade) GetTradeFee(symbol, orderID string) (feeUSD float64, err error) {
ts := fmt.Sprintf("%d", time.Now().UnixMilli())
method := "GET"
requestPath := "/api/v2/mix/order/fills?symbol=" + symbol + "&orderId=" + orderID
host := "https://api.bitget.com"
sign := b.sign(method, requestPath, ts, "")
url := host + requestPath
req, _ := http.NewRequest(method, url, nil)
req.Header.Set("ACCESS-KEY", b.APIKey)
req.Header.Set("ACCESS-SIGN", sign)
req.Header.Set("ACCESS-TIMESTAMP", ts)
req.Header.Set("ACCESS-PASSPHRASE", b.Passphrase)
if b.paperMode {
req.Header.Set("paptrading", "1")
}
resp, err := b.client.Do(req)
if err != nil {
return 0, fmt.Errorf("http: %w", err)
}
defer resp.Body.Close()
respBody, _ := io.ReadAll(resp.Body)
var raw struct {
Code string `json:"code"`
Msg string `json:"msg"`
Data []json.RawMessage `json:"data"`
}
if err := json.Unmarshal(respBody, &raw); err != nil {
return 0, fmt.Errorf("parse: %s", string(respBody))
}
if raw.Code != "00000" {
return 0, fmt.Errorf("bitget error: %s - %s", raw.Code, raw.Msg)
}
var totalFee float64
for _, item := range raw.Data {
var fill struct {
FillFee string `json:"fillFee"`
}
if err := json.Unmarshal(item, &fill); err != nil {
continue
}
f, _ := strconv.ParseFloat(fill.FillFee, 64)
totalFee += math.Abs(f)
}
return totalFee, nil
}
func (b *BitgetTrade) sign(method, requestPath, timestamp, body string) string {
raw := timestamp + method + requestPath + body
mac := hmac.New(sha256.New, []byte(b.APISecret))
@@ -128,7 +179,6 @@ func (b *BitgetTrade) GetBalance() (float64, error) {
return 0, fmt.Errorf("bitget error: %s - %s", code, msg)
}
// Parse data as array of accounts
dataRaw, ok := raw["data"]
if !ok || dataRaw == nil {
return 0, fmt.Errorf("no data in response")
+28
View File
@@ -83,6 +83,7 @@ func (h *HyperLiquidTrade) IsConfigured() bool {
return h.configured
}
// PlaceMarketOrder places a market order and returns the raw JSON response.
func (h *HyperLiquidTrade) PlaceMarketOrder(coin, side, sz string) (string, error) {
if !h.configured {
return "", fmt.Errorf("HL not configured")
@@ -120,6 +121,33 @@ func (h *HyperLiquidTrade) PlaceMarketOrder(coin, side, sz string) (string, erro
return string(respJSON), nil
}
// GetTradeFee parses the MarketOpen JSON response to extract filled size and
// estimates the actual fee from the exchange taker rate.
func (h *HyperLiquidTrade) GetTradeFee(orderResponseJSON string, takerFeePct float64) (feeUSD float64, err error) {
var resp struct {
Statuses []struct {
Filled *struct {
TotalSz string `json:"totalSz"`
AvgPx string `json:"avgPx"`
} `json:"filled,omitempty"`
Error *string `json:"error,omitempty"`
} `json:"statuses"`
}
if err := json.Unmarshal([]byte(orderResponseJSON), &resp); err != nil {
return 0, fmt.Errorf("parse order response: %w", err)
}
for _, st := range resp.Statuses {
if st.Filled != nil {
sz, _ := strconv.ParseFloat(st.Filled.TotalSz, 64)
px, _ := strconv.ParseFloat(st.Filled.AvgPx, 64)
if sz > 0 && px > 0 {
return sz * px * takerFeePct / 100, nil
}
}
}
return 0, fmt.Errorf("no filled status in response")
}
func (h *HyperLiquidTrade) GetBalance() (float64, error) {
if !h.configured {
return 0, fmt.Errorf("HL not configured")