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