mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
avoid recording trades in backtest by default
introducing a RecordTrades option
This commit is contained in:
parent
85bb9f214e
commit
0472b7f21e
|
@ -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) {
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user