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, }