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