diff --git a/pkg/exchange/util.go b/pkg/exchange/util.go new file mode 100644 index 000000000..639103722 --- /dev/null +++ b/pkg/exchange/util.go @@ -0,0 +1,29 @@ +package exchange + +import "github.com/c9s/bbgo/pkg/types" + +func GetSessionAttributes(exchange types.Exchange) (isMargin, isFutures, isIsolated bool, isolatedSymbol string) { + if marginExchange, ok := exchange.(types.MarginExchange); ok { + marginSettings := marginExchange.GetMarginSettings() + isMargin = marginSettings.IsMargin + if isMargin { + isIsolated = marginSettings.IsIsolatedMargin + if marginSettings.IsIsolatedMargin { + isolatedSymbol = marginSettings.IsolatedMarginSymbol + } + } + } + + if futuresExchange, ok := exchange.(types.FuturesExchange); ok { + futuresSettings := futuresExchange.GetFuturesSettings() + isFutures = futuresSettings.IsFutures + if isFutures { + isIsolated = futuresSettings.IsIsolatedFutures + if futuresSettings.IsIsolatedFutures { + isolatedSymbol = futuresSettings.IsolatedFuturesSymbol + } + } + } + + return isMargin, isFutures, isIsolated, isolatedSymbol +} diff --git a/pkg/service/backtest.go b/pkg/service/backtest.go index 256514fcf..7b43f8d03 100644 --- a/pkg/service/backtest.go +++ b/pkg/service/backtest.go @@ -13,6 +13,7 @@ import ( "github.com/pkg/errors" log "github.com/sirupsen/logrus" + exchange2 "github.com/c9s/bbgo/pkg/exchange" "github.com/c9s/bbgo/pkg/exchange/batch" "github.com/c9s/bbgo/pkg/types" ) @@ -25,7 +26,7 @@ func (s *BacktestService) SyncKLineByInterval(ctx context.Context, exchange type log.Infof("synchronizing %s klines with interval %s: %s <=> %s", exchange.Name(), interval, startTime, endTime) // TODO: use isFutures here - _, _, isIsolated, isolatedSymbol := getExchangeAttributes(exchange) + _, _, isIsolated, isolatedSymbol := exchange2.GetSessionAttributes(exchange) // override symbol if isolatedSymbol is not empty if isIsolated && len(isolatedSymbol) > 0 { symbol = isolatedSymbol diff --git a/pkg/service/deposit.go b/pkg/service/deposit.go index b892cdb81..6b49cce13 100644 --- a/pkg/service/deposit.go +++ b/pkg/service/deposit.go @@ -7,6 +7,7 @@ import ( sq "github.com/Masterminds/squirrel" "github.com/jmoiron/sqlx" + "github.com/c9s/bbgo/pkg/exchange" "github.com/c9s/bbgo/pkg/exchange/batch" "github.com/c9s/bbgo/pkg/types" ) @@ -17,7 +18,7 @@ type DepositService struct { // Sync syncs the withdraw records into db func (s *DepositService) Sync(ctx context.Context, ex types.Exchange, startTime time.Time) error { - isMargin, isFutures, isIsolated, _ := getExchangeAttributes(ex) + isMargin, isFutures, isIsolated, _ := exchange.GetSessionAttributes(ex) if isMargin || isFutures || isIsolated { // only works in spot return nil diff --git a/pkg/service/order.go b/pkg/service/order.go index 4780bfc0d..8cdff47ec 100644 --- a/pkg/service/order.go +++ b/pkg/service/order.go @@ -10,6 +10,7 @@ import ( "github.com/jmoiron/sqlx" log "github.com/sirupsen/logrus" + exchange2 "github.com/c9s/bbgo/pkg/exchange" "github.com/c9s/bbgo/pkg/exchange/batch" "github.com/c9s/bbgo/pkg/types" ) @@ -19,7 +20,7 @@ type OrderService struct { } func (s *OrderService) Sync(ctx context.Context, exchange types.Exchange, symbol string, startTime time.Time) error { - isMargin, isFutures, isIsolated, isolatedSymbol := getExchangeAttributes(exchange) + isMargin, isFutures, isIsolated, isolatedSymbol := exchange2.GetSessionAttributes(exchange) // override symbol if isolatedSymbol is not empty if isIsolated && len(isolatedSymbol) > 0 { symbol = isolatedSymbol diff --git a/pkg/service/reward.go b/pkg/service/reward.go index ef28f0ef8..bf657a601 100644 --- a/pkg/service/reward.go +++ b/pkg/service/reward.go @@ -10,6 +10,7 @@ import ( sq "github.com/Masterminds/squirrel" "github.com/jmoiron/sqlx" + exchange2 "github.com/c9s/bbgo/pkg/exchange" "github.com/c9s/bbgo/pkg/exchange/batch" "github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/types" @@ -29,7 +30,7 @@ func (s *RewardService) Sync(ctx context.Context, exchange types.Exchange, start return ErrExchangeRewardServiceNotImplemented } - isMargin, isFutures, _, _ := getExchangeAttributes(exchange) + isMargin, isFutures, _, _ := exchange2.GetSessionAttributes(exchange) if isMargin || isFutures { return nil } diff --git a/pkg/service/trade.go b/pkg/service/trade.go index cc7e1480e..ae8379fa0 100644 --- a/pkg/service/trade.go +++ b/pkg/service/trade.go @@ -11,6 +11,7 @@ import ( "github.com/pkg/errors" log "github.com/sirupsen/logrus" + exchange2 "github.com/c9s/bbgo/pkg/exchange" "github.com/c9s/bbgo/pkg/exchange/batch" "github.com/c9s/bbgo/pkg/types" ) @@ -53,7 +54,7 @@ func NewTradeService(db *sqlx.DB) *TradeService { } func (s *TradeService) Sync(ctx context.Context, exchange types.Exchange, symbol string, startTime time.Time) error { - isMargin, isFutures, isIsolated, isolatedSymbol := getExchangeAttributes(exchange) + isMargin, isFutures, isIsolated, isolatedSymbol := exchange2.GetSessionAttributes(exchange) // override symbol if isolatedSymbol is not empty if isIsolated && len(isolatedSymbol) > 0 { symbol = isolatedSymbol @@ -412,28 +413,3 @@ func SelectLastTrades(ex types.ExchangeName, symbol string, isMargin, isFutures, Limit(limit) } -func getExchangeAttributes(exchange types.Exchange) (isMargin, isFutures, isIsolated bool, isolatedSymbol string) { - if marginExchange, ok := exchange.(types.MarginExchange); ok { - marginSettings := marginExchange.GetMarginSettings() - isMargin = marginSettings.IsMargin - if isMargin { - isIsolated = marginSettings.IsIsolatedMargin - if marginSettings.IsIsolatedMargin { - isolatedSymbol = marginSettings.IsolatedMarginSymbol - } - } - } - - if futuresExchange, ok := exchange.(types.FuturesExchange); ok { - futuresSettings := futuresExchange.GetFuturesSettings() - isFutures = futuresSettings.IsFutures - if isFutures { - isIsolated = futuresSettings.IsIsolatedFutures - if futuresSettings.IsIsolatedFutures { - isolatedSymbol = futuresSettings.IsolatedFuturesSymbol - } - } - } - - return isMargin, isFutures, isIsolated, isolatedSymbol -} diff --git a/pkg/service/withdraw.go b/pkg/service/withdraw.go index af0a0df83..f8448d0e0 100644 --- a/pkg/service/withdraw.go +++ b/pkg/service/withdraw.go @@ -7,6 +7,7 @@ import ( sq "github.com/Masterminds/squirrel" "github.com/jmoiron/sqlx" + "github.com/c9s/bbgo/pkg/exchange" "github.com/c9s/bbgo/pkg/exchange/batch" "github.com/c9s/bbgo/pkg/types" ) @@ -17,7 +18,7 @@ type WithdrawService struct { // Sync syncs the withdrawal records into db func (s *WithdrawService) Sync(ctx context.Context, ex types.Exchange, startTime time.Time) error { - isMargin, isFutures, isIsolated, _ := getExchangeAttributes(ex) + isMargin, isFutures, isIsolated, _ := exchange.GetSessionAttributes(ex) if isMargin || isFutures || isIsolated { // only works in spot return nil