Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: [xalign] fix reversed market #1585

Merged
merged 2 commits into from Mar 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 24 additions & 4 deletions pkg/strategy/xalign/strategy.go
Expand Up @@ -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)

Expand All @@ -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]

Expand All @@ -128,10 +132,24 @@ func (s *Strategy) selectSessionForCurrency(ctx context.Context, sessions map[st
}

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)
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)
Expand Down Expand Up @@ -376,7 +394,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)
Expand Down