grid: refactor trade handler with trade collector

This commit is contained in:
c9s 2021-11-05 00:30:04 +08:00
parent 7787edffa0
commit 7db7596abe

View File

@ -99,6 +99,8 @@ type Strategy struct {
// activeOrders is the locally maintained active order book of the maker orders.
activeOrders *bbgo.LocalActiveOrderBook
tradeCollector *bbgo.TradeCollector
// groupID is the group ID used for the strategy instance for canceling orders
groupID uint32
}
@ -375,31 +377,11 @@ func (s *Strategy) placeGridOrders(orderExecutor bbgo.OrderExecutor, session *bb
}
func (s *Strategy) tradeUpdateHandler(trade types.Trade) {
if trade.Symbol != s.Symbol {
return
}
if s.orderStore.Exists(trade.OrderID) {
log.Infof("received trade update of order %d: %+v", trade.OrderID, trade)
if s.TradeService != nil {
if err := s.TradeService.Mark(context.Background(), trade.ID, ID); err != nil {
log.WithError(err).Error("trade mark error")
}
}
if trade.Side == types.SideTypeSelf {
return
}
profit, netProfit, madeProfit := s.state.Position.AddTrade(trade)
if madeProfit {
s.Notify("%s average cost profit: %f %s, net profit =~ %f %s",
s.Symbol,
profit.Float64(), s.Market.QuoteCurrency,
netProfit.Float64(), s.Market.QuoteCurrency)
}
}
}
func (s *Strategy) handleFilledOrder(filledOrder types.Order) {
@ -602,6 +584,17 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
s.activeOrders.OnFilled(s.handleFilledOrder)
s.activeOrders.BindStream(session.UserDataStream)
s.tradeCollector = bbgo.NewTradeCollector(s.Symbol, s.state.Position, s.orderStore)
s.tradeCollector.OnTrade(func(trade types.Trade) {
s.Notifiability.Notify(trade)
s.state.ProfitStats.AddTrade(trade)
s.tradeUpdateHandler(trade)
})
s.tradeCollector.OnPositionUpdate(func(position *bbgo.Position) {
s.Notifiability.Notify(position)
})
s.tradeCollector.BindStream(session.UserDataStream)
s.Graceful.OnShutdown(func(ctx context.Context, wg *sync.WaitGroup) {
defer wg.Done()
@ -618,8 +611,6 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
}
})
session.UserDataStream.OnTradeUpdate(s.tradeUpdateHandler)
session.UserDataStream.OnStart(func() {
// if we have orders in the state data, we can restore them
if len(s.state.Orders) > 0 {