Search in sources :

Example 11 with OrderBook

use of org.knowm.xchange.dto.marketdata.OrderBook in project XChange by knowm.

the class LakeBTCAdapterTest method testAdaptOrderbook.

@Test
public void testAdaptOrderbook() throws IOException {
    // Read in the JSON from the example resources
    InputStream is = LakeBTCMarketDataJsonTest.class.getResourceAsStream("/org/knowm/xchange/lakebtc/dto/marketdata/example-orderbook-data.json");
    // Use Jackson to parse it
    ObjectMapper mapper = new ObjectMapper();
    LakeBTCOrderBook orderBook = mapper.readValue(is, LakeBTCOrderBook.class);
    OrderBook adaptedOrderBook = LakeBTCAdapters.adaptOrderBook(orderBook, CurrencyPair.BTC_USD);
    List<LimitOrder> asks = adaptedOrderBook.getAsks();
    assertThat(asks.size()).isEqualTo(3);
    LimitOrder order = asks.get(0);
    assertThat(order.getLimitPrice()).isEqualTo("564.87");
    assertThat(order.getOriginalAmount()).isEqualTo("22.371");
    assertThat(order.getCurrencyPair()).isEqualTo(CurrencyPair.BTC_USD);
}
Also used : LakeBTCOrderBook(org.knowm.xchange.lakebtc.dto.marketdata.LakeBTCOrderBook) OrderBook(org.knowm.xchange.dto.marketdata.OrderBook) InputStream(java.io.InputStream) LakeBTCOrderBook(org.knowm.xchange.lakebtc.dto.marketdata.LakeBTCOrderBook) LimitOrder(org.knowm.xchange.dto.trade.LimitOrder) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test) LakeBTCMarketDataJsonTest(org.knowm.xchange.lakebtc.dto.marketdata.LakeBTCMarketDataJsonTest)

Example 12 with OrderBook

use of org.knowm.xchange.dto.marketdata.OrderBook in project XChange by knowm.

the class BookSanityCheckerTest method testIncorrectBestAskAndBid.

@Test
public void testIncorrectBestAskAndBid() {
    ArrayList<LimitOrder> asks = new ArrayList<>();
    ArrayList<LimitOrder> bids = new ArrayList<>();
    LimitOrder a1 = new LimitOrder(Order.OrderType.ASK, new BigDecimal(0.01), CurrencyPair.ADA_BNB, "1", new Date(), new BigDecimal(0.01));
    asks.add(a1);
    LimitOrder b1 = new LimitOrder(Order.OrderType.BID, new BigDecimal(0.02), CurrencyPair.ADA_BNB, "2", new Date(), new BigDecimal(0.02));
    bids.add(b1);
    OrderBook book = new OrderBook(new Date(), asks, bids);
    Assert.assertEquals(format("Got incorrect best ask and bid %s, %s", a1, b1), BookSanityChecker.hasErrors(book));
}
Also used : OrderBook(org.knowm.xchange.dto.marketdata.OrderBook) ArrayList(java.util.ArrayList) LimitOrder(org.knowm.xchange.dto.trade.LimitOrder) BigDecimal(java.math.BigDecimal) Date(java.util.Date) Test(org.junit.Test)

Example 13 with OrderBook

use of org.knowm.xchange.dto.marketdata.OrderBook in project XChange by knowm.

the class BookSanityCheckerTest method testWithBidNoErrorOnNextOrder.

@Test
public void testWithBidNoErrorOnNextOrder() {
    ArrayList<LimitOrder> asks = new ArrayList<>();
    ArrayList<LimitOrder> bids = new ArrayList<>();
    LimitOrder b1 = new LimitOrder(Order.OrderType.BID, new BigDecimal(-0.01), CurrencyPair.ADA_BNB, "1", new Date(), new BigDecimal(0.01));
    LimitOrder b2 = new LimitOrder(Order.OrderType.BID, new BigDecimal(0.01), CurrencyPair.ADA_BNB, "2", new Date(), new BigDecimal(0.01));
    bids.add(b1);
    bids.add(b2);
    OrderBook book = new OrderBook(new Date(), asks, bids);
    Assert.assertEquals(format("LimitOrder amount is <= 0 for %s", b1), BookSanityChecker.hasErrors(book));
}
Also used : OrderBook(org.knowm.xchange.dto.marketdata.OrderBook) ArrayList(java.util.ArrayList) LimitOrder(org.knowm.xchange.dto.trade.LimitOrder) BigDecimal(java.math.BigDecimal) Date(java.util.Date) Test(org.junit.Test)

