From 6831c40371b7102c381532780936e9d05aeb7ede Mon Sep 17 00:00:00 2001 From: c9s Date: Fri, 15 Mar 2024 15:57:17 +0800 Subject: [PATCH 1/2] xalign: fix reversed market --- pkg/strategy/xalign/strategy.go | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/pkg/strategy/xalign/strategy.go b/pkg/strategy/xalign/strategy.go index b6c4f49ad3..42081761a2 100644 --- a/pkg/strategy/xalign/strategy.go +++ b/pkg/strategy/xalign/strategy.go @@ -90,7 +90,9 @@ func (s *Strategy) Validate() error { return nil } -func (s *Strategy) aggregateBalances(ctx context.Context, sessions map[string]*bbgo.ExchangeSession) (totalBalances types.BalanceMap, sessionBalances map[string]types.BalanceMap) { +func (s *Strategy) aggregateBalances( + ctx context.Context, sessions map[string]*bbgo.ExchangeSession, +) (totalBalances types.BalanceMap, sessionBalances map[string]types.BalanceMap) { totalBalances = make(types.BalanceMap) sessionBalances = make(map[string]types.BalanceMap) @@ -112,7 +114,9 @@ func (s *Strategy) aggregateBalances(ctx context.Context, sessions map[string]*b return totalBalances, sessionBalances } -func (s *Strategy) selectSessionForCurrency(ctx context.Context, sessions map[string]*bbgo.ExchangeSession, currency string, changeQuantity fixedpoint.Value) (*bbgo.ExchangeSession, *types.SubmitOrder) { +func (s *Strategy) selectSessionForCurrency( + ctx context.Context, sessions map[string]*bbgo.ExchangeSession, currency string, changeQuantity fixedpoint.Value, +) (*bbgo.ExchangeSession, *types.SubmitOrder) { for _, sessionName := range s.PreferredSessions { session := sessions[sessionName] @@ -128,10 +132,19 @@ func (s *Strategy) selectSessionForCurrency(ctx context.Context, sessions map[st } for _, quoteCurrency := range quoteCurrencies { + // check both quoteCurrency/currency and currency/quoteCurrency symbol := currency + quoteCurrency market, ok := session.Market(symbol) if !ok { - continue + // for TWD in USDT/TWD market, buy TWD means sell USDT + symbol = quoteCurrency + currency + market, ok = session.Market(symbol) + if !ok { + continue + } + + // reverse side + side = side.Reverse() } ticker, err := session.Exchange.QueryTicker(ctx, symbol) @@ -376,7 +389,9 @@ func (s *Strategy) align(ctx context.Context, sessions map[string]*bbgo.Exchange } } -func (s *Strategy) calculateRefillQuantity(totalBalances types.BalanceMap, currency string, expectedBalance fixedpoint.Value) fixedpoint.Value { +func (s *Strategy) calculateRefillQuantity( + totalBalances types.BalanceMap, currency string, expectedBalance fixedpoint.Value, +) fixedpoint.Value { if b, ok := totalBalances[currency]; ok { netBalance := b.Net() return expectedBalance.Sub(netBalance) From 1d314daa22496a6d60b84fd84eb1cebd639cb76a Mon Sep 17 00:00:00 2001 From: c9s Date: Fri, 15 Mar 2024 15:59:43 +0800 Subject: [PATCH 2/2] xalign: skip same currency --- pkg/strategy/xalign/strategy.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pkg/strategy/xalign/strategy.go b/pkg/strategy/xalign/strategy.go index 42081761a2..00fa4dbf49 100644 --- a/pkg/strategy/xalign/strategy.go +++ b/pkg/strategy/xalign/strategy.go @@ -132,6 +132,11 @@ func (s *Strategy) selectSessionForCurrency( } for _, quoteCurrency := range quoteCurrencies { + // skip the same currency, because there is no such USDT/USDT market + if currency == quoteCurrency { + continue + } + // check both quoteCurrency/currency and currency/quoteCurrency symbol := currency + quoteCurrency market, ok := session.Market(symbol)