Search in sources :

Example 96 with OpenOrders

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

the class TradeMockedIntegrationTest method openOrdersTest.

@Test
public void openOrdersTest() throws Exception {
    stubFor(get(urlPathEqualTo("/exchange/client_orders")).willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBodyFile("client_orders.json")));
    OpenOrders openOrders = tradeService.getOpenOrders();
    assertThat(openOrders).isNotNull();
    assertThat(openOrders.getOpenOrders()).isNotEmpty();
    LimitOrder firstOrder = openOrders.getOpenOrders().get(0);
    assertThat(firstOrder).isNotNull();
    assertThat(firstOrder.getOriginalAmount()).isNotNull().isPositive();
    assertThat(firstOrder.getId()).isNotBlank();
}
Also used : OpenOrders(org.knowm.xchange.dto.trade.OpenOrders) LimitOrder(org.knowm.xchange.dto.trade.LimitOrder) Test(org.junit.Test)

Example 97 with OpenOrders

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

the class OkexTradeService method getOpenOrders.

@Override
public OpenOrders getOpenOrders(OpenOrdersParams params) throws IOException {
    if (!(params instanceof OpenOrdersParamCurrencyPair)) {
        throw new UnsupportedOperationException("Getting open orders is only available for a single market.");
    }
    CurrencyPair pair = ((OpenOrdersParamCurrencyPair) params).getCurrencyPair();
    final String instrument = OkexAdaptersV3.toSpotInstrument(pair);
    // "6": Incomplete(open+partially filled)
    final String state = "6";
    String from = null;
    List<OkexOpenOrder> all = new ArrayList<>();
    boolean stop = false;
    do {
        List<OkexOpenOrder> l = getSpotOrderList(instrument, from, null, orders_limit, state);
        all.addAll(l);
        stop = l.size() < orders_limit;
        if (!stop) {
            from = l.get(l.size() - 1).getOrderId();
        }
    } while (!stop);
    return new OpenOrders(all.stream().map(OkexAdaptersV3::convert).collect(Collectors.toList()));
}
Also used : OkexAdaptersV3(org.knowm.xchange.okcoin.OkexAdaptersV3) OkexOpenOrder(org.knowm.xchange.okcoin.v3.dto.trade.OkexOpenOrder) ArrayList(java.util.ArrayList) OpenOrders(org.knowm.xchange.dto.trade.OpenOrders) OpenOrdersParamCurrencyPair(org.knowm.xchange.service.trade.params.orders.OpenOrdersParamCurrencyPair) DefaultOpenOrdersParamCurrencyPair(org.knowm.xchange.service.trade.params.orders.DefaultOpenOrdersParamCurrencyPair) CancelOrderByCurrencyPair(org.knowm.xchange.service.trade.params.CancelOrderByCurrencyPair) OpenOrdersParamCurrencyPair(org.knowm.xchange.service.trade.params.orders.OpenOrdersParamCurrencyPair) DefaultOpenOrdersParamCurrencyPair(org.knowm.xchange.service.trade.params.orders.DefaultOpenOrdersParamCurrencyPair) CurrencyPair(org.knowm.xchange.currency.CurrencyPair) TradeHistoryParamCurrencyPair(org.knowm.xchange.service.trade.params.TradeHistoryParamCurrencyPair)

Example 98 with OpenOrders

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

the class TestSimulatedExchange method testTradingLimitAsk.

