View Javadoc
1   package org.oxerr.peatio.rest.service.polling;
2   
3   import static org.oxerr.peatio.rest.PeatioAdapters.adaptMarketId;
4   import static org.oxerr.peatio.rest.PeatioAdapters.adaptOpenOrders;
5   import static org.oxerr.peatio.rest.PeatioAdapters.adaptSide;
6   import static org.oxerr.peatio.rest.PeatioAdapters.adaptUserTrades;
7   
8   import java.io.IOException;
9   import java.util.LinkedHashMap;
10  import java.util.Map;
11  
12  import org.oxerr.peatio.rest.PeatioAdapters;
13  import org.oxerr.peatio.rest.PeatioException;
14  import org.oxerr.peatio.rest.dto.Market;
15  import org.oxerr.peatio.rest.dto.Order;
16  import org.oxerr.peatio.rest.dto.Trade;
17  import org.slf4j.Logger;
18  import org.slf4j.LoggerFactory;
19  
20  import com.xeiam.xchange.Exchange;
21  import com.xeiam.xchange.dto.trade.LimitOrder;
22  import com.xeiam.xchange.dto.trade.MarketOrder;
23  import com.xeiam.xchange.dto.trade.OpenOrders;
24  import com.xeiam.xchange.dto.trade.UserTrades;
25  import com.xeiam.xchange.service.polling.trade.PollingTradeService;
26  import com.xeiam.xchange.service.polling.trade.params.TradeHistoryParams;
27  import com.xeiam.xchange.service.polling.trade.params.TradeHistoryParamsAll;
28  
29  /**
30   * Trade service.
31   */
32  public class PeatioTradeService extends PeatioTradeServiceRaw implements
33  		PollingTradeService {
34  
35  	private final Logger log = LoggerFactory.getLogger(PeatioTradeService.class);
36  
37  	public PeatioTradeService(Exchange exchange) {
38  		super(exchange);
39  	}
40  
41  	/**
42  	 * {@inheritDoc}
43  	 */
44  	@Override
45  	public OpenOrders getOpenOrders() throws PeatioException, IOException {
46  		Market[] markets = getMarkets();
47  		Map<Market, Order[]> ordersMap = new LinkedHashMap<>(markets.length);
48  		for (Market market : markets) {
49  			Order[] orders = getOrders(market.getId(), null, null, null, null);
50  			ordersMap.put(market, orders);
51  		}
52  		return adaptOpenOrders(ordersMap);
53  	}
54  
55  	/**
56  	 * {@inheritDoc}
57  	 */
58  	@Override
59  	public String placeMarketOrder(MarketOrder marketOrder)
60  			throws PeatioException, IOException {
61  		String market = adaptMarketId(marketOrder.getCurrencyPair());
62  		String side = adaptSide(marketOrder.getType());
63  		Order order = placeOrder(market, side, marketOrder.getTradableAmount(),
64  				null, "market");
65  		log.debug("order: {}", order);
66  		return String.valueOf(order.getId());
67  	}
68  
69  	/**
70  	 * {@inheritDoc}
71  	 */
72  	@Override
73  	public String placeLimitOrder(LimitOrder limitOrder)
74  			throws PeatioException, IOException {
75  		String market = adaptMarketId(limitOrder.getCurrencyPair());
76  		String side = adaptSide(limitOrder.getType());
77  		Order order = placeOrder(market, side, limitOrder.getTradableAmount(),
78  				limitOrder.getLimitPrice(), "limit");
79  		log.debug("order: {}", order);
80  		return String.valueOf(order.getId());
81  	}
82  
83  	/**
84  	 * {@inheritDoc}
85  	 */
86  	@Override
87  	public boolean cancelOrder(String orderId) throws PeatioException,
88  			IOException {
89  		Order order = deleteOrder(Long.parseLong(orderId));
90  		log.debug("order: {}", order);
91  
92  		// the state before we deleting it, is 'wait', that means we cancelled it successfully.
93  		return order.getState().equals("wait");
94  	}
95  
96  	/**
97  	 * {@inheritDoc}
98  	 */
99  	@Override
100 	public UserTrades getTradeHistory(Object... arguments)
101 			throws PeatioException, IOException {
102 		Market[] markets = getMarkets();
103 		Map<Market, Trade[]> ordersMap = new LinkedHashMap<>(markets.length);
104 		for (Market market : markets) {
105 			Trade[] trades = getMyTrades(market.getId(), null, null, null, null, null);
106 			ordersMap.put(market, trades);
107 		}
108 		return adaptUserTrades(ordersMap);
109 
110 
111 	}
112 
113 	/**
114 	 * {@inheritDoc}
115 	 */
116 	@Override
117 	public UserTrades getTradeHistory(TradeHistoryParams params)
118 			throws PeatioException, IOException {
119 		PeatioTradeHistoryParams p = (PeatioTradeHistoryParams) params;
120 		String market = PeatioAdapters.adaptMarketId(p.getCurrencyPair());
121 		Integer limit = p.getPageLength();
122 		Long timestamp = p.getEndTime() == null ? null : p.getEndTime().getTime();
123 		Long from = p.getStartId() == null ? null : Long.valueOf(p.getStartId());
124 		Long to = p.getEndId() == null ? null : Long.valueOf(p.getEndId());
125 		String orderBy = p.getOrderBy();
126 		return adaptUserTrades(p.getCurrencyPair(),
127 				getMyTrades(market, limit, timestamp, from, to, orderBy));
128 	}
129 
130 	/**
131 	 * {@inheritDoc}
132 	 */
133 	@Override
134 	public TradeHistoryParams createTradeHistoryParams() {
135 		return new TradeHistoryParamsAll();
136 	}
137 
138 	public static class PeatioTradeHistoryParams extends TradeHistoryParamsAll {
139 
140 		private String orderBy;
141 
142 		public String getOrderBy() {
143 			return orderBy;
144 		}
145 
146 		public void setOrderBy(String orderBy) {
147 			this.orderBy = orderBy;
148 		}
149 
150 	}
151 
152 }