Search in sources :

Example 1 with StopOrder

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

the class KucoinTradeRawDemo method genericStopLimitOrder.

private static void genericStopLimitOrder(TradeService tradeService) throws Exception {
    System.out.println("GENERIC STOP LIMIT ORDER...\n");
    StopOrder stopOrder = new StopOrder.Builder(ORDER_TYPE, PAIR).limitPrice(STOP_LIMIT).stopPrice(STOP_PRICE).originalAmount(AMOUNT).build();
    String uuid = tradeService.placeStopOrder(stopOrder);
    System.out.println("Stop order successfully placed. ID=" + uuid);
    // wait for order to propagate
    Thread.sleep(3000);
    OpenOrdersParamCurrencyPair orderParams = (OpenOrdersParamCurrencyPair) tradeService.createOpenOrdersParams();
    orderParams.setCurrencyPair(PAIR);
    OpenOrders openOrders = tradeService.getOpenOrders(orderParams);
    Optional<? extends Order> found = openOrders.getHiddenOrders().stream().filter(o -> o.getId().equals(uuid)).findFirst();
    if (!found.isPresent()) {
        throw new AssertionError("Order not found on book");
    }
    if (!(found.get() instanceof StopOrder)) {
        throw new AssertionError("Stop order not returned");
    }
    StopOrder returnedStopOrder = (StopOrder) found.get();
    if (returnedStopOrder.getLimitPrice().compareTo(STOP_LIMIT) != 0) {
        throw new AssertionError("Limit price mismatch: " + returnedStopOrder.getLimitPrice());
    }
    if (returnedStopOrder.getStopPrice().compareTo(STOP_PRICE) != 0) {
        throw new AssertionError("Stop price mismatch: " + returnedStopOrder.getStopPrice());
    }
    System.out.println("Attempting to cancel order " + uuid);
    boolean cancelled = tradeService.cancelOrder(uuid);
    if (cancelled) {
        System.out.println("Order successfully canceled.");
    } else {
        System.out.println("Order not successfully canceled.");
    }
}
Also used : StopOrder(org.knowm.xchange.dto.trade.StopOrder) LimitOrder(org.knowm.xchange.dto.trade.LimitOrder) KucoinExamplesUtils(org.knowm.xchange.examples.kucoin.KucoinExamplesUtils) OrderCancelResponse(org.knowm.xchange.kucoin.dto.response.OrderCancelResponse) OrderType(org.knowm.xchange.dto.Order.OrderType) UUID(java.util.UUID) Exchange(org.knowm.xchange.Exchange) ExchangeException(org.knowm.xchange.exceptions.ExchangeException) BigDecimal(java.math.BigDecimal) Order(org.knowm.xchange.dto.Order) TradeService(org.knowm.xchange.service.trade.TradeService) Optional(java.util.Optional) OpenOrders(org.knowm.xchange.dto.trade.OpenOrders) CurrencyPair(org.knowm.xchange.currency.CurrencyPair) KucoinTradeServiceRaw(org.knowm.xchange.kucoin.KucoinTradeServiceRaw) OrderCreateApiRequest(org.knowm.xchange.kucoin.dto.request.OrderCreateApiRequest) OrderCreateResponse(org.knowm.xchange.kucoin.dto.response.OrderCreateResponse) OpenOrdersParamCurrencyPair(org.knowm.xchange.service.trade.params.orders.OpenOrdersParamCurrencyPair) TradeHistoryParamCurrencyPair(org.knowm.xchange.service.trade.params.TradeHistoryParamCurrencyPair) StopOrder(org.knowm.xchange.dto.trade.StopOrder) OpenOrders(org.knowm.xchange.dto.trade.OpenOrders) OpenOrdersParamCurrencyPair(org.knowm.xchange.service.trade.params.orders.OpenOrdersParamCurrencyPair)

Example 2 with StopOrder

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

the class TradeServiceIntegration method testPlaceTestOrderTakeProfitLimitOrderShouldNotThrowAnyException.

@Test
public void testPlaceTestOrderTakeProfitLimitOrderShouldNotThrowAnyException() throws IOException {
    final StopOrder takeProfitLimitOrder = sampleTakeProfitLimitOrder();
    tradeService.placeTestOrder(TAKE_PROFIT_LIMIT, takeProfitLimitOrder, takeProfitLimitOrder.getLimitPrice(), takeProfitLimitOrder.getStopPrice());
}
Also used : StopOrder(org.knowm.xchange.dto.trade.StopOrder) Test(org.junit.Test)

Example 3 with StopOrder

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

the class TradeServiceIntegration method testPlaceTestOrderStopLossLimitOrderShouldNotThrowAnyException.

@Test
public void testPlaceTestOrderStopLossLimitOrderShouldNotThrowAnyException() throws IOException {
    final StopOrder stopLimitOrder = sampleStopLimitOrder();
    tradeService.placeTestOrder(STOP_LOSS_LIMIT, stopLimitOrder, stopLimitOrder.getLimitPrice(), stopLimitOrder.getStopPrice());
}
Also used : StopOrder(org.knowm.xchange.dto.trade.StopOrder) Test(org.junit.Test)

Example 4 with StopOrder

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

the class CoinbaseProAdaptersTest method testOrderStatusStopOrderNew.

