Search in sources :

Example 96 with OrderType

use of org.knowm.xchange.dto.Order.OrderType in project XChange by knowm.

the class ItBitAdapters method adaptTradeHistory.

public static UserTrades adaptTradeHistory(ItBitTradeHistory history) {
    List<ItBitUserTrade> itBitTrades = history.getTradingHistory();
    ListMultimap<String, ItBitUserTrade> tradesByOrderId = ArrayListMultimap.create();
    for (ItBitUserTrade itBitTrade : itBitTrades) {
        tradesByOrderId.put(itBitTrade.getOrderId(), itBitTrade);
    }
    List<UserTrade> trades = new ArrayList<>();
    for (String orderId : tradesByOrderId.keySet()) {
        BigDecimal totalValue = BigDecimal.ZERO;
        BigDecimal totalQuantity = BigDecimal.ZERO;
        BigDecimal totalFee = BigDecimal.ZERO;
        for (ItBitUserTrade trade : tradesByOrderId.get(orderId)) {
            // can have multiple trades for same order, so add them all up here to
            // get the average price and total fee
            // we have to do this because there is no trade id
            totalValue = totalValue.add(trade.getCurrency1Amount().multiply(trade.getRate()));
            totalQuantity = totalQuantity.add(trade.getCurrency1Amount());
            totalFee = totalFee.add(trade.getCommissionPaid());
            totalFee = totalFee.subtract(trade.getRebatesApplied());
        }
        BigDecimal volumeWeightedAveragePrice = totalValue.divide(totalQuantity, 8, BigDecimal.ROUND_HALF_UP);
        ItBitUserTrade itBitTrade = tradesByOrderId.get(orderId).get(0);
        OrderType orderType = itBitTrade.getDirection().equals(ItBitUserTrade.Direction.buy) ? OrderType.BID : OrderType.ASK;
        CurrencyPair currencyPair = adaptCcyPair(itBitTrade.getInstrument());
        String ccy = itBitTrade.getCommissionCurrency();
        Currency feeCcy = adaptCcy(ccy == null ? itBitTrade.getRebateCurrency() : ccy);
        UserTrade userTrade = new UserTrade.Builder().type(orderType).originalAmount(totalQuantity).currencyPair(currencyPair).price(volumeWeightedAveragePrice).timestamp(itBitTrade.getTimestamp()).id(// itbit doesn't have trade ids, so we use the order id instead
        orderId).orderId(orderId).feeAmount(totalFee).feeCurrency(feeCcy).build();
        trades.add(userTrade);
    }
    return new UserTrades(trades, TradeSortType.SortByTimestamp);
}
Also used : ItBitUserTrade(org.knowm.xchange.itbit.dto.trade.ItBitUserTrade) ArrayList(java.util.ArrayList) ItBitUserTrade(org.knowm.xchange.itbit.dto.trade.ItBitUserTrade) UserTrade(org.knowm.xchange.dto.trade.UserTrade) BigDecimal(java.math.BigDecimal) UserTrades(org.knowm.xchange.dto.trade.UserTrades) OrderType(org.knowm.xchange.dto.Order.OrderType) Currency(org.knowm.xchange.currency.Currency) CurrencyPair(org.knowm.xchange.currency.CurrencyPair)

Example 97 with OrderType

use of org.knowm.xchange.dto.Order.OrderType in project XChange by knowm.

the class LatokenAdapters method adaptOrder.

public static LimitOrder adaptOrder(Exchange exchange, LatokenOrder order) {
    OrderType type = adaptOrderType(order.getSide());
    CurrencyPair currencyPair = adaptCurrencyPair(exchange, order.getSymbol());
    OrderStatus orderStatus = adaptOrderStatus(order.getOrderStatus());
    return new LimitOrder.Builder(type, currencyPair).originalAmount(order.getAmount()).id(order.getOrderId()).timestamp(order.getTimeCreated()).limitPrice(order.getPrice()).cumulativeAmount(order.getExecutedAmount()).fee(BigDecimal.ZERO).orderStatus(orderStatus).build();
}
Also used : OrderStatus(org.knowm.xchange.dto.Order.OrderStatus) LatokenOrderStatus(org.knowm.xchange.latoken.dto.trade.LatokenOrderStatus) OrderType(org.knowm.xchange.dto.Order.OrderType) CurrencyPair(org.knowm.xchange.currency.CurrencyPair)

Example 98 with OrderType

use of org.knowm.xchange.dto.Order.OrderType in project XChange by knowm.

the class LakeBTCAdapters method adaptTradeHistory.

/**
 * Adapt the user's trades
 *
 * @param transactions
 * @return
 */
public static UserTrades adaptTradeHistory(LakeBTCTradeResponse[] transactions) {
    List<UserTrade> trades = new ArrayList<>();
    long lastTradeId = 0;
    for (LakeBTCTradeResponse trade : transactions) {
        final OrderType orderType = trade.getType().startsWith("buy") ? OrderType.BID : OrderType.ASK;
        BigDecimal originalAmount = trade.getAmount();
        BigDecimal price = trade.getTotal().abs();
        Date timestamp = DateUtils.fromMillisUtc(trade.getAt() * 1000L);
        final String tradeId = trade.getId();
        final CurrencyPair currencyPair = CurrencyPair.BTC_CNY;
        UserTrade userTrade = new UserTrade.Builder().type(orderType).originalAmount(originalAmount).currencyPair(currencyPair).price(price).timestamp(timestamp).feeCurrency(Currency.getInstance(currencyPair.counter.getCurrencyCode())).build();
        trades.add(userTrade);
    }
    return new UserTrades(trades, lastTradeId, Trades.TradeSortType.SortByTimestamp);
}
Also used : LakeBTCTradeResponse(org.knowm.xchange.lakebtc.dto.trade.LakeBTCTradeResponse) UserTrades(org.knowm.xchange.dto.trade.UserTrades) OrderType(org.knowm.xchange.dto.Order.OrderType) ArrayList(java.util.ArrayList) UserTrade(org.knowm.xchange.dto.trade.UserTrade) BigDecimal(java.math.BigDecimal) Date(java.util.Date) CurrencyPair(org.knowm.xchange.currency.CurrencyPair)

