accounting: filter duplicated trades when backtesting

This commit is contained in:
c9s 2022-01-30 02:40:38 +08:00
parent e1fc0e7b8d
commit 6566db1624

View File

@ -3,6 +3,8 @@ package pnl
import (
"time"
log "github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)
@ -44,8 +46,18 @@ func (c *AverageCostCalculator) Calculate(symbol string, trades []types.Trade, c
var totalProfit fixedpoint.Value
var totalNetProfit fixedpoint.Value
var tradeIDs = map[uint64]types.Trade{}
for _, trade := range trades {
if trade.Symbol == symbol {
if _, exists := tradeIDs[trade.ID]; exists {
log.Warnf("duplicated trade: %+v", trade)
continue
}
if trade.Symbol != symbol {
continue
}
profit, netProfit, madeProfit := position.AddTrade(trade)
if madeProfit {
totalProfit += profit
@ -57,13 +69,14 @@ func (c *AverageCostCalculator) Calculate(symbol string, trades []types.Trade, c
} else {
askVolume += trade.Quantity
}
}
if _, ok := currencyFees[trade.FeeCurrency]; !ok {
currencyFees[trade.FeeCurrency] = trade.Fee
} else {
currencyFees[trade.FeeCurrency] += trade.Fee
}
tradeIDs[trade.ID] = trade
}
unrealizedProfit := (fixedpoint.NewFromFloat(currentPrice) - position.AverageCost).Mul(position.GetBase())