Skip to content

Commit

Permalink
Merge pull request c9s#1592 from c9s/c9s/fix-xalign-side-for-reversed…
Browse files Browse the repository at this point in the history
…-quote

FIX: [xalign] correct the base/quote currency balance name when it's reversed
  • Loading branch information
c9s committed Mar 19, 2024
2 parents 22221a8 + c11f886 commit 3e14c14
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions pkg/strategy/xalign/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,18 +131,22 @@ func (s *Strategy) selectSessionForCurrency(
side = types.SideTypeSell
}

for _, quoteCurrency := range quoteCurrencies {
for _, fromQuoteCurrency := range quoteCurrencies {
// skip the same currency, because there is no such USDT/USDT market
if currency == quoteCurrency {
if currency == fromQuoteCurrency {
continue
}

// check both quoteCurrency/currency and currency/quoteCurrency
symbol := currency + quoteCurrency
// check both fromQuoteCurrency/currency and currency/fromQuoteCurrency
baseCurrency := currency
quoteCurrency := fromQuoteCurrency
symbol := currency + fromQuoteCurrency
market, ok := session.Market(symbol)
if !ok {
// for TWD in USDT/TWD market, buy TWD means sell USDT
symbol = quoteCurrency + currency
baseCurrency = fromQuoteCurrency
quoteCurrency = currency
symbol = fromQuoteCurrency + currency
market, ok = session.Market(symbol)
if !ok {
continue
Expand Down Expand Up @@ -196,11 +200,18 @@ func (s *Strategy) selectSessionForCurrency(
continue
}

if expectedQuoteBalance, ok := s.ExpectedBalances[quoteCurrency]; ok {
rest := quoteBalance.Total().Sub(requiredQuoteAmount)
if rest.Compare(expectedQuoteBalance) < 0 {
log.Warnf("required quote amount %f will use up the expected balance %f, skip", requiredQuoteAmount.Float64(), expectedQuoteBalance.Float64())
continue
// for currency = TWD in market USDT/TWD
// since the side is reversed, the quote currency is also "TWD" here.
//
// for currency = BTC in market BTC/USDT and the side is buy
// we want to check if the quote currency USDT used up another expected balance.
if quoteCurrency != currency {
if expectedQuoteBalance, ok := s.ExpectedBalances[quoteCurrency]; ok {
rest := quoteBalance.Total().Sub(requiredQuoteAmount)
if rest.Compare(expectedQuoteBalance) < 0 {
log.Warnf("required quote amount %f will use up the expected balance %f, skip", requiredQuoteAmount.Float64(), expectedQuoteBalance.Float64())
continue
}
}
}

Expand Down Expand Up @@ -232,7 +243,7 @@ func (s *Strategy) selectSessionForCurrency(
price = ticker.Sell
}

baseBalance, ok := session.Account.Balance(currency)
baseBalance, ok := session.Account.Balance(baseCurrency)
if !ok {
continue
}
Expand Down

0 comments on commit 3e14c14

Please sign in to comment.