19#include <boost/make_shared.hpp>
30#include <ql/errors.hpp>
31#include <ql/exercise.hpp>
32#include <ql/instruments/barrieroption.hpp>
33#include <ql/instruments/compositeinstrument.hpp>
34#include <ql/instruments/swap.hpp>
35#include <ql/instruments/vanillaoption.hpp>
44 string domesticCurrency,
string payoffCurrency,
double payoffAmount,
string startDate,
48 barrier_(barrier), startDate_(startDate), calendar_(
calendar), fxIndex_(fxIndex), payoffAmount_(payoffAmount),
49 payoffCurrency_(payoffCurrency) {
51 switch (barrierType) {
56 case Barrier::DownOut:
61 QL_FAIL(
"unknown barrier type");
80 const QuantLib::ext::shared_ptr<Market> market = engineFactory->market();
82 QL_REQUIRE(
tradeActions().empty(),
"TradeActions not supported for FxOption");
84 QL_REQUIRE(
barrier_.
levels().size() == 1,
"Double barriers not supported for FxTouchOptions");
94 BusinessDayConvention payConvention = Unadjusted;
95 Calendar payCalendar = NullCalendar();
96 Date payDate = expiryDate;
99 if (opd->rulesBased()) {
101 payConvention = opd->convention();
102 payCalendar = opd->calendar();
103 payDate = payCalendar.advance(expiryDate, opd->lag(), Days, opd->convention());
105 if (opd->dates().size() > 1)
107 "Found more than 1 payment date. The first one will be used.")
109 payDate = opd->dates().front();
112 QL_REQUIRE(payDate >= expiryDate,
"Settlement date cannot be earlier than expiry date");
116 if (barrierType == Barrier::DownIn || barrierType == Barrier::DownOut)
117 type = Option::Type::Put;
119 type = Option::Type::Call;
126 QL_REQUIRE(rebate == 0,
"Rebates not supported for FxTouchOptions");
127 QL_REQUIRE(payoffAtExpiry ==
true || barrierType == Barrier::Type::DownIn || barrierType == Barrier::Type::UpIn,
128 "Payoff at hit not supported for FxNoTouchOptions");
129 if ((barrierType == Barrier::Type::DownIn || barrierType == Barrier::Type::UpIn) && payoffAtExpiry ==
false)
132 "Option payment data must be rules-based and relative to Exercise for FxOneTouchOption with payoff at hit");
135 bool flipResults =
false;
139 std::swap(fgnCcy, domCcy);
140 type =
type == Option::Call ? Option::Put : Option::Call;
141 switch (barrierType) {
142 case Barrier::DownIn:
143 barrierType = Barrier::UpIn;
146 barrierType = Barrier::DownIn;
148 case Barrier::DownOut:
149 barrierType = Barrier::UpOut;
152 barrierType = Barrier::DownOut;
160 DLOG(
"Setting up FxTouchOption with level " << level <<
" foreign/bought " << fgnCcy <<
" domestic/sold "
165 QuantLib::ext::shared_ptr<QuantExt::FxIndex>
fxIndex;
171 auto buildBarrierOptionWrapperInstr = [
this,
type, level, engineFactory, domCcy, fgnCcy, flipResults, positionType,
172 market, barrierType, rebate,
fxIndex, cal,
173 start](
const Date& expiryDate,
const Date& payDate) {
174 QuantLib::ext::shared_ptr<StrikedTypePayoff> payoff(
new CashOrNothingPayoff(
type, level, 1.0));
177 leg.push_back(QuantLib::ext::shared_ptr<CashFlow>(
new SimpleCashFlow(1.0, payDate)));
179 bool payoffFlag =
true;
181 QuantLib::ext::shared_ptr<Exercise> exercise = QuantLib::ext::make_shared<AmericanExercise>(expiryDate, payoffFlag);
183 QuantLib::ext::shared_ptr<Instrument>
barrier = QuantLib::ext::make_shared<VanillaOption>(payoff, exercise);
184 QuantLib::ext::shared_ptr<Instrument> underlying = QuantLib::ext::make_shared<Swap>(Leg(), leg);
187 QuantLib::ext::shared_ptr<EngineBuilder> builder = engineFactory->builder(
tradeType_);
188 QL_REQUIRE(builder,
"No builder found for " <<
tradeType_);
189 QuantLib::ext::shared_ptr<FxTouchOptionEngineBuilder> fxTouchOptBuilder =
190 QuantLib::ext::dynamic_pointer_cast<FxTouchOptionEngineBuilder>(builder);
191 barrier->setPricingEngine(fxTouchOptBuilder->engine(fgnCcy, domCcy,
type_, payDate, flipResults));
193 if (
type_ ==
"One-Touch") {
196 builder = engineFactory->builder(
"Swap");
197 QL_REQUIRE(builder,
"No builder found for Swap");
198 QuantLib::ext::shared_ptr<SwapEngineBuilderBase> swapBuilder =
199 QuantLib::ext::dynamic_pointer_cast<SwapEngineBuilderBase>(builder);
200 underlying->setPricingEngine(swapBuilder->engine(domCcy, std::string(), std::string()));
203 bool isLong = (positionType == Position::Long) ?
true :
false;
205 std::vector<QuantLib::ext::shared_ptr<Instrument>> additionalInstruments;
206 std::vector<Real> additionalMultipliers;
207 Date lastPremiumDate =
212 Handle<Quote> spot = market->fxRate(fgnCcy.code() + domCcy.code());
214 auto barrierOptionWrapper = QuantLib::ext::make_shared<SingleBarrierOptionWrapper>(
215 barrier, isLong, expiryDate,
false, underlying, barrierType, spot, level, rebate, domCcy, start,
fxIndex,
218 maturity_ = std::max(lastPremiumDate, payDate);
220 return barrierOptionWrapper;
223 auto barrierOptionWrapper = buildBarrierOptionWrapperInstr(expiryDate, payDate);
228 if (start != Date()) {
229 for (Date d = start; d <= expiryDate; d = fixingCal.advance(d, 1 * Days))
235 if (
auto rt = engineFactory->engineData()->globalParameters().find(
"RunType");
236 rt != engineFactory->engineData()->globalParameters().end() && rt->second !=
"PortfolioAnalyser" &&
237 barrierOptionWrapper->exercise()) {
238 QL_REQUIRE(barrierOptionWrapper->exerciseDate() != Date(),
"Option is exercised but exercise date was not defined");
239 expiryDate = barrierOptionWrapper->exerciseDate();
242 if (!payoffAtExpiry &&
type_ ==
"One-Touch") {
243 payDate = payCalendar.advance(expiryDate, payLag, Days, payConvention);
244 barrierOptionWrapper = buildBarrierOptionWrapperInstr(expiryDate, payDate);
256 case Barrier::DownIn:
257 case Barrier::DownOut:
263 QL_FAIL(
"unknown barrier type " <<
type);
270 QL_REQUIRE(fxNode,
"No FxOptionData Node");
274 switch (barrierType) {
275 case Barrier::DownIn:
279 case Barrier::DownOut:
284 QL_FAIL(
"unknown barrier type");
Wrapper for option instruments, tracks whether option has been exercised or not.
Engine builder for Swaps.
Serializable obejct holding barrier data.
const std::string & type() const
virtual void fromXML(ore::data::XMLNode *node) override
virtual ore::data::XMLNode * toXML(ore::data::XMLDocument &doc) const override
std::vector< ore::data::TradeBarrier > levels() const
const std::string & style() const
Serializable object holding generic trade data, reporting dimensions.
Base class for all single asset FX Derivaties.
std::string & domesticCurrency_
std::string & foreignCurrency_
const BarrierData & barrier() const
virtual void fromXML(XMLNode *node) override
FxTouchOption()
Default constructor.
virtual XMLNode * toXML(XMLDocument &doc) const override
const string & type() const
const string & fxIndex() const
bool checkBarrier(Real spot, Barrier::Type type, Real level)
void build(const QuantLib::ext::shared_ptr< EngineFactory > &) override
Build QuantLib/QuantExt instrument, link pricing engine.
void log() const
generate Boost log record to pass to corresponding sinks
Serializable object holding option data.
void setCallPut(const string &callPut)
const string & longShort() const
virtual void fromXML(XMLNode *node) override
virtual XMLNode * toXML(XMLDocument &doc) const override
const bool & payoffAtExpiry() const
const boost::optional< OptionPaymentData > & paymentData() const
const PremiumData & premiumData() const
const vector< string > & exerciseDates() const
void addFixingDate(const QuantLib::Date &fixingDate, const std::string &indexName, const QuantLib::Date &payDate=Date::maxDate(), const bool alwaysAddIfPaysOnSettlement=false, const bool mandatoryFixing=true)
Utility classes for Structured warnings, contains the Trade ID and Type.
TradeActions & tradeActions()
Set the trade actions.
virtual void fromXML(XMLNode *node) override
Date addPremiums(std::vector< QuantLib::ext::shared_ptr< Instrument > > &instruments, std::vector< Real > &multipliers, const Real tradeMultiplier, const PremiumData &premiumData, const Real premiumMultiplier, const Currency &tradeCurrency, const QuantLib::ext::shared_ptr< EngineFactory > &factory, const string &configuration)
void setSensitivityTemplate(const EngineBuilder &builder)
virtual XMLNode * toXML(XMLDocument &doc) const override
RequiredFixings requiredFixings_
QuantLib::ext::shared_ptr< InstrumentWrapper > instrument_
const string & tradeType() const
std::map< std::string, boost::any > additionalData_
Small XML Document wrapper class.
XMLNode * allocNode(const string &nodeName)
util functions that wrap rapidxml
static Real getChildValueAsDouble(XMLNode *node, const string &name, bool mandatory=false, double defaultValue=0.0)
static string getChildValue(XMLNode *node, const string &name, bool mandatory=false, const string &defaultValue=string())
static XMLNode * getChildNode(XMLNode *n, const string &name="")
static XMLNode * addChild(XMLDocument &doc, XMLNode *n, const string &name)
static void appendNode(XMLNode *parent, XMLNode *child)
FX One-Touch/No-Touch Option data model and serialization.
Calendar parseCalendar(const string &s)
Convert text to QuantLib::Calendar.
Date parseDate(const string &s)
Convert std::string to QuantLib::Date.
Currency parseCurrency(const string &s)
Convert text to QuantLib::Currency.
Position::Type parsePositionType(const std::string &s)
Convert text to QuantLib::Position::Type.
Barrier::Type parseBarrierType(const std::string &s)
Convert std::string to QuantLib::BarrierType.
Map text representations to QuantLib/QuantExt types.
Classes and functions for log message handling.
#define DLOG(text)
Logging Macro (Level = Debug)
std::string to_string(const LocationInfo &l)
QuantLib::ext::shared_ptr< QuantExt::FxIndex > buildFxIndex(const string &fxIndex, const string &domestic, const string &foreign, const QuantLib::ext::shared_ptr< Market > &market, const string &configuration, bool useXbsCurves)
Serializable Credit Default Swap.
Map text representations to QuantLib/QuantExt types.
Classes for structured trade warnings.
string conversion utilities