Search in sources :

Example 41 with MarketOrder

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

the class KrakenAdapters method adaptOrder.

public static Order adaptOrder(String orderId, KrakenOrder krakenOrder) {
    OrderType orderType = adaptOrderType(krakenOrder.getOrderDescription().getType());
    CurrencyPair currencyPair = adaptCurrencyPair(krakenOrder.getOrderDescription().getAssetPair());
    OrderStatus orderStatus = adaptOrderStatus(krakenOrder.getStatus());
    BigDecimal filledAmount = krakenOrder.getVolumeExecuted();
    BigDecimal originalAmount = krakenOrder.getVolume();
    BigDecimal fee = krakenOrder.getFee();
    if (orderStatus == OrderStatus.NEW && filledAmount.compareTo(BigDecimal.ZERO) > 0 && filledAmount.compareTo(originalAmount) < 0) {
        orderStatus = OrderStatus.PARTIALLY_FILLED;
    }
    // eg: "opentm":1519731205.9987
    Double time = krakenOrder.getOpenTimestamp() * 1000;
    Date timestamp = new Date(time.longValue());
    if (krakenOrder.getOrderDescription().getOrderType().equals(KrakenOrderType.LIMIT))
        return new LimitOrder(orderType, krakenOrder.getVolume(), currencyPair, orderId, timestamp, krakenOrder.getOrderDescription().getPrice(), krakenOrder.getPrice(), krakenOrder.getVolumeExecuted(), fee, orderStatus, krakenOrder.getUserRefId());
    if (krakenOrder.getOrderDescription().getOrderType().equals(KrakenOrderType.MARKET))
        return new MarketOrder(orderType, krakenOrder.getVolume(), currencyPair, orderId, timestamp, krakenOrder.getPrice(), krakenOrder.getVolumeExecuted(), fee, orderStatus, krakenOrder.getUserRefId());
    throw new NotYetImplementedForExchangeException();
}
Also used : OrderStatus(org.knowm.xchange.dto.Order.OrderStatus) KrakenOrderStatus(org.knowm.xchange.kraken.dto.trade.KrakenOrderStatus) OrderType(org.knowm.xchange.dto.Order.OrderType) KrakenOrderType(org.knowm.xchange.kraken.dto.trade.KrakenOrderType) MarketOrder(org.knowm.xchange.dto.trade.MarketOrder) NotYetImplementedForExchangeException(org.knowm.xchange.exceptions.NotYetImplementedForExchangeException) LimitOrder(org.knowm.xchange.dto.trade.LimitOrder) BigDecimal(java.math.BigDecimal) Date(java.util.Date) CurrencyPair(org.knowm.xchange.currency.CurrencyPair)

Example 42 with MarketOrder

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

the class IndependentReserveAdaptersTest method adaptOrderDetails.

@Test
public void adaptOrderDetails() throws InvalidFormatException {
    IndependentReserveOrderDetailsResponse orderDetailsResponse = new IndependentReserveOrderDetailsResponse("abcf-123", "2014-09-23T12:39:34.3817763Z", "MarketBid", new BigDecimal(5.0), new BigDecimal(4.0), new BigDecimal(100), new BigDecimal(95), new BigDecimal(0), "PartiallyFilled", "Xbt", "Usd");
    MarketOrder order = (MarketOrder) IndependentReserveAdapters.adaptOrderDetails(orderDetailsResponse);
    assertThat(order.getId()).isEqualTo("abcf-123");
    assertThat(order.getTimestamp()).isEqualTo(Date.from(ZonedDateTime.of(2014, 9, 23, 12, 39, 34, 0, UTC).toInstant()));
    assertThat(order.getType()).isEqualTo(Order.OrderType.BID);
    assertThat(order.getOriginalAmount()).isEqualByComparingTo(new BigDecimal(5));
    assertThat(order.getCumulativeAmount()).isEqualByComparingTo(new BigDecimal(4));
    assertThat(order.getAveragePrice()).isEqualByComparingTo(new BigDecimal(95));
    assertThat(order.getFee()).isNull();
    assertThat(order.getStatus()).isEqualTo(Order.OrderStatus.PARTIALLY_FILLED);
    assertThat(order.getCurrencyPair()).isEqualTo(new CurrencyPair("Xbt", "Usd"));
}
Also used : IndependentReserveOrderDetailsResponse(org.knowm.xchange.independentreserve.dto.trade.IndependentReserveOrderDetailsResponse) MarketOrder(org.knowm.xchange.dto.trade.MarketOrder) BigDecimal(java.math.BigDecimal) CurrencyPair(org.knowm.xchange.currency.CurrencyPair) Test(org.junit.Test)

Example 43 with MarketOrder

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

the class LgoExchangeTradeIntegration method placeMarketOrder.

@Test
public void placeMarketOrder() throws IOException {
    LgoExchange lgoExchange = exchangeWithCredentials(false);
    LgoTradeService tradeService = lgoExchange.getTradeService();
    String orderId = tradeService.placeMarketOrder(new MarketOrder(OrderType.ASK, new BigDecimal("2"), CurrencyPair.BTC_USD, null, new Date()));
    System.out.println(orderId);
}
Also used : LgoTradeService(org.knowm.xchange.lgo.service.LgoTradeService) MarketOrder(org.knowm.xchange.dto.trade.MarketOrder) BigDecimal(java.math.BigDecimal) Date(java.util.Date) Test(org.junit.Test)

Example 44 with MarketOrder

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

