Coverage Report

Created: 2025-09-04 07:11

/src/quantlib/ql/experimental/commodities/commoditypricinghelpers.hpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*
4
 Copyright (C) 2008 J. Erik Radmall
5
6
 This file is part of QuantLib, a free-software/open-source library
7
 for financial quantitative analysts and developers - http://quantlib.org/
8
9
 QuantLib is free software: you can redistribute it and/or modify it
10
 under the terms of the QuantLib license.  You should have received a
11
 copy of the license along with this program; if not, please email
12
 <quantlib-dev@lists.sf.net>. The license is also available online at
13
 <https://www.quantlib.org/license.shtml>.
14
15
 This program is distributed in the hope that it will be useful, but WITHOUT
16
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17
 FOR A PARTICULAR PURPOSE.  See the license for more details.
18
*/
19
20
/*! \file commoditypricinghelpers.hpp
21
    \brief Commodity pricing helpers
22
*/
23
24
#ifndef quantlib_commodity_pricing_helpers_hpp
25
#define quantlib_commodity_pricing_helpers_hpp
26
27
#include <ql/experimental/commodities/energycommodity.hpp>
28
#include <ql/experimental/commodities/paymentterm.hpp>
29
#include <ql/experimental/commodities/pricingperiod.hpp>
30
#include <ql/experimental/commodities/unitofmeasureconversionmanager.hpp>
31
#include <ql/currencies/exchangeratemanager.hpp>
32
33
namespace QuantLib {
34
35
    //! commodity index helper
36
    class CommodityPricingHelper {
37
      public:
38
        CommodityPricingHelper();
39
40
        static Real calculateFxConversionFactor(const Currency& fromCurrency,
41
                                                const Currency& toCurrency,
42
                                                const Date& evaluationDate);
43
44
        static Real calculateUomConversionFactor(
45
                                       const CommodityType& commodityType,
46
                                       const UnitOfMeasure& fromUnitOfMeasure,
47
                                       const UnitOfMeasure& toUnitOfMeasure);
48
49
        static Real calculateUnitCost(const CommodityType& commodityType,
50
                                      const CommodityUnitCost& unitCost,
51
                                      const Currency& baseCurrency,
52
                                      const UnitOfMeasure& baseUnitOfMeasure,
53
                                      const Date& evaluationDate);
54
55
        static void createPricingPeriods(
56
                          Date startDate, Date endDate,
57
                          const Quantity& quantity,
58
                          EnergyCommodity::DeliverySchedule deliverySchedule,
59
                          EnergyCommodity::QuantityPeriodicity qtyPeriodicity,
60
                          const PaymentTerm& paymentTerm,
61
                          PricingPeriods& pricingPeriods);
62
    };
63
64
65
    inline Real CommodityPricingHelper::calculateUomConversionFactor(
66
                                       const CommodityType& commodityType,
67
                                       const UnitOfMeasure& fromUnitOfMeasure,
68
0
                                       const UnitOfMeasure& toUnitOfMeasure) {
69
0
        if (toUnitOfMeasure != fromUnitOfMeasure) {
70
0
            UnitOfMeasureConversion uomConv =
71
0
                UnitOfMeasureConversionManager::instance().lookup(
72
0
                           commodityType, fromUnitOfMeasure, toUnitOfMeasure);
73
0
            return uomConv.conversionFactor();
74
0
        }
75
76
0
        return 1;
77
0
    }
78
79
    inline Real CommodityPricingHelper::calculateFxConversionFactor(
80
                                                 const Currency& fromCurrency,
81
                                                 const Currency& toCurrency,
82
0
                                                 const Date& evaluationDate) {
83
0
        if (fromCurrency != toCurrency) {
84
0
            ExchangeRate exchRate =
85
0
                ExchangeRateManager::instance().lookup(fromCurrency,
86
0
                                                       toCurrency,
87
0
                                                       evaluationDate,
88
0
                                                       ExchangeRate::Direct);
89
0
            if (fromCurrency != exchRate.source())
90
0
                return (Real)1 / exchRate.rate();
91
0
            return exchRate.rate();
92
0
        }
93
0
        return 1;
94
0
    }
95
96
    inline Real CommodityPricingHelper::calculateUnitCost(
97
                                       const CommodityType& commodityType,
98
                                       const CommodityUnitCost& unitCost,
99
                                       const Currency& baseCurrency,
100
                                       const UnitOfMeasure& baseUnitOfMeasure,
101
0
                                       const Date& evaluationDate) {
102
0
        if (unitCost.amount().value() != 0) {
103
0
            Real unitCostUomConversionFactor =
104
0
                calculateUomConversionFactor(commodityType,
105
0
                                             unitCost.unitOfMeasure(),
106
0
                                             baseUnitOfMeasure);
107
0
            Real unitCostFxConversionFactor =
108
0
                calculateFxConversionFactor(unitCost.amount().currency(),
109
0
                                            baseCurrency, evaluationDate);
110
0
            return unitCost.amount().value() * unitCostUomConversionFactor
111
0
                 * unitCostFxConversionFactor;
112
0
        }
113
0
        return 0;
114
0
    }
115
116
}
117
118
#endif