Search in sources :

Example 16 with Trade

use of org.knowm.xchange.dto.marketdata.Trade in project XChange by knowm.

the class YoBitMarketDataService method getTrades.

public Trades getTrades(PublicTradesRequestParams params) throws IOException {
    if (params instanceof MultiCurrencyPublicTradesDataRequestParams) {
        MultiCurrencyPublicTradesDataRequestParams multiCurrencyPublicTradesDataRequestParams = (MultiCurrencyPublicTradesDataRequestParams) params;
        YoBitTrades publicTrades = getPublicTrades(multiCurrencyPublicTradesDataRequestParams.currencyPairs);
        List<Trade> all = new ArrayList<>();
        Map<String, List<YoBitTrade>> tradesByCcyPair = publicTrades.getTrades();
        for (String ccyPair : tradesByCcyPair.keySet()) {
            CurrencyPair currencyPair = YoBitAdapters.adaptCurrencyPair(ccyPair);
            List<Trade> trades = YoBitAdapters.adaptTrades(tradesByCcyPair.get(ccyPair), currencyPair).getTrades();
            all.addAll(trades);
        }
        Collections.sort(all, new Comparator<Trade>() {

            @Override
            public int compare(Trade a, Trade b) {
                return a.getTimestamp().compareTo(b.getTimestamp());
            }
        });
        return new Trades(all, Trades.TradeSortType.SortByID);
    }
    throw new IllegalStateException("Don't understand " + params);
}
Also used : YoBitTrades(org.knowm.xchange.yobit.dto.marketdata.YoBitTrades) Trades(org.knowm.xchange.dto.marketdata.Trades) ArrayList(java.util.ArrayList) MultiCurrencyPublicTradesDataRequestParams(org.knowm.xchange.yobit.dto.MultiCurrencyPublicTradesDataRequestParams) Trade(org.knowm.xchange.dto.marketdata.Trade) YoBitTrade(org.knowm.xchange.yobit.dto.marketdata.YoBitTrade) ArrayList(java.util.ArrayList) List(java.util.List) YoBitTrades(org.knowm.xchange.yobit.dto.marketdata.YoBitTrades) CurrencyPair(org.knowm.xchange.currency.CurrencyPair)

Example 17 with Trade

use of org.knowm.xchange.dto.marketdata.Trade in project XChange by knowm.

the class BTCTradeAdapters method adaptTrades.

public static Trades adaptTrades(BTCTradeTrade[] btcTradeTrades, CurrencyPair currencyPair) {
    int length = btcTradeTrades.length;
    List<Trade> trades = new ArrayList<>(length);
    for (BTCTradeTrade btcTradeTrade : btcTradeTrades) {
        trades.add(adaptTrade(btcTradeTrade, currencyPair));
    }
    long lastID = length > 0 ? btcTradeTrades[length - 1].getTid() : 0L;
    return new Trades(trades, lastID, TradeSortType.SortByID);
}
Also used : BTCTradeTrade(org.knowm.xchange.btctrade.dto.marketdata.BTCTradeTrade) UserTrade(org.knowm.xchange.dto.trade.UserTrade) Trade(org.knowm.xchange.dto.marketdata.Trade) UserTrades(org.knowm.xchange.dto.trade.UserTrades) Trades(org.knowm.xchange.dto.marketdata.Trades) ArrayList(java.util.ArrayList) BTCTradeTrade(org.knowm.xchange.btctrade.dto.marketdata.BTCTradeTrade)

Example 18 with Trade

use of org.knowm.xchange.dto.marketdata.Trade in project XChange by knowm.

the class BTCTradeAdaptersTest method testAdaptTradesBTCTradeTradeArrayCurrencyPair.

