Search in sources :

Example 6 with StopOrder

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");
}
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 7 with StopOrder

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);
}
Also used : StopOrder(org.knowm.xchange.dto.trade.StopOrder) LimitOrder(org.knowm.xchange.dto.trade.LimitOrder) FixedRateLoanOrder(org.knowm.xchange.dto.trade.FixedRateLoanOrder) Order(org.knowm.xchange.dto.Order) FloatingRateLoanOrder(org.knowm.xchange.dto.trade.FloatingRateLoanOrder) MarketOrder(org.knowm.xchange.dto.trade.MarketOrder) StopOrder(org.knowm.xchange.dto.trade.StopOrder) Arrays(java.util.Arrays) OrderType(org.knowm.xchange.dto.Order.OrderType) Date(java.util.Date) BitfinexBalancesResponse(org.knowm.xchange.bitfinex.v1.dto.account.BitfinexBalancesResponse) LoggerFactory(org.slf4j.LoggerFactory) BitfinexLendLevel(org.knowm.xchange.bitfinex.v1.dto.marketdata.BitfinexLendLevel) BitfinexOrderType(org.knowm.xchange.bitfinex.v1.BitfinexOrderType) Balance(org.knowm.xchange.dto.account.Balance) BigDecimal(java.math.BigDecimal) CurrencyPairMetaData(org.knowm.xchange.dto.meta.CurrencyPairMetaData) BitfinexDepositWithdrawalHistoryResponse(org.knowm.xchange.bitfinex.v1.dto.account.BitfinexDepositWithdrawalHistoryResponse) Map(java.util.Map) BitfinexTrade(org.knowm.xchange.bitfinex.v1.dto.marketdata.BitfinexTrade) BitfinexPublicTrade(org.knowm.xchange.bitfinex.v2.dto.marketdata.BitfinexPublicTrade) LimitOrder(org.knowm.xchange.dto.trade.LimitOrder) OrderStatus(org.knowm.xchange.dto.Order.OrderStatus) Wallet(org.knowm.xchange.dto.account.Wallet) FixedRateLoanOrder(org.knowm.xchange.dto.trade.FixedRateLoanOrder) MathContext(java.math.MathContext) BitfinexAccountInfosResponse(org.knowm.xchange.bitfinex.v1.dto.trade.BitfinexAccountInfosResponse) BitfinexTradeResponse(org.knowm.xchange.bitfinex.v1.dto.trade.BitfinexTradeResponse) Collection(java.util.Collection) Set(java.util.Set) UserTrades(org.knowm.xchange.dto.trade.UserTrades) UserTrade(org.knowm.xchange.dto.trade.UserTrade) Collectors(java.util.stream.Collectors) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) List(java.util.List) Currency(org.knowm.xchange.currency.Currency) Stream(java.util.stream.Stream) BitfinexLevel(org.knowm.xchange.bitfinex.v1.dto.marketdata.BitfinexLevel) Entry(java.util.Map.Entry) Optional(java.util.Optional) BitfinexTicker(org.knowm.xchange.bitfinex.v1.dto.marketdata.BitfinexTicker) BitfinexDepth(org.knowm.xchange.bitfinex.v1.dto.marketdata.BitfinexDepth) Movement(org.knowm.xchange.bitfinex.v2.dto.account.Movement) CurrencyPairDeserializer(org.knowm.xchange.utils.jackson.CurrencyPairDeserializer) BitfinexUtils(org.knowm.xchange.bitfinex.v1.BitfinexUtils) BitfinexOrderStatusResponse(org.knowm.xchange.bitfinex.v1.dto.trade.BitfinexOrderStatusResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) FundingRecord(org.knowm.xchange.dto.account.FundingRecord) Supplier(java.util.function.Supplier) OrderBook(org.knowm.xchange.dto.marketdata.OrderBook) ArrayList(java.util.ArrayList) TradeSortType(org.knowm.xchange.dto.marketdata.Trades.TradeSortType) BitfinexTickerFundingCurrency(org.knowm.xchange.bitfinex.v2.dto.marketdata.BitfinexTickerFundingCurrency) Order(org.knowm.xchange.dto.Order) BitfinexSymbolDetail(org.knowm.xchange.bitfinex.v1.dto.marketdata.BitfinexSymbolDetail) CurrencyMetaData(org.knowm.xchange.dto.meta.CurrencyMetaData) ExchangeMetaData(org.knowm.xchange.dto.meta.ExchangeMetaData) OpenOrders(org.knowm.xchange.dto.trade.OpenOrders) Ticker(org.knowm.xchange.dto.marketdata.Ticker) Logger(org.slf4j.Logger) Fee(org.knowm.xchange.dto.account.Fee) BitfinexAccountFeesResponse(org.knowm.xchange.bitfinex.v1.dto.account.BitfinexAccountFeesResponse) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) Trades(org.knowm.xchange.dto.marketdata.Trades) FloatingRateLoanOrder(org.knowm.xchange.dto.trade.FloatingRateLoanOrder) BitfinexTradingFeeResponse(org.knowm.xchange.bitfinex.v1.dto.account.BitfinexTradingFeeResponse) MarketOrder(org.knowm.xchange.dto.trade.MarketOrder) Trade(org.knowm.xchange.dto.marketdata.Trade) BitfinexOrderFlags(org.knowm.xchange.bitfinex.v1.dto.trade.BitfinexOrderFlags) BitfinexTickerTraidingPair(org.knowm.xchange.bitfinex.v2.dto.marketdata.BitfinexTickerTraidingPair) CurrencyPair(org.knowm.xchange.currency.CurrencyPair) WalletHealth(org.knowm.xchange.dto.meta.WalletHealth) DateUtils(org.knowm.xchange.utils.DateUtils) ArrayList(java.util.ArrayList) BitfinexOrderType(org.knowm.xchange.bitfinex.v1.BitfinexOrderType) OpenOrders(org.knowm.xchange.dto.trade.OpenOrders) Date(java.util.Date) OrderStatus(org.knowm.xchange.dto.Order.OrderStatus) OrderType(org.knowm.xchange.dto.Order.OrderType) BitfinexOrderType(org.knowm.xchange.bitfinex.v1.BitfinexOrderType) StopOrder(org.knowm.xchange.dto.trade.StopOrder) MarketOrder(org.knowm.xchange.dto.trade.MarketOrder) LimitOrder(org.knowm.xchange.dto.trade.LimitOrder) BitfinexOrderStatusResponse(org.knowm.xchange.bitfinex.v1.dto.trade.BitfinexOrderStatusResponse) CurrencyPair(org.knowm.xchange.currency.CurrencyPair)

Example 8 with StopOrder

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.");
    }
}
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 9 with StopOrder

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;
}
Also used : StopOrder(org.knowm.xchange.dto.trade.StopOrder) Order(org.knowm.xchange.dto.Order) LimitOrder(org.knowm.xchange.dto.trade.LimitOrder) HuobiOrder(org.knowm.xchange.huobi.dto.trade.HuobiOrder) MarketOrder(org.knowm.xchange.dto.trade.MarketOrder) OrderType(org.knowm.xchange.dto.Order.OrderType) StopOrder(org.knowm.xchange.dto.trade.StopOrder) MarketOrder(org.knowm.xchange.dto.trade.MarketOrder) LimitOrder(org.knowm.xchange.dto.trade.LimitOrder) BigDecimal(java.math.BigDecimal) CurrencyPair(org.knowm.xchange.currency.CurrencyPair)

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