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 ( import (
"time" "time"
log "github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/types"
) )
@ -44,19 +46,28 @@ func (c *AverageCostCalculator) Calculate(symbol string, trades []types.Trade, c
var totalProfit fixedpoint.Value var totalProfit fixedpoint.Value
var totalNetProfit fixedpoint.Value var totalNetProfit fixedpoint.Value
for _, trade := range trades { var tradeIDs = map[uint64]types.Trade{}
if trade.Symbol == symbol {
profit, netProfit, madeProfit := position.AddTrade(trade)
if madeProfit {
totalProfit += profit
totalNetProfit += netProfit
}
if trade.IsBuyer { for _, trade := range trades {
bidVolume += trade.Quantity if _, exists := tradeIDs[trade.ID]; exists {
} else { log.Warnf("duplicated trade: %+v", trade)
askVolume += trade.Quantity continue
} }
if trade.Symbol != symbol {
continue
}
profit, netProfit, madeProfit := position.AddTrade(trade)
if madeProfit {
totalProfit += profit
totalNetProfit += netProfit
}
if trade.IsBuyer {
bidVolume += trade.Quantity
} else {
askVolume += trade.Quantity
} }
if _, ok := currencyFees[trade.FeeCurrency]; !ok { if _, ok := currencyFees[trade.FeeCurrency]; !ok {
@ -64,6 +75,8 @@ func (c *AverageCostCalculator) Calculate(symbol string, trades []types.Trade, c
} else { } else {
currencyFees[trade.FeeCurrency] += trade.Fee currencyFees[trade.FeeCurrency] += trade.Fee
} }
tradeIDs[trade.ID] = trade
} }
unrealizedProfit := (fixedpoint.NewFromFloat(currentPrice) - position.AverageCost).Mul(position.GetBase()) unrealizedProfit := (fixedpoint.NewFromFloat(currentPrice) - position.AverageCost).Mul(position.GetBase())