xnav: support asset recording

This commit is contained in:
c9s 2022-05-04 14:23:35 +08:00
parent 95f7d85183
commit 5cd7e61006
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -8,7 +8,7 @@ import (
"github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/pkg/errors" "github.com/pkg/errors"
log "github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/slack-go/slack" "github.com/slack-go/slack"
"github.com/c9s/bbgo/pkg/bbgo" "github.com/c9s/bbgo/pkg/bbgo"
@ -21,6 +21,8 @@ const ID = "xnav"
const stateKey = "state-v1" const stateKey = "state-v1"
var log = logrus.WithField("strategy", ID)
func init() { func init() {
bbgo.RegisterStrategy(ID, &Strategy{}) bbgo.RegisterStrategy(ID, &Strategy{})
} }
@ -59,6 +61,7 @@ type Strategy struct {
Notifiability *bbgo.Notifiability Notifiability *bbgo.Notifiability
*bbgo.Graceful *bbgo.Graceful
*bbgo.Persistence *bbgo.Persistence
*bbgo.Environment
Interval types.Duration `json:"interval"` Interval types.Duration `json:"interval"`
ReportOnStart bool `json:"reportOnStart"` ReportOnStart bool `json:"reportOnStart"`
@ -77,43 +80,40 @@ func (s *Strategy) CrossSubscribe(sessions map[string]*bbgo.ExchangeSession) {}
func (s *Strategy) recordNetAssetValue(ctx context.Context, sessions map[string]*bbgo.ExchangeSession) { func (s *Strategy) recordNetAssetValue(ctx context.Context, sessions map[string]*bbgo.ExchangeSession) {
totalAssets := types.AssetMap{} totalAssets := types.AssetMap{}
totalBalances := types.BalanceMap{} totalBalances := types.BalanceMap{}
totalBorrowed := map[string]fixedpoint.Value{} allPrices := map[string]fixedpoint.Value{}
lastPrices := map[string]fixedpoint.Value{} sessionBalances := map[string]types.BalanceMap{}
for _, session := range sessions { priceTime := time.Now()
if err := session.UpdateAccount(ctx) ; err != nil {
// iterate the sessions and record them
for sessionName, session := range sessions {
// update the account balances and the margin information
if err := session.UpdateAccount(ctx); err != nil {
log.WithError(err).Errorf("can not update account") log.WithError(err).Errorf("can not update account")
return return
} }
account := session.GetAccount() account := session.GetAccount()
balances := account.Balances() balances := account.Balances()
if err := session.UpdatePrices(ctx); err != nil { if err := session.UpdatePrices(ctx, balances.Currencies(), "USDT"); err != nil {
log.WithError(err).Error("price update failed") log.WithError(err).Error("price update failed")
return return
} }
for _, b := range balances { sessionBalances[sessionName] = balances
if tb, ok := totalBalances[b.Currency]; ok { totalBalances = totalBalances.Add(balances)
tb.Available = tb.Available.Add(b.Available)
tb.Locked = tb.Locked.Add(b.Locked)
totalBalances[b.Currency] = tb
if b.Borrowed.Sign() > 0 {
totalBorrowed[b.Currency] = totalBorrowed[b.Currency].Add(b.Borrowed)
}
} else {
totalBalances[b.Currency] = b
totalBorrowed[b.Currency] = b.Borrowed
}
}
prices := session.LastPrices() prices := session.LastPrices()
assets := balances.Assets(prices, priceTime)
// merge prices
for m, p := range prices { for m, p := range prices {
lastPrices[m] = p allPrices[m] = p
} }
s.Environment.RecordAsset(priceTime, sessionName, session.ExchangeName, session.SubAccount, assets)
} }
assets := totalBalances.Assets(lastPrices) assets := totalBalances.Assets(allPrices, time.Now())
for currency, asset := range assets { for currency, asset := range assets {
// calculated if it's dust only when InUSD (usd value) is defined. // calculated if it's dust only when InUSD (usd value) is defined.
if s.IgnoreDusts && !asset.InUSD.IsZero() && asset.InUSD.Compare(Ten) < 0 { if s.IgnoreDusts && !asset.InUSD.IsZero() && asset.InUSD.Compare(Ten) < 0 {
@ -123,6 +123,8 @@ func (s *Strategy) recordNetAssetValue(ctx context.Context, sessions map[string]
totalAssets[currency] = asset totalAssets[currency] = asset
} }
s.Environment.RecordAsset(priceTime, "ALL", "", "", totalAssets)
s.Notifiability.Notify(totalAssets) s.Notifiability.Notify(totalAssets)
if s.state != nil { if s.state != nil {
@ -189,6 +191,10 @@ func (s *Strategy) CrossRun(ctx context.Context, _ bbgo.OrderExecutionRouter, se
s.recordNetAssetValue(ctx, sessions) s.recordNetAssetValue(ctx, sessions)
} }
if s.Environment.BacktestService != nil {
log.Warnf("xnav does not support backtesting")
}
go func() { go func() {
ticker := time.NewTicker(util.MillisecondsJitter(s.Interval.Duration(), 1000)) ticker := time.NewTicker(util.MillisecondsJitter(s.Interval.Duration(), 1000))
defer ticker.Stop() defer ticker.Stop()