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;
}
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);
}
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);
}
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;
}
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);
}
}
Aggregations