View Javadoc
1   package org.oxerr.huobi.websocket.dto.response.payload;
2   
3   import java.math.BigDecimal;
4   
5   import org.oxerr.huobi.websocket.dto.Depth;
6   import org.oxerr.huobi.websocket.dto.DepthDiff;
7   import org.oxerr.huobi.websocket.dto.response.historydata.ReqMarketDepthTopResponse;
8   
9   /**
10   * Payload of {@link ReqMarketDepthTopResponse}.
11   */
12  public class ReqMarketDepthTopPayload extends AbstractPayload implements Depth {
13  
14  	private final String symbolId;
15  	private final long time;
16  	private long version;
17  
18  	private final String bidName;
19  	private BigDecimal[] bidPrice;
20  	private BigDecimal[] bidTotal;
21  	private BigDecimal[] bidAmount;
22  
23  	private final String askName;
24  	private BigDecimal[] askPrice;
25  	private BigDecimal[] askTotal;
26  	private BigDecimal[] askAmount;
27  
28  	/**
29  	 * Top条行情深度
30  	 *
31  	 * @param symbolId 交易代码
32  	 * @param time 时间
33  	 * @param version 快照版本,方便进行差量更新,如果终端的最后快照版本不一致,则需要重新获取最新完整行情深度。
34  	 * @param bidName 买单文字描述
35  	 * @param bidPrice 买单价格
36  	 * @param bidTotal 累计买单量
37  	 * @param bidAmount 买单量
38  	 * @param askName 卖单文字描述
39  	 * @param askPrice 卖单价格
40  	 * @param askTotal 累计卖单量
41  	 * @param askAmount 卖单量
42  	 */
43  	public ReqMarketDepthTopPayload(String symbolId, long time, long version,
44  
45  	String bidName, BigDecimal[] bidPrice, BigDecimal[] bidTotal,
46  			BigDecimal[] bidAmount,
47  
48  			String askName, BigDecimal[] askPrice, BigDecimal[] askTotal,
49  			BigDecimal[] askAmount
50  
51  	) {
52  		this.symbolId = symbolId;
53  		this.time = time;
54  		this.version = version;
55  		this.bidName = bidName;
56  		this.bidPrice = bidPrice;
57  		this.bidTotal = bidTotal;
58  		this.bidAmount = bidAmount;
59  		this.askName = askName;
60  		this.askPrice = askPrice;
61  		this.askTotal = askTotal;
62  		this.askAmount = askAmount;
63  	}
64  
65  	@Override
66  	public String getSymbolId() {
67  		return symbolId;
68  	}
69  
70  	@Override
71  	public long getTime() {
72  		return time;
73  	}
74  
75  	@Override
76  	public long getVersion() {
77  		return version;
78  	}
79  
80  	public String getBidName() {
81  		return bidName;
82  	}
83  
84  	@Override
85  	public BigDecimal[] getBidPrice() {
86  		return bidPrice;
87  	}
88  
89  	public BigDecimal[] getBidTotal() {
90  		return bidTotal;
91  	}
92  
93  	@Override
94  	public BigDecimal[] getBidAmount() {
95  		return bidAmount;
96  	}
97  
98  	public String getAskName() {
99  		return askName;
100 	}
101 
102 	@Override
103 	public BigDecimal[] getAskPrice() {
104 		return askPrice;
105 	}
106 
107 	public BigDecimal[] getAskTotal() {
108 		return askTotal;
109 	}
110 
111 	@Override
112 	public BigDecimal[] getAskAmount() {
113 		return askAmount;
114 	}
115 
116 	@Override
117 	public void merge(DepthDiff diff) {
118 		if (diff.getVersionOld() != this.getVersion()) {
119 			throw new IllegalArgumentException(
120 					String.format("Mismatched version. Depth version is %s, but diff expected old version is %s.",
121 							diff.getVersionOld(), this.getVersion()));
122 		}
123 
124 		this.version = diff.getVersion();
125 
126 		// bid update
127 		for (int i = 0, l = diff.getBidUpdate().getRow().length; i < l; i++) {
128 			BigDecimal price = diff.getBidUpdate().getPrice()[i];
129 			BigDecimal amount = diff.getBidUpdate().getAmount()[i];
130 			int row = diff.getBidUpdate().getRow()[i];
131 
132 			this.bidPrice[row] = price;
133 			this.bidAmount[row] = amount;
134 			this.bidTotal[row] = price.multiply(amount);
135 		}
136 
137 		// bid delete
138 		for (int i = 0, l = diff.getBidDelete().length; i < l; i++) {
139 			int row = diff.getBidDelete()[i];
140 
141 			this.bidPrice[row] = null;
142 			this.bidAmount[row] = null;
143 			this.bidTotal[row] = null;
144 		}
145 
146 		// bid insert
147 		for (int i = 0, l = diff.getBidInsert().getRow().length; i < l; i++) {
148 			BigDecimal price = diff.getBidInsert().getPrice()[i];
149 			BigDecimal amount = diff.getBidInsert().getAmount()[i];
150 			int row = diff.getBidInsert().getRow()[i];
151 
152 			this.bidPrice[row] = price;
153 			this.bidAmount[row] = amount;
154 			this.bidTotal[row] = price.multiply(amount);
155 		}
156 
157 		// ask update
158 		for (int i = 0, l = diff.getAskUpdate().getRow().length; i < l; i++) {
159 			BigDecimal price = diff.getAskUpdate().getPrice()[i];
160 			BigDecimal amount = diff.getAskUpdate().getAmount()[i];
161 			int row = diff.getAskUpdate().getRow()[i];
162 
163 			this.askPrice[row] = price;
164 			this.askAmount[row] = amount;
165 			this.askTotal[row] = price.multiply(amount);
166 		}
167 
168 		// ask delete
169 		for (int i = 0, l = diff.getAskDelete().length; i < l; i++) {
170 			int row = diff.getAskDelete()[i];
171 
172 			this.askPrice[row] = null;
173 			this.askAmount[row] = null;
174 			this.askTotal[row] = null;
175 		}
176 
177 		// ask insert
178 		for (int i = 0, l = diff.getAskInsert().getRow().length; i < l; i++) {
179 			BigDecimal price = diff.getAskInsert().getPrice()[i];
180 			BigDecimal amount = diff.getAskInsert().getAmount()[i];
181 			int row = diff.getAskInsert().getRow()[i];
182 
183 			this.askPrice[row] = price;
184 			this.askAmount[row] = amount;
185 			this.askTotal[row] = price.multiply(amount);
186 		}
187 	}
188 
189 }