Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
couponpricer.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2016 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
25
26#include <ql/cashflows/overnightindexedcoupon.hpp>
27
28namespace QuantExt {
29
30namespace {
31
32class PricerSetter : public AcyclicVisitor,
33 public Visitor<CashFlow>,
34 public Visitor<Coupon>,
35 public Visitor<QuantLib::OvernightIndexedCoupon>,
36 public Visitor<QuantExt::OvernightIndexedCoupon>,
37 public Visitor<CappedFlooredOvernightIndexedCoupon>,
38 public Visitor<AverageONIndexedCoupon>,
39 public Visitor<QuantExt::SubPeriodsCoupon1>,
40 public Visitor<FormulaBasedCoupon> {
41private:
42 const QuantLib::ext::shared_ptr<FloatingRateCouponPricer> pricer_;
43
44public:
45 PricerSetter(const QuantLib::ext::shared_ptr<FloatingRateCouponPricer>& pricer) : pricer_(pricer) {}
46
47 void visit(CashFlow& c) override;
48 void visit(Coupon& c) override;
49 void visit(QuantLib::OvernightIndexedCoupon& c) override;
50 void visit(QuantExt::OvernightIndexedCoupon& c) override;
51 void visit(CappedFlooredOvernightIndexedCoupon& c) override;
52 void visit(AverageONIndexedCoupon& c) override;
53 void visit(QuantExt::SubPeriodsCoupon1& c) override;
54 void visit(FormulaBasedCoupon& c) override;
55};
56
57void PricerSetter::visit(CashFlow&) {
58 // nothing to do
59}
60
61void PricerSetter::visit(Coupon&) {
62 // nothing to do
63}
64
65void PricerSetter::visit(QuantLib::OvernightIndexedCoupon& c) {
66 // Special pricer for BRL CDI
67 QuantLib::ext::shared_ptr<BRLCdi> brlCdiIndex = QuantLib::ext::dynamic_pointer_cast<BRLCdi>(c.index());
68 if (brlCdiIndex) {
69 const QuantLib::ext::shared_ptr<BRLCdiCouponPricer> brlCdiCouponPricer =
70 QuantLib::ext::dynamic_pointer_cast<BRLCdiCouponPricer>(pricer_);
71 QL_REQUIRE(brlCdiCouponPricer, "Pricer not compatible with BRL CDI coupon");
72 c.setPricer(brlCdiCouponPricer);
73 } else {
74 c.setPricer(pricer_);
75 }
76}
77
78void PricerSetter::visit(QuantExt::OvernightIndexedCoupon& c) {
79 // Special pricer for BRL CDI
80 QuantLib::ext::shared_ptr<BRLCdi> brlCdiIndex = QuantLib::ext::dynamic_pointer_cast<BRLCdi>(c.index());
81 if (brlCdiIndex) {
82 const QuantLib::ext::shared_ptr<BRLCdiCouponPricer> brlCdiCouponPricer =
83 QuantLib::ext::dynamic_pointer_cast<BRLCdiCouponPricer>(pricer_);
84 QL_REQUIRE(brlCdiCouponPricer, "Pricer not compatible with BRL CDI coupon");
85 c.setPricer(brlCdiCouponPricer);
86 } else {
87 c.setPricer(pricer_);
88 }
89}
90
91void PricerSetter::visit(CappedFlooredOvernightIndexedCoupon& c) {
92 const QuantLib::ext::shared_ptr<CappedFlooredOvernightIndexedCouponPricer> p =
93 QuantLib::ext::dynamic_pointer_cast<CappedFlooredOvernightIndexedCouponPricer>(pricer_);
94 // we can set a pricer for the capped floored on coupon or the underlying on coupon
95 if (p)
96 c.setPricer(p);
97 else
98 c.underlying()->accept(*this);
99}
100
101void PricerSetter::visit(AverageONIndexedCoupon& c) {
102 const QuantLib::ext::shared_ptr<AverageONIndexedCouponPricer> averageONIndexedCouponPricer =
103 QuantLib::ext::dynamic_pointer_cast<AverageONIndexedCouponPricer>(pricer_);
104 QL_REQUIRE(averageONIndexedCouponPricer, "Pricer not compatible with Average ON Indexed coupon");
105 c.setPricer(averageONIndexedCouponPricer);
106}
107
108void PricerSetter::visit(QuantExt::SubPeriodsCoupon1& c) {
109 const QuantLib::ext::shared_ptr<QuantExt::SubPeriodsCouponPricer1> subPeriodsCouponPricer =
110 QuantLib::ext::dynamic_pointer_cast<QuantExt::SubPeriodsCouponPricer1>(pricer_);
111 QL_REQUIRE(subPeriodsCouponPricer, "Pricer not compatible with sub-periods coupon");
112 c.setPricer(subPeriodsCouponPricer);
113}
114
115void PricerSetter::visit(FormulaBasedCoupon& c) {
116 auto p = QuantLib::ext::dynamic_pointer_cast<FormulaBasedCouponPricer>(pricer_);
117 QL_REQUIRE(p, "Pricer not compatible with Formula Based coupon");
118 c.setPricer(p);
119}
120} // namespace
121
122void setCouponPricer(const Leg& leg, const QuantLib::ext::shared_ptr<FloatingRateCouponPricer>& pricer) {
123 PricerSetter setter(pricer);
124 for (Size i = 0; i < leg.size(); ++i) {
125 leg[i]->accept(setter);
126 }
127}
128
129void setCouponPricers(const Leg& leg, const std::vector<QuantLib::ext::shared_ptr<FloatingRateCouponPricer> >& pricers) {
130
131 Size nCashFlows = leg.size();
132 QL_REQUIRE(nCashFlows > 0, "No cashflows");
133
134 Size nPricers = pricers.size();
135 QL_REQUIRE(nCashFlows >= nPricers,
136 "Mismatch between leg size (" << nCashFlows << ") and number of pricers (" << nPricers << ")");
137
138 for (Size i = 0; i < nCashFlows; ++i) {
139 PricerSetter setter(i < nPricers ? pricers[i] : pricers[nPricers - 1]);
140 leg[i]->accept(setter);
141 }
142}
143} // namespace QuantExt
Pricer for average overnight indexed coupons.
Coupon pricer for a BRL CDI coupon.
const QuantLib::ext::shared_ptr< FloatingRateCouponPricer > pricer_
Utility functions for setting coupon pricers on legs.
formula based coupon
void setCouponPricers(const Leg &leg, const std::vector< QuantLib::ext::shared_ptr< FloatingRateCouponPricer > > &pricers)
Set Coupon Pricers.
void setCouponPricer(const Leg &leg, const QuantLib::ext::shared_ptr< FloatingRateCouponPricer > &pricer)
Set Coupon Pricer.
coupon paying the compounded daily overnight rate, copy of QL class, added includeSpread flag
Pricer for sub-period coupons.