Java Code Examples for org.knowm.xchange.Exchange#getMarketDataService()

The following examples show how to use org.knowm.xchange.Exchange#getMarketDataService() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ParallelTickerStrategy.java    From arbitrader with MIT License 4 votes vote down vote up
@Override
public List<Ticker> getTickers(Exchange exchange, List<CurrencyPair> currencyPairs) {
    MarketDataService marketDataService = exchange.getMarketDataService();
    Integer tickerBatchDelay = exchangeService.getExchangeMetadata(exchange).getTicker().get("batchDelay");
    int tickerPartitionSize = exchangeService.getExchangeMetadata(exchange).getTicker()
        .getOrDefault("batchSize", Integer.MAX_VALUE);

    long start = System.currentTimeMillis();

    List<Ticker> tickers = ListUtils.partition(currencyPairs, tickerPartitionSize)
        .stream()
        .peek(partition -> {
            if (tickerBatchDelay != null) {
                try {
                    LOGGER.debug("Sleeping for {} ms...", tickerBatchDelay);
                    Thread.sleep(tickerBatchDelay);
                } catch (InterruptedException e) {
                    LOGGER.trace("Sleep interrupted");
                }
            }
        })
        .map(partition ->
            partition
                .parallelStream()
                .map(currencyPair -> {
                    try {
                        try {
                            Ticker ticker = marketDataService.getTicker(exchangeService.convertExchangePair(exchange, currencyPair));

                            LOGGER.debug("Fetched ticker: {} {} {}/{}",
                                exchange.getExchangeSpecification().getExchangeName(),
                                ticker.getCurrencyPair(),
                                ticker.getBid(),
                                ticker.getAsk());

                            return ticker;
                        } catch (UndeclaredThrowableException ute) {
                            // Method proxying in rescu can enclose a real exception in this UTE, so we need to unwrap and re-throw it.
                            throw ute.getCause();
                        }
                    } catch (Throwable t) {
                        errorCollectorService.collect(exchange, t);
                        LOGGER.debug("Unexpected checked exception: " + t.getMessage(), t);
                    }

                    return null;
                })
                .filter(Objects::nonNull)
                .collect(Collectors.toList())
        )
        .flatMap(List::stream)
        .collect(Collectors.toList());

    long completion = System.currentTimeMillis() - start;

    if (completion > notificationConfiguration.getLogs().getSlowTickerWarning()) {
        LOGGER.warn("Slow Tickers! Fetched {} tickers via parallelStream for {} in {} ms",
            tickers.size(),
            exchange.getExchangeSpecification().getExchangeName(),
            System.currentTimeMillis() - start);
    }

    return tickers;
}
 
Example 2
Source File: SingleCallTickerStrategy.java    From arbitrader with MIT License 4 votes vote down vote up
@Override
public List<Ticker> getTickers(Exchange exchange, List<CurrencyPair> currencyPairs) {
    MarketDataService marketDataService = exchange.getMarketDataService();

    long start = System.currentTimeMillis();

    try {
        try {
            CurrencyPairsParam param = () -> currencyPairs.stream()
                .map(currencyPair -> exchangeService.convertExchangePair(exchange, currencyPair))
                .collect(Collectors.toList());

            List<Ticker> tickers = marketDataService.getTickers(param);

            tickers.forEach(ticker -> LOGGER.debug("Fetched ticker: {} {} {}/{}",
                exchange.getExchangeSpecification().getExchangeName(),
                ticker.getCurrencyPair(),
                ticker.getBid(),
                ticker.getAsk()));

            long completion = System.currentTimeMillis() - start;

            if (completion > notificationConfiguration.getLogs().getSlowTickerWarning()) {
                LOGGER.warn("Slow Tickers! Fetched {} tickers via getTickers() for {} in {} ms",
                    tickers.size(),
                    exchange.getExchangeSpecification().getExchangeName(),
                    System.currentTimeMillis() - start);
            }

            return tickers;
        } catch (UndeclaredThrowableException ute) {
            // Method proxying in rescu can enclose a real exception in this UTE, so we need to unwrap and re-throw it.
            throw ute.getCause();
        }
    } catch (Throwable t) {
        errorCollectorService.collect(exchange, t);
        LOGGER.debug("Unexpected checked exception: " + t.getMessage(), t);
    }

    return Collections.emptyList();
}