• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lballabio / QuantLib / 11344565157

15 Oct 2024 10:52AM UTC coverage: 72.787% (+0.001%) from 72.786%
11344565157

push

github

web-flow
Inherit CommodityIndex from Index (#2094)

0 of 12 new or added lines in 4 files covered. (0.0%)

3 existing lines in 1 file now uncovered.

55138 of 75753 relevant lines covered (72.79%)

8710266.6 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/ql/experimental/commodities/energyfuture.cpp
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
 <http://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
#include <ql/experimental/commodities/commoditysettings.hpp>
21
#include <ql/experimental/commodities/energyfuture.hpp>
22
#include <ql/settings.hpp>
23
#include <utility>
24

25
namespace QuantLib {
26

27
    EnergyFuture::EnergyFuture(Integer buySell,
×
28
                               Quantity quantity,
29
                               CommodityUnitCost tradePrice,
30
                               ext::shared_ptr<CommodityIndex> index,
31
                               const CommodityType& commodityType,
32
                               const ext::shared_ptr<SecondaryCosts>& secondaryCosts)
×
33
    : EnergyCommodity(commodityType, secondaryCosts), buySell_(buySell),
×
34
      quantity_(std::move(quantity)), tradePrice_(std::move(tradePrice)), index_(std::move(index)) {
×
35
        registerWith(Settings::instance().evaluationDate());
×
36
        registerWith(index_);
×
37
    }
×
38

39
    bool EnergyFuture::isExpired() const {
×
40
        return false;
×
41
    }
42

43
    void EnergyFuture::performCalculations() const {
×
44

45
        NPV_ = 0.0;
×
46
        additionalResults_.clear();
47

48
        Date evaluationDate = Settings::instance().evaluationDate();
×
49
        const Currency& baseCurrency =
50
            CommoditySettings::instance().currency();
×
51
        const UnitOfMeasure baseUnitOfMeasure =
52
            CommoditySettings::instance().unitOfMeasure();
×
53

54
        Real quantityUomConversionFactor =
55
            calculateUomConversionFactor(
×
56
                           quantity_.commodityType(),
57
                           baseUnitOfMeasure,
58
                           quantity_.unitOfMeasure()) * index_->lotQuantity();
×
59
        Real indexUomConversionFactor =
60
            calculateUomConversionFactor(index_->commodityType(),
×
61
                                         index_->unitOfMeasure(),
62
                                         baseUnitOfMeasure);
63
        Real tradePriceUomConversionFactor =
64
            calculateUomConversionFactor(quantity_.commodityType(),
×
65
                                         tradePrice_.unitOfMeasure(),
66
                                         baseUnitOfMeasure);
67

68
        Real tradePriceFxConversionFactor =
69
            calculateFxConversionFactor(tradePrice_.amount().currency(),
×
70
                                        baseCurrency, evaluationDate);
71
        Real indexPriceFxConversionFactor =
72
            calculateFxConversionFactor(index_->currency(), baseCurrency,
×
73
                                        evaluationDate);
74

75
        Real quoteValue = 0;
76

77
        Date lastQuoteDate = index_->lastQuoteDate();
×
78
        if (lastQuoteDate >= evaluationDate - 1) {
×
NEW
79
            quoteValue = index_->fixing(evaluationDate);
×
80
        } else {
81
            quoteValue = index_->forwardPrice(evaluationDate);
×
82
            std::ostringstream message;
×
83
            message << "curve [" << index_->name()
×
84
                    << "] has last quote date of "
×
85
                    << io::iso_date(lastQuoteDate)
×
86
                    << " using forward price from ["
87
                    << index_->forwardCurve()->name() << "]";
×
88
            addPricingError(PricingError::Warning, message.str());
×
89
        }
×
90

91
        QL_REQUIRE(quoteValue != Null<Real>(),
×
92
                   "missing quote for [" << index_->name() << "]");
93

94
        Real tradePriceValue =
95
            tradePrice_.amount().value() * tradePriceUomConversionFactor
×
96
            * tradePriceFxConversionFactor;
×
97
        Real quotePriceValue = quoteValue * indexUomConversionFactor
×
98
            * indexPriceFxConversionFactor;
99

100
        Real quantityAmount = quantity_.amount() * quantityUomConversionFactor;
×
101

102
        Real delta = (((quotePriceValue - tradePriceValue) * quantityAmount)
×
103
                      * index_->lotQuantity()) * buySell_;
×
104

105
        NPV_ = delta;
×
106

107
        calculateSecondaryCostAmounts(quantity_.commodityType(),
×
108
                                      quantity_.amount(), evaluationDate);
109
        for (auto & secondaryCostAmount : secondaryCostAmounts_) {
×
110
            Real amount = secondaryCostAmount.second.value();
111
            NPV_ -= amount;
×
112
        }
113

114
        // additionalResults_["brokerCommission"] =
115
        //     -(brokerCommissionValue * quantityAmount);
116
    }
×
117

118
}
119

STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc