pull out .Subscribe from trader.Run

This commit is contained in:
c9s 2021-02-01 17:13:54 +08:00
parent fca6115a0a
commit a60aeb4771
4 changed files with 10 additions and 1 deletions

View File

@ -148,6 +148,8 @@ type ExchangeSession struct {
usedSymbols map[string]struct{}
initializedSymbols map[string]struct{}
logger *log.Entry
}
func NewExchangeSession(name string, exchange types.Exchange) *ExchangeSession {
@ -174,6 +176,7 @@ func NewExchangeSession(name string, exchange types.Exchange) *ExchangeSession {
orderStores: make(map[string]*OrderStore),
usedSymbols: make(map[string]struct{}),
initializedSymbols: make(map[string]struct{}),
logger: log.WithField("session", name),
}
}

View File

@ -127,7 +127,7 @@ func (trader *Trader) SetRiskControls(riskControls *RiskControls) {
trader.riskControls = riskControls
}
func (trader *Trader) Run(ctx context.Context) error {
func (trader *Trader) Subscribe() {
// pre-subscribe the data
for sessionName, strategies := range trader.exchangeStrategies {
session := trader.environment.sessions[sessionName]
@ -143,7 +143,9 @@ func (trader *Trader) Run(ctx context.Context) error {
subscriber.CrossSubscribe(trader.environment.sessions)
}
}
}
func (trader *Trader) Run(ctx context.Context) error {
if err := trader.environment.Init(ctx); err != nil {
return err
}

View File

@ -218,6 +218,8 @@ var BacktestCmd = &cobra.Command{
log.Warnf("backtest does not support CrossExchangeStrategy, strategies won't be added.")
}
trader.Subscribe()
if err := trader.Run(ctx); err != nil {
return err
}

View File

@ -214,6 +214,8 @@ func runConfig(basectx context.Context, userConfig *bbgo.Config, enableApiServer
}
}
trader.Subscribe()
if err := trader.Run(ctx); err != nil {
return err
}