com.xeiam.xchange.examples.openexchangerates.marketdata.TickerDemo Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of xchange-examples Show documentation
Show all versions of xchange-examples Show documentation
Provides a set of examples that demonstrate how to use XChange in client applications
The newest version!
package com.xeiam.xchange.examples.openexchangerates.marketdata;
import java.io.IOException;
import com.xeiam.xchange.Exchange;
import com.xeiam.xchange.ExchangeFactory;
import com.xeiam.xchange.ExchangeSpecification;
import com.xeiam.xchange.currency.CurrencyPair;
import com.xeiam.xchange.dto.marketdata.Ticker;
import com.xeiam.xchange.oer.OERExchange;
import com.xeiam.xchange.oer.dto.marketdata.OERRates;
import com.xeiam.xchange.oer.service.polling.OERMarketDataServiceRaw;
import com.xeiam.xchange.service.polling.marketdata.PollingMarketDataService;
/**
* Demonstrate requesting Ticker at Open Exchange Rates
*/
public class TickerDemo {
public static void main(String[] args) throws IOException {
// Use the factory to get the Open Exchange Rates exchange API
ExchangeSpecification exchangeSpecification = new ExchangeSpecification(OERExchange.class.getName());
exchangeSpecification.setPlainTextUri("http://openexchangerates.org");
exchangeSpecification.setApiKey("ab32c922bca749ec9345b4717914ee1f");
Exchange openExchangeRates = ExchangeFactory.INSTANCE.createExchange(exchangeSpecification);
generic(openExchangeRates);
raw(openExchangeRates);
}
private static void generic(Exchange openExchangeRates) throws IOException {
// Interested in the polling market data feed
PollingMarketDataService marketDataService = openExchangeRates.getPollingMarketDataService();
// Get the latest ticker data showing EUR/USD
Ticker ticker = marketDataService.getTicker(CurrencyPair.EUR_USD);
System.out.println("Last: " + ticker.getLast().toString());
// Alternate way to print out ticker currency and amount
System.out.println("ticker: " + ticker.toString());
// Request another ticker. it will return a cached object
ticker = marketDataService.getTicker(CurrencyPair.JPY_USD);
System.out.println("cached Last: " + ticker.getLast().toString());
// Request BTC ticker. it will return a cached object
ticker = marketDataService.getTicker(CurrencyPair.BTC_USD);
System.out.println("cached Last: " + ticker.getLast().toString());
}
private static void raw(Exchange openExchangeRates) throws IOException {
OERMarketDataServiceRaw oERMarketDataServiceRaw = (OERMarketDataServiceRaw) openExchangeRates.getPollingMarketDataService();
// Get the latest ticker data showing BTC to EUR
OERRates oERRates = oERMarketDataServiceRaw.getOERTicker();
System.out.println(oERRates.toString());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy