View Javadoc
1   package org.oxerr.okcoin.examples.rest;
2   
3   import static org.apache.commons.lang3.builder.ToStringBuilder.reflectionToString;
4   import static org.apache.commons.lang3.builder.ToStringStyle.MULTI_LINE_STYLE;
5   import static org.knowm.xchange.currency.CurrencyPair.BTC_CNY;
6   import static org.knowm.xchange.currency.CurrencyPair.BTC_USD;
7   import static org.knowm.xchange.currency.CurrencyPair.LTC_CNY;
8   import static org.knowm.xchange.currency.CurrencyPair.LTC_USD;
9   
10  import java.io.IOException;
11  import java.util.Arrays;
12  
13  import org.knowm.xchange.Exchange;
14  import org.knowm.xchange.ExchangeFactory;
15  import org.knowm.xchange.ExchangeSpecification;
16  import org.knowm.xchange.currency.CurrencyPair;
17  import org.knowm.xchange.dto.marketdata.OrderBook;
18  import org.knowm.xchange.dto.marketdata.Ticker;
19  import org.knowm.xchange.dto.marketdata.Trades;
20  import org.knowm.xchange.exceptions.ExchangeException;
21  import org.knowm.xchange.service.marketdata.MarketDataService;
22  import org.oxerr.okcoin.rest.OKCoinExchange;
23  import org.oxerr.okcoin.rest.dto.CandlestickChart;
24  import org.oxerr.okcoin.rest.dto.Depth;
25  import org.oxerr.okcoin.rest.dto.TickerResponse;
26  import org.oxerr.okcoin.rest.dto.Trade;
27  import org.oxerr.okcoin.rest.service.OKCoinMarketDataServiceRaw;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  /**
32   * Demonstration of getting market data.
33   */
34  public class MarketDataDemo {
35  
36  	private final Logger log = LoggerFactory.getLogger(MarketDataDemo.class);
37  
38  	private final MarketDataService mdService;
39  	private final OKCoinMarketDataServiceRaw rawMdService;
40  
41  	public MarketDataDemo(Exchange exchange) {
42  		mdService = exchange.getMarketDataService();
43  		rawMdService = (OKCoinMarketDataServiceRaw) mdService;
44  	}
45  
46  	public void demoTicker(CurrencyPair currencyPair) throws IOException {
47  		Ticker ticker = mdService.getTicker(currencyPair);
48  		log.info("{} ticker: {}", currencyPair, reflectionToString(ticker, MULTI_LINE_STYLE));
49  	}
50  
51  	public void demoTicker(String symbol) throws IOException {
52  		TickerResponse response = rawMdService.getTicker(symbol);
53  		log.info("ticker date: {}", response.getDate());
54  		log.info("{} ticker: {}", symbol, reflectionToString(response.getTicker(), MULTI_LINE_STYLE));
55  	}
56  
57  	public void demoDepth(CurrencyPair currencyPair) throws ExchangeException, IOException {
58  		OrderBook orderBook = mdService.getOrderBook(currencyPair);
59  		log.info("{} best bid: {}", currencyPair, orderBook.getBids().get(0));
60  		log.info("{} best ask: {}", currencyPair, orderBook.getAsks().get(0));
61  	}
62  
63  	public void demoDepth(String symbol) throws IOException {
64  		Depth depth = rawMdService.getDepth(symbol, null, null);
65  		log.info("{} best bid: {}@{}", symbol, depth.getBids()[0][1], depth.getBids()[0][0]);
66  		log.info("{} best ask: {}@{}", symbol,
67  				depth.getAsks()[depth.getAsks().length - 1][1],
68  				depth.getAsks()[depth.getAsks().length - 1][0]);
69  	}
70  
71  	public void demoTrades(CurrencyPair currencyPair) throws IOException {
72  		Trades trades = mdService.getTrades(currencyPair);
73  		log.info("trades: {}", trades.getTrades());
74  		log.info("last ID: {}", trades.getlastID());
75  	}
76  
77  	public void demoTrades(String symbol) throws IOException {
78  		Trade[] trades = rawMdService.getTrades(symbol, null);
79  		log.info("trades: {}", Arrays.toString(trades));
80  	}
81  
82  	public void demoCandlestickChart(String symbol, String type, Integer size,
83  			Long since) throws IOException {
84  		CandlestickChart chart = rawMdService.getCandlestickChart(symbol, type, size, since);
85  		log.info("candlestick chart: {}", chart);
86  	}
87  
88  	public static void main(String[] args) throws IOException {
89  		// www.okcoin.cn
90  		Exchange domesticExchange = ExchangeFactory.INSTANCE
91  				.createExchange(OKCoinExchange.class.getName());
92  		MarketDataDemo domesticDemo = new MarketDataDemo(domesticExchange);
93  
94  		domesticDemo.demoTicker(BTC_CNY);
95  		domesticDemo.demoTicker("btc_cny");
96  
97  		domesticDemo.demoTicker(LTC_CNY);
98  		domesticDemo.demoTicker("ltc_cny");
99  
100 		domesticDemo.demoDepth(BTC_CNY);
101 		domesticDemo.demoDepth("btc_cny");
102 
103 		domesticDemo.demoDepth(LTC_CNY);
104 		domesticDemo.demoDepth("ltc_cny");
105 
106 		domesticDemo.demoTrades(BTC_CNY);
107 		domesticDemo.demoTrades("btc_cny");
108 
109 		domesticDemo.demoTrades(LTC_CNY);
110 		domesticDemo.demoTrades("ltc_cny");
111 
112 		domesticDemo.demoCandlestickChart("btc_cny", "1min", 1, null);
113 		domesticDemo.demoCandlestickChart("ltc_cny", "1min", 1, null);
114 
115 		// www.okcoin.com
116 		ExchangeSpecification spec = new ExchangeSpecification(OKCoinExchange.class);
117 		spec.setSslUri("https://www.okcoin.com");
118 		Exchange intlExchange = ExchangeFactory.INSTANCE.createExchange(spec);
119 		MarketDataDemo intlDemo = new MarketDataDemo(intlExchange);
120 
121 		intlDemo.demoTicker(BTC_USD);
122 		intlDemo.demoTicker("btc_usd");
123 
124 		intlDemo.demoTicker(LTC_USD);
125 		intlDemo.demoTicker("ltc_usd");
126 
127 		intlDemo.demoDepth(BTC_USD);
128 		intlDemo.demoDepth("btc_usd");
129 
130 		intlDemo.demoDepth(LTC_USD);
131 		intlDemo.demoDepth("ltc_usd");
132 
133 		intlDemo.demoTrades(BTC_USD);
134 		intlDemo.demoTrades("btc_usd");
135 
136 		intlDemo.demoTrades(LTC_USD);
137 		intlDemo.demoTrades("ltc_usd");
138 
139 		intlDemo.demoCandlestickChart("btc_usd", "1min", 1, null);
140 		intlDemo.demoCandlestickChart("ltc_usd", "1min", 1, null);
141 	}
142 
143 }