View Javadoc
1   package org.oxerr.okcoin.examples.rest;
2   
3   import java.io.IOException;
4   import java.math.BigDecimal;
5   import java.util.Arrays;
6   
7   import org.knowm.xchange.Exchange;
8   import org.knowm.xchange.ExchangeFactory;
9   import org.knowm.xchange.ExchangeSpecification;
10  import org.knowm.xchange.currency.CurrencyPair;
11  import org.knowm.xchange.dto.Order.OrderType;
12  import org.knowm.xchange.service.trade.TradeService;
13  import org.oxerr.okcoin.rest.OKCoinExchange;
14  import org.oxerr.okcoin.rest.dto.IcebergOrder;
15  import org.oxerr.okcoin.rest.dto.IcebergOrderHistory;
16  import org.oxerr.okcoin.rest.service.OKCoinTradeServiceRaw;
17  import org.slf4j.Logger;
18  import org.slf4j.LoggerFactory;
19  
20  /**
21   * Demonstration of iceberg orders management.
22   */
23  public class IcebergOrderDemo {
24  
25  	private final Logger log = LoggerFactory.getLogger(IcebergOrderDemo.class);
26  
27  	private final TradeService tradeService;
28  	private final OKCoinTradeServiceRaw rawTradeService;
29  	private final CurrencyPair currencyPair;
30  
31  	public IcebergOrderDemo(Exchange exchange, CurrencyPair currencyPair) {
32  		tradeService = exchange.getTradeService();
33  		rawTradeService = (OKCoinTradeServiceRaw) tradeService;
34  		this.currencyPair = currencyPair;
35  	}
36  
37  	public void demoGetIcebergOrderHistory() throws IOException {
38  		int page = 1;
39  		IcebergOrderHistory icebergOrderHistory;
40  		do {
41  			log.info("page: {}", page);
42  			icebergOrderHistory = rawTradeService.getIcebergOrderHistory(currencyPair, page);
43  			logOrders(icebergOrderHistory.getOrders());
44  			page++;
45  		} while(icebergOrderHistory.hasNextPage() && page < 10);
46  	}
47  
48  	public void demoGetOpenIcebergOrders() throws IOException {
49  		IcebergOrder[] icebergOrders = rawTradeService.getOpenIcebergOrders(currencyPair).getOrders();
50  		logOrders(icebergOrders);
51  	}
52  
53  	private void logOrders(IcebergOrder[] icebergOrders) {
54  		Arrays.stream(icebergOrders).forEach(
55  			o -> {
56  				log.info("ID: {}, Date: {}, Side: {}, Total Order Amount: {}, Average Order Amount: {}, Price Variance: {},  Highest/Lowest Price: {}, Filled: {}, Status: {}.",
57  					o.getId(),
58  					o.getDate(),
59  					o.getSide(),
60  					o.getTradeValue(),
61  					o.getSingleAvg(),
62  					o.getDepthRange(),
63  					o.getProtectedPrice(),
64  					o.getFilled(),
65  					o.getStatus());
66  			}
67  		);
68  	}
69  
70  	public void demoBuyIcebergOrder() throws IOException {
71  		BigDecimal totalOrderAmount = new BigDecimal("100"); // CNY
72  		BigDecimal averageOrderAmount = new BigDecimal("1"); // CNY
73  		BigDecimal priceVariance = new BigDecimal("0.1");
74  		BigDecimal highestBuyPrice = new BigDecimal("1"); // CNY
75  
76  		long id = rawTradeService.placeIcebergOrder(currencyPair, OrderType.BID,
77  			totalOrderAmount, averageOrderAmount, priceVariance, highestBuyPrice);
78  		log.info("buy iceberg order ID: {}", id);
79  	}
80  
81  	public void demoSellIcebergOrder() throws IOException {
82  		BigDecimal totalOrderAmount = new BigDecimal("0.01"); // BTC
83  		BigDecimal averageOrderAmount = new BigDecimal("0.01"); // BTC
84  		BigDecimal priceVariance = new BigDecimal("0.1");
85  		BigDecimal lowestSellPrice = new BigDecimal(9999_9999); // CNY
86  
87  		long id = rawTradeService.placeIcebergOrder(currencyPair, OrderType.ASK,
88  				totalOrderAmount, averageOrderAmount, priceVariance, lowestSellPrice);
89  		log.info("buy iceberg order ID: {}", id);
90  	}
91  
92  	public void demoCancelIcebergOrder(long id) throws IOException {
93  		boolean cancelled = rawTradeService.cancelIcebergeOrder(currencyPair, id);
94  		log.info("cancel iceberg order. ID: {}, result: {}.", id, cancelled);
95  	}
96  
97  	public static void main(String[] args) throws IOException {
98  		String username = args[0], password = args[1],
99  			tradePassword = args.length > 2 ? args[2] : null;
100 
101 		ExchangeSpecification spec = new ExchangeSpecification(OKCoinExchange.class);
102 		spec.setUserName(username);
103 		spec.setPassword(password);
104 		spec.setExchangeSpecificParametersItem(OKCoinExchange.TRADE_PASSWORD_PARAMETER, tradePassword);
105 
106 		Exchange domesticExchange = ExchangeFactory.INSTANCE.createExchange(spec);
107 		IcebergOrderDemo icebergOrderDemo = new IcebergOrderDemo(domesticExchange, CurrencyPair.BTC_CNY);
108 
109 		// icebergOrderDemo.demoBuyIcebergOrder();
110 		// icebergOrderDemo.demoSellIcebergOrder();
111 
112 		icebergOrderDemo.demoGetOpenIcebergOrders();
113 		icebergOrderDemo.demoGetIcebergOrderHistory();
114 
115 		// icebergOrderDemo.demoCancelIcebergOrder(12813L);
116 	}
117 
118 }