From 2c9583cccb9343a08cb8fdf5bcb49758194a1e35 Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 18 Dec 2023 22:17:52 +0800 Subject: [PATCH 1/3] xdepthmaker: remove redundant notification --- pkg/strategy/xdepthmaker/strategy.go | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/pkg/strategy/xdepthmaker/strategy.go b/pkg/strategy/xdepthmaker/strategy.go index d947a3dc42..e6d52cab64 100644 --- a/pkg/strategy/xdepthmaker/strategy.go +++ b/pkg/strategy/xdepthmaker/strategy.go @@ -35,10 +35,6 @@ func init() { bbgo.RegisterStrategy(ID, &Strategy{}) } -func notifyTrade(trade types.Trade, _, _ fixedpoint.Value) { - bbgo.Notify(trade) -} - type CrossExchangeMarketMakingStrategy struct { ctx, parent context.Context cancel context.CancelFunc @@ -133,11 +129,19 @@ func (s *CrossExchangeMarketMakingStrategy) Initialize( // bbgo.Sync(ctx, s) }) + // global order store s.orderStore = core.NewOrderStore(s.Position.Symbol) s.orderStore.BindStream(hedgeSession.UserDataStream) s.orderStore.BindStream(makerSession.UserDataStream) + // global trade collector s.tradeCollector = core.NewTradeCollector(symbol, s.Position, s.orderStore) + s.tradeCollector.OnTrade(func(trade types.Trade, profit fixedpoint.Value, netProfit fixedpoint.Value) { + bbgo.Notify(trade) + }) + s.tradeCollector.OnPositionUpdate(func(position *types.Position) { + bbgo.Notify(position) + }) s.tradeCollector.OnTrade(func(trade types.Trade, profit, netProfit fixedpoint.Value) { c := trade.PositionChange() @@ -169,7 +173,6 @@ func (s *CrossExchangeMarketMakingStrategy) Initialize( }) s.tradeCollector.BindStream(s.hedgeSession.UserDataStream) s.tradeCollector.BindStream(s.makerSession.UserDataStream) - return nil } @@ -344,6 +347,8 @@ func (s *Strategy) CrossRun( return err } + log.Infof("makerSession: %s hedgeSession: %s", makerSession.Name, hedgeSession.Name) + if err := s.CrossExchangeMarketMakingStrategy.Initialize(ctx, s.Environment, makerSession, hedgeSession, s.Symbol, ID, s.InstanceID()); err != nil { return err } @@ -351,14 +356,6 @@ func (s *Strategy) CrossRun( s.pricingBook = types.NewStreamBook(s.Symbol) s.pricingBook.BindStream(s.hedgeSession.MarketDataStream) - if s.NotifyTrade { - s.tradeCollector.OnTrade(notifyTrade) - } - - s.tradeCollector.OnPositionUpdate(func(position *types.Position) { - bbgo.Notify(position) - }) - s.stopC = make(chan struct{}) if s.RecoverTrade { From 84085e09b56d1a8c9a38c9bfbbfacb648f6f1c5c Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 18 Dec 2023 22:29:11 +0800 Subject: [PATCH 2/3] xdepthmaker: fix duplicated binding --- pkg/strategy/xdepthmaker/strategy.go | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/pkg/strategy/xdepthmaker/strategy.go b/pkg/strategy/xdepthmaker/strategy.go index e6d52cab64..a2d05110e5 100644 --- a/pkg/strategy/xdepthmaker/strategy.go +++ b/pkg/strategy/xdepthmaker/strategy.go @@ -136,12 +136,6 @@ func (s *CrossExchangeMarketMakingStrategy) Initialize( // global trade collector s.tradeCollector = core.NewTradeCollector(symbol, s.Position, s.orderStore) - s.tradeCollector.OnTrade(func(trade types.Trade, profit fixedpoint.Value, netProfit fixedpoint.Value) { - bbgo.Notify(trade) - }) - s.tradeCollector.OnPositionUpdate(func(position *types.Position) { - bbgo.Notify(position) - }) s.tradeCollector.OnTrade(func(trade types.Trade, profit, netProfit fixedpoint.Value) { c := trade.PositionChange() @@ -156,20 +150,6 @@ func (s *CrossExchangeMarketMakingStrategy) Initialize( // TODO: make this atomic s.CoveredPosition = s.CoveredPosition.Add(c) } - - s.ProfitStats.AddTrade(trade) - - if profit.Compare(fixedpoint.Zero) == 0 { - s.Environ.RecordPosition(s.Position, trade, nil) - } else { - log.Infof("%s generated profit: %v", symbol, profit) - - p := s.Position.NewProfit(trade, profit, netProfit) - bbgo.Notify(&p) - s.ProfitStats.AddProfit(p) - - s.Environ.RecordPosition(s.Position, trade, &p) - } }) s.tradeCollector.BindStream(s.hedgeSession.UserDataStream) s.tradeCollector.BindStream(s.makerSession.UserDataStream) From 47b12edc4d31bf2060331f59f7ab6b80482039b6 Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 18 Dec 2023 22:30:16 +0800 Subject: [PATCH 3/3] xdepthmaker: call bbgo.Sync on shutdown --- pkg/strategy/xdepthmaker/strategy.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/strategy/xdepthmaker/strategy.go b/pkg/strategy/xdepthmaker/strategy.go index a2d05110e5..912a44d802 100644 --- a/pkg/strategy/xdepthmaker/strategy.go +++ b/pkg/strategy/xdepthmaker/strategy.go @@ -462,6 +462,7 @@ func (s *Strategy) CrossRun( log.WithError(err).Errorf("graceful cancel %s order error", s.Symbol) } + bbgo.Sync(ctx, s) bbgo.Notify("%s: %s position", ID, s.Symbol, s.Position) })