View Javadoc
1   package org.oxerr.huobi.xchange.service.streaming;
2   
3   import static com.xeiam.xchange.dto.Order.OrderType.ASK;
4   import static com.xeiam.xchange.dto.Order.OrderType.BID;
5   
6   import java.util.ArrayList;
7   import java.util.Date;
8   import java.util.List;
9   
10  import org.oxerr.huobi.websocket.dto.Depth;
11  import org.oxerr.huobi.websocket.dto.TradeDetail;
12  import org.oxerr.huobi.websocket.dto.response.marketdata.payload.MarketOverviewPayload;
13  
14  import com.xeiam.xchange.currency.CurrencyPair;
15  import com.xeiam.xchange.dto.Order.OrderType;
16  import com.xeiam.xchange.dto.marketdata.OrderBook;
17  import com.xeiam.xchange.dto.marketdata.Ticker;
18  import com.xeiam.xchange.dto.marketdata.Trade;
19  import com.xeiam.xchange.dto.trade.LimitOrder;
20  
21  /**
22   * Various adapters for converting from Huobi WebSocket DTOs to XChange DTOs.
23   */
24  public final class HuobiSocketIOAdapters {
25  
26  	private HuobiSocketIOAdapters() {
27  	}
28  
29  	public static String adaptSymbol(CurrencyPair currencyPair) {
30  		return (currencyPair.baseSymbol + currencyPair.counterSymbol)
31  			.toLowerCase();
32  	}
33  
34  	public static CurrencyPair adaptCurrencyPair(String symbolId) {
35  		return new CurrencyPair(symbolId.substring(0, 3).toUpperCase(),
36  				symbolId.substring(3, 6).toUpperCase());
37  	}
38  
39  	public static OrderType adaptOrderType(int direction) {
40  		OrderType type;
41  		switch (direction) {
42  		case 1:
43  			type = BID;
44  			break;
45  		case 2:
46  			type = ASK;
47  			break;
48  		default:
49  			type = BID;
50  			break;
51  		}
52  		return type;
53  	}
54  
55  	public static Ticker adaptTicker(MarketOverviewPayload marketOverviewPayload) {
56  		return new Ticker.Builder()
57  			.timestamp(new Date())
58  			.currencyPair(adaptCurrencyPair(marketOverviewPayload.getSymbolId()))
59  			.last(marketOverviewPayload.getPriceNew())
60  			.high(marketOverviewPayload.getPriceHigh())
61  			.low(marketOverviewPayload.getPriceLow())
62  			.ask(marketOverviewPayload.getPriceAsk())
63  			.bid(marketOverviewPayload.getPriceBid())
64  			.volume(marketOverviewPayload.getTotalAmount())
65  			.build();
66  	}
67  
68  	public static OrderBook adaptOrderBook(Depth depth) {
69  		Date timeStamp = new Date(depth.getVersion());
70  		List<LimitOrder> asks = new ArrayList<>(depth.getAskPrice().length);
71  		List<LimitOrder> bids = new ArrayList<>(depth.getBidPrice().length);
72  		CurrencyPair currencyPair = adaptCurrencyPair(depth.getSymbolId());
73  
74  		// asks should be sorted ascending
75  		for (int i = 0, l = depth.getAskPrice().length; i < l; i++) {
76  			LimitOrder limitOrder = new LimitOrder.Builder(ASK, currencyPair)
77  				.limitPrice(depth.getAskPrice()[i])
78  				.tradableAmount(depth.getAskAmount()[i])
79  				.build();
80  			asks.add(limitOrder);
81  		}
82  
83  		// bids should be sorted descending
84  		for (int i = 0, l = depth.getBidPrice().length; i < l; i++) {
85  			LimitOrder limitOrder = new LimitOrder.Builder(BID, currencyPair)
86  				.limitPrice(depth.getBidPrice()[i])
87  				.tradableAmount(depth.getBidAmount()[i]).build();
88  			bids.add(limitOrder);
89  		}
90  
91  		OrderBook orderBook = new OrderBook(timeStamp, asks, bids);
92  		return orderBook;
93  	}
94  
95  	public static Trade[] adaptTrades(TradeDetail tradeDetail) {
96  		int length = tradeDetail.getTradeId().length;
97  		Trade[] trades = new Trade[length];
98  		CurrencyPair currencyPair = adaptCurrencyPair(tradeDetail.getSymbolId());
99  		for (int i = 0; i < length; i++) {
100 			trades[i] = new Trade(adaptOrderType(tradeDetail.getDirection()[i]),
101 				tradeDetail.getAmount()[i],
102 				currencyPair,
103 				tradeDetail.getPrice()[i],
104 				new Date(tradeDetail.getTime()[i] * 1000),
105 				String.valueOf(tradeDetail.getTradeId()[i]));
106 		}
107 		return trades;
108 	}
109 
110 }