diff --git a/pkg/bbgo/environment.go b/pkg/bbgo/environment.go index 4b82f7f76..0f3c3b59d 100644 --- a/pkg/bbgo/environment.go +++ b/pkg/bbgo/environment.go @@ -164,13 +164,17 @@ func (environ *Environment) Init(ctx context.Context) (err error) { session.marketDataStores[kline.Symbol].AddKLine(kline) }) + if environ.TradeService != nil { + session.Stream.OnTradeUpdate(func(trade types.Trade) { + if err := environ.TradeService.Insert(trade); err != nil { + log.WithError(err).Errorf("trade insert error: %+v", trade) + } + }) + } + session.Stream.OnTradeUpdate(func(trade types.Trade) { // append trades session.Trades[trade.Symbol] = append(session.Trades[trade.Symbol], trade) - - if err := environ.TradeService.Insert(trade); err != nil { - log.WithError(err).Errorf("trade insert error: %+v", trade) - } }) // move market data store dispatch to here, use one callback to dispatch the market data diff --git a/pkg/cmd/run.go b/pkg/cmd/run.go index 6ec231413..c663a2d7f 100644 --- a/pkg/cmd/run.go +++ b/pkg/cmd/run.go @@ -65,11 +65,13 @@ func compileRunFile(filepath string, config *bbgo.Config) error { func runConfig(ctx context.Context, userConfig *bbgo.Config) error { environ := bbgo.NewEnvironment() - db, err := cmdutil.ConnectMySQL() - if err != nil { - return err + if viper.IsSet("mysql-url") { + db, err := cmdutil.ConnectMySQL() + if err != nil { + return err + } + environ.SyncTrades(db) } - environ.SyncTrades(db) if len(userConfig.Sessions) == 0 { for _, n := range bbgo.SupportedExchanges { @@ -97,7 +99,6 @@ func runConfig(ctx context.Context, userConfig *bbgo.Config) error { } } - notification := bbgo.Notifiability{ SymbolChannelRouter: bbgo.NewPatternChannelRouter(nil), SessionChannelRouter: bbgo.NewPatternChannelRouter(nil), @@ -125,7 +126,6 @@ func runConfig(ctx context.Context, userConfig *bbgo.Config) error { environ.ConfigureNotification(userConfig.Notifications) } - trader := bbgo.NewTrader(environ) if userConfig.RiskControls != nil {