knowm / XChange

XChange is a Java library providing a streamlined API for interacting with 60+ Bitcoin and Altcoin exchanges providing a consistent interface for trading and accessing market data.
http://knowm.org/open-source/xchange/
MIT License
3.81k stars 1.93k forks source link

BitfinexAdapters.adaptTicker(org.knowm.xchange.bitfinex.v2.dto.marketdata.BitfinexTicker bitfinexTicker) NPE #4818

Open pavlo-pastushok opened 5 months ago

pavlo-pastushok commented 5 months ago

During Bitfinex remoteInit I received an error related to tickers adaptation

BigDecimal percentageChange =
        bitfinexTicker.getDailyChangePerc().multiply(new BigDecimal("100"), new MathContext(8));

a line above throws an exception

Cannot invoke "java.math.BigDecimal.multiply(java.math.BigDecimal, java.math.MathContext)" because the return value of "org.knowm.xchange.bitfinex.v2.dto.marketdata.BitfinexTicker.getDailyChangePerc()" is null

because some tickers have dailyChangePerc field as null

for example:

["tALT2612:USD",100,null,100,null,null,null,null,null,null,null]
["tALT2612:UST",100,null,100,null,null,null,null,null,null,null]
pavlo-pastushok commented 5 months ago

did anyone facing same issue?

rizer1980 commented 5 months ago

Hello, @pavlo-pastushok i checked https://api.bitfinex.com/v2/tickers?symbols=tALT2612%3AUSD strange, but results are different, sometimes one, sometimes another. ["tALT2612:USD",100,0,100,0,0,0,100,1.0E-6,0,0] ["tALT2612:USD",100,null,100,null,null,null,null,null,null,null]

We need to understand that this is normal operation of the exchange API, and then create an exception handler. Or is this some kind of temporary error?

pavlo-pastushok commented 5 months ago

Hi @rizer1980

I am not sure, but I received this situation more than 10 times a day during last 3 days I will try to contact support with this question

rustyruzzee commented 4 months ago

@rizer1980 @pavlo-pastushok

I have faced a similar issue. It seemed that exchange metadata was not populated for currency pairs with USDT as the quoted currency. Upon checking, it looks like during the remote init process, currency pairs derived from tickers and symbol details are processed inconsistently, at least in terms of adapting the currencies. This resulted in

BigDecimal last = lastPrices.get(currencyPair); in BitfinexAdapters.adaptMetaData() not finding USDT quoted symbols, because the ticker-derived list of symbols (lastPrices) had the quoted currency as UST, but the symbols-details-derived (symbolDetails) currency as USDT.

The fix appears to be needed in BitfinexAdapters.adaptTicker(org.knowm.xchange.bitfinex.v2.dto.marketdata.BitfinexTicker bitfinexTicker)

Change: CurrencyPair currencyPair = CurrencyPairDeserializer.getCurrencyPairFromString(bitfinexTicker.getSymbol().substring(1)); to: CurrencyPair currencyPair = BitfinexAdapters.adaptCurrencyPair(bitfinexTicker.getSymbol());

This will then adapt ticker-derived symbols and symbols-details-derived symbols in the same way, and results in UST being converted to USDT in both cases.

Note that getTicker() and getTickers() on BitfinexMarketDataService also call BitfinexAdapters.adaptTicker, so a regression test could be extended to these use cases as well.

Hope this helps with any bug fix that may be deemed necessary :)

gewure commented 1 month ago

@rustyruzzee nice. shouldnt this be a PR?