Search in sources :

Example 1 with BitfinexTradingFeeResponse

use of org.knowm.xchange.bitfinex.v1.dto.account.BitfinexTradingFeeResponse in project XChange by knowm.

the class BitfinexAdapters method adaptDynamicTradingFees.

/**
 * Each element in the response array contains a set of currencies that are at a given fee tier.
 * The API returns the fee per currency in each tier and does not make any promises that they are
 * all the same, so this adapter will use the fee per currency instead of the fee per tier.
 */
public static Map<CurrencyPair, Fee> adaptDynamicTradingFees(BitfinexTradingFeeResponse[] responses, List<CurrencyPair> currencyPairs) {
    Map<CurrencyPair, Fee> result = new HashMap<>();
    for (BitfinexTradingFeeResponse response : responses) {
        BitfinexTradingFeeResponse.BitfinexTradingFeeResponseRow[] responseRows = response.getTradingFees();
        for (BitfinexTradingFeeResponse.BitfinexTradingFeeResponseRow responseRow : responseRows) {
            Currency currency = Currency.getInstance(responseRow.getCurrency());
            BigDecimal percentToFraction = BigDecimal.ONE.divide(BigDecimal.ONE.scaleByPowerOfTen(2));
            Fee fee = new Fee(responseRow.getMakerFee().multiply(percentToFraction), responseRow.getTakerFee().multiply(percentToFraction));
            for (CurrencyPair pair : currencyPairs) {
                // Fee is typically assessed in units counter.
                if (pair.base.equals(currency)) {
                    if (result.put(pair, fee) != null) {
                        throw new IllegalStateException("Fee for currency pair " + pair + " is overspecified");
                    }
                }
            }
        }
    }
    return result;
}
Also used : HashMap(java.util.HashMap) Fee(org.knowm.xchange.dto.account.Fee) Currency(org.knowm.xchange.currency.Currency) BitfinexTickerFundingCurrency(org.knowm.xchange.bitfinex.v2.dto.marketdata.BitfinexTickerFundingCurrency) BigDecimal(java.math.BigDecimal) CurrencyPair(org.knowm.xchange.currency.CurrencyPair) BitfinexTradingFeeResponse(org.knowm.xchange.bitfinex.v1.dto.account.BitfinexTradingFeeResponse)

Example 2 with BitfinexTradingFeeResponse

use of org.knowm.xchange.bitfinex.v1.dto.account.BitfinexTradingFeeResponse in project XChange by knowm.

the class BitfinexAdaptersTest method shouldAdaptDynamicTradingFees.

@Test
public void shouldAdaptDynamicTradingFees() throws IOException {
    InputStream is = BitfinexFeesJSONTest.class.getResourceAsStream("/v1/account/example-account-info-fees.json");
    ObjectMapper mapper = new ObjectMapper();
    BitfinexTradingFeeResponse[] readValues = mapper.readValue(is, BitfinexTradingFeeResponse[].class);
    assertEquals(2, readValues.length);
    List<CurrencyPair> currencyPairs = new ArrayList<CurrencyPair>(Arrays.asList(new CurrencyPair[] { CurrencyPair.BTC_LTC, CurrencyPair.LTC_AUD, CurrencyPair.ETH_BTC, CurrencyPair.DGC_BTC, CurrencyPair.BTC_USD }));
    Map<CurrencyPair, Fee> feesPerPair = BitfinexAdapters.adaptDynamicTradingFees(readValues, currencyPairs);
    assertEquals(currencyPairs.size(), feesPerPair.size());
    BigDecimal point001 = BigDecimal.ONE.divide(BigDecimal.ONE.scaleByPowerOfTen(3));
    BigDecimal point002 = point001.multiply(new BigDecimal(2));
    BigDecimal point00025 = new BigDecimal(25).divide(BigDecimal.ONE.scaleByPowerOfTen(5));
    BigDecimal point0001 = BigDecimal.ONE.divide(BigDecimal.ONE.scaleByPowerOfTen(4));
    Fee btcLTCFee = feesPerPair.get(CurrencyPair.BTC_LTC);
    Fee btcExpectedFee = new Fee(point001, point002);
    assertEquals(btcExpectedFee, btcLTCFee);
    Fee btcUSDFee = feesPerPair.get(CurrencyPair.BTC_USD);
    assertEquals(btcExpectedFee, btcUSDFee);
    Fee ltcFee = feesPerPair.get(CurrencyPair.LTC_AUD);
    assertEquals(new Fee(point001, point002), ltcFee);
    Fee ethFee = feesPerPair.get(CurrencyPair.ETH_BTC);
    assertEquals(new Fee(point001, point002), ethFee);
    Fee dgcFee = feesPerPair.get(CurrencyPair.DGC_BTC);
    assertEquals(new Fee(point00025, point0001), dgcFee);
}
Also used : InputStream(java.io.InputStream) Fee(org.knowm.xchange.dto.account.Fee) ArrayList(java.util.ArrayList) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) BigDecimal(java.math.BigDecimal) BitfinexTradingFeeResponse(org.knowm.xchange.bitfinex.v1.dto.account.BitfinexTradingFeeResponse) CurrencyPair(org.knowm.xchange.currency.CurrencyPair) BitfinexFeesJSONTest(org.knowm.xchange.bitfinex.v1.dto.account.BitfinexFeesJSONTest) BitfinexWalletJSONTest(org.knowm.xchange.bitfinex.v1.dto.account.BitfinexWalletJSONTest) Test(org.junit.Test)

Aggregations

BigDecimal (java.math.BigDecimal)2 BitfinexTradingFeeResponse (org.knowm.xchange.bitfinex.v1.dto.account.BitfinexTradingFeeResponse)2 CurrencyPair (org.knowm.xchange.currency.CurrencyPair)2 Fee (org.knowm.xchange.dto.account.Fee)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Test (org.junit.Test)1 BitfinexFeesJSONTest (org.knowm.xchange.bitfinex.v1.dto.account.BitfinexFeesJSONTest)1 BitfinexWalletJSONTest (org.knowm.xchange.bitfinex.v1.dto.account.BitfinexWalletJSONTest)1 BitfinexTickerFundingCurrency (org.knowm.xchange.bitfinex.v2.dto.marketdata.BitfinexTickerFundingCurrency)1 Currency (org.knowm.xchange.currency.Currency)1