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.dto.trade.LimitOrder;
13  import org.knowm.xchange.service.trade.TradeService;
14  import org.oxerr.okcoin.rest.OKCoinException;
15  import org.oxerr.okcoin.rest.OKCoinExchange;
16  import org.oxerr.okcoin.rest.dto.BatchTradeResult;
17  import org.oxerr.okcoin.rest.dto.CancelOrderResult;
18  import org.oxerr.okcoin.rest.dto.OrderData;
19  import org.oxerr.okcoin.rest.dto.TradeResult;
20  import org.oxerr.okcoin.rest.dto.Type;
21  import org.oxerr.okcoin.rest.service.OKCoinTradeServiceRaw;
22  import org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  
25  /**
26   * Demonstration of placing order.
27   */
28  public class TradeDemo {
29  
30  	private final Logger log = LoggerFactory.getLogger(TradeDemo.class);
31  
32  	private final TradeService tradeService;
33  	private final OKCoinTradeServiceRaw rawTradeService;
34  
35  	public TradeDemo(Exchange exchange) {
36  		tradeService = exchange.getTradeService();
37  		rawTradeService = (OKCoinTradeServiceRaw) tradeService;
38  	}
39  
40  	public void demoLimitOrder() throws OKCoinException, IOException {
41  		LimitOrder order = new LimitOrder
42  			.Builder(OrderType.BID, CurrencyPair.BTC_CNY)
43  			.tradableAmount(new BigDecimal("1"))
44  			.limitPrice(new BigDecimal("0.01"))
45  			.build();
46  		// place a limit order 1@0.01
47  		String orderId = tradeService.placeLimitOrder(order);
48  		log.info("placed a limit order: {}", orderId);
49  
50  		// cancel the above order
51  		boolean cancelled = tradeService.cancelOrder(orderId);
52  		log.info("order {} cancellation result: {}", orderId, cancelled);
53  	}
54  
55  	public void demoLimitOrderRaw() throws OKCoinException, IOException {
56  		String symbol = "btc_cny";
57  
58  		// place a limit order 1@0.01
59  		TradeResult ret = rawTradeService.trade("btc_cny", Type.BUY, new BigDecimal("0.01"), new BigDecimal("1"));
60  		long orderId = ret.getOrderId();
61  		log.info("placed a limit order: {}", orderId);
62  
63  		// cancel the above order
64  		CancelOrderResult cxlRet = rawTradeService.cancelOrder(symbol, orderId);
65  		log.info("order {} cancellation result: {}", orderId, cxlRet);
66  	}
67  
68  	public void demoBatchTrade() throws OKCoinException, IOException {
69  		OrderData[] orders = new OrderData[] {
70  			new OrderData(new BigDecimal("0.01"), new BigDecimal("1"), Type.BUY),
71  			new OrderData(new BigDecimal("0.02"), new BigDecimal("2"), Type.BUY),
72  		};
73  		BatchTradeResult result = rawTradeService.batchTrade("btc_cny", Type.BUY, orders);
74  
75  		Arrays.stream(result.getOrderInfo()).forEach(info -> {
76  			try {
77  				cancelOrder(info.getOrderId());
78  			} catch (IOException e) {
79  				log.warn(e.getMessage(), e);
80  			}
81  		});
82  	}
83  
84  	public void cancelOrder(String orderId) throws OKCoinException, IOException {
85  		boolean cancelled = tradeService.cancelOrder(orderId);
86  		log.info("order {} cancellation result: {}", orderId, cancelled);
87  	}
88  
89  	public void cancelOrder(long orderId) throws OKCoinException, IOException {
90  		CancelOrderResult cxlRet = rawTradeService.cancelOrder("btc_cny", orderId);
91  		log.info("order {} cancellation result: {}", orderId, cxlRet);
92  	}
93  
94  	public static void main(String[] args) throws OKCoinException, IOException {
95  		String apiKey = args[0], secretKey = args[1];
96  
97  		ExchangeSpecification spec = new ExchangeSpecification(OKCoinExchange.class);
98  		spec.setApiKey(apiKey);
99  		spec.setSecretKey(secretKey);
100 
101 		Exchange domesticExchange = ExchangeFactory.INSTANCE.createExchange(spec);
102 		TradeDemo tradeDemo = new TradeDemo(domesticExchange);
103 
104 		tradeDemo.demoLimitOrder();
105 		// tradeDemo.demoLimitOrderRaw();
106 
107 		// tradeDemo.demoBatchTrade();
108 	}
109 
110 }