diff --git a/pkg/bbgo/sync.go b/pkg/bbgo/sync.go deleted file mode 100644 index 0f9a7ea3a..000000000 --- a/pkg/bbgo/sync.go +++ /dev/null @@ -1,91 +0,0 @@ -package bbgo - -import ( - "context" - "time" - - "github.com/sirupsen/logrus" -) - -func stringSliceContains(slice []string, needle string) bool { - for _, s := range slice { - if s == needle { - return true - } - } - - return false -} - -func FindPossibleSymbols(session *ExchangeSession) (symbols []string, err error) { - var balances = session.Account.Balances() - var fiatCurrencies = []string{"USDC", "USDT", "USD", "TWD", "EUR", "GBP"} - var fiatAssets []string - - for _, currency := range fiatCurrencies { - if balance, ok := balances[currency]; ok && balance.Total() > 0 { - fiatAssets = append(fiatAssets, currency) - } - } - - var symbolMap = map[string]struct{}{} - - for _, market := range session.Markets() { - // ignore the markets that are not fiat currency markets - if !stringSliceContains(fiatAssets, market.QuoteCurrency) { - continue - } - - // ignore the asset that we don't have in the balance sheet - balance, hasAsset := balances[market.BaseCurrency] - if !hasAsset || balance.Total() == 0 { - continue - } - - symbolMap[market.Symbol] = struct{}{} - } - - for s := range symbolMap { - symbols = append(symbols, s) - } - - return symbols, nil -} - -// SyncSessionSymbols syncs the trades from the given exchange session -func SyncSessionSymbols(ctx context.Context, environ *Environment, session *ExchangeSession, startTime time.Time, symbols ...string) error { - for _, symbol := range symbols { - logrus.Debugf("syncing trades from exchange session %s...", session.Name) - if err := environ.TradeSync.SyncTrades(ctx, session.Exchange, symbol, startTime); err != nil { - return err - } - - logrus.Debugf("syncing orders from exchange session %s...", session.Name) - if err := environ.TradeSync.SyncOrders(ctx, session.Exchange, symbol, startTime); err != nil { - return err - } - } - - return nil -} - -func getSessionSymbols(session *ExchangeSession, defaultSymbols ...string) ([]string, error) { - if session.IsolatedMargin { - return []string{session.IsolatedMarginSymbol}, nil - } - - if len(defaultSymbols) > 0 { - return defaultSymbols, nil - } - - return FindPossibleSymbols(session) -} - -func SyncSession(ctx context.Context, environ *Environment, session *ExchangeSession, startTime time.Time, defaultSymbols ...string) error { - symbols, err := getSessionSymbols(session, defaultSymbols...) - if err != nil { - return err - } - - return SyncSessionSymbols(ctx, environ, session, startTime, symbols...) -} diff --git a/pkg/cmd/sync.go b/pkg/cmd/sync.go index 2406b2837..c21c92cf1 100644 --- a/pkg/cmd/sync.go +++ b/pkg/cmd/sync.go @@ -102,7 +102,7 @@ var SyncCmd = &cobra.Command{ return err } - if err := bbgo.SyncSession(ctx, environ, session, startTime, defaultSymbols...) ; err != nil { + if err := environ.TradeSync.SyncSession(ctx, session, startTime, defaultSymbols...) ; err != nil { return err } diff --git a/pkg/service/sync.go b/pkg/service/sync.go index f83ed2a5e..2c7c2b2c6 100644 --- a/pkg/service/sync.go +++ b/pkg/service/sync.go @@ -6,6 +6,7 @@ import ( "github.com/sirupsen/logrus" + "github.com/c9s/bbgo/pkg/bbgo" batch2 "github.com/c9s/bbgo/pkg/exchange/batch" "github.com/c9s/bbgo/pkg/types" ) @@ -131,3 +132,89 @@ func (s *SyncService) SyncTrades(ctx context.Context, exchange types.Exchange, s return <-errC } + + +// SyncSessionSymbols syncs the trades from the given exchange session +func (s *SyncService) SyncSessionSymbols(ctx context.Context, session *bbgo.ExchangeSession, startTime time.Time, symbols ...string) error { + for _, symbol := range symbols { + logrus.Debugf("syncing trades from exchange session %s...", session.Name) + if err := s.SyncTrades(ctx, session.Exchange, symbol, startTime); err != nil { + return err + } + + logrus.Debugf("syncing orders from exchange session %s...", session.Name) + if err := s.SyncOrders(ctx, session.Exchange, symbol, startTime); err != nil { + return err + } + } + + return nil +} + +func (s *SyncService) SyncSession(ctx context.Context, session *bbgo.ExchangeSession, startTime time.Time, defaultSymbols ...string) error { + symbols, err := getSessionSymbols(session, defaultSymbols...) + if err != nil { + return err + } + + return s.SyncSessionSymbols(ctx, session, startTime, symbols...) +} + +func getSessionSymbols(session *bbgo.ExchangeSession, defaultSymbols ...string) ([]string, error) { + if session.IsolatedMargin { + return []string{session.IsolatedMarginSymbol}, nil + } + + if len(defaultSymbols) > 0 { + return defaultSymbols, nil + } + + return FindPossibleSymbols(session) +} + + +func FindPossibleSymbols(session *bbgo.ExchangeSession) (symbols []string, err error) { + var balances = session.Account.Balances() + var fiatCurrencies = []string{"USDC", "USDT", "USD", "TWD", "EUR", "GBP"} + var fiatAssets []string + + for _, currency := range fiatCurrencies { + if balance, ok := balances[currency]; ok && balance.Total() > 0 { + fiatAssets = append(fiatAssets, currency) + } + } + + var symbolMap = map[string]struct{}{} + + for _, market := range session.Markets() { + // ignore the markets that are not fiat currency markets + if !stringSliceContains(fiatAssets, market.QuoteCurrency) { + continue + } + + // ignore the asset that we don't have in the balance sheet + balance, hasAsset := balances[market.BaseCurrency] + if !hasAsset || balance.Total() == 0 { + continue + } + + symbolMap[market.Symbol] = struct{}{} + } + + for s := range symbolMap { + symbols = append(symbols, s) + } + + return symbols, nil +} + + +func stringSliceContains(slice []string, needle string) bool { + for _, s := range slice { + if s == needle { + return true + } + } + + return false +}