bbgo: add environment config for disabling some klines defaults

This commit is contained in:
c9s 2023-11-11 07:42:29 +08:00
parent 28c8fda2db
commit b28b5e4097
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
3 changed files with 50 additions and 39 deletions

View File

@ -326,6 +326,11 @@ type ServiceConfig struct {
GoogleSpreadSheetService *GoogleSpreadSheetServiceConfig `json:"googleSpreadSheet" yaml:"googleSpreadSheet"`
}
type EnvironmentConfig struct {
DisableDefaultKLineSubscription bool `json:"disableDefaultKLineSubscription"`
DisableHistoryKLinePreload bool `json:"disableHistoryKLinePreload"`
}
type Config struct {
Build *BuildConfig `json:"build,omitempty" yaml:"build,omitempty"`
@ -343,6 +348,8 @@ type Config struct {
Service *ServiceConfig `json:"services,omitempty" yaml:"services,omitempty"`
Environment *EnvironmentConfig `json:"environment,omitempty" yaml:"environment,omitempty"`
Sessions map[string]*ExchangeSession `json:"sessions,omitempty" yaml:"sessions,omitempty"`
RiskControls *RiskControls `json:"riskControls,omitempty" yaml:"riskControls,omitempty"`

View File

@ -108,13 +108,13 @@ type Environment struct {
syncStatus SyncStatus
syncConfig *SyncConfig
loggingConfig *LoggingConfig
loggingConfig *LoggingConfig
environmentConfig *EnvironmentConfig
sessions map[string]*ExchangeSession
}
func NewEnvironment() *Environment {
now := time.Now()
return &Environment{
// default trade scan time

View File

@ -472,49 +472,53 @@ func (session *ExchangeSession) initSymbol(ctx context.Context, environ *Environ
}
if sub.Symbol == symbol {
klineSubscriptions[types.Interval(sub.Options.Interval)] = struct{}{}
klineSubscriptions[sub.Options.Interval] = struct{}{}
}
}
}
// always subscribe the 1m kline so we can make sure the connection persists.
klineSubscriptions[minInterval] = struct{}{}
for interval := range klineSubscriptions {
// avoid querying the last unclosed kline
endTime := environ.startTime
var i int64
for i = 0; i < KLinePreloadLimit; i += 1000 {
var duration time.Duration = time.Duration(-i * int64(interval.Duration()))
e := endTime.Add(duration)
kLines, err := session.Exchange.QueryKLines(ctx, symbol, interval, types.KLineQueryOptions{
EndTime: &e,
Limit: 1000, // indicators need at least 100
})
if err != nil {
return err
}
if len(kLines) == 0 {
log.Warnf("no kline data for %s %s (end time <= %s)", symbol, interval, e)
continue
}
// update last prices by the given kline
lastKLine := kLines[len(kLines)-1]
if interval == minInterval {
session.lastPrices[symbol] = lastKLine.Close
}
for _, k := range kLines {
// let market data store trigger the update, so that the indicator could be updated too.
marketDataStore.AddKLine(k)
}
}
if !(environ.environmentConfig != nil && environ.environmentConfig.DisableDefaultKLineSubscription) {
// subscribe the 1m kline by default so we can make sure the connection persists.
klineSubscriptions[minInterval] = struct{}{}
}
log.Infof("%s last price: %v", symbol, session.lastPrices[symbol])
if !(environ.environmentConfig != nil && environ.environmentConfig.DisableHistoryKLinePreload) {
for interval := range klineSubscriptions {
// avoid querying the last unclosed kline
endTime := environ.startTime
var i int64
for i = 0; i < KLinePreloadLimit; i += 1000 {
var duration time.Duration = time.Duration(-i * int64(interval.Duration()))
e := endTime.Add(duration)
kLines, err := session.Exchange.QueryKLines(ctx, symbol, interval, types.KLineQueryOptions{
EndTime: &e,
Limit: 1000, // indicators need at least 100
})
if err != nil {
return err
}
if len(kLines) == 0 {
log.Warnf("no kline data for %s %s (end time <= %s)", symbol, interval, e)
continue
}
// update last prices by the given kline
lastKLine := kLines[len(kLines)-1]
if interval == minInterval {
session.lastPrices[symbol] = lastKLine.Close
}
for _, k := range kLines {
// let market data store trigger the update, so that the indicator could be updated too.
marketDataStore.AddKLine(k)
}
}
}
log.Infof("%s last price: %v", symbol, session.lastPrices[symbol])
}
session.initializedSymbols[symbol] = struct{}{}
return nil