001 /**
002 * Copyright 2007 Mike Kroutikov.
003 *
004 * This program is free software; you can redistribute it and/or modify
005 * it under the terms of the Lesser GNU General Public License as
006 * published by the Free Software Foundation; either version 3 of
007 * the License, or (at your option) any later version.
008 *
009 * This program is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012 * Lesser GNU General Public License for more details.
013 *
014 * You should have received a copy of the Lesser GNU General Public License
015 * along with this program. If not, see <http://www.gnu.org/licenses/>.
016 *
017 * Derived from code developed by Opentick Corporation, http://www.opentick.com.
018 */
019
020 package org.otfeed.event;
021
022 import static org.otfeed.event.IdentityUtil.equalsTo;
023 import static org.otfeed.event.IdentityUtil.safeCompare;
024 import static org.otfeed.event.IdentityUtil.safeHashCode;
025
026 import java.io.Serializable;
027 import java.util.Date;
028
029 /**
030 * This class provides either a real-time or a historical level I quote from an exchange.
031 * This comes from the exchange when the price or size of either the bid or ask changes.
032 * Both the bid and the ask prices along with their sizes are provided in a quote.
033 */
034 public final class OTQuote implements Comparable<OTQuote>, Serializable {
035
036 private static final long serialVersionUID = -2523292044375141090L;
037
038 private Date timestamp;
039 private int bidSize;
040 private int askSize;
041 private double bidPrice;
042 private double askPrice;
043 private String askExchange;
044 private char indicator;
045 private char tickIndicator;
046 private String bidExchange;
047 private String exchange;
048 private String symbol;
049
050 /**
051 * Default constructor.
052 */
053 public OTQuote() { }
054
055 /**
056 * Constructor.
057 * @param timestamp Time when the event occurred.
058 * @param bidSize Number of round lots in the bid.
059 * @param bidPrice Bid price.
060 * @param askSize Number of round lots in the ask.
061 * @param askPrice Ask price.
062 * @param askExchange Ask exchange identifier.
063 * @param indicator Bid exchange identifier.
064 * @param tickIndicator Indicator.
065 * @param bidExchange Bid exchange identifier.
066 * @param exchange Exchange code.
067 * @param symbol Symbol code.
068 */
069 public OTQuote(Date timestamp,
070 int bidSize,
071 double bidPrice,
072 int askSize,
073 double askPrice,
074 String askExchange,
075 char indicator,
076 char tickIndicator,
077 String bidExchange,
078 String exchange,
079 String symbol) {
080 this.timestamp = timestamp;
081 this.bidSize = bidSize;
082 this.bidPrice = bidPrice;
083 this.askSize = askSize;
084 this.askPrice = askPrice;
085 this.askExchange = askExchange;
086 this.indicator = indicator;
087 this.tickIndicator = tickIndicator;
088 this.bidExchange = bidExchange;
089 this.exchange = exchange;
090 this.symbol = symbol;
091 }
092
093 /**
094 *
095 * @return Time when the event occurred.
096 */
097 public Date getTimestamp() {
098 return timestamp;
099 }
100
101 /**
102 * Sets time of the event.
103 * @param timestamp Time when the event occurred.
104 */
105 public void setTimestamp(Date timestamp) {
106 this.timestamp = timestamp;
107 }
108
109 /**
110 *
111 * @return Number of round lots in the bid.
112 */
113 public int getBidSize() {
114 return bidSize;
115 }
116
117 /**
118 * Sets number of round lots in the bid.
119 * @param bidSize Number of round lots in the bid.
120 */
121 public void setBidSize(int bidSize) {
122 this.bidSize = bidSize;
123 }
124
125 /**
126 *
127 * @return Bid price.
128 */
129 public double getBidPrice() {
130 return bidPrice;
131 }
132
133 /**
134 * Sets bid price.
135 * @param bidPrice Bid price.
136 */
137 public void setBidPrice(double bidPrice) {
138 this.bidPrice = bidPrice;
139 }
140
141 /**
142 *
143 * @return Number of round lots in the ask.
144 */
145 public int getAskSize() {
146 return askSize;
147 }
148
149 /**
150 * Sets number of round lots in the ask.
151 * @param askSize Number of round lots in the ask.
152 */
153 public void setAskSize(int askSize) {
154 this.askSize = askSize;
155 }
156
157 /**
158 *
159 * @return Ask price.
160 */
161 public double getAskPrice() {
162 return askPrice;
163 }
164
165 /**
166 * Sets ask price.
167 * @param askPrice Ask price.
168 */
169 public void setAskPrice(double askPrice) {
170 this.askPrice = askPrice;
171 }
172
173 /**
174 *
175 * @return Ask exchange identifier.
176 */
177 public String getAskExchange() {
178 return askExchange;
179 }
180
181 /**
182 * Stes ask exchange identifier.
183 * @param askExchange Ask exchange identifier.
184 */
185 public void setAskExchange(String askExchange) {
186 this.askExchange = askExchange;
187 }
188
189 /**
190 *
191 * @return Indicator.
192 */
193 public char getIndicator() {
194 return indicator;
195 }
196
197 /**
198 * Sets indicator.
199 * @param indicator Indicator.
200 */
201 public void setIndicator(char indicator) {
202 this.indicator = indicator;
203 }
204
205 /**
206 *
207 * @return Tick indicator: D - Down, U - Up, N - Not provided.
208 */
209 public char getTickIndicator() {
210 return tickIndicator;
211 }
212
213 /**
214 * Sets tick indicator: D - Down, U - Up, N - Not provided.
215 * @param tickIndicator Tick indicator: D - Down, U - Up, N - Not provided.
216 */
217 public void setTickIndicator(char tickIndicator) {
218 this.tickIndicator = tickIndicator;
219 }
220
221 /**
222 *
223 * @return Bid exchange identifier.
224 */
225 public String getBidExchange() {
226 return bidExchange;
227 }
228
229 /**
230 * Sets bidExchange value.
231 */
232 public void setBidExchange(String val) {
233 bidExchange = val;
234 }
235
236 /**
237 *
238 * @return Exchange code.
239 */
240 public String getExchange() {
241 return exchange;
242 }
243
244 /**
245 * Sets exchange code.
246 * @param exchange Exchange code.
247 */
248 public void setExchange(String exchange) {
249 this.exchange = exchange;
250 }
251
252 /**
253 *
254 * @return Symbol code; this field is provided for option chains only, because there can be more than one symbol appropriate to the requested underlyer and expiry date.
255 */
256 public String getSymbol() {
257 return symbol;
258 }
259
260 /**
261 * Symbol code.
262 * @param symbol Symbol code.
263 */
264 public void setSymbol(String symbol) {
265 this.symbol = symbol;
266 }
267
268 @Override
269 public String toString() {
270 return "OTQuote: timestamp=" + timestamp + ", askprice=" + askPrice + ", bidprice=" + bidPrice + ", asksize=" + askSize + ", bidsize=" + bidSize + ", askexchange=" + askExchange + ", indicator=" + indicator + ", tickindicator=" + tickIndicator + ", bidExchange=" + bidExchange + ", exchange=" + exchange + ", symbol=" + symbol;
271 }
272
273 @Override
274 public int hashCode() {
275 return safeHashCode(timestamp)
276 + 3 * safeHashCode(bidSize)
277 + 5 * safeHashCode(askSize)
278 + 7 * safeHashCode(bidPrice)
279 + 11 * safeHashCode(askPrice)
280 + 13 * safeHashCode(askExchange)
281 + 17 * safeHashCode(indicator)
282 + 19 * safeHashCode(tickIndicator)
283 + 23 * safeHashCode(bidExchange)
284 + 29 * safeHashCode(exchange)
285 + 31 * safeHashCode(symbol);
286 }
287
288 @Override
289 public boolean equals(Object o) {
290 return equalsTo(this, o);
291 }
292
293 public int compareTo(OTQuote other) {
294 int rc;
295
296 if((rc = safeCompare(timestamp, other.timestamp)) != 0) {
297 return rc;
298 }
299
300 if((rc = safeCompare(bidSize, other.bidSize)) != 0) {
301 return rc;
302 }
303
304 if((rc = safeCompare(askSize, other.askSize)) != 0) {
305 return rc;
306 }
307
308 if((rc = safeCompare(bidPrice, other.bidPrice)) != 0) {
309 return rc;
310 }
311
312 if((rc = safeCompare(askPrice, other.askPrice)) != 0) {
313 return rc;
314 }
315
316 if((rc = safeCompare(askExchange, other.askExchange)) != 0) {
317 return rc;
318 }
319
320 if((rc = safeCompare(indicator, other.indicator)) != 0) {
321 return rc;
322 }
323
324 if((rc = safeCompare(tickIndicator, other.tickIndicator)) != 0) {
325 return rc;
326 }
327
328 if((rc = safeCompare(bidExchange, other.bidExchange)) != 0) {
329 return rc;
330 }
331
332 if((rc = safeCompare(exchange, other.exchange)) != 0) {
333 return rc;
334 }
335
336 if((rc = safeCompare(symbol, other.symbol)) != 0) {
337 return rc;
338 }
339
340 return 0;
341 }
342 }