From 4ce6e856240e491868b3bbaea0c56b8cff1731d3 Mon Sep 17 00:00:00 2001 From: c9s Date: Sat, 20 Feb 2021 11:29:33 +0800 Subject: [PATCH] add sync status to the environment --- pkg/bbgo/environment.go | 62 +++++++++++++++++++++++++++++++++-------- pkg/cmd/sync.go | 8 ++++-- 2 files changed, 56 insertions(+), 14 deletions(-) diff --git a/pkg/bbgo/environment.go b/pkg/bbgo/environment.go index 0d2fb75d0..adee884d6 100644 --- a/pkg/bbgo/environment.go +++ b/pkg/bbgo/environment.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "os" + "sync" "time" "github.com/codingconcepts/env" @@ -53,15 +54,20 @@ type Environment struct { TradeSync *service.SyncService // startTime is the time of start point (which is used in the backtest) - startTime time.Time - tradeScanTime time.Time - sessions map[string]*ExchangeSession + startTime time.Time + + // syncStartTime is the time point we want to start the sync (for trades and orders) + syncStartTime time.Time + syncing bool + syncMutex sync.Mutex + + sessions map[string]*ExchangeSession } func NewEnvironment() *Environment { return &Environment{ // default trade scan time - tradeScanTime: time.Now().AddDate(0, -1, 0), // sync from 1 month ago + syncStartTime: time.Now().AddDate(-1, 0, 0), // defaults to sync from 1 year ago sessions: make(map[string]*ExchangeSession), startTime: time.Now(), } @@ -83,7 +89,7 @@ func (environ *Environment) SelectSessions(names ...string) map[string]*Exchange sessions := make(map[string]*ExchangeSession) for _, name := range names { - if s, ok := environ.Session(name) ; ok { + if s, ok := environ.Session(name); ok { sessions[name] = s } } @@ -410,9 +416,9 @@ func (environ *Environment) SetStartTime(t time.Time) *Environment { return environ } -// SyncTradesFrom overrides the default trade scan time (-7 days) -func (environ *Environment) SyncTradesFrom(t time.Time) *Environment { - environ.tradeScanTime = t +// SetSyncStartTime overrides the default trade scan time (-7 days) +func (environ *Environment) SetSyncStartTime(t time.Time) *Environment { + environ.syncStartTime = t return environ } @@ -441,13 +447,48 @@ func (environ *Environment) Connect(ctx context.Context) error { return nil } -func (environ *Environment) SyncSession(ctx context.Context, session *ExchangeSession, startTime time.Time, defaultSymbols ...string) error { +func (environ *Environment) IsSyncing() bool { + return environ.syncing +} + +// Sync syncs all registered exchange sessions +func (environ *Environment) Sync(ctx context.Context) error { + environ.syncMutex.Lock() + defer environ.syncMutex.Unlock() + environ.syncing = true + + defer func() { + environ.syncing = false + }() + + for _, session := range environ.sessions { + if err := environ.syncSession(ctx, session); err != nil { + return err + } + } + + return nil +} + +func (environ *Environment) SyncSession(ctx context.Context, session *ExchangeSession, defaultSymbols ...string) error { + environ.syncMutex.Lock() + defer environ.syncMutex.Unlock() + + environ.syncing = true + defer func() { + environ.syncing = false + }() + + return environ.syncSession(ctx, session, defaultSymbols...) +} + +func (environ *Environment) syncSession(ctx context.Context, session *ExchangeSession, defaultSymbols ...string) error { symbols, err := getSessionSymbols(session, defaultSymbols...) if err != nil { return err } - return environ.TradeSync.SyncSessionSymbols(ctx, session.Exchange, startTime, symbols...) + return environ.TradeSync.SyncSessionSymbols(ctx, session.Exchange, environ.syncStartTime, symbols...) } func getSessionSymbols(session *ExchangeSession, defaultSymbols ...string) ([]string, error) { @@ -461,4 +502,3 @@ func getSessionSymbols(session *ExchangeSession, defaultSymbols ...string) ([]st return session.FindPossibleSymbols() } - diff --git a/pkg/cmd/sync.go b/pkg/cmd/sync.go index af6fb8a4d..a2e055406 100644 --- a/pkg/cmd/sync.go +++ b/pkg/cmd/sync.go @@ -59,8 +59,8 @@ var SyncCmd = &cobra.Command{ } var ( - // default start time - startTime = time.Now().AddDate(0, -3, 0) + // default sync start time + startTime = time.Now().AddDate(-1, 0, 0) ) if len(since) > 0 { @@ -85,6 +85,8 @@ var SyncCmd = &cobra.Command{ return err } + environ.SetSyncStartTime(startTime) + var defaultSymbols []string if len(symbol) > 0 { defaultSymbols = []string{symbol} @@ -102,7 +104,7 @@ var SyncCmd = &cobra.Command{ return err } - if err := environ.SyncSession(ctx, session, startTime, defaultSymbols...) ; err != nil { + if err := environ.SyncSession(ctx, session, defaultSymbols...); err != nil { return err }