@Test
public void testTradingLimitAsk() throws IOException {
    // When
    String orderId = exchange.getTradeService().placeLimitOrder(new LimitOrder.Builder(ASK, BTC_USD).limitPrice(new BigDecimal(97)).originalAmount(new BigDecimal("0.7")).build());
    OrderBook orderBook = exchange.getMarketDataService().getOrderBook(BTC_USD);
    Ticker ticker = exchange.getMarketDataService().getTicker(BTC_USD);
    Balance baseBalance = exchange.getAccountService().getAccountInfo().getWallet().getBalance(BTC);
    Balance counterBalance = exchange.getAccountService().getAccountInfo().getWallet().getBalance(USD);
    // THen
    assertThat(orderBook.getAsks()).hasSize(5);
    assertThat(orderBook.getBids()).hasSize(5);
    assertThat(ticker.getAsk()).isEqualTo(new BigDecimal(97));
    assertThat(ticker.getBid()).isEqualTo(new BigDecimal(96));
    assertThat(ticker.getLast()).isEqualTo(new BigDecimal(97));
    OpenOrders orders = getOpenOrders();
    assertThat(orders.getOpenOrders()).hasSize(1);
    assertThat(orders.getOpenOrders().get(0).getRemainingAmount()).isEqualTo(new BigDecimal("0.3"));
    assertThat(orders.getOpenOrders().get(0).getCumulativeAmount()).isEqualTo(new BigDecimal("0.4"));
    assertThat(orders.getOpenOrders().get(0).getAveragePrice()).isEqualTo(new BigDecimal(97));
    assertThat(orders.getOpenOrders().get(0).getId()).isEqualTo(orderId);
    assertThat(orders.getOpenOrders().get(0).getStatus()).isEqualTo(PARTIALLY_FILLED);
    assertThat(getTradeHistory(exchange).getTrades()).hasSize(1);
    BigDecimal expectedUsdProceeds = new BigDecimal(97).multiply(new BigDecimal("0.4"));
    assertThat(baseBalance.getTotal()).isEqualTo(INITIAL_BALANCE.subtract(new BigDecimal("0.4")));
    assertThat(baseBalance.getFrozen()).isEqualTo(new BigDecimal("0.3"));
    assertThat(baseBalance.getAvailable()).isEqualTo(INITIAL_BALANCE.subtract(new BigDecimal("0.7")));
    assertThat(counterBalance.getTotal()).isEqualTo(INITIAL_BALANCE.add(expectedUsdProceeds));
    assertThat(counterBalance.getFrozen()).isEqualTo(ZERO);
    assertThat(counterBalance.getAvailable()).isEqualTo(INITIAL_BALANCE.add(expectedUsdProceeds));
}
Also used : OrderBook(org.knowm.xchange.dto.marketdata.OrderBook) Ticker(org.knowm.xchange.dto.marketdata.Ticker) Balance(org.knowm.xchange.dto.account.Balance) OpenOrders(org.knowm.xchange.dto.trade.OpenOrders) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 99 with OpenOrders

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

the class TestSimulatedExchange method testTradingLimitBid.

@Test
public void testTradingLimitBid() throws IOException {
    // When
    String orderId1 = exchange.getTradeService().placeLimitOrder(new LimitOrder.Builder(BID, BTC_USD).limitPrice(new BigDecimal(99)).originalAmount(new BigDecimal("0.7")).build());
    String orderId2 = exchange.getTradeService().placeLimitOrder(new LimitOrder.Builder(BID, BTC_USD).limitPrice(new BigDecimal(90)).originalAmount(new BigDecimal("1")).build());
    OrderBook orderBook = exchange.getMarketDataService().getOrderBook(BTC_USD);
    Ticker ticker = exchange.getMarketDataService().getTicker(BTC_USD);
    Balance baseBalance = exchange.getAccountService().getAccountInfo().getWallet().getBalance(BTC);
    Balance counterBalance = exchange.getAccountService().getAccountInfo().getWallet().getBalance(USD);
    // THen
    assertThat(orderBook.getAsks()).hasSize(2);
    assertThat(orderBook.getBids()).hasSize(8);
    assertThat(ticker.getAsk()).isEqualTo(new BigDecimal(100));
    assertThat(ticker.getBid()).isEqualTo(new BigDecimal(99));
    assertThat(ticker.getLast()).isEqualTo(new BigDecimal(99));
    OpenOrders orders = getOpenOrders();
    assertThat(orders.getOpenOrders()).hasSize(2);
    Order order1 = orders.getAllOpenOrders().stream().filter(o -> o.getId().equals(orderId1)).findFirst().get();
    Order order2 = orders.getAllOpenOrders().stream().filter(o -> o.getId().equals(orderId2)).findFirst().get();
    assertThat(order1.getRemainingAmount()).isEqualTo(new BigDecimal("0.10"));
    assertThat(order1.getCumulativeAmount()).isEqualTo(new BigDecimal("0.60"));
    assertThat(order1.getAveragePrice()).isEqualTo(new BigDecimal("98.50"));
    assertThat(order1.getStatus()).isEqualTo(PARTIALLY_FILLED);
    assertThat(order2.getRemainingAmount()).isEqualTo(new BigDecimal(1));
    assertThat(order2.getCumulativeAmount()).isEqualTo(ZERO);
    assertThat(order2.getAveragePrice()).isNull();
    assertThat(order2.getStatus()).isEqualTo(NEW);
    assertThat(getTradeHistory(exchange).getTrades()).hasSize(3);
    BigDecimal expectedUsdCost = new BigDecimal(98).multiply(new BigDecimal("0.30")).add(new BigDecimal(99).multiply(new BigDecimal("0.30")));
    BigDecimal expectedUsdReserved = new BigDecimal(99).multiply(new BigDecimal("0.10")).add(new BigDecimal(90).multiply(new BigDecimal(1)));
    assertThat(baseBalance.getTotal()).isEqualTo(INITIAL_BALANCE.add(new BigDecimal("0.60")));
    assertThat(baseBalance.getFrozen()).isEqualTo(ZERO);
    assertThat(baseBalance.getAvailable()).isEqualTo(INITIAL_BALANCE.add(new BigDecimal("0.60")));
    assertThat(counterBalance.getTotal()).isEqualTo(INITIAL_BALANCE.subtract(expectedUsdCost));
    assertThat(counterBalance.getFrozen()).isEqualTo(expectedUsdReserved);
    assertThat(counterBalance.getAvailable()).isEqualTo(INITIAL_BALANCE.subtract(expectedUsdCost).subtract(expectedUsdReserved));
}
Also used : Order(org.knowm.xchange.dto.Order) LimitOrder(org.knowm.xchange.dto.trade.LimitOrder) MarketOrder(org.knowm.xchange.dto.trade.MarketOrder) OrderBook(org.knowm.xchange.dto.marketdata.OrderBook) Ticker(org.knowm.xchange.dto.marketdata.Ticker) Balance(org.knowm.xchange.dto.account.Balance) OpenOrders(org.knowm.xchange.dto.trade.OpenOrders) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 100 with OpenOrders

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

