dxFeed Graal CXX API v6.0.0
Loading...
Searching...
No Matches
OptionChainsBuilder.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#include "./OptionChain.hpp"
12#include "./OptionSeries.hpp"
13
14#include <unordered_map>
15
17
18/**
19 * Builder class for a set of option chains grouped by product or underlying symbol.
20 *
21 * <h3>Threads and clocks</h3>
22 *
23 * This class is <b>NOT</b> thread-safe and cannot be used from multiple threads without external synchronization.
24 *
25 * @tparam T The type of option instrument instances.
26 */
27template <class T> class OptionChainsBuilder {
28 std::string product_{};
29 std::string underlying_{};
30 OptionSeries<T> series_{};
31 std::string cfi_{};
32 double strike_{};
33 std::unordered_map<std::string, OptionChain<T>> chains_{};
34
35 OptionChain<T> &getOrCreateChain(const std::string &symbol) {
36 return chains_.try_emplace(symbol, OptionChain<T>(symbol)).first->second;
37 }
38
39 public:
40 /**
41 * Creates new option chains builder.
42 */
45
46 /**
47 * Changes product for futures and options on futures (underlying asset name).
48 *
49 * Example: "/YG".
50 *
51 * @param product The product for futures and options on futures (underlying asset name).
52 */
53 void setProduct(const StringLike &product) {
54 product_ = product == String::NUL || product.empty() ? String::EMPTY : std::string(product);
55 }
56
57 /**
58 * Changes primary underlying symbol for options.
59 *
60 * Example: "C", "/YGM9"
61 *
62 * @param underlying The primary underlying symbol for options.
63 */
64 void setUnderlying(const StringLike &underlying) {
65 underlying_ = underlying == String::NUL || underlying.empty() ? String::EMPTY : std::string(underlying);
66 }
67
68 /**
69 * Changes day id of expiration.
70 * Example: @ref day_util::#getDayIdByYearMonthDay() "dxfcpp::day_util::getDayIdByYearMonthDay"(20090117).
71 *
72 * @param expiration The day id of expiration.
73 */
74 void setExpiration(std::int32_t expiration) {
75 series_.expiration_ = expiration;
76 }
77
78 /**
79 * Changes day id of last trading day.
80 * Example: @ref day_util::#getDayIdByYearMonthDay() "dxfcpp::day_util::getDayIdByYearMonthDay"(20090116).
81 *
82 * @param lastTrade The day id of last trading day.
83 */
84 void setLastTrade(std::int32_t lastTrade) {
85 series_.lastTrade_ = lastTrade;
86 }
87
88 /**
89 * Changes market value multiplier.
90 * Example: 100, 33.2.
91 *
92 * @param multiplier The market value multiplier.
93 */
94 void setMultiplier(double multiplier) {
95 series_.multiplier_ = multiplier;
96 }
97
98 /**
99 * Changes shares per contract for options.
100 *
101 * Example: 1, 100.
102 *
103 * @param spc The shares per contract for options.
104 */
105 void setSPC(double spc) {
106 series_.spc_ = spc;
107 }
108
109 /**
110 * Changes additional underlyings for options, including additional cash.
111 * It shall use following format:
112 * ```
113 * <VALUE> ::= <empty> | <LIST>
114 * <LIST> ::= <AU> | <AU> <semicolon> <space> <LIST>
115 * <AU> ::= <UNDERLYING> <space> <SPC>
116 * the list shall be sorted by <UNDERLYING>.
117 * ```
118 * Example: "SE 50", "FIS 53; US$ 45.46".
119 *
120 * @param additionalUnderlyings The additional underlyings for options, including additional cash.
121 */
122 void setAdditionalUnderlyings(const StringLike &additionalUnderlyings) {
123 series_.additionalUnderlyings_ = additionalUnderlyings == String::NUL || additionalUnderlyings.empty()
124 ? String::EMPTY
125 : std::string(additionalUnderlyings);
126 }
127
128 /**
129 * Changes maturity month-year as provided for corresponding FIX tag (200).
130 * It can use several different formats depending on data source:
131 * <ul>
132 * <li>YYYYMM – if only year and month are specified
133 * <li>YYYYMMDD – if full date is specified
134 * <li>YYYYMMwN – if week number (within a month) is specified
135 * </ul>
136 *
137 * @param mmy The maturity month-year as provided for corresponding FIX tag (200).
138 */
139 void setMMY(const std::string &mmy) {
140 series_.mmy_ = mmy == String::NUL || mmy.empty() ? String::EMPTY : mmy;
141 }
142
143 /**
144 * Changes type of option.
145 * It shall use one of following values:
146 * <ul>
147 * <li>STAN = Standard Options
148 * <li>LEAP = Long-term Equity AnticiPation Securities
149 * <li>SDO = Special Dated Options
150 * <li>BINY = Binary Options
151 * <li>FLEX = FLexible EXchange Options
152 * <li>VSO = Variable Start Options
153 * <li>RNGE = Range
154 * </ul>
155 *
156 * @param optionType The type of option.
157 */
158 void setOptionType(const StringLike &optionType) {
159 series_.optionType_ = optionType == String::NUL || optionType.empty() ? String::EMPTY : std::string(optionType);
160 }
161
162 /**
163 * Changes the expiration cycle style, such as "Weeklys", "Quarterlys".
164 *
165 * @param expirationStyle The expiration cycle style.
166 */
167 void setExpirationStyle(const StringLike &expirationStyle) {
168 series_.expirationStyle_ =
169 expirationStyle == String::NUL || expirationStyle.empty() ? String::EMPTY : std::string(expirationStyle);
170 }
171
172 /**
173 * Changes settlement price determination style, such as "Open", "Close".
174 *
175 * @param settlementStyle The settlement price determination style.
176 */
177 void setSettlementStyle(const StringLike &settlementStyle) {
178 series_.settlementStyle_ =
179 settlementStyle == String::NUL || settlementStyle.empty() ? String::EMPTY : std::string(settlementStyle);
180 }
181
182 /**
183 * Changes Classification of Financial Instruments code.
184 * It is a mandatory field as it is the only way to distinguish Call/Put option type,
185 * American/European exercise, Cash/Physical delivery.
186 * It shall use six-letter CFI code from ISO 10962 standard.
187 * It is allowed to use 'X' extensively and to omit trailing letters (assumed to be 'X').
188 * See <a href="http://en.wikipedia.org/wiki/ISO_10962">ISO 10962 on Wikipedia</a>.
189 *
190 * Example: "OC" for generic call, "OP" for generic put.
191 *
192 * @param cfi CFI code.
193 */
194 void setCFI(const StringLike &cfi) {
195 cfi_ = cfi == String::NUL || cfi.empty() ? String::EMPTY : std::string(cfi);
196 series_.cfi_ = cfi_.size() < 2 ? cfi_ : cfi_[0] + std::string("X") + cfi_.substr(2);
197 }
198
199 /**
200 * Changes strike price for options.
201 *
202 * Example: 80, 22.5.
203 *
204 * @param strike The strike price for options.
205 */
206 void setStrike(double strike) {
207 strike_ = strike;
208 }
209
210 /**
211 * Adds an option instrument to this builder.
212 *
213 * Option is added to chains for the currently set @ref ::setProduct() "product" and/or
214 * @ref ::setUnderlying() "underlying" to the @ref OptionSeries "series" that corresponding
215 * to all other currently set attributes. This method is safe in the sense that is ignores
216 * illegal state of the builder. It only adds an option when all the following conditions are met:
217 * <ul>
218 * <li>@ref ::setCFI() "CFI code" is set and starts with either "OC" for call or "OP" for put;
219 * <li>@ref ::setExpiration() "expiration" is set and is not zero;
220 * <li>@ref ::setStrike() "strike" is set and is not NaN nor Inf;
221 * <li>@ref ::setProduct() "product" or @ref ::setUnderlying() "underlying symbol" are set;
222 * </ul>
223 * All the attributes remain set as before after the call to this method, but
224 * @ref ::getChains() "chains" are updated correspondingly.
225 *
226 * @param option The option to add.
227 */
228 void addOption(std::shared_ptr<T> option) {
229 bool isCall = cfi_.starts_with("OC");
230
231 if (!isCall && !cfi_.starts_with("OP") /*is not put*/) {
232 return;
233 }
234
235 if (series_.expiration_ == 0) {
236 return;
237 }
238
239 if (std::isnan(strike_) || std::isinf(strike_)) {
240 return;
241 }
242
243 if (!product_.empty()) {
244 getOrCreateChain(product_).addOption(series_, isCall, strike_, option);
245 }
246
247 if (!underlying_.empty()) {
248 getOrCreateChain(underlying_).addOption(series_, isCall, strike_, option);
249 }
250 }
251
252 /**
253 * Returns a view of chains created by this builder.
254 * It updates as new options are added with @ref ::addOption() "addOption" method.
255 *
256 * @return The view of chains created by this builder.
257 */
259 return chains_;
260 }
261
262 /**
263 * Builds options chains for all options from the given collections of @ref InstrumentProfile "instrument profiles".
264 *
265 * @tparam Collection The collection type.
266 * @param instruments The collection of instrument profiles.
267 *
268 * @return The builder with all the options from the instruments collection.
269 */
270 template <typename Collection, typename Element = std::decay_t<decltype(std::begin(Collection()))>,
271 typename Profile = std::decay_t<decltype(*Element())>>
272 static OptionChainsBuilder<InstrumentProfile> build(const Collection &instruments) {
273 static_assert(std::is_same_v<Profile, std::shared_ptr<InstrumentProfile>>,
274 "The collection element must be of type `std::shared_ptr<InstrumentProfile>`");
275 OptionChainsBuilder<InstrumentProfile> ocb{};
276
277 for (const std::shared_ptr<InstrumentProfile> &ip : instruments) {
278 if ("OPTION" != ip->getType()) {
279 continue;
280 }
281
282 ocb.setProduct(ip->getProduct());
283 ocb.setUnderlying(ip->getUnderlying());
284 ocb.setExpiration(ip->getExpiration());
285 ocb.setLastTrade(ip->getLastTrade());
286 ocb.setMultiplier(ip->getMultiplier());
287 ocb.setSPC(ip->getSPC());
288 ocb.setAdditionalUnderlyings(ip->getAdditionalUnderlyings());
289 ocb.setMMY(ip->getMMY());
290 ocb.setOptionType(ip->getOptionType());
291 ocb.setExpirationStyle(ip->getExpirationStyle());
292 ocb.setSettlementStyle(ip->getSettlementStyle());
293 ocb.setCFI(ip->getCFI());
294 ocb.setStrike(ip->getStrike());
295 ocb.addOption(ip);
296 }
297
298 return ocb;
299 }
300};
301
303
#define DXFCXX_DISABLE_MSC_WARNINGS_POP()
Definition Conf.hpp:31
#define DXFCPP_END_NAMESPACE
Definition Conf.hpp:97
#define DXFCPP_BEGIN_NAMESPACE
Definition Conf.hpp:94
#define DXFCXX_DISABLE_MSC_WARNINGS_PUSH(warnings)
Definition Conf.hpp:30
Builder class for a set of option chains grouped by product or underlying symbol.
Definition OptionChainsBuilder.hpp:27
const std::unordered_map< std::string, OptionChain< T > > & getChains()
Returns a view of chains created by this builder.
Definition OptionChainsBuilder.hpp:258
void setCFI(const StringLike &cfi)
Changes Classification of Financial Instruments code.
Definition OptionChainsBuilder.hpp:194
void setMultiplier(double multiplier)
Changes market value multiplier.
Definition OptionChainsBuilder.hpp:94
void setSettlementStyle(const StringLike &settlementStyle)
Changes settlement price determination style, such as "Open", "Close".
Definition OptionChainsBuilder.hpp:177
void setStrike(double strike)
Changes strike price for options.
Definition OptionChainsBuilder.hpp:206
void setAdditionalUnderlyings(const StringLike &additionalUnderlyings)
Changes additional underlyings for options, including additional cash.
Definition OptionChainsBuilder.hpp:122
void setProduct(const StringLike &product)
Changes product for futures and options on futures (underlying asset name).
Definition OptionChainsBuilder.hpp:53
void setUnderlying(const StringLike &underlying)
Changes primary underlying symbol for options.
Definition OptionChainsBuilder.hpp:64
void setExpiration(std::int32_t expiration)
Changes day id of expiration.
Definition OptionChainsBuilder.hpp:74
void setSPC(double spc)
Changes shares per contract for options.
Definition OptionChainsBuilder.hpp:105
void setExpirationStyle(const StringLike &expirationStyle)
Changes the expiration cycle style, such as "Weeklys", "Quarterlys".
Definition OptionChainsBuilder.hpp:167
void setMMY(const std::string &mmy)
Changes maturity month-year as provided for corresponding FIX tag (200).
Definition OptionChainsBuilder.hpp:139
void setOptionType(const StringLike &optionType)
Changes type of option.
Definition OptionChainsBuilder.hpp:158
void addOption(std::shared_ptr< T > option)
Adds an option instrument to this builder.
Definition OptionChainsBuilder.hpp:228
void setLastTrade(std::int32_t lastTrade)
Changes day id of last trading day.
Definition OptionChainsBuilder.hpp:84
static OptionChainsBuilder< InstrumentProfile > build(const Collection &instruments)
Builds options chains for all options from the given collections of instrument profiles.
Definition OptionChainsBuilder.hpp:272
OptionChainsBuilder()
Creates new option chains builder.
Definition OptionChainsBuilder.hpp:43
A lightweight wrapper around strings or string-like inputs.
Definition StringUtils.hpp:27