use of org.knowm.xchange.dto.trade.StopOrder in project XChange by knowm.
the class CoinbaseProAdaptersTest method testOrderStatusStopOrderStopped.
@Test
public void testOrderStatusStopOrderStopped() 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-stopped.json");
final CoinbaseProOrder coinbaseProOrder = mapper.readValue(is, CoinbaseProOrder.class);
final Order order = CoinbaseProAdapters.adaptOrder(coinbaseProOrder);
assertThat(order.getStatus()).isEqualTo(Order.OrderStatus.STOPPED);
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");
}
use of org.knowm.xchange.dto.trade.StopOrder in project XChange by knowm.
the class BitfinexAdapters method adaptOrders.
public static OpenOrders adaptOrders(BitfinexOrderStatusResponse[] activeOrders) {
List<LimitOrder> limitOrders = new ArrayList<>();
List<Order> hiddenOrders = new ArrayList<>();
for (BitfinexOrderStatusResponse order : activeOrders) {
OrderType orderType = order.getSide().equalsIgnoreCase("buy") ? OrderType.BID : OrderType.ASK;
OrderStatus status = adaptOrderStatus(order);
CurrencyPair currencyPair = adaptCurrencyPair(order.getSymbol());
Date timestamp = convertBigDecimalTimestampToDate(order.getTimestamp());
Supplier<MarketOrder> marketOrderCreator = () -> new MarketOrder(orderType, order.getOriginalAmount(), currencyPair, String.valueOf(order.getId()), timestamp, order.getAvgExecutionPrice(), order.getExecutedAmount(), null, status);
Supplier<LimitOrder> limitOrderCreator = () -> new LimitOrder(orderType, order.getOriginalAmount(), currencyPair, String.valueOf(order.getId()), timestamp, order.getPrice(), order.getAvgExecutionPrice(), order.getExecutedAmount(), null, status);
Supplier<StopOrder> stopOrderCreator = () -> new StopOrder(orderType, order.getOriginalAmount(), currencyPair, String.valueOf(order.getId()), timestamp, order.getPrice(), null, order.getAvgExecutionPrice(), order.getExecutedAmount(), status);
LimitOrder limitOrder = null;
StopOrder stopOrder = null;
MarketOrder marketOrder = null;
Optional<BitfinexOrderType> bitfinexOrderType = Arrays.stream(BitfinexOrderType.values()).filter(v -> v.getValue().equals(order.getType())).findFirst();
if (bitfinexOrderType.isPresent()) {
switch(bitfinexOrderType.get()) {
case FILL_OR_KILL:
limitOrder = limitOrderCreator.get();
limitOrder.addOrderFlag(BitfinexOrderFlags.FILL_OR_KILL);
break;
case MARGIN_FILL_OR_KILL:
limitOrder = limitOrderCreator.get();
limitOrder.addOrderFlag(BitfinexOrderFlags.FILL_OR_KILL);
limitOrder.addOrderFlag(BitfinexOrderFlags.MARGIN);
break;
case MARGIN_LIMIT:
limitOrder = limitOrderCreator.get();
limitOrder.addOrderFlag(BitfinexOrderFlags.MARGIN);
break;
case MARGIN_STOP:
stopOrder = stopOrderCreator.get();
stopOrder.addOrderFlag(BitfinexOrderFlags.STOP);
stopOrder.addOrderFlag(BitfinexOrderFlags.MARGIN);
break;
case MARGIN_STOP_LIMIT:
stopLimitWarning();
stopOrder = stopOrderCreator.get();
stopOrder.addOrderFlag(BitfinexOrderFlags.STOP);
stopOrder.addOrderFlag(BitfinexOrderFlags.MARGIN);
break;
case MARGIN_TRAILING_STOP:
limitOrder = limitOrderCreator.get();
limitOrder.addOrderFlag(BitfinexOrderFlags.TRAILING_STOP);
limitOrder.addOrderFlag(BitfinexOrderFlags.MARGIN);
break;
case STOP:
stopOrder = stopOrderCreator.get();
stopOrder.addOrderFlag(BitfinexOrderFlags.STOP);
break;
case STOP_LIMIT:
stopLimitWarning();
stopOrder = stopOrderCreator.get();
stopOrder.addOrderFlag(BitfinexOrderFlags.STOP);
break;
case TRAILING_STOP:
limitOrder = limitOrderCreator.get();
limitOrder.addOrderFlag(BitfinexOrderFlags.TRAILING_STOP);
break;
case LIMIT:
limitOrder = limitOrderCreator.get();
break;
case MARGIN_MARKET:
case MARKET:
marketOrder = marketOrderCreator.get();
break;
default:
log.warn("Unhandled Bitfinex order type [{}]. Defaulting to limit order", order.getType());
limitOrder = limitOrderCreator.get();
break;
}
} else {
log.warn("Unknown Bitfinex order type [{}]. Defaulting to limit order", order.getType());
limitOrder = limitOrderCreator.get();
}
if (limitOrder != null) {
limitOrders.add(limitOrder);
} else if (stopOrder != null) {
hiddenOrders.add(stopOrder);
} else if (marketOrder != null) {
hiddenOrders.add(marketOrder);
}
}
return new OpenOrders(limitOrders, hiddenOrders);
}
use of org.knowm.xchange.dto.trade.StopOrder in project XChange by knowm.
the class KucoinTradeRawDemo method genericStopMarketOrder.
private static void genericStopMarketOrder(TradeService tradeService) throws Exception {
System.out.println("GENERIC STOP MARKET ORDER...\n");
StopOrder stopOrder = new StopOrder.Builder(ORDER_TYPE, PAIR).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);
System.out.println(tradeService.getOpenOrders());
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() != null) {
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.");
}
}
use of org.knowm.xchange.dto.trade.StopOrder in project XChange by knowm.
the class HuobiAdapters method adaptOrder.
private static Order adaptOrder(HuobiOrder openOrder) {
Order order = null;
OrderType orderType = adaptOrderType(openOrder.getType());
CurrencyPair currencyPair = adaptCurrencyPair(openOrder.getSymbol());
BigDecimal openOrderAvgPrice;
if (openOrder.getFieldAmount() == null || openOrder.getFieldAmount().compareTo(BigDecimal.ZERO) == 0) {
openOrderAvgPrice = BigDecimal.ZERO;
} else {
openOrderAvgPrice = openOrder.getFieldCashAmount().divide(openOrder.getFieldAmount(), 8, BigDecimal.ROUND_DOWN);
}
if (openOrder.isMarket()) {
order = new MarketOrder(orderType, openOrder.getAmount(), currencyPair, String.valueOf(openOrder.getId()), openOrder.getCreatedAt(), openOrderAvgPrice, openOrder.getFieldAmount(), openOrder.getFieldFees(), adaptOrderStatus(openOrder.getState()), openOrder.getClOrdId());
}
if (openOrder.isLimit()) {
order = new LimitOrder(orderType, openOrder.getAmount(), currencyPair, String.valueOf(openOrder.getId()), openOrder.getCreatedAt(), openOrder.getPrice(), openOrderAvgPrice, openOrder.getFieldAmount(), openOrder.getFieldFees(), adaptOrderStatus(openOrder.getState()), openOrder.getClOrdId());
}
if (openOrder.isStop()) {
order = new StopOrder(orderType, openOrder.getAmount(), currencyPair, String.valueOf(openOrder.getId()), openOrder.getCreatedAt(), openOrder.getStopPrice(), openOrder.getPrice(), openOrderAvgPrice, openOrder.getFieldAmount(), openOrder.getFieldFees(), adaptOrderStatus(openOrder.getState()), openOrder.getClOrdId(), openOrder.getOperator().equals("lte") ? Intention.STOP_LOSS : Intention.TAKE_PROFIT);
}
order.setAveragePrice(openOrderAvgPrice);
return order;
}
Aggregations