Merge pull request #124 from ychi/feat/calculate-asset-with-ticker

feat/calculate asset with ticker
This commit is contained in:
Yo-An Lin 2021-02-09 17:49:22 +08:00 committed by GitHub
commit bb394c8d38
2 changed files with 21 additions and 19 deletions

View File

@ -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 = append(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()

View File

@ -3,7 +3,6 @@ package cmd
import (
"context"
"strings"
"time"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
@ -94,17 +93,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[symbol]
if !ok {
return errors.New("no ticker data for current price")
}
currentPrice := currentTick.Last
calculator := &pnl.AverageCostCalculator{
TradingFeeCurrency: tradingFeeCurrency,
}