improve asset summary layout and format

This commit is contained in:
c9s 2022-03-18 17:13:37 +08:00
parent 71af9961a1
commit f85db9be61
3 changed files with 61 additions and 26 deletions

View File

@ -624,11 +624,21 @@ func (session *ExchangeSession) UpdatePrices(ctx context.Context) (err error) {
return err
}
var lastTime time.Time
for k, v := range tickers {
session.lastPrices[k] = v.Last
// for {Crypto}/USDT markets
if strings.HasSuffix(k, "USDT") {
session.lastPrices[k] = v.Last
} else if strings.HasPrefix(k, "USDT") {
session.lastPrices[k] = fixedpoint.One.Div(v.Last)
}
if v.Time.After(lastTime) {
lastTime = v.Time
}
}
session.lastPriceUpdatedAt = time.Now()
session.lastPriceUpdatedAt = lastTime
return err
}

View File

@ -2,17 +2,19 @@ package xnav
import (
"context"
"github.com/c9s/bbgo/pkg/fixedpoint"
"sync"
"time"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/slack-go/slack"
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/service"
"github.com/c9s/bbgo/pkg/types"
"github.com/c9s/bbgo/pkg/util"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/slack-go/slack"
)
const ID = "xnav"
@ -101,7 +103,8 @@ func (s *Strategy) recordNetAssetValue(ctx context.Context, sessions map[string]
assets := totalBalances.Assets(lastPrices)
for currency, asset := range assets {
if s.IgnoreDusts && asset.InUSD.Compare(Ten) < 0 {
// calculated if it's dust only when InUSD (usd value) is defined.
if s.IgnoreDusts && !asset.InUSD.IsZero() && asset.InUSD.Compare(Ten) < 0 {
continue
}

View File

@ -52,19 +52,33 @@ type Asset struct {
type AssetMap map[string]Asset
func (m AssetMap) PlainText() (o string) {
var assets = m.Slice()
// sort assets
sort.Slice(assets, func(i, j int) bool {
return assets[i].InUSD.Compare(assets[j].InUSD) > 0
})
sumUsd := fixedpoint.Zero
sumBTC := fixedpoint.Zero
for _, a := range m {
for _, a := range assets {
usd := a.InUSD
btc := a.InBTC
o += fmt.Sprintf(" %s: %s (≈ %s) (≈ %s)",
a.Currency,
a.Total.String(),
USD.FormatMoney(usd),
BTC.FormatMoney(btc),
) + "\n"
sumUsd = sumUsd.Add(usd)
sumBTC = sumBTC.Add(btc)
if !a.InUSD.IsZero() {
o += fmt.Sprintf(" %s: %s (≈ %s) (≈ %s)",
a.Currency,
a.Total.String(),
USD.FormatMoney(usd),
BTC.FormatMoney(btc),
) + "\n"
sumUsd = sumUsd.Add(usd)
sumBTC = sumBTC.Add(btc)
} else {
o += fmt.Sprintf(" %s: %s",
a.Currency,
a.Total.String(),
) + "\n"
}
}
o += fmt.Sprintf(" Summary: (≈ %s) (≈ %s)",
USD.FormatMoney(sumUsd),
@ -97,16 +111,24 @@ func (m AssetMap) SlackAttachment() slack.Attachment {
}
for _, a := range assets {
fields = append(fields, slack.AttachmentField{
Title: a.Currency,
Value: fmt.Sprintf("%s (≈ %s) (≈ %s) (%s)",
a.Total.String(),
USD.FormatMoney(a.InUSD),
BTC.FormatMoney(a.InBTC),
a.InUSD.Div(totalUSD).FormatPercentage(2),
),
Short: false,
})
if !a.InUSD.IsZero() {
fields = append(fields, slack.AttachmentField{
Title: a.Currency,
Value: fmt.Sprintf("%s (≈ %s) (≈ %s) (%s)",
a.Total.String(),
USD.FormatMoney(a.InUSD),
BTC.FormatMoney(a.InBTC),
a.InUSD.Div(totalUSD).FormatPercentage(2),
),
Short: false,
})
} else {
fields = append(fields, slack.AttachmentField{
Title: a.Currency,
Value: fmt.Sprintf("%s", a.Total.String()),
Short: false,
})
}
}
return slack.Attachment{