org.knowm.xchange.service.marketdata.MarketDataService Java Examples

The following examples show how to use org.knowm.xchange.service.marketdata.MarketDataService. 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();
}
 
Example #3
Source File: ExchangeBuilder.java    From arbitrader with MIT License 4 votes vote down vote up
public Exchange build() throws IOException {
    Exchange exchange = mock(Exchange.class);
    ExchangeSpecification specification = mock(ExchangeSpecification.class);
    ExchangeConfiguration metadata = new ExchangeConfiguration();
    MarketDataService marketDataService = mock(MarketDataService.class);

    metadata.setHomeCurrency(homeCurrency);
    metadata.setTradingPairs(tradingPairs);
    metadata.setMargin(isMarginSupported);

    when(exchange.getExchangeSpecification()).thenReturn(specification);
    when(specification.getExchangeName()).thenReturn(name);
    when(specification.getExchangeSpecificParametersItem(METADATA_KEY)).thenReturn(metadata);
    when(exchange.getMarketDataService()).thenReturn(marketDataService);

    if (tickerException != null) {
        when(marketDataService.getTicker(any())).thenThrow(tickerException);
        when(marketDataService.getTickers(any(Params.class))).thenThrow(tickerException);
    }

    if (tickers != null && !tickers.isEmpty()) {
        tickers.forEach(ticker -> {
            try {
                when(marketDataService.getTicker(eq(ticker.getCurrencyPair()))).thenReturn(ticker);
            } catch (IOException e) {
                // nothing to do here if we couldn't build the mock
            }
        });

        if (isGetTickersImplemented) {
            when(marketDataService.getTickers(any())).thenReturn(tickers);
        } else {
            when(marketDataService.getTickers(any())).thenThrow(new NotYetImplementedForExchangeException());
        }
    }

    if (bids != null || asks != null) {
        OrderBook orderBook = new OrderBook(
            new Date(),
            generateOrders(currencyPair, Order.OrderType.ASK),
            generateOrders(currencyPair, Order.OrderType.BID)
        );

        when(marketDataService.getOrderBook(eq(currencyPair))).thenReturn(orderBook);
    }

    if (!balances.isEmpty()) {
        Wallet wallet = Wallet.Builder.from(balances).build();
        AccountInfo accountInfo = new AccountInfo(wallet);
        AccountService accountService = mock(AccountService.class);

        when(accountService.getAccountInfo()).thenReturn(accountInfo);
        when(exchange.getAccountService()).thenReturn(accountService);
    }

    if (tickerStrategy != null) {
        when(specification.getExchangeSpecificParametersItem(TICKER_STRATEGY_KEY)).thenReturn(tickerStrategy);
    }

    if (exchangeMetaData != null) {
        when(exchange.getExchangeMetaData()).thenReturn(exchangeMetaData);
    }

    if (tradeService != null) {
        when(exchange.getTradeService()).thenReturn(tradeService);
    }

    return exchange;
}
 
Example #4
Source File: BaseExchange.java    From zheshiyigeniubidexiangmu with MIT License 2 votes vote down vote up
public MarketDataService getMarketDataService() {

    return marketDataService;
  }
 
Example #5
Source File: Exchange.java    From zheshiyigeniubidexiangmu with MIT License 2 votes vote down vote up
/**
 * A market data service typically consists of a regularly updated list of the available prices
 * for the various symbols
 *
 * <p>This is the non-streaming (blocking) version of the service
 *
 * @return The exchange's market data service
 */
MarketDataService getMarketDataService();