2020-11-05 06:12:19 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-01-19 17:46:17 +00:00
|
|
|
"os"
|
2020-11-05 06:12:19 +00:00
|
|
|
"time"
|
|
|
|
|
2021-01-19 17:46:17 +00:00
|
|
|
"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"
|
|
|
|
|
2021-01-14 07:10:11 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/bbgo"
|
2020-11-05 06:12:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2021-01-19 17:46:17 +00:00
|
|
|
SyncCmd.Flags().String("session", "", "the exchange session name for sync")
|
2021-02-18 14:07:54 +00:00
|
|
|
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{
|
|
|
|
Use: "sync",
|
2020-11-11 08:11:41 +00:00
|
|
|
Short: "sync trades, orders",
|
2020-11-05 06:12:19 +00:00
|
|
|
SilenceUsage: true,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2021-01-19 17:46:17 +00:00
|
|
|
configFile, err := cmd.Flags().GetString("config")
|
2020-11-05 06:12:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-19 17:46:17 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-01-19 17:46:17 +00:00
|
|
|
since, err := cmd.Flags().GetString("since")
|
2020-11-05 06:12:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-14 07:10:11 +00:00
|
|
|
environ := bbgo.NewEnvironment()
|
2021-04-21 07:06:53 +00:00
|
|
|
if err := environ.ConfigureDatabase(ctx); err != nil {
|
2021-02-06 03:33:49 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-01-19 18:32:55 +00:00
|
|
|
sessionName, err := cmd.Flags().GetString("session")
|
2021-01-19 17:46:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-19 18:32:55 +00:00
|
|
|
symbol, err := cmd.Flags().GetString("symbol")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-01-19 17:46:17 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2021-02-18 14:07:54 +00:00
|
|
|
if len(symbol) > 0 {
|
2022-01-26 16:17:11 +00:00
|
|
|
syncSymbols = []string{symbol}
|
2021-02-18 14:07:54 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 14:40:46 +00:00
|
|
|
var selectedSessions []string
|
2021-02-18 14:07:54 +00:00
|
|
|
|
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-11 08:11:41 +00:00
|
|
|
}
|
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 {
|
2021-01-19 18:32:55 +00:00
|
|
|
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-06 13:40:48 +00:00
|
|
|
|
2020-11-05 06:12:19 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|