Files
exchange-monitor-go/exchange/bitget_trade.go
T

232 lines
6.2 KiB
Go

package exchange
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"math"
"net/http"
"strconv"
"strings"
"time"
)
// BitgetTrade handles order placement on Bitget (live or paper).
type BitgetTrade struct {
APIKey string
APISecret string
Passphrase string
client *http.Client
paperMode bool
}
// NewBitgetTrade creates a BitgetTrade. Paper mode is auto-detected
// from the API key prefix: "bg_" → Bitget paper trading.
func NewBitgetTrade(apiKey, apiSecret, passphrase string) *BitgetTrade {
return &BitgetTrade{
APIKey: apiKey,
APISecret: apiSecret,
Passphrase: passphrase,
client: &http.Client{Timeout: 10 * time.Second},
paperMode: strings.HasPrefix(apiKey, "bg_"),
}
}
// PlaceMarketOrder places a market order on Bitget.
// side: "buy" or "sell"
// symbol: "BTCUSDT" (always without _UMCBL suffix — appended internally for live)
// size: contract size in coin units (e.g. 0.001 for BTC)
// tradeSide: "open" or "close" — only used in paper mode
func (b *BitgetTrade) PlaceMarketOrder(side, symbol, size, tradeSide string) (string, error) {
ts := fmt.Sprintf("%d", time.Now().UnixMilli())
method := "POST"
var requestPath string
var host string
if b.paperMode {
requestPath = "/api/v2/mix/order/place-order"
host = "https://api.bitget.com"
} else {
requestPath = "/api/v2/mix/order/place"
host = "https://api.bitget.com"
}
body := map[string]interface{}{
"marginCoin": "USDT",
"side": side,
"orderType": "market",
"timeInForce": "IOC", // immediate-or-cancel for market orders
}
if b.paperMode {
body["symbol"] = symbol
body["marginMode"] = "crossed"
body["tradeSide"] = tradeSide
} else {
body["symbol"] = symbol + "_UMCBL"
}
bodyJSON, _ := json.Marshal(body)
sign := b.sign(method, requestPath, ts, string(bodyJSON))
url := host + requestPath
req, err := http.NewRequest(method, url, strings.NewReader(string(bodyJSON)))
if err != nil {
return "", fmt.Errorf("create request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
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 "", fmt.Errorf("http request: %w", err)
}
defer resp.Body.Close()
respBody, _ := io.ReadAll(resp.Body)
var result struct {
Code string `json:"code"`
Msg string `json:"msg"`
Data struct {
OrderID string `json:"orderId"`
} `json:"data"`
}
if err := json.Unmarshal(respBody, &result); err != nil {
return "", fmt.Errorf("parse response: %s", string(respBody))
}
if result.Code != "00000" {
return "", fmt.Errorf("bitget error: %s - %s", result.Code, result.Msg)
}
return result.Data.OrderID, nil
}
func (b *BitgetTrade) sign(method, requestPath, timestamp, body string) string {
raw := timestamp + method + requestPath + body
mac := hmac.New(sha256.New, []byte(b.APISecret))
mac.Write([]byte(raw))
return base64.StdEncoding.EncodeToString(mac.Sum(nil))
}
// GetBalance queries available balance.
func (b *BitgetTrade) GetBalance() (float64, error) {
ts := fmt.Sprintf("%d", time.Now().UnixMilli())
method := "GET"
var host, requestPath string
if b.paperMode {
host = "https://api.bitget.com"
requestPath = "/api/v2/mix/account/accounts?productType=USDT-FUTURES"
} else {
host = "https://api.bitget.com"
requestPath = "/api/v2/mix/account/accounts?productType=UMCBL"
}
sign := b.sign(method, requestPath, ts, "")
url := host + requestPath
req, err := http.NewRequest(method, url, nil)
if err != nil {
return 0, fmt.Errorf("create request: %w", err)
}
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 request: %w", err)
}
defer resp.Body.Close()
respBody, _ := io.ReadAll(resp.Body)
var result struct {
Code string `json:"code"`
Msg string `json:"msg"`
Data []struct {
MarginCoin string `json:"marginCoin"`
Available string `json:"available"`
} `json:"data"`
}
if err := json.Unmarshal(respBody, &result); err != nil {
return 0, fmt.Errorf("parse: %s", string(respBody))
}
if result.Code != "00000" {
return 0, fmt.Errorf("bitget error: %s - %s", result.Code, result.Msg)
}
for _, acct := range result.Data {
if acct.MarginCoin == "USDT" {
bal, _ := strconv.ParseFloat(acct.Available, 64)
return bal, nil
}
}
return 0, fmt.Errorf("no USDT margin account found")
}
// GetBitgetSize calculates the contract size for a given USD amount.
// Returns size as a decimal string complying with Bitget's USDT-FUTURES precision.
// Enforces the exchange's minimum: minTradeNum contracts AND $5 min notional.
// Uses math.Floor to round DOWN to the nearest valid step (B#5: prevent rounding up).
func GetBitgetSize(symbol string, amountUSD, price float64) string {
if amountUSD < 5 {
amountUSD = 5 // Bitget minimum notional
}
sz := amountUSD / price // raw coin count
switch symbol {
case "DOGEUSDT":
if sz < 1 {
sz = 1
}
sz = math.Floor(sz) // step=1
return fmt.Sprintf("%.0f", sz) // minTradeNum=1, sizeMultiplier=1
case "LINKUSDT":
if sz < 1 {
sz = 1
}
sz = math.Floor(sz) // step=1
return fmt.Sprintf("%.0f", sz) // minTradeNum=1, sizeMultiplier=1
case "ONDOUSDT":
sz = math.Floor(sz*10) / 10 // step=0.1
if sz < 0.1 {
sz = 0.1
}
return fmt.Sprintf("%.1f", sz) // minTradeNum=0.1, sizeMultiplier=0.1
case "OPUSDT":
sz = math.Floor(sz*10) / 10 // step=0.1
if sz < 0.1 {
sz = 0.1
}
return fmt.Sprintf("%.1f", sz) // minTradeNum=0.1, sizeMultiplier=0.1
case "WIFUSDT":
sz = math.Floor(sz*10) / 10 // step=0.1
if sz < 0.1 {
sz = 0.1
}
return fmt.Sprintf("%.1f", sz) // minTradeNum=0.1, sizeMultiplier=0.1
case "ARBUSDT":
sz = math.Floor(sz*100) / 100 // step=0.01
if sz < 0.01 {
sz = 0.01
}
return fmt.Sprintf("%.2f", sz) // minTradeNum=0.01, sizeMultiplier=0.01
default:
return fmt.Sprintf("%.4f", sz)
}
}