2020-11-05 06:12:19 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-01-19 17:46:17 +00:00
|
|
|
"fmt"
|
|
|
|
"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-02-02 09:26:35 +00:00
|
|
|
"github.com/spf13/viper"
|
2020-11-05 06:12:19 +00:00
|
|
|
|
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")
|
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",
|
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-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
|
|
|
}
|
|
|
|
|
2021-01-19 17:46:17 +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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-01-19 18:32:55 +00:00
|
|
|
if len(sessionName) > 0 {
|
|
|
|
session, ok := environ.Session(sessionName)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("session %s not found", sessionName)
|
|
|
|
}
|
2021-01-19 17:46:17 +00:00
|
|
|
|
2021-01-19 18:32:55 +00:00
|
|
|
return syncSession(ctx, environ, session, symbol, startTime)
|
2020-11-11 08:11:41 +00:00
|
|
|
}
|
2020-11-06 16:49:17 +00:00
|
|
|
|
2021-01-19 18:32:55 +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-06 13:40:48 +00:00
|
|
|
|
2020-11-05 06:12:19 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2021-01-19 18:32:55 +00:00
|
|
|
|
|
|
|
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 {
|
2021-01-19 18:32:55 +00:00
|
|
|
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
|
|
|
|
}
|