Search in sources :

Example 16 with LimitOrder

use of org.knowm.xchange.dto.trade.LimitOrder 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 17 with LimitOrder

use of org.knowm.xchange.dto.trade.LimitOrder 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 18 with LimitOrder

use of org.knowm.xchange.dto.trade.LimitOrder 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 19 with LimitOrder

use of org.knowm.xchange.dto.trade.LimitOrder 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 20 with LimitOrder

use of org.knowm.xchange.dto.trade.LimitOrder in project XChange by knowm.

the class dydxStreamingAdapters method dydxOrderBookChanges.

public static List<LimitOrder> dydxOrderBookChanges(org.knowm.xchange.dto.Order.OrderType orderType, CurrencyPair currencyPair, String[][] changes, SortedMap<BigDecimal, BigDecimal> sideEntries, int maxDepth, boolean isV1) {
    if (sideEntries == null) {
        return Collections.emptyList();
    }
    if (changes != null) {
        for (String[] level : changes) {
            BigDecimal price;
            BigDecimal volume;
            if (isV1) {
                price = new BigDecimal(level[level.length - 2]).movePointRight(amounts.get(currencyPair.base) - amounts.get(currencyPair.counter));
                volume = new BigDecimal(level[level.length - 1]).movePointLeft(amounts.get(currencyPair.base));
            } else {
                price = new BigDecimal(level[level.length - 2]);
                volume = new BigDecimal(level[level.length - 1]);
            }
            sideEntries.put(price, volume);
        }
    }
    Stream<Map.Entry<BigDecimal, BigDecimal>> stream = sideEntries.entrySet().stream().filter(level -> level.getValue().compareTo(BigDecimal.ZERO) != 0);
    if (maxDepth != 0) {
        stream = stream.limit(maxDepth);
    }
    return stream.map(level -> new LimitOrder(orderType, level.getValue(), currencyPair, "0", null, level.getKey())).collect(Collectors.toList());
}
Also used : LimitOrder(org.knowm.xchange.dto.trade.LimitOrder) PLINK(org.knowm.xchange.currency.Currency.PLINK) HashMap(java.util.HashMap) USD(org.knowm.xchange.currency.Currency.USD) Collectors(java.util.stream.Collectors) DAI(org.knowm.xchange.currency.Currency.DAI) BigDecimal(java.math.BigDecimal) List(java.util.List) Currency(org.knowm.xchange.currency.Currency) USDC(org.knowm.xchange.currency.Currency.USDC) Stream(java.util.stream.Stream) PBTC(org.knowm.xchange.currency.Currency.PBTC) WETH(org.knowm.xchange.currency.Currency.WETH) Map(java.util.Map) Collections(java.util.Collections) CurrencyPair(org.knowm.xchange.currency.CurrencyPair) SortedMap(java.util.SortedMap) LimitOrder(org.knowm.xchange.dto.trade.LimitOrder) BigDecimal(java.math.BigDecimal)

Aggregations

LimitOrder (org.knowm.xchange.dto.trade.LimitOrder)337 BigDecimal (java.math.BigDecimal)188 Test (org.junit.Test)122 Date (java.util.Date)110 ArrayList (java.util.ArrayList)95 CurrencyPair (org.knowm.xchange.currency.CurrencyPair)90 OrderBook (org.knowm.xchange.dto.marketdata.OrderBook)89 OpenOrders (org.knowm.xchange.dto.trade.OpenOrders)77 Order (org.knowm.xchange.dto.Order)50 OrderType (org.knowm.xchange.dto.Order.OrderType)39 Exchange (org.knowm.xchange.Exchange)31 MarketOrder (org.knowm.xchange.dto.trade.MarketOrder)29 List (java.util.List)28 TradeService (org.knowm.xchange.service.trade.TradeService)28 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)23 IOException (java.io.IOException)20 Collectors (java.util.stream.Collectors)20 MarketDataService (org.knowm.xchange.service.marketdata.MarketDataService)17 InputStream (java.io.InputStream)15 DefaultOpenOrdersParamCurrencyPair (org.knowm.xchange.service.trade.params.orders.DefaultOpenOrdersParamCurrencyPair)15