Example 99 with OrderType

use of org.knowm.xchange.dto.Order.OrderType in project XChange by knowm.

the class LivecoinAdapters method adaptUserTrade.

public static UserTrade adaptUserTrade(Map map) {
    OrderType type = OrderType.BID;
    if (map.get("type").toString().equals("SELL"))
        type = OrderType.ASK;
    Currency ccyA = Currency.getInstance(map.get("fixedCurrency").toString());
    Currency ccyB = Currency.getInstance(map.get("variableCurrency").toString());
    BigDecimal amountA = new BigDecimal(map.get("amount").toString());
    BigDecimal amountB = new BigDecimal(map.get("variableAmount").toString());
    int scale = Math.max(amountA.scale(), amountB.scale());
    BigDecimal price = amountB.divide(amountA, scale, RoundingMode.HALF_UP);
    String id = map.get("id").toString();
    return new UserTrade.Builder().type(type).originalAmount(amountA).currencyPair(new CurrencyPair(ccyA, ccyB)).price(price).timestamp(DateUtils.fromMillisUtc(Long.parseLong(map.get("date").toString()))).id(id).orderId(Optional.ofNullable(map.get("externalKey")).map(Object::toString).orElse(null)).feeAmount(new BigDecimal(map.get("fee").toString())).feeCurrency(getInstance(map.get("taxCurrency").toString())).build();
}
Also used : OrderType(org.knowm.xchange.dto.Order.OrderType) Currency(org.knowm.xchange.currency.Currency) BigDecimal(java.math.BigDecimal) CurrencyPair(org.knowm.xchange.currency.CurrencyPair)

Example 100 with OrderType

use of org.knowm.xchange.dto.Order.OrderType in project XChange by knowm.

the class LatokenTradeServiceIntegration method newOrder.

@Test
public void newOrder() throws Exception {
    CurrencyPair pair = CurrencyPair.ETH_BTC;
    OrderType type = OrderType.BID;
    BigDecimal amount = BigDecimal.valueOf(0.01);
    BigDecimal limitPrice = BigDecimal.valueOf(0.018881);
    LimitOrder newOrder = new LimitOrder.Builder(type, pair).originalAmount(amount).limitPrice(limitPrice).timestamp(new Date(System.currentTimeMillis())).build();
    // Test order
    LatokenTestOrder testOrder = tradeService.placeLatokenTestOrder(pair, "", LatokenOrderSide.buy, limitPrice, amount);
    System.out.println(testOrder);
    // Place order
    String newOrderId = tradeService.placeLimitOrder(newOrder);
    System.out.println(newOrderId);
    // Check open orders
    DefaultOpenOrdersParamCurrencyPair params = (DefaultOpenOrdersParamCurrencyPair) tradeService.createOpenOrdersParams();
    params.setCurrencyPair(CurrencyPair.ETH_BTC);
    List<LimitOrder> openOrders = tradeService.getOpenOrders(params).getOpenOrders();
    System.out.println(openOrders);
    // Cancel
    tradeService.cancelLatokenOrder(newOrderId);
    // Check open orders
    openOrders = tradeService.getOpenOrders().getOpenOrders();
    System.out.println(openOrders);
}
Also used : OrderType(org.knowm.xchange.dto.Order.OrderType) LatokenTestOrder(org.knowm.xchange.latoken.dto.trade.LatokenTestOrder) LimitOrder(org.knowm.xchange.dto.trade.LimitOrder) BigDecimal(java.math.BigDecimal) Date(java.util.Date) DefaultOpenOrdersParamCurrencyPair(org.knowm.xchange.service.trade.params.orders.DefaultOpenOrdersParamCurrencyPair) CurrencyPair(org.knowm.xchange.currency.CurrencyPair) DefaultOpenOrdersParamCurrencyPair(org.knowm.xchange.service.trade.params.orders.DefaultOpenOrdersParamCurrencyPair) Test(org.junit.Test)

Aggregations

OrderType (org.knowm.xchange.dto.Order.OrderType)104 CurrencyPair (org.knowm.xchange.currency.CurrencyPair)68 Date (java.util.Date)65 BigDecimal (java.math.BigDecimal)64 UserTrade (org.knowm.xchange.dto.trade.UserTrade)38 LimitOrder (org.knowm.xchange.dto.trade.LimitOrder)36 ArrayList (java.util.ArrayList)35 Trade (org.knowm.xchange.dto.marketdata.Trade)25 UserTrades (org.knowm.xchange.dto.trade.UserTrades)19 Order (org.knowm.xchange.dto.Order)15 Currency (org.knowm.xchange.currency.Currency)12 MarketOrder (org.knowm.xchange.dto.trade.MarketOrder)12 OpenOrders (org.knowm.xchange.dto.trade.OpenOrders)11 Test (org.junit.Test)10 Trades (org.knowm.xchange.dto.marketdata.Trades)10 OrderStatus (org.knowm.xchange.dto.Order.OrderStatus)9 List (java.util.List)6 BitfinexOrderType (org.knowm.xchange.bitfinex.v1.BitfinexOrderType)5 OrderBook (org.knowm.xchange.dto.marketdata.OrderBook)5 StopOrder (org.knowm.xchange.dto.trade.StopOrder)5