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