mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-26 08:45:16 +00:00
grid: refactor trade handler with trade collector
This commit is contained in:
parent
7787edffa0
commit
7db7596abe
|
@ -99,6 +99,8 @@ type Strategy struct {
|
||||||
// activeOrders is the locally maintained active order book of the maker orders.
|
// activeOrders is the locally maintained active order book of the maker orders.
|
||||||
activeOrders *bbgo.LocalActiveOrderBook
|
activeOrders *bbgo.LocalActiveOrderBook
|
||||||
|
|
||||||
|
tradeCollector *bbgo.TradeCollector
|
||||||
|
|
||||||
// groupID is the group ID used for the strategy instance for canceling orders
|
// groupID is the group ID used for the strategy instance for canceling orders
|
||||||
groupID uint32
|
groupID uint32
|
||||||
}
|
}
|
||||||
|
@ -375,31 +377,11 @@ func (s *Strategy) placeGridOrders(orderExecutor bbgo.OrderExecutor, session *bb
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Strategy) tradeUpdateHandler(trade types.Trade) {
|
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 s.TradeService != nil {
|
||||||
if err := s.TradeService.Mark(context.Background(), trade.ID, ID); err != nil {
|
if err := s.TradeService.Mark(context.Background(), trade.ID, ID); err != nil {
|
||||||
log.WithError(err).Error("trade mark error")
|
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) {
|
func (s *Strategy) handleFilledOrder(filledOrder types.Order) {
|
||||||
|
@ -430,7 +412,7 @@ func (s *Strategy) handleFilledOrder(filledOrder types.Order) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if amount <= s.Market.MinNotional {
|
if amount <= s.Market.MinNotional {
|
||||||
quantity = bbgo.AdjustFloatQuantityByMinAmount(quantity, price, s.Market.MinNotional * 1.001)
|
quantity = bbgo.AdjustFloatQuantityByMinAmount(quantity, price, s.Market.MinNotional*1.001)
|
||||||
|
|
||||||
// update amount
|
// update amount
|
||||||
amount = quantity * price
|
amount = quantity * price
|
||||||
|
@ -602,6 +584,17 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
|
||||||
s.activeOrders.OnFilled(s.handleFilledOrder)
|
s.activeOrders.OnFilled(s.handleFilledOrder)
|
||||||
s.activeOrders.BindStream(session.UserDataStream)
|
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) {
|
s.Graceful.OnShutdown(func(ctx context.Context, wg *sync.WaitGroup) {
|
||||||
defer wg.Done()
|
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() {
|
session.UserDataStream.OnStart(func() {
|
||||||
// if we have orders in the state data, we can restore them
|
// if we have orders in the state data, we can restore them
|
||||||
if len(s.state.Orders) > 0 {
|
if len(s.state.Orders) > 0 {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user