core: add TradeConverter to the trade collector

This commit is contained in:
c9s 2024-08-07 17:07:31 +08:00
parent fad7ef219e
commit ffb2c14f1d
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -12,6 +12,8 @@ import (
"github.com/c9s/bbgo/pkg/types"
)
type TradeConverter func(trade types.Trade) (types.Trade, error)
//go:generate callbackgen -type TradeCollector
type TradeCollector struct {
Symbol string
@ -25,6 +27,8 @@ type TradeCollector struct {
mu sync.Mutex
tradeConverters []TradeConverter
recoverCallbacks []func(trade types.Trade)
tradeCallbacks []func(trade types.Trade, profit, netProfit fixedpoint.Value)
@ -49,6 +53,24 @@ func NewTradeCollector(symbol string, position *types.Position, orderStore *Orde
}
}
func (c *TradeCollector) AddTradeConverter(converter TradeConverter) {
c.tradeConverters = append(c.tradeConverters, converter)
}
func (c *TradeCollector) convertTrade(trade types.Trade) types.Trade {
for _, converter := range c.tradeConverters {
convTrade, err := converter(trade)
if err != nil {
logrus.WithError(err).Errorf("trade converter error, trade: %s", trade.String())
continue
}
trade = convTrade
}
return trade
}
// OrderStore returns the order store used by the trade collector
func (c *TradeCollector) OrderStore() *OrderStore {
return c.orderStore