From 332ca7ffe88d82026114fea9e0521cf651773b33 Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 26 Oct 2020 13:48:59 +0800 Subject: [PATCH] make trade sync optional --- pkg/bbgo/environment.go | 49 +++++++++++++++++++++++++++-------------- pkg/cmd/run.go | 6 ++++- 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/pkg/bbgo/environment.go b/pkg/bbgo/environment.go index d61105a41..f9e2f60c9 100644 --- a/pkg/bbgo/environment.go +++ b/pkg/bbgo/environment.go @@ -113,11 +113,22 @@ func NewEnvironment(db *sqlx.DB) *Environment { TradeSync: &service.TradeSync{ Service: tradeService, }, + + // default trade scan time tradeScanTime: time.Now().AddDate(0, 0, -7), // sync from 7 days ago sessions: make(map[string]*ExchangeSession), } } +func (environ *Environment) SyncTrades(db *sqlx.DB) *Environment { + environ.TradeService = &service.TradeService{DB: db} + environ.TradeSync = &service.TradeSync{ + Service: environ.TradeService, + } + + return environ +} + func (environ *Environment) AddExchange(name string, exchange types.Exchange) (session *ExchangeSession) { session = NewExchangeSession(name, exchange) environ.sessions[name] = session @@ -160,9 +171,10 @@ func (environ *Environment) Init(ctx context.Context) (err error) { return nil } -// SetTradeScanTime overrides the default trade scan time (-7 days) -func (environ *Environment) SetTradeScanTime(t time.Time) *Environment { +// SyncTradesFrom overrides the default trade scan time (-7 days) +func (environ *Environment) SyncTradesFrom(t time.Time) *Environment { environ.tradeScanTime = t + return environ } @@ -185,25 +197,28 @@ func (environ *Environment) Connect(ctx context.Context) error { // trade sync and market data store depends on subscribed symbols so we have to do this here. for symbol := range loadedSymbols { - log.Infof("syncing trades from %s for symbol %s...", session.Exchange.Name(), symbol) - if err := environ.TradeSync.Sync(ctx, session.Exchange, symbol, environ.tradeScanTime); err != nil { - return err - } - var trades []types.Trade - tradingFeeCurrency := session.Exchange.PlatformFeeCurrency() - if strings.HasPrefix(symbol, tradingFeeCurrency) { - trades, err = environ.TradeService.QueryForTradingFeeCurrency(symbol, tradingFeeCurrency) - } else { - trades, err = environ.TradeService.Query(symbol) + if environ.TradeSync != nil { + log.Infof("syncing trades from %s for symbol %s...", session.Exchange.Name(), symbol) + if err := environ.TradeSync.Sync(ctx, session.Exchange, symbol, environ.tradeScanTime); err != nil { + return err + } + + tradingFeeCurrency := session.Exchange.PlatformFeeCurrency() + if strings.HasPrefix(symbol, tradingFeeCurrency) { + trades, err = environ.TradeService.QueryForTradingFeeCurrency(symbol, tradingFeeCurrency) + } else { + trades, err = environ.TradeService.Query(symbol) + } + + if err != nil { + return err + } + + log.Infof("symbol %s: %d trades loaded", symbol, len(trades)) } - if err != nil { - return err - } - - log.Infof("symbol %s: %d trades loaded", symbol, len(trades)) session.Trades[symbol] = trades currentPrice, err := session.Exchange.QueryAveragePrice(ctx, symbol) diff --git a/pkg/cmd/run.go b/pkg/cmd/run.go index 8eeb01f95..40bbb1c76 100644 --- a/pkg/cmd/run.go +++ b/pkg/cmd/run.go @@ -70,11 +70,13 @@ func runConfig(ctx context.Context, config *config.Config) error { // configure notifiers slackToken := viper.GetString("slack-token") if len(slackToken) > 0 { + log.Infof("found slack configured, setting up log hook...") log.AddHook(slacklog.NewLogHook(slackToken, viper.GetString("slack-error-channel"))) } notifierSet := &bbgo.Notifiability{} if len(slackToken) > 0 { + log.Infof("adding slack notifier...") var notifier = slacknotifier.New(slackToken, viper.GetString("slack-channel")) notifierSet.AddNotifier(notifier) } @@ -104,10 +106,12 @@ func runConfig(ctx context.Context, config *config.Config) error { for _, report := range config.PnLReporters { if len(report.AverageCostBySymbols) > 0 { - trader.ReportPnL(notifier). + + trader.ReportPnL(notifierSet). AverageCostBySymbols(report.AverageCostBySymbols...). Of(report.Of...). When(report.When...) + } else { return errors.Errorf("unsupported PnL reporter: %+v", report) }