bbgo_origin/pkg/cmd/sync.go

135 lines
3.0 KiB
Go
Raw Normal View History

2020-11-05 06:12:19 +00:00
package cmd
import (
"context"
"fmt"
"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"
2021-02-02 09:26:35 +00:00
"github.com/spf13/viper"
2020-11-05 06:12:19 +00:00
"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")
2020-11-05 06:12:19 +00:00
SyncCmd.Flags().String("symbol", "BTCUSDT", "trading symbol")
SyncCmd.Flags().String("since", "", "sync from time")
RootCmd.AddCommand(SyncCmd)
}
var SyncCmd = &cobra.Command{
Use: "sync",
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()
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()
2021-02-02 09:26:35 +00:00
if viper.IsSet("mysql-url") {
dsn := viper.GetString("mysql-url")
if err := environ.ConfigureDatabase(ctx, dsn); err != nil {
return err
}
2020-11-05 06:12:19 +00:00
}
if err := environ.AddExchangesFromConfig(userConfig); err != nil {
2020-11-05 06:12:19 +00:00
return err
}
var (
// default start time
startTime = time.Now().AddDate(0, -3, 0)
)
if len(since) > 0 {
loc, err := time.LoadLocation("Asia/Taipei")
if err != nil {
return err
}
startTime, err = time.ParseInLocation("2006-01-02", since, loc)
if err != nil {
return err
}
}
sessionName, err := cmd.Flags().GetString("session")
if err != nil {
return err
}
symbol, err := cmd.Flags().GetString("symbol")
if err != nil {
return err
}
if len(sessionName) > 0 {
session, ok := environ.Session(sessionName)
if !ok {
return fmt.Errorf("session %s not found", sessionName)
}
return syncSession(ctx, environ, session, symbol, startTime)
}
2020-11-06 16:49:17 +00:00
for _, session := range environ.Sessions() {
if err := syncSession(ctx, environ, session, symbol, startTime); err != nil {
return err
}
2020-11-06 16:49:17 +00:00
}
2020-11-05 06:12:19 +00:00
return nil
},
}
func syncSession(ctx context.Context, environ *bbgo.Environment, session *bbgo.ExchangeSession, symbol string, startTime time.Time) error {
log.Infof("starting syncing exchange session %s", session.Name)
2021-02-02 09:26:35 +00:00
if session.IsolatedMargin {
log.Infof("session is configured as isolated margin session, using isolated margin symbol %s instead of %s", session.IsolatedMarginSymbol, symbol)
symbol = session.IsolatedMarginSymbol
}
log.Infof("syncing trades from exchange session %s...", session.Name)
if err := environ.TradeSync.SyncTrades(ctx, session.Exchange, symbol, startTime); err != nil {
return err
}
log.Infof("syncing orders from exchange session %s...", session.Name)
if err := environ.TradeSync.SyncOrders(ctx, session.Exchange, symbol, startTime); err != nil {
return err
}
log.Infof("exchange session %s synchronization done", session.Name)
return nil
}