Search in sources :

Example 96 with UserTrade

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

the class CoinjarStreamingAdaptersTest method testAdaptUserTrade.

@Test
public void testAdaptUserTrade() {
    CoinjarWebSocketUserTradeEvent.Payload.Fill fill = new CoinjarWebSocketUserTradeEvent.Payload.Fill("93.82", "2018-08-28T05:16:37.405702Z", 9130900, "0.01000000", "buy", "BTCAUD", "9382.00000000", 280117631, "taker");
    CoinjarWebSocketUserTradeEvent.Payload payload = new CoinjarWebSocketUserTradeEvent.Payload(fill);
    CoinjarWebSocketUserTradeEvent event = new CoinjarWebSocketUserTradeEvent("private", "private:fill", null, payload);
    UserTrade userTrade = CoinjarStreamingAdapters.adaptUserTrade(event);
    UserTrade expected = new UserTrade.Builder().id("9130900").orderId("280117631").currencyPair(CurrencyPair.BTC_AUD).originalAmount(new BigDecimal("0.01000000")).timestamp(Date.from(ZonedDateTime.parse("2018-08-28T05:16:37.405702Z").toInstant())).price(new BigDecimal("9382.00000000")).type(Order.OrderType.BID).build();
    assertThat(userTrade).isEqualTo(expected);
}
Also used : CoinjarWebSocketUserTradeEvent(info.bitrich.xchangestream.coinjar.dto.CoinjarWebSocketUserTradeEvent) UserTrade(org.knowm.xchange.dto.trade.UserTrade) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 97 with UserTrade

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

the class KrakenStreamingTradeService method adaptKrakenUserTrade.

