From 4387b078c0cd6a5775346d04697c0701ba841a9e Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 19 Sep 2022 19:28:29 +0800 Subject: [PATCH] bbgo: add basic notification switch --- pkg/bbgo/environment.go | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/pkg/bbgo/environment.go b/pkg/bbgo/environment.go index 14c214df7..4ea457830 100644 --- a/pkg/bbgo/environment.go +++ b/pkg/bbgo/environment.go @@ -301,13 +301,6 @@ func (environ *Environment) ConfigurePersistence(conf *PersistenceConfig) error return nil } -// ConfigureNotificationRouting configures the notification rules -// for symbol-based routes, we should register the same symbol rules for each session. -// for session-based routes, we should set the fixed callbacks for each session -func (environ *Environment) ConfigureNotificationRouting(conf *NotificationConfig) error { - return nil -} - func (environ *Environment) SetStartTime(t time.Time) *Environment { environ.startTime = t return environ @@ -659,7 +652,7 @@ func (environ *Environment) ConfigureNotificationSystem(userConfig *Config) erro } if userConfig.Notifications != nil { - if err := environ.ConfigureNotificationRouting(userConfig.Notifications); err != nil { + if err := environ.ConfigureNotification(userConfig.Notifications); err != nil { return err } } @@ -667,6 +660,32 @@ func (environ *Environment) ConfigureNotificationSystem(userConfig *Config) erro return nil } +func (environ *Environment) ConfigureNotification(config *NotificationConfig) error { + if config.Switches != nil { + if config.Switches.Trade { + tradeHandler := func(trade types.Trade) { + Notify(trade) + } + + for _, session := range environ.sessions { + session.UserDataStream.OnTradeUpdate(tradeHandler) + } + } + + if config.Switches.OrderUpdate { + orderUpdateHandler := func(order types.Order) { + Notify(order) + } + + for _, session := range environ.sessions { + session.UserDataStream.OnOrderUpdate(orderUpdateHandler) + } + } + } + + return nil +} + // getAuthStoreID returns the authentication store id // if telegram bot token is defined, the bot id will be used. // if not, env var $USER will be used.