initial
This commit is contained in:
84
trade/binance_new.go
Normal file
84
trade/binance_new.go
Normal file
@@ -0,0 +1,84 @@
|
||||
package trade
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/utils"
|
||||
"github.com/adshao/go-binance/v2"
|
||||
"github.com/adshao/go-binance/v2/futures"
|
||||
)
|
||||
|
||||
type BinanceClient struct {
|
||||
LastUPL map[string]float64
|
||||
Cli *binance.Client
|
||||
Futures *futures.Client
|
||||
}
|
||||
|
||||
func NewBinanceClient(apiKey, apiSecret string) *BinanceClient {
|
||||
return &BinanceClient{
|
||||
LastUPL: make(map[string]float64),
|
||||
Cli: binance.NewClient(apiKey, apiSecret),
|
||||
Futures: futures.NewClient(apiKey, apiSecret),
|
||||
}
|
||||
}
|
||||
|
||||
// 通过 API 获取账户的合约可用余额
|
||||
func (bn *BinanceClient) GetFuturesAccountBalance() (map[string]*AccountsBalance, error) {
|
||||
res, err := bn.Futures.NewGetAccountService().Do(context.Background())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// assets, _ := json.Marshal(res.Assets)
|
||||
// log.Println("==>", string(assets))
|
||||
|
||||
ac := make(map[string]*AccountsBalance)
|
||||
for _, v := range res.Assets {
|
||||
ac[v.Asset] = &AccountsBalance{
|
||||
AccountEquity: utils.String2Float64(v.WalletBalance),
|
||||
Available: utils.String2Float64(v.AvailableBalance),
|
||||
}
|
||||
}
|
||||
|
||||
return ac, nil
|
||||
}
|
||||
|
||||
// 设置开仓杠杆
|
||||
func (bn *BinanceClient) SetLeverage(symbols []string, leverage int) {
|
||||
// 调整开仓杠杆:
|
||||
for _, symbol := range symbols {
|
||||
res, err := bn.Futures.NewChangeLeverageService().Symbol(symbol).Leverage(leverage).Do(context.Background())
|
||||
if err != nil {
|
||||
log.Println("[ERROR]", symbol, "ChangeLeverage:", leverage, "res", res, "Err", err)
|
||||
} else {
|
||||
log.Println("[INFO]", symbol, "ChangeLeverage:", leverage, "res", res)
|
||||
}
|
||||
time.Sleep(1 * time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
// 设置保证金模式
|
||||
func (bn *BinanceClient) SetMarginType(symbols []string, marginType string) {
|
||||
for _, symbol := range symbols {
|
||||
err := bn.Futures.NewChangeMarginTypeService().Symbol(symbol).MarginType(futures.MarginType(marginType)).Do(context.Background())
|
||||
if err != nil {
|
||||
log.Println("[ERROR]", symbol, "ChangeMarginType:", marginType, "Err", err)
|
||||
} else {
|
||||
log.Println("[INFO]", symbol, "ChangeMarginType:", marginType)
|
||||
}
|
||||
time.Sleep(1 * time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
// 设置持仓模式
|
||||
func (bn *BinanceClient) SetDual(dual bool) {
|
||||
err := bn.Futures.NewChangePositionModeService().DualSide(dual).Do(context.Background())
|
||||
if err != nil {
|
||||
log.Println("[ERROR]", "ChangePositionMode:", dual, "Err", err)
|
||||
} else {
|
||||
log.Println("[INFO]", "ChangePositionMode:", dual)
|
||||
}
|
||||
time.Sleep(1 * time.Second)
|
||||
}
|
||||
Reference in New Issue
Block a user