View Javadoc
1   package org.oxerr.okcoin.rest.service;
2   
3   import java.io.IOException;
4   import java.util.ArrayList;
5   import java.util.Arrays;
6   import java.util.Collection;
7   import java.util.List;
8   
9   import org.knowm.xchange.Exchange;
10  import org.knowm.xchange.currency.CurrencyPair;
11  import org.knowm.xchange.dto.Order;
12  import org.knowm.xchange.dto.Order.OrderType;
13  import org.knowm.xchange.dto.trade.LimitOrder;
14  import org.knowm.xchange.dto.trade.MarketOrder;
15  import org.knowm.xchange.dto.trade.OpenOrders;
16  import org.knowm.xchange.dto.trade.UserTrades;
17  import org.knowm.xchange.exceptions.ExchangeException;
18  import org.knowm.xchange.exceptions.NotAvailableFromExchangeException;
19  import org.knowm.xchange.exceptions.NotYetImplementedForExchangeException;
20  import org.knowm.xchange.service.trade.TradeService;
21  import org.knowm.xchange.service.trade.params.DefaultTradeHistoryParamPaging;
22  import org.knowm.xchange.service.trade.params.TradeHistoryParamCurrencyPair;
23  import org.knowm.xchange.service.trade.params.TradeHistoryParamPaging;
24  import org.knowm.xchange.service.trade.params.TradeHistoryParams;
25  import org.knowm.xchange.service.trade.params.orders.DefaultOpenOrdersParamCurrencyPair;
26  import org.knowm.xchange.service.trade.params.orders.OpenOrdersParams;
27  import org.oxerr.okcoin.rest.OKCoinAdapters;
28  import org.oxerr.okcoin.rest.OKCoinException;
29  import org.oxerr.okcoin.rest.dto.CancelOrderResult;
30  import org.oxerr.okcoin.rest.dto.OrderHistory;
31  import org.oxerr.okcoin.rest.dto.OrderResult;
32  import org.oxerr.okcoin.rest.dto.Type;
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  
36  /**
37   * {@link TradeService} implementation.
38   */
39  public class OKCoinTradeService extends OKCoinTradeServiceRaw implements
40  		TradeService {
41  
42  	private final Logger log = LoggerFactory.getLogger(OKCoinTradeService.class);
43  
44  	public OKCoinTradeService(Exchange exchange) {
45  		super(exchange);
46  	}
47  
48  	/**
49  	 * {@inheritDoc}
50  	 */
51  	@Override
52  	public OpenOrders getOpenOrders() throws OKCoinException, IOException {
53  		Collection<CurrencyPair> symbols = this.exchange.getExchangeSymbols();
54  		List<OrderResult> orderResults = new ArrayList<>(symbols.size());
55  
56  		for (CurrencyPair symbol : symbols) {
57  			log.debug("Getting order: {}", symbol);
58  			OrderResult orderResult = getOrder(
59  				OKCoinAdapters.adaptSymbol(symbol), -1);
60  			orderResults.add(orderResult);
61  		}
62  
63  		return OKCoinAdapters.adaptOpenOrders(orderResults);
64  	}
65  
66  	/**
67  	 * {@inheritDoc}
68  	 */
69  	@Override
70  	public OpenOrders getOpenOrders(OpenOrdersParams params)
71  			throws ExchangeException, NotAvailableFromExchangeException,
72  			NotYetImplementedForExchangeException, IOException {
73  		DefaultOpenOrdersParamCurrencyPair p = (DefaultOpenOrdersParamCurrencyPair) params;
74  		OrderResult orderResult = getOrder(OKCoinAdapters.adaptSymbol(p.getCurrencyPair()), -1);
75  		return OKCoinAdapters.adaptOpenOrders(Arrays.asList(orderResult));
76  	}
77  
78  	/**
79  	 * {@inheritDoc}
80  	 */
81  	@Override
82  	public DefaultOpenOrdersParamCurrencyPair createOpenOrdersParams() {
83  		return new DefaultOpenOrdersParamCurrencyPair();
84  	}
85  
86  	/**
87  	 * {@inheritDoc}
88  	 */
89  	@Override
90  	public String placeMarketOrder(MarketOrder marketOrder)
91  			throws ExchangeException, NotAvailableFromExchangeException,
92  			NotYetImplementedForExchangeException, IOException {
93  		throw new NotYetImplementedForExchangeException();
94  	}
95  
96  	/**
97  	 * {@inheritDoc}
98  	 */
99  	@Override
100 	public String placeLimitOrder(LimitOrder limitOrder)
101 			throws OKCoinException, IOException {
102 		long orderId = trade(OKCoinAdapters.adaptSymbol(limitOrder.getCurrencyPair()),
103 				limitOrder.getType() == OrderType.BID ? Type.BUY : Type.SELL,
104 				limitOrder.getLimitPrice(),
105 				limitOrder.getTradableAmount())
106 				.getOrderId();
107 		return String.valueOf(orderId);
108 	}
109 
110 	/**
111 	 * {@inheritDoc}
112 	 */
113 	@Override
114 	public boolean cancelOrder(String orderId) throws OKCoinException,
115 			IOException {
116 		boolean ret = false;
117 		long id = Long.parseLong(orderId);
118 
119 		for (CurrencyPair symbol : this.exchange.getExchangeSymbols()) {
120 			try {
121 				CancelOrderResult cancelResult = cancelOrder(
122 						OKCoinAdapters.adaptSymbol(symbol), id);
123 
124 				if (id == cancelResult.getOrderId()) {
125 					ret = true;
126 				}
127 				break;
128 			} catch (OKCoinException e) {
129 				if (e.getErrorCode() == 10009) {
130 					// order not found.
131 					continue;
132 				}
133 			}
134 		}
135 		return ret;
136 	}
137 
138 	@Deprecated
139 	public UserTrades getTradeHistory(Object... arguments)
140 			throws OKCoinException, IOException {
141 		int argc = arguments.length;
142 
143 		CurrencyPair currencyPair = argc > 0 ? (CurrencyPair) arguments[0] : null;
144 		if (currencyPair == null) {
145 			throw new IllegalArgumentException();
146 		}
147 
148 		int currentPage = argc > 1 ? ((Number) arguments[1]).intValue() : 0;
149 		int pageLength = argc > 2 ? ((Number) arguments[2]).intValue() : 200;
150 
151 		String symbol = OKCoinAdapters.adaptSymbol(currencyPair);
152 		int status = 1; // 1 for filled orders
153 		OrderHistory history = getOrderHistory(symbol, status, currentPage, pageLength);
154 		return OKCoinAdapters.adaptUserTrades(history);
155 	}
156 
157 	/**
158 	 * {@inheritDoc}
159 	 */
160 	@Override
161 	public UserTrades getTradeHistory(TradeHistoryParams params)
162 			throws OKCoinException, IOException {
163 		OKCoinTradeHistoryParams p = (OKCoinTradeHistoryParams) params;
164 
165 		String symbol = OKCoinAdapters.adaptSymbol(p.getCurrencyPair());
166 		int status = 1; //  1 for filled orders
167 		int currentPage = p.getPageNumber();
168 		int pageLength = p.getPageLength();
169 
170 		OrderHistory history = getOrderHistory(symbol, status, currentPage, pageLength);
171 		return OKCoinAdapters.adaptUserTrades(history);
172 	}
173 
174 	/**
175 	 * {@inheritDoc}
176 	 */
177 	@Override
178 	public TradeHistoryParams createTradeHistoryParams() {
179 		return new OKCoinTradeHistoryParams();
180 	}
181 
182 	public static class OKCoinTradeHistoryParams extends
183 			DefaultTradeHistoryParamPaging implements
184 			TradeHistoryParamCurrencyPair, TradeHistoryParamPaging {
185 
186 		private CurrencyPair pair;
187 
188 		/**
189 		 * {@inheritDoc}
190 		 */
191 		@Override
192 		public void setCurrencyPair(CurrencyPair pair) {
193 			this.pair = pair;
194 		}
195 
196 		/**
197 		 * {@inheritDoc}
198 		 */
199 		@Override
200 		public CurrencyPair getCurrencyPair() {
201 			return pair;
202 		}
203 
204 	}
205 
206 	/**
207 	 * {@inheritDoc}
208 	 */
209 	@Override
210 	public Collection<Order> getOrder(String... orderIds)
211 			throws ExchangeException, NotAvailableFromExchangeException,
212 			NotYetImplementedForExchangeException, IOException {
213 		throw new NotYetImplementedForExchangeException();
214 	}
215 
216 }