Example 14 with OrderBook

use of org.knowm.xchange.dto.marketdata.OrderBook in project XChange by knowm.

the class BookSanityCheckerTest method testWithAsksWithBidsNoErrors.

@Test
public void testWithAsksWithBidsNoErrors() {
    ArrayList<LimitOrder> asks = new ArrayList<>();
    ArrayList<LimitOrder> bids = new ArrayList<>();
    asks.add(new LimitOrder(Order.OrderType.ASK, new BigDecimal(0.02), CurrencyPair.ADA_BNB, "1", new Date(), new BigDecimal(0.02)));
    bids.add(new LimitOrder(Order.OrderType.BID, new BigDecimal(0.01), CurrencyPair.ADA_BNB, "2", new Date(), new BigDecimal(0.01)));
    OrderBook book = new OrderBook(new Date(), asks, bids);
    Assert.assertNull(BookSanityChecker.hasErrors(book));
}
Also used : OrderBook(org.knowm.xchange.dto.marketdata.OrderBook) ArrayList(java.util.ArrayList) LimitOrder(org.knowm.xchange.dto.trade.LimitOrder) BigDecimal(java.math.BigDecimal) Date(java.util.Date) Test(org.junit.Test)

Example 15 with OrderBook

use of org.knowm.xchange.dto.marketdata.OrderBook in project XChange by knowm.

the class FtxStreamingMarketDataService method getOrderBook.

@Override
public Observable<OrderBook> getOrderBook(CurrencyPair currencyPair, Object... args) {
    OrderBook orderBook = new OrderBook(null, Lists.newArrayList(), Lists.newArrayList());
    String channelName = "orderbook:" + FtxAdapters.adaptCurrencyPairToFtxMarket(currencyPair);
    return service.subscribeChannel(channelName).map(res -> {
        try {
            return FtxStreamingAdapters.adaptOrderbookMessage(orderBook, currencyPair, res);
        } catch (IllegalStateException e) {
            LOG.warn("Resubscribing {} channel after adapter error {}", currencyPair, e.getMessage());
            orderBook.getBids().clear();
            orderBook.getAsks().clear();
            // Resubscribe to the channel
            this.service.sendMessage(service.getUnsubscribeMessage(channelName, args));
            this.service.sendMessage(service.getSubscribeMessage(channelName, args));
            return new OrderBook(null, Lists.newArrayList(), Lists.newArrayList(), false);
        }
    }).filter(ob -> ob.getBids().size() > 0 && ob.getAsks().size() > 0);
}
Also used : StreamingMarketDataService(info.bitrich.xchangestream.core.StreamingMarketDataService) Lists(com.google.common.collect.Lists) Ticker(org.knowm.xchange.dto.marketdata.Ticker) Logger(org.slf4j.Logger) FtxAdapters(org.knowm.xchange.ftx.FtxAdapters) LoggerFactory(org.slf4j.LoggerFactory) Observable(io.reactivex.Observable) Trade(org.knowm.xchange.dto.marketdata.Trade) CurrencyPair(org.knowm.xchange.currency.CurrencyPair) OrderBook(org.knowm.xchange.dto.marketdata.OrderBook) OrderBook(org.knowm.xchange.dto.marketdata.OrderBook)

Aggregations

OrderBook (org.knowm.xchange.dto.marketdata.OrderBook)193 Test (org.junit.Test)88 LimitOrder (org.knowm.xchange.dto.trade.LimitOrder)87 BigDecimal (java.math.BigDecimal)53 MarketDataService (org.knowm.xchange.service.marketdata.MarketDataService)44 Date (java.util.Date)43 CurrencyPair (org.knowm.xchange.currency.CurrencyPair)42 ArrayList (java.util.ArrayList)36 Exchange (org.knowm.xchange.Exchange)34 Ticker (org.knowm.xchange.dto.marketdata.Ticker)32 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)25 InputStream (java.io.InputStream)19 Trades (org.knowm.xchange.dto.marketdata.Trades)18 Trade (org.knowm.xchange.dto.marketdata.Trade)15 List (java.util.List)13 JsonNode (com.fasterxml.jackson.databind.JsonNode)11 Collectors (java.util.stream.Collectors)10 ExchangeSpecification (org.knowm.xchange.ExchangeSpecification)10 IOException (java.io.IOException)9 Order (org.knowm.xchange.dto.Order)9