bbgo: improve RecordPosition method

This commit is contained in:
c9s 2022-03-11 16:58:45 +08:00
parent 5732555c2c
commit 322f31a56a
2 changed files with 23 additions and 10 deletions

View File

@ -92,6 +92,7 @@ type Environment struct {
syncStatusMutex sync.Mutex syncStatusMutex sync.Mutex
syncStatus SyncStatus syncStatus SyncStatus
syncConfig *SyncConfig
sessions map[string]*ExchangeSession sessions map[string]*ExchangeSession
} }
@ -445,7 +446,8 @@ func (environ *Environment) SetSyncStartTime(t time.Time) *Environment {
return environ return environ
} }
func (environ *Environment) BindSync(userConfig *Config) { func (environ *Environment) BindSync(config *SyncConfig) {
// skip this if we are running back-test
if environ.BacktestService != nil { if environ.BacktestService != nil {
return return
} }
@ -455,10 +457,12 @@ func (environ *Environment) BindSync(userConfig *Config) {
return return
} }
if userConfig.Sync == nil || userConfig.Sync.UserDataStream == nil { if config == nil || config.UserDataStream == nil {
return return
} }
environ.syncConfig = config
tradeWriter := func(trade types.Trade) { tradeWriter := func(trade types.Trade) {
if err := environ.TradeService.Insert(trade); err != nil { if err := environ.TradeService.Insert(trade); err != nil {
log.WithError(err).Errorf("trade insert error: %+v", trade) log.WithError(err).Errorf("trade insert error: %+v", trade)
@ -477,10 +481,10 @@ func (environ *Environment) BindSync(userConfig *Config) {
for _, session := range environ.sessions { for _, session := range environ.sessions {
// if trade sync is on, we will write all received trades // if trade sync is on, we will write all received trades
if userConfig.Sync.UserDataStream.Trades { if config.UserDataStream.Trades {
session.UserDataStream.OnTradeUpdate(tradeWriter) session.UserDataStream.OnTradeUpdate(tradeWriter)
} }
if userConfig.Sync.UserDataStream.FilledOrders { if config.UserDataStream.FilledOrders {
session.UserDataStream.OnOrderUpdate(orderWriter) session.UserDataStream.OnOrderUpdate(orderWriter)
} }
} }
@ -580,11 +584,7 @@ func (environ *Environment) RecordPosition(position *types.Position, trade types
return return
} }
if environ.DatabaseService == nil { if environ.DatabaseService == nil || environ.ProfitService == nil || environ.PositionService == nil {
return
}
if environ.ProfitService == nil {
return return
} }
@ -603,6 +603,17 @@ func (environ *Environment) RecordPosition(position *types.Position, trade types
if err := environ.ProfitService.Insert(profit); err != nil { if err := environ.ProfitService.Insert(profit); err != nil {
log.WithError(err).Errorf("can not insert profit record: %+v", profit) log.WithError(err).Errorf("can not insert profit record: %+v", profit)
} }
// if:
// 1) we are not using sync
// 2) and not sync-ing trades from the user data stream
if environ.TradeService != nil && (environ.syncConfig == nil ||
(environ.syncConfig.UserDataStream == nil) ||
(environ.syncConfig.UserDataStream != nil && !environ.syncConfig.UserDataStream.Trades)) {
if err := environ.TradeService.Insert(trade); err != nil {
log.WithError(err).Errorf("can not insert trade record: %+v", trade)
}
}
} }
func (environ *Environment) RecordProfit(profit types.Profit) { func (environ *Environment) RecordProfit(profit types.Profit) {

View File

@ -161,7 +161,9 @@ func runConfig(basectx context.Context, cmd *cobra.Command, userConfig *bbgo.Con
return err return err
} }
environ.BindSync(userConfig) if userConfig.Sync != nil {
environ.BindSync(userConfig.Sync)
}
} }
trader := bbgo.NewTrader(environ) trader := bbgo.NewTrader(environ)