Files
coin/account/ac.go
2026-01-09 15:48:31 +08:00

100 lines
2.2 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package account
import (
"encoding/json"
"log"
"time"
"git.apinb.com/quant/strategy/internal/core/trade"
"git.apinb.com/quant/strategy/internal/models"
)
var (
BaseAssets string
PlanKeyName string
IsDebug bool = false
UpdHistory bool = false
)
// 根据基本币监控帐号可用资金变动仓位以及最近7天的交易情况
func NewAccountByBinance(base string, p *trade.Spec, debug, updHistory bool) {
BaseAssets = base
PlanKeyName = p.PlanKeyName
IsDebug = debug
UpdHistory = updHistory
trade.NewAccounts()
if p.Api.Exchange == "BINANCE" {
//trade.InitExchange_Binance(p.BinanceFuturesClient, p.AllowSymbols, p.StrategyConf.Leverage)
InitAccount_Binance(p)
}
go WatchPositions(p)
if UpdHistory {
// go WatchHistory(p)
}
}
func InitAccount_Binance(p *trade.Spec) {
var err error
ac, err := p.BinanceClient.GetFuturesAccountBalance()
if err != nil {
log.Println(err)
return
} else {
trade.AccountsAssets.Set(BaseAssets, ac[BaseAssets])
}
log.Println("WatchAccount", PlanKeyName, BaseAssets, "Account Equity:", ac[BaseAssets].AccountEquity, "Account Available:", ac[BaseAssets].Available)
orders, err := trade.RefreshPositions(p)
if err != nil {
log.Println(err)
return
}
log.Println("Positions Number", len(orders))
}
func WatchAccount_Binance(p *trade.Spec) {
for {
var err error
ac, err := p.BinanceClient.GetFuturesAccountBalance()
if err != nil {
log.Println(err)
} else {
trade.AccountsAssets.Set(BaseAssets, ac[BaseAssets])
log.Println("WatchAccount", PlanKeyName, BaseAssets, "Account Equity:", ac[BaseAssets].AccountEquity, "Account Available:", ac[BaseAssets].Available)
}
time.Sleep(1 * time.Hour)
}
}
func WatchPositions(p *trade.Spec) {
for {
orders, err := trade.RefreshPositions(p)
if err != nil {
log.Println(err)
}
if IsDebug {
jsonBytes, _ := json.Marshal(orders)
log.Println("WatchPositions", string(jsonBytes))
}
time.Sleep(2 * time.Second)
}
}
func WatchHistory(p *trade.Spec) {
for {
trade, pl := trade.RefreshHistoryTotal(p)
models.UpdateTotalTrans(PlanKeyName, trade, pl)
if IsDebug {
log.Println("WatchHistory", "Trade Number", trade, "Trade RealizedPnl", pl)
}
time.Sleep(10 * time.Minute)
}
}