Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/knowm/XChange into ftx-s…
Browse files Browse the repository at this point in the history
…tream
  • Loading branch information
makarid committed Mar 9, 2021
2 parents 443236d + d38eeb1 commit a3df72b
Show file tree
Hide file tree
Showing 28 changed files with 824 additions and 60 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package org.knowm.xchange.btcmarkets;

import java.io.IOException;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import org.knowm.xchange.btcmarkets.dto.marketdata.BTCMarketsOrderBook;
import org.knowm.xchange.btcmarkets.dto.marketdata.BTCMarketsTicker;
import org.knowm.xchange.btcmarkets.dto.v3.marketdata.BTCMarketsTrade;

@Path("/")
@Produces(MediaType.APPLICATION_JSON)
Expand All @@ -24,4 +27,17 @@ BTCMarketsTicker getTicker(
BTCMarketsOrderBook getOrderBook(
@PathParam("instrument") String instrument, @PathParam("currency") String currency)
throws IOException;

@GET
@Path("/v3/markets/{marketId}/trades")
List<BTCMarketsTrade> getTrades(@PathParam("marketId") String marketId) throws IOException;

@GET
@Path("/v3/markets/{marketId}/trades")
List<BTCMarketsTrade> getTrades(
@QueryParam("before") Long before,
@QueryParam("after") Long after,
@QueryParam("limit") Integer limit,
@PathParam("marketId") String marketId)
throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.knowm.xchange.btcmarkets.dto.trade.BTCMarketsOrder;
import org.knowm.xchange.btcmarkets.dto.trade.BTCMarketsOrders;
import org.knowm.xchange.btcmarkets.dto.trade.BTCMarketsUserTrade;
import org.knowm.xchange.btcmarkets.dto.v3.marketdata.BTCMarketsTrade;
import org.knowm.xchange.btcmarkets.dto.v3.trade.BTCMarketsTradeHistoryResponse;
import org.knowm.xchange.currency.Currency;
import org.knowm.xchange.currency.CurrencyPair;
Expand All @@ -21,6 +22,8 @@
import org.knowm.xchange.dto.account.Wallet;
import org.knowm.xchange.dto.marketdata.OrderBook;
import org.knowm.xchange.dto.marketdata.Ticker;
import org.knowm.xchange.dto.marketdata.Trade;
import org.knowm.xchange.dto.marketdata.Trades;
import org.knowm.xchange.dto.marketdata.Trades.TradeSortType;
import org.knowm.xchange.dto.trade.LimitOrder;
import org.knowm.xchange.dto.trade.OpenOrders;
Expand Down Expand Up @@ -249,4 +252,28 @@ public static List<FundingRecord> adaptFundingHistory(
}
return result;
}

