Search in sources :

Example 16 with MarketOrder

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

the class CoindirectAdapters method adaptOrder.

public static Order adaptOrder(CoindirectOrder order) {
    Order.OrderType type = convert(order.side);
    CurrencyPair currencyPair = toCurrencyPair(order.symbol);
    Order.OrderStatus orderStatus = adaptOrderStatus(order.status);
    final BigDecimal averagePrice;
    if (order.executedAmount.signum() == 0) {
        averagePrice = BigDecimal.ZERO;
    } else {
        averagePrice = order.executedPrice;
    }
    Order result = null;
    if (order.type.equals(CoindirectOrder.Type.MARKET)) {
        result = new MarketOrder(type, order.amount, currencyPair, order.uuid, order.dateCreated, averagePrice, order.executedAmount, order.executedFees, orderStatus);
    } else if (order.type.equals(CoindirectOrder.Type.LIMIT)) {
        result = new LimitOrder(type, order.amount, currencyPair, order.uuid, order.dateCreated, order.price, averagePrice, order.executedAmount, order.executedFees, orderStatus);
    }
    return result;
}
Also used : Order(org.knowm.xchange.dto.Order) LimitOrder(org.knowm.xchange.dto.trade.LimitOrder) CoindirectOrder(org.knowm.xchange.coindirect.dto.trade.CoindirectOrder) MarketOrder(org.knowm.xchange.dto.trade.MarketOrder) MarketOrder(org.knowm.xchange.dto.trade.MarketOrder) LimitOrder(org.knowm.xchange.dto.trade.LimitOrder) BigDecimal(java.math.BigDecimal) CurrencyPair(org.knowm.xchange.currency.CurrencyPair)

Example 17 with MarketOrder

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

the class BTCMarketsTradeDemo method main.

public static void main(String[] args) throws Exception {
    Exchange btcMarketsExchange = BTCMarketsExampleUtils.createTestExchange();
    TradeService tradeService = btcMarketsExchange.getTradeService();
    final OpenOrdersParamCurrencyPair openOrdersParams = (OpenOrdersParamCurrencyPair) tradeService.createOpenOrdersParams();
    openOrdersParams.setCurrencyPair(CurrencyPair.BTC_AUD);
    System.out.println("Open Orders: " + tradeService.getOpenOrders(openOrdersParams));
    // Place a limit sell order at a high price
    LimitOrder sellOrder = new LimitOrder((OrderType.ASK), new BigDecimal("0.003"), CurrencyPair.BTC_AUD, null, null, new BigDecimal("2000"));
    String limitOrderReturnValue = tradeService.placeLimitOrder(sellOrder);
    System.out.println("Limit Order return value: " + limitOrderReturnValue);
    System.out.println("Waiting a while for the order to get registered...");
    Thread.sleep(2000);
    System.out.println("Open Orders: " + tradeService.getOpenOrders(openOrdersParams));
    openOrdersParams.setCurrencyPair(CurrencyPair.LTC_BTC);
    System.out.println("Open Orders for LTC/BTC: " + tradeService.getOpenOrders(openOrdersParams));
    // Cancel the added order.
    boolean cancelResult = tradeService.cancelOrder(limitOrderReturnValue);
    System.out.println("Canceling returned " + cancelResult);
    System.out.println("Open Orders: " + tradeService.getOpenOrders(openOrdersParams));
    // An example of a sell market order
    MarketOrder sellMarketOrder = new MarketOrder((OrderType.ASK), new BigDecimal("0.003"), CurrencyPair.BTC_AUD, null, null);
    String marketSellOrderId = tradeService.placeMarketOrder(sellMarketOrder);
    System.out.println("Market Order return value: " + marketSellOrderId);
    // An example of a buy limit order.
    LimitOrder buyOrder = new LimitOrder((OrderType.BID), new BigDecimal("0.002"), CurrencyPair.BTC_AUD, null, null, new BigDecimal("240"));
    String buyLimiOrderId = tradeService.placeLimitOrder(buyOrder);
    System.out.println("Limit Order return value: " + buyLimiOrderId);
    // An example of a buy market order
    MarketOrder buyMarketOrder = new MarketOrder((OrderType.BID), new BigDecimal("0.004"), CurrencyPair.BTC_AUD, null, null);
    String buyMarketOrderId = tradeService.placeMarketOrder(buyMarketOrder);
    System.out.println("Market Order return value: " + buyMarketOrderId);
    // Get history of executed trades.
    BTCMarketsTradeService.HistoryParams params = (BTCMarketsTradeService.HistoryParams) tradeService.createTradeHistoryParams();
    params.setPageLength(10);
    params.setCurrencyPair(CurrencyPair.BTC_AUD);
    final UserTrades tradeHistory = tradeService.getTradeHistory(params);
    System.out.println("Trade history: " + tradeHistory);
}
Also used : Exchange(org.knowm.xchange.Exchange) UserTrades(org.knowm.xchange.dto.trade.UserTrades) TradeService(org.knowm.xchange.service.trade.TradeService) BTCMarketsTradeService(org.knowm.xchange.btcmarkets.service.BTCMarketsTradeService) MarketOrder(org.knowm.xchange.dto.trade.MarketOrder) LimitOrder(org.knowm.xchange.dto.trade.LimitOrder) BigDecimal(java.math.BigDecimal) OpenOrdersParamCurrencyPair(org.knowm.xchange.service.trade.params.orders.OpenOrdersParamCurrencyPair) BTCMarketsTradeService(org.knowm.xchange.btcmarkets.service.BTCMarketsTradeService)

