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