-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptionsIntegrationTest.java
More file actions
237 lines (208 loc) · 9.08 KB
/
Copy pathOptionsIntegrationTest.java
File metadata and controls
237 lines (208 loc) · 9.08 KB
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
package com.marketdata.sdk;
import static org.assertj.core.api.Assertions.assertThat;
import com.marketdata.sdk.options.ExpirationFilter;
import com.marketdata.sdk.options.OptionQuote;
import com.marketdata.sdk.options.OptionSide;
import com.marketdata.sdk.options.OptionsChainRequest;
import com.marketdata.sdk.options.OptionsExpirationsRequest;
import com.marketdata.sdk.options.OptionsLookupRequest;
import com.marketdata.sdk.options.OptionsQuoteRequest;
import com.marketdata.sdk.options.OptionsQuotesRequest;
import com.marketdata.sdk.options.StrikeRange;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
/**
* Integration tests for the {@code options} resource against the live Market Data API. Gated by the
* {@code MARKETDATA_RUN_INTEGRATION_TESTS=true} environment variable in {@code build.gradle.kts}; a
* valid {@code MARKETDATA_TOKEN} is also required (the {@link MarketDataClient} constructor's
* startup validation will surface a clear error if missing).
*
* <p>Tests assert <strong>shape</strong> ("AAPL has at least one expiration", "every quote row
* carries finite greeks") rather than specific values, since the live data drifts daily. AAPL is
* used as the underlying everywhere — it is the largest options market by volume so the response is
* always non-empty during market hours and historical queries are well-populated outside them.
*
* <p>Status is asserted as {@code 200 || 203}: the API returns <strong>203 Non-Authoritative
* Information</strong> when it serves cached/delayed data (outside market hours, or on a
* cached/delayed plan), which the SDK surfaces as a normal success. Pinning to 200 would make these
* tests flap with market hours.
*/
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class OptionsIntegrationTest {
private static final String UNDERLYING = "AAPL";
private MarketDataClient client;
@BeforeAll
void setUp() {
client = new MarketDataClient();
}
@AfterAll
void tearDown() {
if (client != null) {
client.close();
}
}
@Test
void csvExpirationsReturnsRawCsvText() {
CsvResponse resp =
client.options().asCsv().expirations(OptionsExpirationsRequest.of(UNDERLYING));
assertThat(resp.statusCode()).isIn(200, 203);
assertThat(resp.isCsv()).isTrue();
// expirations CSV is a single date column, so assert non-blank text rather than a delimiter.
assertThat(resp.csv()).isNotBlank();
}
@Test
void lookupConvertsHumanDescriptionToOccSymbol() {
// A far-future date keeps the test stable against expiration drift — the endpoint converts
// the description regardless of whether such a contract actually exists today.
OptionsLookupResponse resp =
client.options().lookup(OptionsLookupRequest.of("AAPL 1/16/2026 $200 Call"));
assertThat(resp.statusCode()).isIn(200, 203);
assertThat(resp.values())
.as("OCC symbol shape: 4-6 letter root + YYMMDD + C/P + 8-digit strike")
.matches("[A-Z]{1,6}\\d{6}[CP]\\d{8}");
}
@Test
void expirationsReturnsAtLeastOneFutureDate() {
OptionsExpirationsResponse resp =
client.options().expirations(OptionsExpirationsRequest.of(UNDERLYING));
assertThat(resp.statusCode()).isIn(200, 203);
assertThat(resp.values()).as("AAPL has options expirations year-round").isNotEmpty();
assertThat(resp.updated()).isNotNull();
assertThat(resp.updated().getZone().getId()).isEqualTo("America/New_York");
}
@Test
void chainReturnsFilteredContracts() {
// Light filter: a narrow strike-limit window keeps the response small without depending on
// a specific dte that might fall on a non-trading day.
OptionsChainResponse resp =
client
.options()
.chain(
OptionsChainRequest.builder(UNDERLYING)
.side(OptionSide.CALL)
.strikeLimit(5)
.strikeRange(StrikeRange.ITM)
.build());
assertThat(resp.statusCode()).isIn(200, 203);
assertThat(resp.values()).isNotEmpty();
OptionQuote first = resp.values().get(0);
assertThat(first.optionSymbol()).startsWith(UNDERLYING);
assertThat(first.side()).isEqualTo("call");
assertThat(first.strike()).isGreaterThan(0.0);
}
@Test
void chainExpirationAllSpansMultipleExpirations() {
// expiration=all is the distinguishing case: omitting the filter returns only the front-month
// expiration, whereas all() returns the full chain. strikeLimit(1) keeps it to ~one contract
// per expiration so the payload stays small while still proving the span.
List<OptionQuote> chain =
client
.options()
.chain(
OptionsChainRequest.builder(UNDERLYING)
.expirationFilter(ExpirationFilter.all())
.side(OptionSide.CALL)
.strikeLimit(1)
.build())
.values();
long distinctExpirations = chain.stream().map(OptionQuote::expiration).distinct().count();
assertThat(distinctExpirations)
.as("expiration=all returns every expiration, not just the front-month")
.isGreaterThan(1);
}
@Test
void chainDecodesOptionalRhoColumn() {
// rho is an optional column: the live feed may or may not populate it. Assert the SDK decodes
// whatever comes back without error — every row's rho is either null (omitted) or a finite
// double, never a ParseError from a missing required column.
List<OptionQuote> chain =
client
.options()
.chain(
OptionsChainRequest.builder(UNDERLYING)
.side(OptionSide.CALL)
.strikeLimit(3)
.strikeRange(StrikeRange.ITM)
.build())
.values();
assertThat(chain).isNotEmpty();
for (OptionQuote q : chain) {
Double rho = q.rho();
if (rho != null) {
assertThat(rho.doubleValue()).isFinite();
}
}
}
@Test
void quoteCountbackBoundsHistoricalSeries() {
// countback=N with to caps the EOD series to at most N rows. A front-month ITM AAPL call has
// ample recent history, so we expect between 1 and 5 rows — the upper bound is the real
// assertion that countback reached the wire.
String optionSymbol = sampleOptionSymbol();
OptionsQuotesResponse resp =
client
.options()
.quote(
OptionsQuoteRequest.builder(optionSymbol).to(LocalDate.now()).countback(5).build());
assertThat(resp.values())
.as("countback caps the series to at most 5 rows")
.hasSizeBetween(1, 5);
}
@Test
void quoteFetchesSingleContract() {
// Derive a real option symbol from the chain so the quote endpoint has a live contract to
// resolve — lookup gives a well-formed symbol but doesn't guarantee one exists in the API's
// data set.
String optionSymbol = sampleOptionSymbol();
OptionsQuotesResponse resp = client.options().quote(OptionsQuoteRequest.of(optionSymbol));
assertThat(resp.statusCode()).isIn(200, 203);
assertThat(resp.values()).hasSize(1);
OptionQuote q = resp.values().get(0);
assertThat(q.optionSymbol()).isEqualTo(optionSymbol);
assertThat(q.underlying()).isEqualTo(UNDERLYING);
}
@Test
void quotesFansOutToMultipleContracts() {
// Use the first two contracts from the chain so both are guaranteed to exist.
List<OptionQuote> chain =
client
.options()
.chain(
OptionsChainRequest.builder(UNDERLYING)
.side(OptionSide.CALL)
.strikeLimit(2)
.strikeRange(StrikeRange.ITM)
.build())
.values();
assertThat(chain).hasSizeGreaterThanOrEqualTo(2);
// optionSymbol is @Nullable on the row (every field is, to support columns projection); on a
// live chain it's always present, so assert that and keep the symbols non-null for the fan-out.
String first = java.util.Objects.requireNonNull(chain.get(0).optionSymbol());
String second = java.util.Objects.requireNonNull(chain.get(1).optionSymbol());
Map<String, OptionsQuotesResponse> resp =
client.options().quotes(OptionsQuotesRequest.builder(first, second).build());
assertThat(resp.keySet()).containsExactly(first, second);
assertThat(resp.get(first).values().get(0).optionSymbol()).isEqualTo(first);
assertThat(resp.get(second).values().get(0).optionSymbol()).isEqualTo(second);
}
/** Fetch one real option symbol — used by {@link #quoteFetchesSingleContract()}. */
private String sampleOptionSymbol() {
List<OptionQuote> chain =
client
.options()
.chain(
OptionsChainRequest.builder(UNDERLYING)
.side(OptionSide.CALL)
.strikeLimit(1)
.strikeRange(StrikeRange.ITM)
.build())
.values();
assertThat(chain).isNotEmpty();
return chain.get(0).optionSymbol();
}
}