@Test
public void testAdaptTradesBTCTradeTradeArrayCurrencyPair() throws IOException {
    BTCTradeTrade[] btcTradeTrades = mapper.readValue(getClass().getResource("dto/marketdata/trades.json"), BTCTradeTrade[].class);
    Trades trades = BTCTradeAdapters.adaptTrades(btcTradeTrades, CurrencyPair.BTC_CNY);
    List<Trade> tradeList = trades.getTrades();
    assertEquals(new Date(1403397140L * 1000L), tradeList.get(0).getTimestamp());
    assertEquals(new BigDecimal("3703"), tradeList.get(0).getPrice());
    assertEquals(new BigDecimal("3.50000000000"), tradeList.get(0).getOriginalAmount());
    assertEquals("2895575", tradeList.get(0).getId());
    assertEquals(OrderType.BID, tradeList.get(0).getType());
    assertEquals(new Date(1403397191L * 1000L), tradeList.get(1).getTimestamp());
    assertEquals(new BigDecimal("3704"), tradeList.get(1).getPrice());
    assertEquals(new BigDecimal("0.00200000000"), tradeList.get(1).getOriginalAmount());
    assertEquals("2895576", tradeList.get(1).getId());
    assertEquals(OrderType.BID, tradeList.get(1).getType());
    assertEquals(new Date(1403428819L * 1000L), tradeList.get(tradeList.size() - 2).getTimestamp());
    assertEquals(new BigDecimal("3740.01"), tradeList.get(tradeList.size() - 2).getPrice());
    assertEquals(new BigDecimal("0.01000000000"), tradeList.get(tradeList.size() - 2).getOriginalAmount());
    assertEquals("2896235", tradeList.get(tradeList.size() - 2).getId());
    assertEquals(OrderType.ASK, tradeList.get(tradeList.size() - 2).getType());
    assertEquals(new Date(1403428797L * 1000L), tradeList.get(tradeList.size() - 1).getTimestamp());
    assertEquals(new BigDecimal("3752"), tradeList.get(tradeList.size() - 1).getPrice());
    assertEquals(new BigDecimal("16.70000000000"), tradeList.get(tradeList.size() - 1).getOriginalAmount());
    assertEquals("2896239", tradeList.get(tradeList.size() - 1).getId());
    assertEquals(OrderType.BID, tradeList.get(tradeList.size() - 1).getType());
}
Also used : BTCTradeTrade(org.knowm.xchange.btctrade.dto.marketdata.BTCTradeTrade) Trade(org.knowm.xchange.dto.marketdata.Trade) Trades(org.knowm.xchange.dto.marketdata.Trades) Date(java.util.Date) BigDecimal(java.math.BigDecimal) BTCTradeTrade(org.knowm.xchange.btctrade.dto.marketdata.BTCTradeTrade) Test(org.junit.Test)

Example 19 with Trade

use of org.knowm.xchange.dto.marketdata.Trade in project XChange by knowm.

the class YoBitTradeDemo method main.

public static void main(String[] args) throws IOException {
    Exchange yoBitExchange = ExchangeFactory.INSTANCE.createExchange(YoBitExchange.class);
    // Interested in the public market data feed (no authentication)
    MarketDataService marketDataService = yoBitExchange.getMarketDataService();
    System.out.println("fetching data...");
    Trades trades = marketDataService.getTrades(CurrencyPair.BTC_USD);
    System.out.println("received data.");
    for (Trade trade : trades.getTrades()) {
        System.out.println(trade.getType() + " " + trade.getCurrencyPair() + " Price: " + trade.getPrice() + " Amount: " + trade.getOriginalAmount());
    }
}
Also used : Exchange(org.knowm.xchange.Exchange) YoBitExchange(org.knowm.xchange.yobit.YoBitExchange) Trade(org.knowm.xchange.dto.marketdata.Trade) Trades(org.knowm.xchange.dto.marketdata.Trades) MarketDataService(org.knowm.xchange.service.marketdata.MarketDataService)

Example 20 with Trade

use of org.knowm.xchange.dto.marketdata.Trade in project XChange by knowm.

the class OkCoinStreamingMarketDataService method getTrades.

/**
 * #### spot #### 4. ok_sub_spot_X_deals 订阅成交记录
 *
 * <p>#### future #### 5. ok_sub_futureusd_X_trade_Y 订阅合约交易信息 5. ok_sub_futureusd_X_trade_Y
 * Subscribe Contract Trade Record
 *
 * @param currencyPair Currency pair of the trades
 * @param args the first arg {@link FuturesContract}
 * @return
 */