public static Trades adaptMarketTrades(
List<BTCMarketsTrade> btcMarketsTrades, CurrencyPair currencyPair) {

Iterator<BTCMarketsTrade> marketTrades = btcMarketsTrades.iterator();
List<Trade> trades = new ArrayList<Trade>();
while (marketTrades.hasNext()) {
BTCMarketsTrade marketTrade = marketTrades.next();
logger.debug(marketTrade.toString());
Trade trade =
new Trade(
adaptOrderType(marketTrade.getSide()),
marketTrade.getAmount(),
currencyPair,
marketTrade.getPrice(),
marketTrade.getTimestamp(),
marketTrade.getId().toString(),
null,
null);
trades.add(trade);
}

return new Trades(trades);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import org.knowm.xchange.btcmarkets.dto.BTCMarketsException;
import org.knowm.xchange.btcmarkets.dto.v3.BTCMarketsExceptionV3;
import org.knowm.xchange.btcmarkets.dto.v3.account.BTCMarketsAddressesResponse;
import org.knowm.xchange.btcmarkets.dto.v3.account.BTCMarketsTradingFeesResponse;
import org.knowm.xchange.btcmarkets.dto.v3.trade.BTCMarketsPlaceOrderRequest;
Expand All @@ -25,7 +26,7 @@ BTCMarketsPlaceOrderResponse placeOrder(
@HeaderParam("BM-AUTH-TIMESTAMP") SynchronizedValueFactory<Long> nonceFactory,
@HeaderParam("BM-AUTH-SIGNATURE") BTCMarketsDigestV3 signer,
BTCMarketsPlaceOrderRequest order)
throws BTCMarketsException, IOException;
throws BTCMarketsExceptionV3, IOException;

@GET
@Path("addresses")
Expand All @@ -34,7 +35,7 @@ BTCMarketsAddressesResponse depositAddress(
@HeaderParam("BM-AUTH-TIMESTAMP") SynchronizedValueFactory<Long> nonceFactory,
@HeaderParam("BM-AUTH-SIGNATURE") BTCMarketsDigestV3 signer,
@QueryParam("assetName") String assetName)
throws BTCMarketsException, IOException;
throws BTCMarketsExceptionV3, IOException;

@GET
@Path("trades")
Expand All @@ -46,13 +47,13 @@ List<BTCMarketsTradeHistoryResponse> trades(
@QueryParam("before") String before,
@QueryParam("after") String after,
@QueryParam("limit") Integer limit)
throws BTCMarketsException, IOException;
throws BTCMarketsExceptionV3, IOException;

@GET
@Path("accounts/me/trading-fees")
BTCMarketsTradingFeesResponse tradingFees(
@HeaderParam("BM-AUTH-APIKEY") String publicKey,
@HeaderParam("BM-AUTH-TIMESTAMP") SynchronizedValueFactory<Long> nonceFactory,
@HeaderParam("BM-AUTH-SIGNATURE") BTCMarketsDigestV3 signer)
throws BTCMarketsException, IOException;
throws BTCMarketsExceptionV3, IOException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.knowm.xchange.btcmarkets.dto.v3.marketdata;

import org.knowm.xchange.currency.CurrencyPair;
import org.knowm.xchange.instrument.Instrument;
import org.knowm.xchange.service.marketdata.params.Params;

public class BTCMarketsMarketTradeParams implements Params {
public Integer limit;
public Long before;
public Long after;
public CurrencyPair currencyPair;

public BTCMarketsMarketTradeParams(
Instrument currencyPair, Integer limit, Long before, Long after) {
super();
this.limit = limit;
this.before = before;
this.after = after;
this.currencyPair = (CurrencyPair) currencyPair;
}

@Override
public String toString() {

return String.format(
"BTCMarketsMarketTradeParams: {limit: %s, before: %s, after: %s, CurrencyPair: %s}",
limit, before, after, currencyPair);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package org.knowm.xchange.btcmarkets.dto.v3.marketdata;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import java.math.BigDecimal;
import java.util.Date;
import org.knowm.xchange.utils.jackson.ISO8601DateDeserializer;

public class BTCMarketsTrade {

/* Sample message:
"id": "4107372347",
"price": "0.265",
"amount": "11.25",
"timestamp": "2019-09-02T12:49:42.874000Z",
"side": "Ask"
*/

private final Long id;
private final BigDecimal price;
private final BigDecimal amount;
private final Date timestamp;
private final String side;

public BTCMarketsTrade(
@JsonProperty("id") Long id,
@JsonProperty("price") BigDecimal price,
@JsonProperty("amount") BigDecimal amount,
@JsonProperty("timestamp") @JsonDeserialize(using = ISO8601DateDeserializer.class)
Date timestamp,
@JsonProperty("side") String side) {
super();
this.id = id;
this.price = price;
this.amount = amount;
this.timestamp = timestamp;
this.side = side;
}

public Long getId() {
return id;
}

public BigDecimal getPrice() {
return price;
}

public BigDecimal getAmount() {
return amount;
}

public Date getTimestamp() {
return timestamp;
}

public String getSide() {
return side;
}

@Override
public String toString() {
return String.format(
"BTCMarketsTrade{id=%s, price=%s, amount=%s, timestamp='%s', side='%s'} ",
id, price, amount, timestamp, side);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
import org.knowm.xchange.Exchange;
import org.knowm.xchange.btcmarkets.BTCMarketsAdapters;
import org.knowm.xchange.btcmarkets.dto.marketdata.BTCMarketsTicker;
import org.knowm.xchange.btcmarkets.dto.v3.marketdata.BTCMarketsMarketTradeParams;
import org.knowm.xchange.currency.CurrencyPair;
import org.knowm.xchange.dto.marketdata.OrderBook;
import org.knowm.xchange.dto.marketdata.Ticker;
import org.knowm.xchange.dto.marketdata.Trades;
import org.knowm.xchange.service.marketdata.MarketDataService;
import org.knowm.xchange.service.marketdata.params.Params;

/** @author Matija Mazi */
/** @author Matija Mazi (with additions from CDP) */
public class BTCMarketsMarketDataService extends BTCMarketsMarketDataServiceRaw
implements MarketDataService {

Expand All @@ -27,4 +30,19 @@ public Ticker getTicker(CurrencyPair currencyPair, Object... args) throws IOExce
public OrderBook getOrderBook(CurrencyPair currencyPair, Object... args) throws IOException {
return BTCMarketsAdapters.adaptOrderBook(getBTCMarketsOrderBook(currencyPair), currencyPair);
}

@Override
public Trades getTrades(CurrencyPair currencyPair, Object... args) throws IOException {

return BTCMarketsAdapters.adaptMarketTrades(getBTCMarketsTrade(currencyPair), currencyPair);
}

/** @param params use {@link BTCMarketsMarketTradeParams} for params */
@Override
public Trades getTrades(Params params) throws IOException {

return BTCMarketsAdapters.adaptMarketTrades(
getBTCMarketsTrade(((BTCMarketsMarketTradeParams) params).currencyPair, params),
((BTCMarketsMarketTradeParams) params).currencyPair);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package org.knowm.xchange.btcmarkets.service;

import java.io.IOException;
import java.util.List;
import org.knowm.xchange.Exchange;
import org.knowm.xchange.btcmarkets.dto.marketdata.BTCMarketsOrderBook;
import org.knowm.xchange.btcmarkets.dto.marketdata.BTCMarketsTicker;
import org.knowm.xchange.btcmarkets.dto.v3.marketdata.BTCMarketsMarketTradeParams;
import org.knowm.xchange.btcmarkets.dto.v3.marketdata.BTCMarketsTrade;
import org.knowm.xchange.currency.CurrencyPair;
import org.knowm.xchange.service.marketdata.params.Params;

public class BTCMarketsMarketDataServiceRaw extends BTCMarketsBaseService {

Expand All @@ -21,4 +25,18 @@ public BTCMarketsOrderBook getBTCMarketsOrderBook(CurrencyPair currencyPair) thr
return btcmPublic.getOrderBook(
currencyPair.base.getCurrencyCode(), currencyPair.counter.getCurrencyCode());
}

public List<BTCMarketsTrade> getBTCMarketsTrade(CurrencyPair currencyPair) throws IOException {
return btcmPublic.getTrades(
currencyPair.base.getCurrencyCode() + "-" + currencyPair.counter.getCurrencyCode());
}

public List<BTCMarketsTrade> getBTCMarketsTrade(CurrencyPair currencyPair, Params parameters)
throws IOException {
return btcmPublic.getTrades(
((BTCMarketsMarketTradeParams) parameters).before,
((BTCMarketsMarketTradeParams) parameters).after,
((BTCMarketsMarketTradeParams) parameters).limit,
currencyPair.base.getCurrencyCode() + "-" + currencyPair.counter.getCurrencyCode());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.knowm.xchange.btcmarkets.dto.marketdata.BTCMarketsTicker;
import org.knowm.xchange.btcmarkets.dto.trade.BTCMarketsOrder;
import org.knowm.xchange.btcmarkets.dto.trade.BTCMarketsUserTrade;
import org.knowm.xchange.btcmarkets.dto.v3.marketdata.BTCMarketsTrade;
import org.knowm.xchange.currency.CurrencyPair;
import org.knowm.xchange.dto.Order;
import org.knowm.xchange.dto.account.Balance;
Expand Down Expand Up @@ -126,4 +127,14 @@ public static void assertEquals(BTCMarketsTicker o1, BTCMarketsTicker o2) {
assertThat(o1.getTimestamp()).isEqualTo(o2.getTimestamp());
assertThat(o1.toString()).isEqualTo(o2.toString());
}

public static void assertEquals(BTCMarketsTrade o1, BTCMarketsTrade o2) {
assertThat(o1.getId()).isEqualTo(o2.getId());
assertThat(o1.getAmount()).isEqualTo(o2.getAmount());
assertThat(o1.getSide()).isEqualTo(o2.getSide());
assertThat(o1.getPrice()).isEqualTo(o2.getPrice());
assertThat(o1.getTimestamp()).isEqualTo(o2.getTimestamp());
assertThat(o1.toString()).isEqualTo(o2.toString());

}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package org.knowm.xchange.btcmarkets.service;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.mockito.Mockito.when;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import org.junit.Test;
import org.knowm.xchange.btcmarkets.BtcMarketsAssert;
import org.knowm.xchange.btcmarkets.dto.marketdata.BTCMarketsOrderBook;
import org.knowm.xchange.btcmarkets.dto.v3.marketdata.BTCMarketsTrade;
import org.knowm.xchange.currency.CurrencyPair;
import org.knowm.xchange.dto.marketdata.OrderBook;
import org.knowm.xchange.dto.marketdata.Ticker;
import org.knowm.xchange.dto.trade.LimitOrder;
import org.knowm.xchange.exceptions.NotYetImplementedForExchangeException;

public class BTCMarketsMarketDataServiceTest extends BTCMarketsServiceTest {

Expand Down Expand Up @@ -58,14 +59,21 @@ public void shouldGetOrderBook() throws IOException {
BtcMarketsAssert.assertEquals(bids.get(i), expectedBids[i]);
}
}

@Test
public void shouldGetTrades() throws IOException {

List<BTCMarketsTrade> tradesMock = Arrays.asList(parse(BTCMarketsTrade[].class, "v3"));

when(btcMarkets.getTrades("BTC-AUD")).thenReturn(tradesMock);

List<BTCMarketsTrade> trades = btcMarkets.getTrades("BTC-AUD");

@Test(expected = NotYetImplementedForExchangeException.class)
public void shouldFailWhenGetTrades() throws IOException {
// when
btcMarketsMarketDataService.getTrades(CurrencyPair.BTC_AUD);

// then
fail(
"BTCMarketsMarketDataService should throw NotYetImplementedForExchangeException when call getTrades");
assertThat(trades).hasSize(2);
for (int i =0; i < trades.size(); i++) {
BtcMarketsAssert.assertEquals(trades.get(i), EXCPECTED_BTC_AUD_MARKET_TRADES.get(i));

}
}

}
Loading

0 comments on commit a3df72b

Please sign in to comment.