the class LgoStreamingTradeServiceTest method it_handles_open_orders_updates.

@Test
public void it_handles_open_orders_updates() throws IOException, ParseException {
    JsonNode snapshot = TestUtils.getJsonContent("/trade/orders-snapshot.json");
    JsonNode update = TestUtils.getJsonContent("/trade/orders-update.json");
    when(streamingService.subscribeChannel(anyString())).thenReturn(Observable.just(snapshot, update));
    Observable<OpenOrders> openOrders = service.getOpenOrders(CurrencyPair.BTC_USD);
    verify(streamingService).subscribeChannel("user-BTC-USD");
    Date date1 = dateFormat.parse("2019-07-23T15:36:14.304Z");
    Date date2 = dateFormat.parse("2019-07-18T12:24:21.891Z");
    Date date3 = dateFormat.parse("2019-07-24T08:37:19.116Z");
    LimitOrder order1 = new LimitOrder(Order.OrderType.ASK, BigDecimal.ONE, BigDecimal.ZERO, CurrencyPair.BTC_USD, "156389617430400001", date1, new BigDecimal(6000));
    order1.setOrderStatus(Order.OrderStatus.NEW);
    LimitOrder order2 = new LimitOrder(Order.OrderType.ASK, BigDecimal.ONE, BigDecimal.ZERO, CurrencyPair.BTC_USD, "156345266189100001", date2, new BigDecimal(6000));
    order2.setOrderStatus(Order.OrderStatus.NEW);
    LimitOrder order3 = new LimitOrder(Order.OrderType.BID, new BigDecimal(2), BigDecimal.ZERO, CurrencyPair.BTC_USD, "156395743911600001", date3, new BigDecimal(8000));
    order3.setOrderStatus(Order.OrderStatus.NEW);
// TODO fix this.
// assertThat(openOrders.blockingFirst())
// .usingRecursiveComparison()
// .isEqualTo(new OpenOrders(Arrays.asList(order2, order1)));
// assertThat(openOrders.blockingLast())
// .usingRecursiveComparison()
// .isEqualTo(new OpenOrders(Arrays.asList(order3, order1)));
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) OpenOrders(org.knowm.xchange.dto.trade.OpenOrders) LimitOrder(org.knowm.xchange.dto.trade.LimitOrder) Date(java.util.Date) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Aggregations

OpenOrders (org.knowm.xchange.dto.trade.OpenOrders)117 LimitOrder (org.knowm.xchange.dto.trade.LimitOrder)79 Test (org.junit.Test)40 BigDecimal (java.math.BigDecimal)36 ArrayList (java.util.ArrayList)33 CurrencyPair (org.knowm.xchange.currency.CurrencyPair)31 TradeService (org.knowm.xchange.service.trade.TradeService)26 Date (java.util.Date)23 Order (org.knowm.xchange.dto.Order)21 OrderType (org.knowm.xchange.dto.Order.OrderType)20 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)16 MarketOrder (org.knowm.xchange.dto.trade.MarketOrder)15 UserTrades (org.knowm.xchange.dto.trade.UserTrades)14 OpenOrdersParamCurrencyPair (org.knowm.xchange.service.trade.params.orders.OpenOrdersParamCurrencyPair)14 Exchange (org.knowm.xchange.Exchange)13 DefaultOpenOrdersParamCurrencyPair (org.knowm.xchange.service.trade.params.orders.DefaultOpenOrdersParamCurrencyPair)12 TradeHistoryParamCurrencyPair (org.knowm.xchange.service.trade.params.TradeHistoryParamCurrencyPair)10 InputStream (java.io.InputStream)9 List (java.util.List)9 StopOrder (org.knowm.xchange.dto.trade.StopOrder)8