Example 18 with MarketOrder

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

the class BTCTrukTradeDemo method generic.

private static void generic(TradeService tradeService) throws IOException {
    MarketOrder marketOrder = new MarketOrder((OrderType.ASK), new BigDecimal(".1"), CurrencyPair.BTC_USD);
    String marketOrderReturnValue = tradeService.placeMarketOrder(marketOrder);
    System.out.println("Market Order return value: " + marketOrderReturnValue);
}
Also used : MarketOrder(org.knowm.xchange.dto.trade.MarketOrder) BigDecimal(java.math.BigDecimal)

Example 19 with MarketOrder

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

the class PositionShortFluxTestMock method getXChangeTradeServiceMock.

@Override
public org.knowm.xchange.service.trade.TradeService getXChangeTradeServiceMock() throws IOException {
    org.knowm.xchange.service.trade.TradeService mock = mock(org.knowm.xchange.service.trade.TradeService.class);
    // No trades or orders returned - they will be directly emitted during the test.
    given(mock.getTradeHistory(any())).willReturn(new UserTrades(Collections.emptyList(), SortByTimestamp));
    given(mock.getOpenOrders()).willReturn(new OpenOrders(Collections.emptyList()));
    // Position 1.
    // Opening order creation result.
    MarketOrder m = new MarketOrder(Order.OrderType.ASK, new BigDecimal("10"), XCHANGE_ETH_BTC, null, null);
    given(mock.placeMarketOrder(m)).willReturn("ORDER00010");
    // Closing order creation result.
    m = new MarketOrder(Order.OrderType.BID, new BigDecimal("1000"), XCHANGE_ETH_BTC, null, null);
    given(mock.placeMarketOrder(m)).willReturn("ORDER00011");
    // Position 2.
    // Opening order creation result.
    m = new MarketOrder(Order.OrderType.ASK, new BigDecimal("0.0002"), XCHANGE_ETH_USDT, null, null);
    given(mock.placeMarketOrder(m)).willReturn("ORDER00020");
    // Closing order creation result.
    m = new MarketOrder(Order.OrderType.BID, new BigDecimal("0.0002"), XCHANGE_ETH_USDT, null, null);
    given(mock.placeMarketOrder(m)).willReturn("ORDER00021");
    return mock;
}
Also used : UserTrades(org.knowm.xchange.dto.trade.UserTrades) MarketOrder(org.knowm.xchange.dto.trade.MarketOrder) OpenOrders(org.knowm.xchange.dto.trade.OpenOrders) BigDecimal(java.math.BigDecimal)

Example 20 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 CassandreStrategyInterface 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));
        // We create the order in database with the PENDING_NEW status.
        OrderDTO order = OrderDTO.builder().orderId(tradeService.placeMarketOrder(m)).type(orderTypeDTO).strategy(strategy.getConfiguration().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 errorMessage = "Error calling createMarketOrder for " + amount + " " + currencyPair + ": " + e.getMessage();
        e.printStackTrace();
        logger.error(errorMessage);
        return new OrderCreationResultDTO(errorMessage, 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