the class LgoExchangeTradeIntegration method placeEncryptedMarketOrder.

@Test
public void placeEncryptedMarketOrder() throws IOException {
    LgoExchange lgoExchange = exchangeWithCredentials(true);
    LgoTradeService tradeService = lgoExchange.getTradeService();
    String orderId = tradeService.placeMarketOrder(new MarketOrder(OrderType.ASK, new BigDecimal("2"), CurrencyPair.BTC_USD, null, new Date()));
    System.out.println(orderId);
}
Also used : LgoTradeService(org.knowm.xchange.lgo.service.LgoTradeService) MarketOrder(org.knowm.xchange.dto.trade.MarketOrder) BigDecimal(java.math.BigDecimal) Date(java.util.Date) Test(org.junit.Test)

Example 45 with MarketOrder

use of org.knowm.xchange.dto.trade.MarketOrder in project cassandre-trading-bot by cassandre-tech.

the class TradeServiceXChangeImplementation method createMarketOrder.

/**
 * Creates market order.
 *
 * @param strategy     strategy
 * @param orderTypeDTO order type
 * @param currencyPair currency pair
 * @param amount       amount
 * @return order creation result
 */
private OrderCreationResultDTO createMarketOrder(final GenericCassandreStrategy strategy, final OrderTypeDTO orderTypeDTO, final CurrencyPairDTO currencyPair, final BigDecimal amount) {
    try {
        // Making the order.
        MarketOrder m = new MarketOrder(UTIL_MAPPER.mapToOrderType(orderTypeDTO), amount.setScale(currencyPair.getBaseCurrencyPrecision(), FLOOR), CURRENCY_MAPPER.mapToCurrencyPair(currencyPair), getGeneratedOrderId(), null);
        logger.debug("Sending market order: {} - {} - {}", orderTypeDTO, currencyPair, amount.setScale(currencyPair.getBaseCurrencyPrecision(), FLOOR));
        // Sending the order.
        OrderDTO order = OrderDTO.builder().orderId(tradeService.placeMarketOrder(m)).type(orderTypeDTO).strategy(strategy.getStrategyDTO()).currencyPair(currencyPair).amount(CurrencyAmountDTO.builder().value(amount).currency(currencyPair.getBaseCurrency()).build()).cumulativeAmount(CurrencyAmountDTO.builder().value(amount).currency(currencyPair.getBaseCurrency()).build()).averagePrice(CurrencyAmountDTO.builder().value(strategy.getLastPriceForCurrencyPair(currencyPair)).currency(currencyPair.getQuoteCurrency()).build()).marketPrice(CurrencyAmountDTO.builder().value(strategy.getLastPriceForCurrencyPair(currencyPair)).currency(currencyPair.getQuoteCurrency()).build()).status(PENDING_NEW).timestamp(ZonedDateTime.now()).build();
        // We save the order.
        Optional<Order> savedOrder = orderRepository.findByOrderId(order.getOrderId());
        if (savedOrder.isEmpty()) {
            savedOrder = Optional.of(orderRepository.save(ORDER_MAPPER.mapToOrder(order)));
        }
        final OrderCreationResultDTO result = new OrderCreationResultDTO(ORDER_MAPPER.mapToOrderDTO(savedOrder.get()));
        logger.debug("Order created: {}", result);
        return result;
    } catch (Exception e) {
        final String error = "TradeService - Error calling createMarketOrder for " + amount + " " + currencyPair + ": " + e.getMessage();
        e.printStackTrace();
        logger.error(error);
        return new OrderCreationResultDTO(error, e);
    }
}
Also used : Order(tech.cassandre.trading.bot.domain.Order) LimitOrder(org.knowm.xchange.dto.trade.LimitOrder) MarketOrder(org.knowm.xchange.dto.trade.MarketOrder) MarketOrder(org.knowm.xchange.dto.trade.MarketOrder) OrderDTO(tech.cassandre.trading.bot.dto.trade.OrderDTO) OrderCreationResultDTO(tech.cassandre.trading.bot.dto.trade.OrderCreationResultDTO) NotAvailableFromExchangeException(org.knowm.xchange.exceptions.NotAvailableFromExchangeException) IOException(java.io.IOException)

Aggregations

MarketOrder (org.knowm.xchange.dto.trade.MarketOrder)45 BigDecimal (java.math.BigDecimal)37 Test (org.junit.Test)20 Date (java.util.Date)17 LimitOrder (org.knowm.xchange.dto.trade.LimitOrder)15 CurrencyPair (org.knowm.xchange.currency.CurrencyPair)10 TradeService (org.knowm.xchange.service.trade.TradeService)9 UserTrades (org.knowm.xchange.dto.trade.UserTrades)8 Order (org.knowm.xchange.dto.Order)7 OrderType (org.knowm.xchange.dto.Order.OrderType)6 LgoProductsTest (org.knowm.xchange.lgo.dto.product.LgoProductsTest)6 OpenOrders (org.knowm.xchange.dto.trade.OpenOrders)5 IOException (java.io.IOException)4 ThrowingCallable (org.assertj.core.api.ThrowableAssert.ThrowingCallable)4 Exchange (org.knowm.xchange.Exchange)4 StopOrder (org.knowm.xchange.dto.trade.StopOrder)4 OrderStatus (org.knowm.xchange.dto.Order.OrderStatus)3 ArrayList (java.util.ArrayList)2 ExchangeSpecification (org.knowm.xchange.ExchangeSpecification)2 UserTrade (org.knowm.xchange.dto.trade.UserTrade)2