Skip to content

Commit

Permalink
Update the data API to v3 and parameterize currencies in service calls
Browse files Browse the repository at this point in the history
  • Loading branch information
leonemagevski committed Jul 27, 2018
1 parent ec3adeb commit 8974f6c
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 88 deletions.
8 changes: 4 additions & 4 deletions xchange-mercadobitcoin/api-specification.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ Mercado Bitcoin Exchange API specification

Documentation
-------------
- [Mercado Bitcoin API](https://www.mercadobitcoin.net/api/)
- [Mercado Bitcoin Trade API](https://www.mercadobitcoin.net/org.knowm.xchange.mercadobitcoin.dto.trade-api/)
- [Mercado Bitcoin API](https://www.mercadobitcoin.com.br/api-doc/)
- [Mercado Bitcoin Trade API](https://www.mercadobitcoin.com.br/trade-api/)

Support
-------
Expand All @@ -13,5 +13,5 @@ Support
Limitations
--------------
- Minimum interval for each call for info API: 60 secs or IP can be blocked.
- Minimum interval for each call for org.knowm.xchange.mercadobitcoin.dto.trade API: 1 sec (due to a tonce implementation limitation)
- Maximum json vector size: 1000
- Minimum interval for each call for trade API: 1 sec (due to a tonce implementation limitation)
- Maximum json vector size: 1000
Original file line number Diff line number Diff line change
Expand Up @@ -25,53 +25,30 @@ public interface MercadoBitcoin {
* list of price and amount.
*/
@GET
@Path("/v1/orderbook/")
MercadoBitcoinOrderBook getOrderBookBTC() throws IOException;

/**
* Returns "bids" and "asks". Each is a list of open orders and each order is represented as a
* list of price and amount.
*/
@GET
@Path("/v1/orderbook_litecoin/")
MercadoBitcoinOrderBook getOrderBookLTC() throws IOException;

@GET
@Path("/v2/ticker/")
MercadoBitcoinTicker getTickerBTC() throws IOException;

@GET
@Path("/v2/ticker_litecoin/")
MercadoBitcoinTicker getTickerLTC() throws IOException;

@GET
@Path("/v1/trades/")
MercadoBitcoinTransaction[] getTransactionsBTC() throws IOException;

@GET
@Path("/v1/trades_litecoin/")
MercadoBitcoinTransaction[] getTransactionsLTC() throws IOException;
@Path("/{baseCurrency}/orderbook/")
MercadoBitcoinOrderBook getOrderBook(@PathParam("baseCurrency") String baseCurrency)
throws IOException;

@GET
@Path("/v1/trades/{start_timestamp: [0-9]}/")
MercadoBitcoinTransaction[] getTransactionsBTC(@PathParam("start_timestamp") Long startTimestamp)
throws IOException;
@Path("/{baseCurrency}/ticker/")
MercadoBitcoinTicker getTicker(@PathParam("baseCurrency") String baseCurrency) throws IOException;

@GET
@Path("/v1/trades_litecoin/{start_timestamp: [0-9]}/")
MercadoBitcoinTransaction[] getTransactionsLTC(@PathParam("start_timestamp") Long startTimestamp)
@Path("/{baseCurrency}/trades/")
MercadoBitcoinTransaction[] getTransactions(@PathParam("baseCurrency") String baseCurrency)
throws IOException;

@GET
@Path("/v1/trades/{start_timestamp: [0-9]}/{end_timestamp: [0-9]}/")
MercadoBitcoinTransaction[] getTransactionsBTC(
@PathParam("start_timestamp") Long startTimestamp,
@PathParam("end_timestamp") Long endTimestamp)
@Path("/{baseCurrency}/trades/{start_timestamp: [0-9]}/")
MercadoBitcoinTransaction[] getTransactions(
@PathParam("baseCurrency") String baseCurrency,
@PathParam("start_timestamp") Long startTimestamp)
throws IOException;

@GET
@Path("/v1/trades_litecoin/{start_timestamp: [0-9]}/{end_timestamp: [0-9]}/")
MercadoBitcoinTransaction[] getTransactionsLTC(
@Path("/{baseCurrency}/trades/{start_timestamp: [0-9]}/{end_timestamp: [0-9]}/")
MercadoBitcoinTransaction[] getTransactions(
@PathParam("baseCurrency") String baseCurrency,
@PathParam("start_timestamp") Long startTimestamp,
@PathParam("end_timestamp") Long endTimestamp)
throws IOException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.knowm.xchange.mercadobitcoin;

import java.util.Arrays;
import java.util.List;
import org.knowm.xchange.currency.Currency;
import org.knowm.xchange.currency.CurrencyPair;
import org.knowm.xchange.dto.trade.LimitOrder;
Expand All @@ -8,6 +10,12 @@
/** @author Felipe Micaroni Lalli */
public final class MercadoBitcoinUtils {

public static final List<CurrencyPair> availablePairs =
Arrays.asList(
CurrencyPair.BTC_BRL,
new CurrencyPair(Currency.LTC, Currency.BRL),
new CurrencyPair(Currency.BCH, Currency.BRL));

private MercadoBitcoinUtils() {}

/** Return something like <code>btc_brl:83948239</code> */
Expand All @@ -31,4 +39,11 @@ public static String makeMercadoBitcoinOrderId(LimitOrder limitOrder) {

return makeMercadoBitcoinOrderId(limitOrder.getCurrencyPair(), limitOrder.getId());
}

public static void verifyCurrencyPairAvailability(CurrencyPair currencyPair) {

if (!availablePairs.contains(currencyPair)) {
throw new NotAvailableFromExchangeException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
import java.io.IOException;
import java.math.BigDecimal;
import org.knowm.xchange.Exchange;
import org.knowm.xchange.currency.Currency;
import org.knowm.xchange.currency.CurrencyPair;
import org.knowm.xchange.exceptions.ExchangeException;
import org.knowm.xchange.exceptions.NotAvailableFromExchangeException;
import org.knowm.xchange.mercadobitcoin.MercadoBitcoin;
import org.knowm.xchange.mercadobitcoin.MercadoBitcoinUtils;
import org.knowm.xchange.mercadobitcoin.dto.marketdata.MercadoBitcoinOrderBook;
import org.knowm.xchange.mercadobitcoin.dto.marketdata.MercadoBitcoinTicker;
import org.knowm.xchange.mercadobitcoin.dto.marketdata.MercadoBitcoinTransaction;
Expand Down Expand Up @@ -36,67 +35,42 @@ public MercadoBitcoinMarketDataServiceRaw(Exchange exchange) {
public MercadoBitcoinOrderBook getMercadoBitcoinOrderBook(CurrencyPair currencyPair)
throws IOException {

if (currencyPair.equals(CurrencyPair.BTC_BRL)) {
return mercadoBitcoin.getOrderBookBTC();
} else if (currencyPair.equals(new CurrencyPair(Currency.LTC, Currency.BRL))) {
return mercadoBitcoin.getOrderBookLTC();
} else {
throw new NotAvailableFromExchangeException();
}
MercadoBitcoinUtils.verifyCurrencyPairAvailability(currencyPair);
return mercadoBitcoin.getOrderBook(currencyPair.base.getSymbol());
}

public MercadoBitcoinTicker getMercadoBitcoinTicker(CurrencyPair currencyPair)
throws IOException {

if (currencyPair.equals(CurrencyPair.BTC_BRL)) {
return mercadoBitcoin.getTickerBTC();
} else if (currencyPair.equals(new CurrencyPair(Currency.LTC, Currency.BRL))) {
return mercadoBitcoin.getTickerLTC();
} else {
throw new NotAvailableFromExchangeException();
}
MercadoBitcoinUtils.verifyCurrencyPairAvailability(currencyPair);
return mercadoBitcoin.getTicker(currencyPair.base.getSymbol());
}

public MercadoBitcoinTransaction[] getMercadoBitcoinTransactions(
CurrencyPair currencyPair, Object... args) throws IOException {

MercadoBitcoinUtils.verifyCurrencyPairAvailability(currencyPair);

MercadoBitcoinTransaction[] transactions;

if (args.length == 0) {
if (currencyPair.equals(CurrencyPair.BTC_BRL)) {
transactions = mercadoBitcoin.getTransactionsBTC(); // default values: offset=0, limit=100
} else if (currencyPair.equals(new CurrencyPair(Currency.LTC, Currency.BRL))) {
transactions = mercadoBitcoin.getTransactionsLTC();
} else {
throw new NotAvailableFromExchangeException();
}

// default values: offset=0, limit=100
transactions = mercadoBitcoin.getTransactions(currencyPair.base.getSymbol());

} else if (args.length == 1) {
BigDecimal time = new BigDecimal((Long) args[0]);

if (currencyPair.equals(CurrencyPair.BTC_BRL)) {
transactions = mercadoBitcoin.getTransactionsBTC(time.longValue() / 1000L);
} else if (currencyPair.equals(new CurrencyPair(Currency.LTC, Currency.BRL))) {
transactions = mercadoBitcoin.getTransactionsLTC(time.longValue() / 1000L);
} else {
throw new NotAvailableFromExchangeException();
}
transactions =
mercadoBitcoin.getTransactions(currencyPair.base.getSymbol(), time.longValue() / 1000L);

} else if (args.length == 2) {
BigDecimal timeStart = new BigDecimal((Long) args[0]);
BigDecimal timeEnd = new BigDecimal((Long) args[1]);

if (currencyPair.equals(CurrencyPair.BTC_BRL)) {
transactions =
mercadoBitcoin.getTransactionsBTC(
timeStart.longValue() / 1000L, timeEnd.longValue() / 1000L);
} else if (currencyPair.equals(new CurrencyPair(Currency.LTC, Currency.BRL))) {
transactions =
mercadoBitcoin.getTransactionsLTC(
timeStart.longValue() / 1000L, timeEnd.longValue() / 1000L);
} else {
throw new NotAvailableFromExchangeException();
}

transactions =
mercadoBitcoin.getTransactions(
currencyPair.base.getSymbol(),
timeStart.longValue() / 1000L,
timeEnd.longValue() / 1000L);
} else {
throw new ExchangeException("Invalid argument length. Must be 0, 1 or 2.");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.knowm.xchange.mercadobitcoin.service.marketdata;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.Test;
import org.knowm.xchange.Exchange;
import org.knowm.xchange.ExchangeFactory;
import org.knowm.xchange.currency.CurrencyPair;
import org.knowm.xchange.dto.marketdata.OrderBook;
import org.knowm.xchange.mercadobitcoin.MercadoBitcoinExchange;
import org.knowm.xchange.mercadobitcoin.MercadoBitcoinUtils;
import org.knowm.xchange.service.marketdata.MarketDataService;

public class OrderBookFetchIntegration {

@Test
public void orderbookFetchTest() throws Exception {

Exchange exchange =
ExchangeFactory.INSTANCE.createExchange(MercadoBitcoinExchange.class.getName());
MarketDataService marketDataService = exchange.getMarketDataService();

OrderBook orderBook;
for (CurrencyPair pair : MercadoBitcoinUtils.availablePairs) {
orderBook = marketDataService.getOrderBook(pair);
System.out.println(orderBook.toString());
assertThat(orderBook).isNotNull();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.knowm.xchange.currency.CurrencyPair;
import org.knowm.xchange.dto.marketdata.Ticker;
import org.knowm.xchange.mercadobitcoin.MercadoBitcoinExchange;
import org.knowm.xchange.mercadobitcoin.MercadoBitcoinUtils;
import org.knowm.xchange.service.marketdata.MarketDataService;

/** @author timmolter */
Expand All @@ -19,8 +20,12 @@ public void tickerFetchTest() throws Exception {
Exchange exchange =
ExchangeFactory.INSTANCE.createExchange(MercadoBitcoinExchange.class.getName());
MarketDataService marketDataService = exchange.getMarketDataService();
Ticker ticker = marketDataService.getTicker(new CurrencyPair("BTC", "BRL"));
System.out.println(ticker.toString());
assertThat(ticker).isNotNull();

Ticker ticker;
for (CurrencyPair pair : MercadoBitcoinUtils.availablePairs) {
ticker = marketDataService.getTicker(pair);
System.out.println(ticker.toString());
assertThat(ticker).isNotNull();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.knowm.xchange.mercadobitcoin.service.marketdata;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.Test;
import org.knowm.xchange.Exchange;
import org.knowm.xchange.ExchangeFactory;
import org.knowm.xchange.currency.CurrencyPair;
import org.knowm.xchange.dto.marketdata.Trades;
import org.knowm.xchange.mercadobitcoin.MercadoBitcoinExchange;
import org.knowm.xchange.mercadobitcoin.MercadoBitcoinUtils;
import org.knowm.xchange.service.marketdata.MarketDataService;

public class TradesFetchIntegration {

@Test
public void orderbookFetchTest() throws Exception {

Exchange exchange =
ExchangeFactory.INSTANCE.createExchange(MercadoBitcoinExchange.class.getName());
MarketDataService marketDataService = exchange.getMarketDataService();

Trades trades;

for (CurrencyPair pair : MercadoBitcoinUtils.availablePairs) {
trades = marketDataService.getTrades(pair);
System.out.println(trades.toString());
assertThat(trades).isNotNull();
}
}
}

0 comments on commit 8974f6c

Please sign in to comment.