diff --git a/pkg/bbgo/config.go b/pkg/bbgo/config.go index 8b2def140..0bee2d364 100644 --- a/pkg/bbgo/config.go +++ b/pkg/bbgo/config.go @@ -98,8 +98,10 @@ type Backtest struct { StartTime string `json:"startTime" yaml:"startTime"` EndTime string `json:"endTime" yaml:"endTime"` - Account BacktestAccount `json:"account" yaml:"account"` - Symbols []string `json:"symbols" yaml:"symbols"` + // RecordTrades is an option, if set to true, back-testing should record the trades into database + RecordTrades bool `json:"recordTrades,omitempty" yaml:"recordTrades,omitempty"` + Account BacktestAccount `json:"account" yaml:"account"` + Symbols []string `json:"symbols" yaml:"symbols"` } func (t Backtest) ParseEndTime() (time.Time, error) { diff --git a/pkg/bbgo/session.go b/pkg/bbgo/session.go index 450c53f0b..e58e2ff55 100644 --- a/pkg/bbgo/session.go +++ b/pkg/bbgo/session.go @@ -302,13 +302,18 @@ func (session *ExchangeSession) Init(ctx context.Context, environ *Environment) session.UserDataStream.OnOrderUpdate(session.OrderExecutor.EmitOrderUpdate) session.Account.BindStream(session.UserDataStream) - // insert trade into db right before everything - if environ.TradeService != nil { - session.UserDataStream.OnTradeUpdate(func(trade types.Trade) { - if err := environ.TradeService.Insert(trade); err != nil { - log.WithError(err).Errorf("trade insert error: %+v", trade) - } - }) + // TODO: move this logic to Environment struct + // if back-test service is not set, meaning we are not back-testing + // we should insert trade into db right before everything + if environ.BacktestService == nil { + // if trade service is configured, we have the db configured + if environ.TradeService != nil { + session.UserDataStream.OnTradeUpdate(func(trade types.Trade) { + if err := environ.TradeService.Insert(trade); err != nil { + log.WithError(err).Errorf("trade insert error: %+v", trade) + } + }) + } } session.MarketDataStream.OnKLineClosed(func(kline types.KLine) { diff --git a/pkg/cmd/backtest.go b/pkg/cmd/backtest.go index 1ef28736e..f7cc9ec4a 100644 --- a/pkg/cmd/backtest.go +++ b/pkg/cmd/backtest.go @@ -221,17 +221,19 @@ var BacktestCmd = &cobra.Command{ } } - log.Warn("!!! To run backtest, you should use an isolated database for storing backtest trades !!!") - log.Warn("!!! The trade record in the current database WILL ALL BE DELETE !!!") - - if !force { - if !confirmation("Are you sure to continue?") { - return nil + if userConfig.Backtest.RecordTrades { + log.Warn("!!! Trade recording is enabled for back-testing !!!") + log.Warn("!!! To run back-testing, you should use an isolated database for storing back-testing trades !!!") + log.Warn("!!! The trade record in the current database WILL ALL BE DELETED BEFORE THIS BACK-TESTING !!!") + if !force { + if !confirmation("Are you sure to continue?") { + return nil + } } - } - if err := environ.TradeService.DeleteAll(); err != nil { - return err + if err := environ.TradeService.DeleteAll(); err != nil { + return err + } } backtestExchange := backtest.NewExchange(exchangeName, backtestService, userConfig.Backtest) @@ -315,18 +317,18 @@ var BacktestCmd = &cobra.Command{ InitialBalances types.BalanceMap `json:"initialBalances,omitempty"` FinalBalances types.BalanceMap `json:"finalBalances,omitempty"` }{ - Symbol: symbol, - PnLReport: report, + Symbol: symbol, + PnLReport: report, InitialBalances: initBalances, - FinalBalances: finalBalances, + FinalBalances: finalBalances, } - jsonOutput, err := json.MarshalIndent(&result,"", " ") + jsonOutput, err := json.MarshalIndent(&result, "", " ") if err != nil { return err } - if err := ioutil.WriteFile(filepath.Join(outputDirectory, symbol + ".json"), jsonOutput, 0644) ; err != nil { + if err := ioutil.WriteFile(filepath.Join(outputDirectory, symbol+".json"), jsonOutput, 0644); err != nil { return err } }