From 61c98432f2fc084ed8d87b3941afff2a93460e2f Mon Sep 17 00:00:00 2001 From: ycchen Date: Sat, 6 Feb 2021 22:11:58 +0100 Subject: [PATCH 1/3] feat: tickers for asset calculation --- pkg/bbgo/session.go | 20 ++++++++++---------- pkg/cmd/pnl.go | 19 +++++++++++-------- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/pkg/bbgo/session.go b/pkg/bbgo/session.go index 60b647a30..f34bff567 100644 --- a/pkg/bbgo/session.go +++ b/pkg/bbgo/session.go @@ -492,19 +492,19 @@ func (session *ExchangeSession) UpdatePrices(ctx context.Context) (err error) { balances := session.Account.Balances() + symbols := make([]string, len(balances)) for _, b := range balances { - priceSymbol := b.Currency + "USDT" - startTime := time.Now().Add(-10 * time.Minute) - klines, err := session.Exchange.QueryKLines(ctx, priceSymbol, types.Interval1m, types.KLineQueryOptions{ - Limit: 100, - StartTime: &startTime, - }) + symbols[len(symbols)] = b.Currency + "USDT" + } - if err != nil || len(klines) == 0 { - continue - } + tickers, err := session.Exchange.QueryTickers(ctx, symbols...) - session.lastPrices[priceSymbol] = klines[len(klines)-1].Close + if err != nil || len(tickers) == 0 { + return err + } + + for k, v := range tickers { + session.lastPrices[k] = v.Last } session.lastPriceUpdatedAt = time.Now() diff --git a/pkg/cmd/pnl.go b/pkg/cmd/pnl.go index d3f27aac3..52bfc9e12 100644 --- a/pkg/cmd/pnl.go +++ b/pkg/cmd/pnl.go @@ -94,17 +94,20 @@ var PnLCmd = &cobra.Command{ log.Infof("found checkpoints: %+v", checkpoints) log.Infof("stock: %f", stockManager.Stocks.Quantity()) - now := time.Now() - kLines, err := exchange.QueryKLines(ctx, symbol, types.Interval1m, types.KLineQueryOptions{ - Limit: 100, - EndTime: &now, - }) + tickers, err := exchange.QueryTickers(ctx, symbol) - if len(kLines) == 0 { - return errors.New("no kline data for current price") + if err != nil { + return err } - currentPrice := kLines[len(kLines)-1].Close + currentTick, ok := tickers[strings.ToUpper(symbol)] + + if !ok { + return errors.New("no ticker data for current price") + } + + currentPrice := currentTick.Last + calculator := &pnl.AverageCostCalculator{ TradingFeeCurrency: tradingFeeCurrency, } From 6655e16889159a6f377c7d3e25f486b2eb89b21c Mon Sep 17 00:00:00 2001 From: ycchen Date: Mon, 8 Feb 2021 00:02:08 +0100 Subject: [PATCH 2/3] minor tweaks --- pkg/bbgo/session.go | 2 +- pkg/cmd/pnl.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/bbgo/session.go b/pkg/bbgo/session.go index f34bff567..ebc1413d3 100644 --- a/pkg/bbgo/session.go +++ b/pkg/bbgo/session.go @@ -494,7 +494,7 @@ func (session *ExchangeSession) UpdatePrices(ctx context.Context) (err error) { symbols := make([]string, len(balances)) for _, b := range balances { - symbols[len(symbols)] = b.Currency + "USDT" + symbols = append(symbols, b.Currency + "USDT") } tickers, err := session.Exchange.QueryTickers(ctx, symbols...) diff --git a/pkg/cmd/pnl.go b/pkg/cmd/pnl.go index 52bfc9e12..a1c06e8c7 100644 --- a/pkg/cmd/pnl.go +++ b/pkg/cmd/pnl.go @@ -100,7 +100,7 @@ var PnLCmd = &cobra.Command{ return err } - currentTick, ok := tickers[strings.ToUpper(symbol)] + currentTick, ok := tickers[symbol] if !ok { return errors.New("no ticker data for current price") From 2b285c020214e75ccfe26b29b80cb3e9d200fcde Mon Sep 17 00:00:00 2001 From: ycchen Date: Mon, 8 Feb 2021 22:43:20 +0100 Subject: [PATCH 3/3] fix: remove unused --- pkg/cmd/pnl.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/cmd/pnl.go b/pkg/cmd/pnl.go index a1c06e8c7..31fa36bc1 100644 --- a/pkg/cmd/pnl.go +++ b/pkg/cmd/pnl.go @@ -3,7 +3,6 @@ package cmd import ( "context" "strings" - "time" "github.com/pkg/errors" log "github.com/sirupsen/logrus"