Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
commoditycashflow.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2021 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
20
21using QuantLib::AcyclicVisitor;
22using QuantLib::Visitor;
23using QuantLib::Calendar;
24using QuantLib::Date;
25using QuantLib::Days;
26using std::set;
27
28namespace QuantExt {
29
30void CommodityCashFlow::accept(QuantLib::AcyclicVisitor& v) {
31 if (QuantLib::Visitor<CommodityCashFlow>* v1 = dynamic_cast<QuantLib::Visitor<CommodityCashFlow>*>(&v))
32 v1->visit(*this);
33 else
34 CashFlow::accept(v);
35}
36
37CommodityCashFlow::CommodityCashFlow(QuantLib::Real quantity, QuantLib::Real spread, QuantLib::Real gearing,
38 bool useFuturePrice, const ext::shared_ptr<CommodityIndex>& index,
39 const ext::shared_ptr<FxIndex>& fxIndex)
40 : quantity_(quantity), spread_(spread), gearing_(gearing), useFuturePrice_(useFuturePrice), index_(index),
41 fxIndex_(fxIndex) {
42 registerWith(index_);
43 if (fxIndex) {
44 registerWith(fxIndex);
45 }
46}
47
48set<Date> pricingDates(const Date& s, const Date& e, const Calendar& pricingCalendar,
49 bool excludeStart, bool includeEnd, bool useBusinessDays) {
50
51 // If start date is after end date, return no dates.
52 if (s > e)
53 return set<Date>();
54
55 Date start = s;
56 Date end = e;
57
58 // Cover the possible exclusion of the start date
59 if ((useBusinessDays && pricingCalendar.isBusinessDay(start)) && excludeStart) {
60 start = pricingCalendar.advance(start, 1, Days);
61 }
62
63 if ((!useBusinessDays && pricingCalendar.isHoliday(start)) && excludeStart) {
64 while (pricingCalendar.isHoliday(start) && start <= end)
65 start++;
66 }
67
68 // Cover the possible exclusion of the end date
69 if ((useBusinessDays && pricingCalendar.isBusinessDay(end)) && !includeEnd) {
70 end = pricingCalendar.advance(end, -1, Days);
71 }
72
73 if ((!useBusinessDays && pricingCalendar.isHoliday(end)) && !includeEnd) {
74 while (pricingCalendar.isHoliday(end) && start <= end)
75 end--;
76 }
77
78 // Create the set of dates, which may be empty.
79 set<Date> res;
80 for (; start <= end; start++) {
81 if (isPricingDate(start, pricingCalendar, useBusinessDays))
82 res.insert(start);
83 }
84
85 return res;
86}
87
88bool isPricingDate(const Date& d, const Calendar& pricingCalendar, bool useBusinessDays) {
89 return ((useBusinessDays && pricingCalendar.isBusinessDay(d)) ||
90 (!useBusinessDays && pricingCalendar.isHoliday(d)));
91}
92
93}
void accept(QuantLib::AcyclicVisitor &v) override
ext::shared_ptr< FxIndex > fxIndex() const
ext::shared_ptr< CommodityIndex > index_
CommodityCashFlow(QuantLib::Real quantity, QuantLib::Real spread, QuantLib::Real gearing, bool useFuturePrice, const ext::shared_ptr< CommodityIndex > &index, const ext::shared_ptr< FxIndex > &fxIndex)
Some data and logic shared among commodity cashflows.
SimpleQuote & spread_
bool isPricingDate(const Date &d, const Calendar &pricingCalendar, bool useBusinessDays)
set< Date > pricingDates(const Date &s, const Date &e, const Calendar &pricingCalendar, bool excludeStart, bool includeEnd, bool useBusinessDays)