mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-22 06:53:52 +00:00
accounting: filter duplicated trades when backtesting
This commit is contained in:
parent
e1fc0e7b8d
commit
6566db1624
|
@ -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())
|
||||
|
|
Loading…
Reference in New Issue
Block a user