View Javadoc
1   package org.oxerr.huobi.rest;
2   
3   import static com.xeiam.xchange.currency.Currencies.BTC;
4   import static com.xeiam.xchange.currency.Currencies.CNY;
5   import static com.xeiam.xchange.currency.Currencies.LTC;
6   import static com.xeiam.xchange.dto.Order.OrderType.ASK;
7   import static com.xeiam.xchange.dto.Order.OrderType.BID;
8   import static com.xeiam.xchange.dto.marketdata.Trades.TradeSortType.SortByTimestamp;
9   
10  import java.math.BigDecimal;
11  import java.text.ParseException;
12  import java.util.ArrayList;
13  import java.util.Arrays;
14  import java.util.Date;
15  import java.util.List;
16  
17  import org.apache.commons.lang3.time.DateUtils;
18  import org.oxerr.huobi.rest.dto.marketdata.Depth;
19  import org.oxerr.huobi.rest.dto.marketdata.OrderBookTAS;
20  import org.oxerr.huobi.rest.dto.marketdata.TickerObject;
21  import org.oxerr.huobi.rest.dto.marketdata.TradeObject;
22  import org.oxerr.huobi.rest.dto.trade.Order;
23  import org.oxerr.huobi.rest.dto.trade.PlaceOrderResult;
24  
25  import com.xeiam.xchange.currency.CurrencyPair;
26  import com.xeiam.xchange.dto.Order.OrderType;
27  import com.xeiam.xchange.dto.account.AccountInfo;
28  import com.xeiam.xchange.dto.marketdata.OrderBook;
29  import com.xeiam.xchange.dto.marketdata.Ticker;
30  import com.xeiam.xchange.dto.marketdata.Trade;
31  import com.xeiam.xchange.dto.marketdata.Trades;
32  import com.xeiam.xchange.dto.trade.LimitOrder;
33  import com.xeiam.xchange.dto.trade.Wallet;
34  import com.xeiam.xchange.exceptions.ExchangeException;
35  
36  /**
37   * Various adapters for converting from HUOBI DTOs to XChange DTOs
38   */
39  public final class HuobiAdapters {
40  
41  	private HuobiAdapters() {
42  	}
43  
44  	public static Ticker adaptTicker(
45  			org.oxerr.huobi.rest.dto.marketdata.Ticker huobiTicker,
46  			CurrencyPair currencyPair) {
47  		TickerObject ticker = huobiTicker.getTicker();
48  		return new Ticker.Builder()
49  			.currencyPair(currencyPair)
50  			.last(ticker.getLast())
51  			.bid(ticker.getBuy())
52  			.ask(ticker.getSell())
53  			.high(ticker.getHigh())
54  			.low(ticker.getLow())
55  			.volume(ticker.getVol())
56  			.build();
57  	}
58  
59  	public static OrderBook adaptOrderBook(
60  			Depth huobiDepth,
61  			CurrencyPair currencyPair) {
62  		List<LimitOrder> asks = adaptOrderBook(huobiDepth.getAsks(), ASK, currencyPair);
63  		List<LimitOrder> bids = adaptOrderBook(huobiDepth.getBids(), BID, currencyPair);
64  
65  		return new OrderBook(null, asks, bids);
66  	}
67  
68  	private static List<LimitOrder> adaptOrderBook(
69  			BigDecimal[][] orders,
70  			OrderType type,
71  			CurrencyPair currencyPair) {
72  		List<LimitOrder> limitOrders = new ArrayList<>(orders.length);
73  		for (BigDecimal[] order : orders) {
74  			LimitOrder limitOrder = new LimitOrder(
75  				type, order[1], currencyPair, null, null, order[0]);
76  			limitOrders.add(limitOrder);
77  		}
78  		return limitOrders;
79  	}
80  
81  	public static Trades adaptTrades(
82  			OrderBookTAS huobiDetail,
83  			CurrencyPair currencyPair) {
84  		List<Trade> trades = adaptTrades(huobiDetail.getTrades(), currencyPair);
85  		return new Trades(trades, SortByTimestamp);
86  	}
87  
88  	private static List<Trade> adaptTrades(
89  			TradeObject[] trades,
90  			CurrencyPair currencyPair) {
91  		List<Trade> tradeList = new ArrayList<>(trades.length);
92  		for (TradeObject trade : trades) {
93  			tradeList.add(adaptTrade(trade, currencyPair));
94  		}
95  		return tradeList;
96  	}
97  
98  	private static Trade adaptTrade(
99  			TradeObject trade,
100 			CurrencyPair currencyPair) {
101 		OrderType type = trade.getType().equals("买入") ? BID : ASK;
102 		final Date time;
103 		try {
104 			time = DateUtils.parseDate(trade.getTime(), "HH:mm:ss");
105 		} catch (ParseException e) {
106 			throw new ExchangeException(e.getMessage(), e);
107 		}
108 		return new Trade(
109 				type,
110 				trade.getAmount(),
111 				currencyPair,
112 				trade.getPrice(),
113 				time,
114 				null);
115 	}
116 
117 	public static AccountInfo adaptAccountInfo(org.oxerr.huobi.rest.dto.account.AccountInfo a) {
118 		Wallet cny = new Wallet(CNY,
119 				a.getAvailableCnyDisplay().add(a.getFrozenCnyDisplay()));
120 		Wallet btc = new Wallet(BTC,
121 				a.getAvailableBtcDisplay().add(a.getFrozenBtcDisplay()));
122 		Wallet ltc = new Wallet(LTC,
123 				a.getAvailableLtcDisplay().add(a.getFrozenLtcDisplay()));
124 		List<Wallet> wallets = Arrays.asList(cny, btc, ltc);
125 		return new AccountInfo(null, wallets);
126 	}
127 
128 	public static String adaptPlaceOrderResult(PlaceOrderResult result) {
129 		if (result.getCode() == 0) {
130 			return String.valueOf(result.getId());
131 		} else {
132 			throw new ExchangeException(result.getMsg());
133 		}
134 	}
135 
136 	public static List<LimitOrder> adaptOpenOrders(
137 			Order[] orders,
138 			CurrencyPair currencyPair) {
139 		List<LimitOrder> openOrders = new ArrayList<>();
140 		for (Order order : orders) {
141 			openOrders.add(adaptOpenOrder(order, currencyPair));
142 		}
143 		return openOrders;
144 	}
145 
146 	private static LimitOrder adaptOpenOrder(
147 		Order order, CurrencyPair currencyPair) {
148 		return new LimitOrder(
149 				order.getType() == 1 ? BID : ASK,
150 						order.getOrderAmount().subtract(order.getProcessedAmount()),
151 						currencyPair,
152 						String.valueOf(order.getId()),
153 						new Date(order.getOrderTime() * 1000),
154 						order.getOrderPrice());
155 	}
156 
157 }