@Test
public void testOrderStatusStopOrderNew() throws JsonParseException, JsonMappingException, IOException {
    final JacksonObjectMapperFactory factory = new DefaultJacksonObjectMapperFactory();
    final ObjectMapper mapper = factory.createObjectMapper();
    final InputStream is = getClass().getResourceAsStream("/org/knowm/xchange/coinbasepro/dto/order/example-stop-order-new.json");
    final CoinbaseProOrder coinbaseProOrder = mapper.readValue(is, CoinbaseProOrder.class);
    final Order order = CoinbaseProAdapters.adaptOrder(coinbaseProOrder);
    assertThat(order.getStatus()).isEqualTo(Order.OrderStatus.NEW);
    assertThat(order.getId()).isEqualTo("853a9989-7dd9-40f8-9392-64237a9eccc4");
    assertThat(order.getCurrencyPair()).isEqualTo((CurrencyPair.BTC_EUR));
    assertThat(order.getOriginalAmount()).isEqualByComparingTo(new BigDecimal("0.01"));
    assertThat(order.getCumulativeAmount()).isEqualByComparingTo(BigDecimal.ZERO);
    assertThat(order.getRemainingAmount()).isEqualByComparingTo(new BigDecimal("0.01"));
    assertThat(order.getType()).isEqualTo(OrderType.ASK);
    assertThat(order.getTimestamp()).isEqualTo(new Date(1515434144454L));
    assertThat(order.getAveragePrice()).isEqualByComparingTo(BigDecimal.ZERO);
    assertThat(StopOrder.class.isAssignableFrom(order.getClass())).isTrue();
    StopOrder stop = (StopOrder) order;
    assertThat(stop.getStopPrice()).isEqualByComparingTo("6364.31");
}
Also used : StopOrder(org.knowm.xchange.dto.trade.StopOrder) Order(org.knowm.xchange.dto.Order) CoinbaseProOrder(org.knowm.xchange.coinbasepro.dto.trade.CoinbaseProOrder) LimitOrder(org.knowm.xchange.dto.trade.LimitOrder) MarketOrder(org.knowm.xchange.dto.trade.MarketOrder) StopOrder(org.knowm.xchange.dto.trade.StopOrder) ByteArrayInputStream(java.io.ByteArrayInputStream) SequenceInputStream(java.io.SequenceInputStream) InputStream(java.io.InputStream) DefaultJacksonObjectMapperFactory(si.mazi.rescu.serialization.jackson.DefaultJacksonObjectMapperFactory) DefaultJacksonObjectMapperFactory(si.mazi.rescu.serialization.jackson.DefaultJacksonObjectMapperFactory) JacksonObjectMapperFactory(si.mazi.rescu.serialization.jackson.JacksonObjectMapperFactory) CoinbaseProOrder(org.knowm.xchange.coinbasepro.dto.trade.CoinbaseProOrder) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) BigDecimal(java.math.BigDecimal) Date(java.util.Date) Test(org.junit.Test)

Example 5 with StopOrder

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

the class CoinbaseProAdaptersTest method assertStopOrderFilled.

private void assertStopOrderFilled(final Order order) {
    assertThat(order.getStatus()).isEqualTo(Order.OrderStatus.FILLED);
    assertThat(order.getId()).isEqualTo("a9098e25-9d4d-4e2c-ab5e-8c057cc4cbee");
    assertThat(order.getCurrencyPair()).isEqualTo((CurrencyPair.BTC_EUR));
    assertThat(order.getOriginalAmount()).isEqualByComparingTo(new BigDecimal("0.08871972"));
    assertThat(order.getCumulativeAmount()).isEqualByComparingTo(new BigDecimal("0.08871972"));
    assertThat(order.getRemainingAmount()).isEqualByComparingTo(BigDecimal.ZERO);
    assertThat(order.getType()).isEqualTo(OrderType.BID);
    assertThat(order.getTimestamp()).isEqualTo(new Date(1515434144454L));
    assertThat(order.getAveragePrice()).isEqualByComparingTo(new BigDecimal("639.3107535312").divide(new BigDecimal("0.08871972"), new MathContext(8)));
    assertThat(StopOrder.class.isAssignableFrom(order.getClass())).isTrue();
    StopOrder stop = (StopOrder) order;
    assertThat(stop.getStopPrice()).isEqualByComparingTo("7205");
}
Also used : StopOrder(org.knowm.xchange.dto.trade.StopOrder) BigDecimal(java.math.BigDecimal) Date(java.util.Date) MathContext(java.math.MathContext)

Aggregations

StopOrder (org.knowm.xchange.dto.trade.StopOrder)9 BigDecimal (java.math.BigDecimal)7 Order (org.knowm.xchange.dto.Order)6 LimitOrder (org.knowm.xchange.dto.trade.LimitOrder)6 Date (java.util.Date)4 Test (org.junit.Test)4 CurrencyPair (org.knowm.xchange.currency.CurrencyPair)4 OrderType (org.knowm.xchange.dto.Order.OrderType)4 MarketOrder (org.knowm.xchange.dto.trade.MarketOrder)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 Optional (java.util.Optional)3 OpenOrders (org.knowm.xchange.dto.trade.OpenOrders)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 SequenceInputStream (java.io.SequenceInputStream)2 MathContext (java.math.MathContext)2 UUID (java.util.UUID)2 Exchange (org.knowm.xchange.Exchange)2 CoinbaseProOrder (org.knowm.xchange.coinbasepro.dto.trade.CoinbaseProOrder)2 DefaultJacksonObjectMapperFactory (si.mazi.rescu.serialization.jackson.DefaultJacksonObjectMapperFactory)2