mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-12 18:13:54 +00:00
core: add TradeConverter to the trade collector
This commit is contained in:
parent
fad7ef219e
commit
ffb2c14f1d
|
@ -12,6 +12,8 @@ import (
|
||||||
"github.com/c9s/bbgo/pkg/types"
|
"github.com/c9s/bbgo/pkg/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type TradeConverter func(trade types.Trade) (types.Trade, error)
|
||||||
|
|
||||||
//go:generate callbackgen -type TradeCollector
|
//go:generate callbackgen -type TradeCollector
|
||||||
type TradeCollector struct {
|
type TradeCollector struct {
|
||||||
Symbol string
|
Symbol string
|
||||||
|
@ -25,6 +27,8 @@ type TradeCollector struct {
|
||||||
|
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
|
|
||||||
|
tradeConverters []TradeConverter
|
||||||
|
|
||||||
recoverCallbacks []func(trade types.Trade)
|
recoverCallbacks []func(trade types.Trade)
|
||||||
|
|
||||||
tradeCallbacks []func(trade types.Trade, profit, netProfit fixedpoint.Value)
|
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
|
// OrderStore returns the order store used by the trade collector
|
||||||
func (c *TradeCollector) OrderStore() *OrderStore {
|
func (c *TradeCollector) OrderStore() *OrderStore {
|
||||||
return c.orderStore
|
return c.orderStore
|
||||||
|
|
Loading…
Reference in New Issue
Block a user