-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathOptionSeries.java
362 lines (335 loc) · 11.9 KB
/
OptionSeries.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
* !++
* QDS - Quick Data Signalling Library
* !-
* Copyright (C) 2002 - 2021 Devexperts LLC
* !-
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
* If a copy of the MPL was not distributed with this file, You can obtain one at
* http://mozilla.org/MPL/2.0/.
* !__
*/
package com.dxfeed.ipf.option;
import com.devexperts.util.DayUtil;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.TreeSet;
/**
* Series of call and put options with different strike sharing the same attributes of
* expiration, last trading day, spc, multiplies, etc.
*
* <h3>Threads and locks</h3>
*
* This class is <b>NOT</b> thread-safe and cannot be used from multiple threads without external synchronization.
*
* @param <T> The type of option instrument instances.
*/
public final class OptionSeries<T> implements Cloneable, Comparable<OptionSeries<T>> {
int expiration;
int lastTrade;
double multiplier;
double spc;
String additionalUnderlyings;
String mmy;
String optionType;
String expirationStyle;
String settlementStyle;
String cfi;
private final SortedMap<Double, T> calls = new TreeMap<>();
private final SortedMap<Double, T> puts = new TreeMap<>();
private List<Double> strikes;
OptionSeries() {
additionalUnderlyings = "";
mmy = "";
optionType = "";
expirationStyle = "";
settlementStyle = "";
cfi = "";
}
OptionSeries(OptionSeries<T> other) {
this.expiration = other.expiration;
this.lastTrade = other.lastTrade;
this.multiplier = other.multiplier;
this.spc = other.spc;
this.additionalUnderlyings = other.additionalUnderlyings;
this.mmy = other.mmy;
this.optionType = other.optionType;
this.expirationStyle = other.expirationStyle;
this.settlementStyle = other.settlementStyle;
this.cfi = other.cfi;
}
/**
* Returns a shall copy of this option series.
* Collections of calls and puts are copied, but option instrument instances are shared with original.
* @return a shall copy of this option series.
*/
@SuppressWarnings("CloneDoesntCallSuperClone")
@Override
public OptionSeries<T> clone() {
OptionSeries<T> clone = new OptionSeries<>(this);
clone.calls.putAll(calls);
clone.puts.putAll(puts);
return clone;
}
/**
* Returns day id of expiration.
* Example: {@link DayUtil#getDayIdByYearMonthDay DayUtil.getDayIdByYearMonthDay}(20090117).
* @return day id of expiration.
*/
public int getExpiration() {
return expiration;
}
/**
* Returns day id of last trading day.
* Example: {@link DayUtil#getDayIdByYearMonthDay DayUtil.getDayIdByYearMonthDay}(20090116).
* @return day id of last trading day.
*/
public int getLastTrade() {
return lastTrade;
}
/**
* Returns market value multiplier.
* Example: 100, 33.2.
* @return market value multiplier.
*/
public double getMultiplier() {
return multiplier;
}
/**
* Returns shares per contract for options.
* Example: 1, 100.
* @return shares per contract for options.
*/
public double getSPC() {
return spc;
}
/**
* Returns additional underlyings for options, including additional cash.
* It shall use following format:
* <pre>
* <VALUE> ::= <empty> | <LIST>
* <LIST> ::= <AU> | <AU> <semicolon> <space> <LIST>
* <AU> ::= <UNDERLYING> <space> <SPC> </pre>
* the list shall be sorted by <UNDERLYING>.
* Example: "SE 50", "FIS 53; US$ 45.46".
* @return additional underlyings for options, including additional cash.
*/
public String getAdditionalUnderlyings() {
return additionalUnderlyings;
}
/**
* Returns maturity month-year as provided for corresponding FIX tag (200).
* It can use several different formats depending on data source:
* <ul>
* <li>YYYYMM – if only year and month are specified
* <li>YYYYMMDD – if full date is specified
* <li>YYYYMMwN – if week number (within a month) is specified
* </ul>
* @return maturity month-year as provided for corresponding FIX tag (200).
*/
public String getMMY() {
return mmy;
}
/**
* Returns type of option.
* It shall use one of following values:
* <ul>
* <li>STAN = Standard Options
* <li>LEAP = Long-term Equity AnticiPation Securities
* <li>SDO = Special Dated Options
* <li>BINY = Binary Options
* <li>FLEX = FLexible EXchange Options
* <li>VSO = Variable Start Options
* <li>RNGE = Range
* </ul>
* @return type of option.
*/
public String getOptionType() {
return optionType;
}
/**
* Returns expiration cycle style, such as "Weeklys", "Quarterlys".
* @return expiration cycle style.
*/
public String getExpirationStyle() {
return expirationStyle;
}
/**
* Returns settlement price determination style, such as "Open", "Close".
* @return settlement price determination style.
*/
public String getSettlementStyle() {
return settlementStyle;
}
/**
* Returns Classification of Financial Instruments code of this option series.
* It shall use six-letter CFI code from ISO 10962 standard.
* It is allowed to use 'X' extensively and to omit trailing letters (assumed to be 'X').
* See <a href="http://en.wikipedia.org/wiki/ISO_10962">ISO 10962 on Wikipedia</a>.
* It starts with "OX" as both {@link #getCalls() calls} and {@link #getPuts()} puts} are stored in a series.
* @return CFI code.
*/
public String getCFI() {
return cfi;
}
/**
* Returns a sorted map of all calls from strike to a corresponding option instrument.
* @return a sorted map of all calls from strike to a corresponding option instrument.
*/
public SortedMap<Double, T> getCalls() {
return calls;
}
/**
* Returns a sorted map of all puts from strike to a corresponding option instrument.
* @return a sorted map of all puts from strike to a corresponding option instrument.
*/
public SortedMap<Double, T> getPuts() {
return puts;
}
/**
* Returns a list of all strikes in ascending order.
* @return list of all strikes in ascending order.
*/
public List<Double> getStrikes() {
if (strikes == null) {
TreeSet<Double> strikesSet = new TreeSet<>(calls.keySet());
strikesSet.addAll(puts.keySet());
strikes = new ArrayList<>(strikesSet);
}
return strikes;
}
/**
* Returns n strikes the are centered around a specified strike value.
* @param n the maximal number of strikes to return.
* @param strike the center strike.
* @return n strikes the are centered around a specified strike value.
* @throws IllegalArgumentException when {@code n < 0}.
*/
public List<Double> getNStrikesAround(int n, double strike) {
if (n < 0)
throw new IllegalArgumentException();
List<Double> strikes = getStrikes();
int i = Collections.binarySearch(strikes, strike);
if (i < 0)
i = -i - 1;
int from = Math.max(0, i - n / 2);
int to = Math.min(strikes.size(), from + n);
return strikes.subList(from, to);
}
/**
* Compares this option series to another one by its attributes.
* Expiration takes precedence in comparison.
* @param o another option series to compare with.
* @return result of comparison.
*/
@Override
public int compareTo(OptionSeries<T> o) {
if (expiration < o.expiration)
return -1;
if (expiration > o.expiration)
return 1;
if (lastTrade < o.lastTrade)
return -1;
if (lastTrade > o.lastTrade)
return 1;
int i = Double.compare(multiplier, o.multiplier);
if (i != 0)
return i;
i = Double.compare(spc, o.spc);
if (i != 0)
return i;
i = additionalUnderlyings.compareTo(o.additionalUnderlyings);
if (i != 0)
return i;
i = mmy.compareTo(o.mmy);
if (i != 0)
return i;
i = optionType.compareTo(o.optionType);
if (i != 0)
return i;
i = expirationStyle.compareTo(o.expirationStyle);
if (i != 0)
return i;
i = settlementStyle.compareTo(o.settlementStyle);
if (i != 0)
return i;
return cfi.compareTo(o.cfi);
}
/**
* Indicates whether some other object is equal to this option series by its attributes.
* @param o another object to compare with.
*/
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof OptionSeries)) return false;
OptionSeries<?> that = (OptionSeries<?>) o;
return expiration == that.expiration &&
lastTrade == that.lastTrade &&
Double.compare(that.multiplier, multiplier) == 0 &&
Double.compare(that.spc, spc) == 0 &&
additionalUnderlyings.equals(that.additionalUnderlyings) &&
expirationStyle.equals(that.expirationStyle) &&
mmy.equals(that.mmy) &&
optionType.equals(that.optionType) &&
cfi.equals(that.cfi) &&
settlementStyle.equals(that.settlementStyle);
}
/**
* Returns a hash code value for this option series.
* @return a hash code value.
*/
@Override
public int hashCode() {
int result;
long temp;
result = expiration;
result = 31 * result + lastTrade;
temp = multiplier != +0.0d ? Double.doubleToLongBits(multiplier) : 0L;
result = 31 * result + (int) (temp ^ (temp >>> 32));
temp = spc != +0.0d ? Double.doubleToLongBits(spc) : 0L;
result = 31 * result + (int) (temp ^ (temp >>> 32));
result = 31 * result + additionalUnderlyings.hashCode();
result = 31 * result + mmy.hashCode();
result = 31 * result + optionType.hashCode();
result = 31 * result + expirationStyle.hashCode();
result = 31 * result + settlementStyle.hashCode();
result = 31 * result + cfi.hashCode();
return result;
}
void addOption(boolean isCall, double strike, T option) {
SortedMap<Double, T> map = isCall ? calls : puts;
if (map.put(strike, option) == null)
strikes = null; // clear cached strikes list
}
/**
* Returns a string representation of this series.
* @return a string representation of this series.
*/
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("expiration=").append(DayUtil.getYearMonthDayByDayId(expiration));
if (lastTrade != 0)
sb.append(", lastTrade=").append(DayUtil.getYearMonthDayByDayId(lastTrade));
if (multiplier != 0)
sb.append(", multiplier=").append(multiplier);
if (spc != 0)
sb.append(", spc=").append(spc);
if (additionalUnderlyings.length() > 0)
sb.append(", additionalUnderlyings=").append(additionalUnderlyings);
if (mmy.length() > 0)
sb.append(", mmy=").append(mmy);
if (optionType.length() > 0)
sb.append(", optionType=").append(optionType);
if (expirationStyle.length() > 0)
sb.append(", expirationStyle=").append(expirationStyle);
if (settlementStyle.length() > 0)
sb.append(", settlementStyle=").append(settlementStyle);
sb.append(", cfi=").append(cfi);
return sb.toString();
}
}