@Override
public Observable<Trade> getTrades(CurrencyPair currencyPair, Object... args) {
    String channel = String.format("ok_sub_spot_%s_%s_deals", currencyPair.base.toString().toLowerCase(), currencyPair.counter.toString().toLowerCase());
    if (args.length > 0) {
        FuturesContract contract = (FuturesContract) args[0];
        channel = String.format("ok_sub_future%s_%s_trade_%s", currencyPair.counter.toString().toLowerCase(), currencyPair.base.toString().toLowerCase(), contract.getName());
    }
    return service.subscribeChannel(channel).map(s -> {
        String[][] trades = mapper.treeToValue(s.get("data"), String[][].class);
        // I don't know how to parse this array of arrays in Jacson.
        OkCoinWebSocketTrade[] okCoinTrades = new OkCoinWebSocketTrade[trades.length];
        for (int i = 0; i < trades.length; ++i) {
            OkCoinWebSocketTrade okCoinWebSocketTrade = new OkCoinWebSocketTrade(trades[i]);
            okCoinTrades[i] = okCoinWebSocketTrade;
        }
        return OkCoinAdapters.adaptTrades(okCoinTrades, currencyPair);
    }).flatMapIterable(Trades::getTrades);
}
Also used : StreamingMarketDataService(info.bitrich.xchangestream.core.StreamingMarketDataService) Ticker(org.knowm.xchange.dto.marketdata.Ticker) OkCoinTickerResponse(org.knowm.xchange.okcoin.dto.marketdata.OkCoinTickerResponse) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HashMap(java.util.HashMap) OrderBook(org.knowm.xchange.dto.marketdata.OrderBook) Trades(org.knowm.xchange.dto.marketdata.Trades) StreamingObjectMapperHelper(info.bitrich.xchangestream.service.netty.StreamingObjectMapperHelper) OkCoinWebSocketTrade(info.bitrich.xchangestream.okcoin.dto.OkCoinWebSocketTrade) BigDecimal(java.math.BigDecimal) Order(org.knowm.xchange.dto.Order) FuturesContract(org.knowm.xchange.okcoin.FuturesContract) OkCoinAdapters(org.knowm.xchange.okcoin.OkCoinAdapters) Map(java.util.Map) OkCoinOrderbook(info.bitrich.xchangestream.okcoin.dto.OkCoinOrderbook) Observable(io.reactivex.Observable) OkCoinDepth(org.knowm.xchange.okcoin.dto.marketdata.OkCoinDepth) Trade(org.knowm.xchange.dto.marketdata.Trade) CurrencyPair(org.knowm.xchange.currency.CurrencyPair) FutureTicker(info.bitrich.xchangestream.okcoin.dto.marketdata.FutureTicker) OkCoinTicker(org.knowm.xchange.okcoin.dto.marketdata.OkCoinTicker) OkCoinWebSocketTrade(info.bitrich.xchangestream.okcoin.dto.OkCoinWebSocketTrade) Trades(org.knowm.xchange.dto.marketdata.Trades) FuturesContract(org.knowm.xchange.okcoin.FuturesContract)

Aggregations

Trade (org.knowm.xchange.dto.marketdata.Trade)95 Trades (org.knowm.xchange.dto.marketdata.Trades)58 UserTrade (org.knowm.xchange.dto.trade.UserTrade)49 ArrayList (java.util.ArrayList)42 Date (java.util.Date)38 BigDecimal (java.math.BigDecimal)30 UserTrades (org.knowm.xchange.dto.trade.UserTrades)29 OrderType (org.knowm.xchange.dto.Order.OrderType)25 Test (org.junit.Test)21 CurrencyPair (org.knowm.xchange.currency.CurrencyPair)15 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)11 Exchange (org.knowm.xchange.Exchange)8 InputStream (java.io.InputStream)7 Order (org.knowm.xchange.dto.Order)7 OrderBook (org.knowm.xchange.dto.marketdata.OrderBook)7 Ticker (org.knowm.xchange.dto.marketdata.Ticker)7 LimitOrder (org.knowm.xchange.dto.trade.LimitOrder)7 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 StreamingObjectMapperHelper (info.bitrich.xchangestream.service.netty.StreamingObjectMapperHelper)5 Observable (io.reactivex.Observable)5