dxFeed Graal CXX API v4.2.0
Loading...
Searching...
No Matches
OptionSeries.hpp
1// Copyright (c) 2025 Devexperts LLC.
2// SPDX-License-Identifier: MPL-2.0
3
4#pragma once
5
6#include "../../internal/Conf.hpp"
7
9
10#include "../../internal/Common.hpp"
11
12#include <algorithm>
13#include <cstdint>
14#include <map>
15#include <memory>
16#include <set>
17#include <sstream>
18#include <string>
19#include <utility>
20#include <vector>
21
23
24template <typename T> class OptionChainsBuilder;
25
26/**
27 * Series of call and put options with different strike sharing the same attributes of expiration, last trading day,
28 * spc, multiplies, etc.
29 *
30 * <h3>Threads and locks</h3>
31 * This class is <b>NOT</b> thread-safe and cannot be used from multiple threads without external synchronization.
32 *
33 * @tparam T The type of option instrument instances.
34 */
35template <typename T> class OptionSeries final {
36 friend class OptionChainsBuilder<T>;
37
38 std::int32_t expiration_ = 0;
39 std::int32_t lastTrade_ = 0;
40 double multiplier_ = 0.0;
41 double spc_ = 0.0;
42 std::string additionalUnderlyings_{};
43 std::string mmy_{};
44 std::string optionType_{};
45 std::string expirationStyle_{};
46 std::string settlementStyle_{};
47 std::string cfi_{};
48
49 std::map<double, std::shared_ptr<T>> calls_{};
50 std::map<double, std::shared_ptr<T>> puts_{};
51
52 mutable std::vector<double> strikes_{}; // Cached list of strikes
53
54 public:
55 /**
56 * Default constructor for the OptionSeries class.
57 *
58 * @return A default-initialized instance of OptionSeries.
59 */
60 OptionSeries() = default;
61
62 /**
63 * Returns day id of expiration.
64 * Example: @ref day_util::#getDayIdByYearMonthDay() "dxfcpp::day_util::getDayIdByYearMonthDay"(20090117).
65 *
66 * @return day id of expiration.
67 */
68 std::int32_t getExpiration() const {
69 return expiration_;
70 }
71
72 /**
73 * Returns day id of last trading day.
74 * Example: @ref day_util::#getDayIdByYearMonthDay() "dxfcpp::day_util::getDayIdByYearMonthDay"(20090116).
75 *
76 * @return The day id of last trading day.
77 */
78 std::int32_t getLastTrade() const {
79 return lastTrade_;
80 }
81
82 /**
83 * Returns market value multiplier.
84 * Example: 100, 33.2.
85 *
86 * @return The market value multiplier.
87 */
88 double getMultiplier() const {
89 return multiplier_;
90 }
91
92 /**
93 * Returns shares per contract for options.
94 * Example: 1, 100.
95 *
96 * @return The shares per contract for options.
97 */
98 double getSPC() const {
99 return spc_;
100 }
101
102 /**
103 * Returns additional underlyings for options, including additional cash.
104 * It shall use following format:
105 * ```
106 * <VALUE> ::= <empty> | <LIST>
107 * <LIST> ::= <AU> | <AU> <semicolon> <space> <LIST>
108 * <AU> ::= <UNDERLYING> <space> <SPC>
109 * ```
110 * The list shall be sorted by <UNDERLYING>.
111 * Example: "SE 50", "FIS 53; US$ 45.46".
112 *
113 * @return The additional underlyings for options, including additional cash.
114 */
115 const std::string &getAdditionalUnderlyings() const {
116 return additionalUnderlyings_;
117 }
118
119 /**
120 * Returns maturity month-year as provided for corresponding FIX tag (200).
121 * It can use several different formats depending on data source:
122 * <ul>
123 * <li>YYYYMM – if only year and month are specified
124 * <li>YYYYMMDD – if full date is specified
125 * <li>YYYYMMwN – if week number (within a month) is specified
126 * </ul>
127 *
128 * @return The maturity month-year as provided for corresponding FIX tag (200).
129 */
130 const std::string &getMMY() const {
131 return mmy_;
132 }
133
134 /**
135 * Returns type of option.
136 * It shall use one of following values:
137 * <ul>
138 * <li>STAN = Standard Options
139 * <li>LEAP = Long-term Equity AnticiPation Securities
140 * <li>SDO = Special Dated Options
141 * <li>BINY = Binary Options
142 * <li>FLEX = FLexible EXchange Options
143 * <li>VSO = Variable Start Options
144 * <li>RNGE = Range
145 * </ul>
146 *
147 * @return The type of option.
148 */
149 const std::string &getOptionType() const {
150 return optionType_;
151 }
152
153 /**
154 * Returns expiration cycle style, such as "Weeklys", "Quarterlys".
155 *
156 * @return The expiration cycle style.
157 */
158 const std::string &getExpirationStyle() const {
159 return expirationStyle_;
160 }
161
162 /**
163 * Returns settlement price determination style, such as "Open", "Close".
164 *
165 * @return The settlement price determination style.
166 */
167 const std::string &getSettlementStyle() const {
168 return settlementStyle_;
169 }
170
171 /**
172 * Returns Classification of Financial Instruments code.
173 * It is a mandatory field for OPTION instruments as it is the only way to distinguish Call/Put type,
174 * American/European exercise, Cash/Physical delivery.
175 * It shall use six-letter CFI code from ISO 10962 standard.
176 * It is allowed to use 'X' extensively and to omit trailing letters (assumed to be 'X').
177 * See <a href="http://en.wikipedia.org/wiki/ISO_10962">ISO 10962 on Wikipedia</a>.
178 * Example: "ESNTPB", "ESXXXX", "ES" , "OPASPS".
179 *
180 * @return The CFI code.
181 */
182 const std::string &getCFI() const {
183 return cfi_;
184 }
185
186 /**
187 * Returns a sorted map of all calls from strike to a corresponding option instrument.
188 *
189 * @return A sorted map of all calls from strike to a corresponding option instrument.
190 */
191 const std::map<double, std::shared_ptr<T>> &getCalls() const {
192 return calls_;
193 }
194
195 /**
196 * Returns a sorted map of all puts from strike to a corresponding option instrument.
197 *
198 * @return A sorted map of all puts from strike to a corresponding option instrument.
199 */
200 const std::map<double, std::shared_ptr<T>> &getPuts() const {
201 return puts_;
202 }
203
204 /**
205 * Returns a list of all strikes in ascending order.
206 *
207 * @return A list of all strikes in ascending order.
208 */
209 const std::vector<double> &getStrikes() const {
210 if (strikes_.empty()) {
211 std::set<double> strikesSet{};
212
213 for (const auto &call : calls_) {
214 strikesSet.insert(call.first);
215 }
216
217 for (const auto &put : puts_) {
218 strikesSet.insert(put.first);
219 }
220
221 strikes_.assign(strikesSet.begin(), strikesSet.end());
222 }
223
224 return strikes_;
225 }
226
227 /**
228 * Returns n strikes which are centered around a specified strike value.
229 *
230 * @param n The maximal number of strikes to return.
231 * @param strike The center strike.
232 * @return n strikes which are centered around a specified strike value.
233 */
234 std::vector<double> getNStrikesAround(std::size_t n, double strike) const {
235 const auto &strikesVector = getStrikes();
236 const auto it = std::lower_bound(strikesVector.begin(), strikesVector.end(), strike);
237 const std::size_t index = std::distance(strikesVector.begin(), it);
238 const std::size_t fromIndex = (index < n / 2) ? 0 : (index - n / 2);
239 const std::size_t toIndex = std::min(strikesVector.size(), fromIndex + n);
240
241 return std::vector<double>(strikesVector.begin() + fromIndex, strikesVector.begin() + toIndex);
242 }
243
244 bool operator==(const OptionSeries &other) const {
245 return expiration_ == other.expiration_ && lastTrade_ == other.lastTrade_ &&
246 math::equals(multiplier_, other.multiplier_) && math::equals(spc_, other.spc_) &&
247 additionalUnderlyings_ == other.additionalUnderlyings_ && expirationStyle_ == other.expirationStyle_ &&
248 mmy_ == other.mmy_ && optionType_ == other.optionType_ && cfi_ == other.cfi_ &&
249 settlementStyle_ == other.settlementStyle_;
250 }
251
252 bool operator!=(const OptionSeries &other) const {
253 return !(*this == other);
254 }
255
256 bool operator<(const OptionSeries &other) const {
257 if (expiration_ != other.expiration_) {
258 return expiration_ < other.expiration_;
259 }
260
261 if (lastTrade_ != other.lastTrade_) {
262 return lastTrade_ < other.lastTrade_;
263 }
264
265 if (multiplier_ != other.multiplier_) {
266 return multiplier_ < other.multiplier_;
267 }
268
269 if (spc_ != other.spc_) {
270 return spc_ < other.spc_;
271 }
272
273 if (additionalUnderlyings_ != other.additionalUnderlyings_) {
274 return additionalUnderlyings_ < other.additionalUnderlyings_;
275 }
276
277 if (mmy_ != other.mmy_) {
278 return mmy_ < other.mmy_;
279 }
280
281 if (optionType_ != other.optionType_) {
282 return optionType_ < other.optionType_;
283 }
284
285 if (expirationStyle_ != other.expirationStyle_) {
286 return expirationStyle_ < other.expirationStyle_;
287 }
288
289 if (settlementStyle_ != other.settlementStyle_) {
290 return settlementStyle_ < other.settlementStyle_;
291 }
292
293 return cfi_ < other.cfi_;
294 }
295
296 void addOption(bool isCall, double strike, std::shared_ptr<T> option) {
297 if (auto &map = isCall ? calls_ : puts_; map.emplace(strike, option).second) {
298 strikes_.clear();
299 }
300 }
301
302 /**
303 * Returns a string representation of the current object.
304 *
305 * @return A string representation.
306 */
307 std::string toString() const {
308 std::ostringstream ss{};
309
310 ss << "expiration=" << expiration_;
311
312 if (lastTrade_ != 0) {
313 ss << ", lastTrade=" << lastTrade_;
314 }
315
316 if (multiplier_ != 0) {
317 ss << ", multiplier=" << multiplier_;
318 }
319
320 if (spc_ != 0) {
321 ss << ", spc=" << spc_;
322 }
323
324 if (!additionalUnderlyings_.empty()) {
325 ss << ", additionalUnderlyings=" << additionalUnderlyings_;
326 }
327
328 if (!mmy_.empty()) {
329 ss << ", mmy=" << mmy_;
330 }
331
332 if (!optionType_.empty()) {
333 ss << ", optionType=" << optionType_;
334 }
335
336 if (!expirationStyle_.empty()) {
337 ss << ", expirationStyle=" << expirationStyle_;
338 }
339
340 if (!settlementStyle_.empty()) {
341 ss << ", settlementStyle=" << settlementStyle_;
342 }
343
344 ss << ", cfi=" << cfi_;
345
346 return ss.str();
347 }
348
349 friend std::ostream &operator<<(std::ostream &os, const OptionSeries &series) {
350 os << series.toString();
351
352 return os;
353 }
354
355 std::size_t hashCode() const {
356 auto result = static_cast<std::size_t>(expiration_);
357
358 hashCombine(result, lastTrade_);
359 hashCombine(result, multiplier_);
360 hashCombine(result, spc_);
361 hashCombine(result, additionalUnderlyings_);
362 hashCombine(result, mmy_);
363 hashCombine(result, optionType_);
364 hashCombine(result, expirationStyle_);
365 hashCombine(result, settlementStyle_);
366 hashCombine(result, cfi_);
367
368 return result;
369 }
370};
371
373
374template <typename T> struct std::hash<dxfcpp::OptionSeries<T>> {
375 std::size_t operator()(const dxfcpp::OptionSeries<T> &optionSeries) const noexcept {
376 return optionSeries.hashCode();
377 }
378};
379
#define DXFCXX_DISABLE_MSC_WARNINGS_POP()
Definition Conf.hpp:22
#define DXFCPP_END_NAMESPACE
Definition Conf.hpp:70
#define DXFCPP_BEGIN_NAMESPACE
Definition Conf.hpp:67
#define DXFCXX_DISABLE_MSC_WARNINGS_PUSH(warnings)
Definition Conf.hpp:21
Builder class for a set of option chains grouped by product or underlying symbol.
Definition OptionChainsBuilder.hpp:27
std::vector< double > getNStrikesAround(std::size_t n, double strike) const
Returns n strikes which are centered around a specified strike value.
Definition OptionSeries.hpp:234
const std::map< double, std::shared_ptr< T > > & getPuts() const
Returns a sorted map of all puts from strike to a corresponding option instrument.
Definition OptionSeries.hpp:200
const std::map< double, std::shared_ptr< T > > & getCalls() const
Returns a sorted map of all calls from strike to a corresponding option instrument.
Definition OptionSeries.hpp:191
const std::string & getCFI() const
Returns Classification of Financial Instruments code.
Definition OptionSeries.hpp:182
const std::string & getMMY() const
Returns maturity month-year as provided for corresponding FIX tag (200).
Definition OptionSeries.hpp:130
std::int32_t getExpiration() const
Returns day id of expiration.
Definition OptionSeries.hpp:68
std::string toString() const
Returns a string representation of the current object.
Definition OptionSeries.hpp:307
const std::string & getOptionType() const
Returns type of option.
Definition OptionSeries.hpp:149
const std::string & getExpirationStyle() const
Returns expiration cycle style, such as "Weeklys", "Quarterlys".
Definition OptionSeries.hpp:158
const std::string & getSettlementStyle() const
Returns settlement price determination style, such as "Open", "Close".
Definition OptionSeries.hpp:167
double getSPC() const
Returns shares per contract for options.
Definition OptionSeries.hpp:98
double getMultiplier() const
Returns market value multiplier.
Definition OptionSeries.hpp:88
const std::vector< double > & getStrikes() const
Returns a list of all strikes in ascending order.
Definition OptionSeries.hpp:209
OptionSeries()=default
Default constructor for the OptionSeries class.
std::int32_t getLastTrade() const
Returns day id of last trading day.
Definition OptionSeries.hpp:78
const std::string & getAdditionalUnderlyings() const
Returns additional underlyings for options, including additional cash.
Definition OptionSeries.hpp:115