Package com.xeiam.xchange.examples.openexchangerates.marketdata

Source Code of com.xeiam.xchange.examples.openexchangerates.marketdata.TickerDemo

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.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());
  }
}
TOP

Related Classes of com.xeiam.xchange.examples.openexchangerates.marketdata.TickerDemo

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.