bbgo_origin/pkg/cmd/sync.go

123 lines
2.6 KiB
Go
Raw Permalink Normal View History

2020-11-05 06:12:19 +00:00
package cmd
import (
"context"
"os"
2020-11-05 06:12:19 +00:00
"time"
"github.com/pkg/errors"
2020-11-06 16:49:17 +00:00
log "github.com/sirupsen/logrus"
2020-11-05 06:12:19 +00:00
"github.com/spf13/cobra"
"github.com/c9s/bbgo/pkg/bbgo"
2020-11-05 06:12:19 +00:00
)
func init() {
SyncCmd.Flags().String("session", "", "the exchange session name for sync")
SyncCmd.Flags().String("symbol", "", "symbol of market for syncing")
2020-11-05 06:12:19 +00:00
SyncCmd.Flags().String("since", "", "sync from time")
RootCmd.AddCommand(SyncCmd)
}
var SyncCmd = &cobra.Command{
2022-05-09 06:04:51 +00:00
Use: "sync [--session=[exchange_name]] [--symbol=[pair_name]] [[--since=yyyy/mm/dd]]",
Short: "sync trades and orders history",
2020-11-05 06:12:19 +00:00
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
configFile, err := cmd.Flags().GetString("config")
2020-11-05 06:12:19 +00:00
if err != nil {
return err
}
if len(configFile) == 0 {
return errors.New("--config option is required")
}
if _, err := os.Stat(configFile); os.IsNotExist(err) {
return err
}
userConfig, err := bbgo.Load(configFile, false)
2020-11-05 06:12:19 +00:00
if err != nil {
return err
}
since, err := cmd.Flags().GetString("since")
2020-11-05 06:12:19 +00:00
if err != nil {
return err
}
environ := bbgo.NewEnvironment()
if err := environ.ConfigureDatabase(ctx); err != nil {
return err
2020-11-05 06:12:19 +00:00
}
2021-02-21 08:52:47 +00:00
if err := environ.ConfigureExchangeSessions(userConfig); err != nil {
2020-11-05 06:12:19 +00:00
return err
}
sessionName, err := cmd.Flags().GetString("session")
if err != nil {
return err
}
symbol, err := cmd.Flags().GetString("symbol")
if err != nil {
return err
}
2022-01-26 16:17:11 +00:00
var (
// default sync start time
defaultSyncStartTime = time.Now().AddDate(-1, 0, 0)
)
var syncStartTime = defaultSyncStartTime
if userConfig.Sync != nil && userConfig.Sync.Since != nil {
syncStartTime = userConfig.Sync.Since.Time()
2022-01-26 16:17:11 +00:00
}
if len(since) > 0 {
syncStartTime, err = time.ParseInLocation("2006-01-02", since, time.Local)
if err != nil {
return err
}
}
environ.SetSyncStartTime(syncStartTime)
// syncSymbols is the symbol list to sync
var syncSymbols []string
if userConfig.Sync != nil && len(userConfig.Sync.Symbols) > 0 {
syncSymbols = userConfig.Sync.Symbols
}
2021-02-20 03:29:33 +00:00
if len(symbol) > 0 {
2022-01-26 16:17:11 +00:00
syncSymbols = []string{symbol}
}
2021-02-18 14:40:46 +00:00
var selectedSessions []string
2022-01-26 16:17:11 +00:00
if userConfig.Sync != nil && len(userConfig.Sync.Sessions) > 0 {
selectedSessions = userConfig.Sync.Sessions
}
2021-02-18 14:40:46 +00:00
if len(sessionName) > 0 {
selectedSessions = []string{sessionName}
}
2020-11-06 16:49:17 +00:00
2021-02-18 14:40:46 +00:00
sessions := environ.SelectSessions(selectedSessions...)
for _, session := range sessions {
2022-01-26 16:17:11 +00:00
if err := environ.SyncSession(ctx, session, syncSymbols...); err != nil {
return err
}
2021-02-18 14:40:46 +00:00
log.Infof("exchange session %s synchronization done", session.Name)
2020-11-06 16:49:17 +00:00
}
2020-11-05 06:12:19 +00:00
return nil
},
}