private List<UserTrade> adaptKrakenUserTrade(KrakenDtoUserTradeHolder[] ownTrades) {
    List<UserTrade> result = new ArrayList<>();
    for (KrakenDtoUserTradeHolder holder : ownTrades) {
        for (Map.Entry<String, KrakenOwnTrade> entry : holder.entrySet()) {
            String tradeId = entry.getKey();
            KrakenOwnTrade dto = entry.getValue();
            CurrencyPair currencyPair = new CurrencyPair(dto.pair);
            result.add(new UserTrade.Builder().id(// The tradeId should be the key of the map, postxid can be null and is
            tradeId).orderId(dto.ordertxid).currencyPair(currencyPair).timestamp(dto.time == null ? null : new Date((long) (dto.time * 1000L))).type(KrakenAdapters.adaptOrderType(KrakenType.fromString(dto.type))).price(dto.price).feeAmount(dto.fee).feeCurrency(currencyPair.counter).originalAmount(dto.vol).build());
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) UserTrade(org.knowm.xchange.dto.trade.UserTrade) HashMap(java.util.HashMap) Map(java.util.Map) Date(java.util.Date) KrakenOwnTrade(info.bitrich.xchangestream.kraken.dto.KrakenOwnTrade) CurrencyPair(org.knowm.xchange.currency.CurrencyPair)

Example 98 with UserTrade

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

the class LgoStreamingTradeServiceTest method it_handles_trades.

@Test
public void it_handles_trades() throws IOException, ParseException {
    JsonNode snapshot = TestUtils.getJsonContent("/trade/user-trades-snapshot.json");
    JsonNode update = TestUtils.getJsonContent("/trade/user-trades-update.json");
    when(streamingService.subscribeChannel(anyString())).thenReturn(Observable.just(snapshot, update));
    Observable<UserTrade> userTrades = service.getUserTrades(CurrencyPair.BTC_USD);
    verify(streamingService).subscribeChannel("user-BTC-USD");
    Date date = dateFormat.parse("2019-08-06T10:00:05.658Z");
    ArrayList<UserTrade> trades = Lists.newArrayList(userTrades.blockingIterable());
    assertThat(trades).hasSize(1);
    assertThat(trades.get(0)).usingRecursiveComparison().isEqualTo(new UserTrade.Builder().type(Order.OrderType.ASK).originalAmount(new BigDecimal("0.50000000")).currencyPair(CurrencyPair.BTC_USD).price(new BigDecimal("955.3000")).timestamp(date).id("4441691").orderId("156508560418400001").feeAmount(new BigDecimal("0.2388")).feeCurrency(Currency.USD).build());
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) UserTrade(org.knowm.xchange.dto.trade.UserTrade) Date(java.util.Date) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 99 with UserTrade

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

the class ExmoTradeService method getOrder.

@Override
public /**
 * Warning: this make multiple api requests (one per parameter) so may result in you exceeding
 * your api limits
 */
Collection<Order> getOrder(OrderQueryParams... orderQueryParams) throws IOException {
    List<Order> results = new ArrayList<>();
    for (OrderQueryParams orderQueryParam : orderQueryParams) {
        String orderId = orderQueryParam.getOrderId();
        Order.OrderType type = null;
        CurrencyPair currencyPair = null;
        String id = null;
        Date timestamp = null;
        BigDecimal totalValue = BigDecimal.ZERO;
        BigDecimal cumulativeAmount = BigDecimal.ZERO;
        BigDecimal fee = BigDecimal.ZERO;
        Order.OrderStatus status = Order.OrderStatus.UNKNOWN;
        ExmoUserTrades exmoUserTrades = userTrades(orderId);
        if (exmoUserTrades == null)
            continue;
        BigDecimal originalAmount = exmoUserTrades.getOriginalAmount();
        for (UserTrade userTrade : exmoUserTrades.getUserTrades()) {
            type = userTrade.getType();
            currencyPair = userTrade.getCurrencyPair();
            id = userTrade.getOrderId();
            if (timestamp == null)
                timestamp = userTrade.getTimestamp();
            if (timestamp.getTime() < userTrade.getTimestamp().getTime())
                timestamp = userTrade.getTimestamp();
            BigDecimal amountForFill = userTrade.getOriginalAmount();
            BigDecimal priceForFill = userTrade.getPrice();
            BigDecimal value = amountForFill.multiply(priceForFill);
            cumulativeAmount = cumulativeAmount.add(amountForFill);
            totalValue = totalValue.add(value);
        }
        BigDecimal averagePrice = totalValue.divide(cumulativeAmount, 8, RoundingMode.HALF_UP);
        Order order = new MarketOrder(type, originalAmount, currencyPair, id, timestamp, averagePrice, cumulativeAmount, fee, status);
        results.add(order);
    }
    return results;
}
Also used : StopOrder(org.knowm.xchange.dto.trade.StopOrder) Order(org.knowm.xchange.dto.Order) LimitOrder(org.knowm.xchange.dto.trade.LimitOrder) MarketOrder(org.knowm.xchange.dto.trade.MarketOrder) ArrayList(java.util.ArrayList) ExmoUserTrades(org.knowm.xchange.exmo.dto.trade.ExmoUserTrades) UserTrade(org.knowm.xchange.dto.trade.UserTrade) Date(java.util.Date) BigDecimal(java.math.BigDecimal) MarketOrder(org.knowm.xchange.dto.trade.MarketOrder) OrderQueryParams(org.knowm.xchange.service.trade.params.orders.OrderQueryParams) CurrencyPair(org.knowm.xchange.currency.CurrencyPair) TradeHistoryParamCurrencyPair(org.knowm.xchange.service.trade.params.TradeHistoryParamCurrencyPair)

Example 100 with UserTrade

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

the class GeminiAdapters method adaptTradeHistory.

public static UserTrades adaptTradeHistory(GeminiTradeResponse[] trades, String symbol) {
    List<UserTrade> pastTrades = new ArrayList<>(trades.length);
    CurrencyPair currencyPair = adaptCurrencyPair(symbol);
    for (GeminiTradeResponse trade : trades) {
        OrderType orderType = trade.getType().equalsIgnoreCase("buy") ? OrderType.BID : OrderType.ASK;
        Date timestamp = convertBigDecimalTimestampToDate(trade.getTimestamp());
        final BigDecimal fee = trade.getFeeAmount();
        pastTrades.add(new UserTrade.Builder().type(orderType).originalAmount(trade.getAmount()).currencyPair(currencyPair).price(trade.getPrice()).timestamp(timestamp).id(trade.getTradeId()).orderId(trade.getOrderId()).feeAmount(fee).feeCurrency(Currency.getInstance(trade.getFeeCurrency())).build());
    }
    return new UserTrades(pastTrades, TradeSortType.SortByTimestamp);
}
Also used : UserTrades(org.knowm.xchange.dto.trade.UserTrades) OrderType(org.knowm.xchange.dto.Order.OrderType) ArrayList(java.util.ArrayList) GeminiTradeResponse(org.knowm.xchange.gemini.v1.dto.trade.GeminiTradeResponse) UserTrade(org.knowm.xchange.dto.trade.UserTrade) Date(java.util.Date) BigDecimal(java.math.BigDecimal) CurrencyPair(org.knowm.xchange.currency.CurrencyPair)

Aggregations

UserTrade (org.knowm.xchange.dto.trade.UserTrade)112 UserTrades (org.knowm.xchange.dto.trade.UserTrades)62 ArrayList (java.util.ArrayList)57 BigDecimal (java.math.BigDecimal)50 CurrencyPair (org.knowm.xchange.currency.CurrencyPair)40 Date (java.util.Date)31 Test (org.junit.Test)25 OrderType (org.knowm.xchange.dto.Order.OrderType)19 List (java.util.List)14 Test (org.junit.jupiter.api.Test)14 TradeHistoryParams (org.knowm.xchange.service.trade.params.TradeHistoryParams)14 IOException (java.io.IOException)13 Currency (org.knowm.xchange.currency.Currency)13 LimitOrder (org.knowm.xchange.dto.trade.LimitOrder)13 TradeHistoryParamCurrencyPair (org.knowm.xchange.service.trade.params.TradeHistoryParamCurrencyPair)12 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)11 TradeService (org.knowm.xchange.service.trade.TradeService)11 InputStream (java.io.InputStream)10 Order (org.knowm.xchange.dto.Order)10 OpenOrders (org.knowm.xchange.dto.trade.OpenOrders)10