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

lballabio / QuantLib / 30255006850

27 Jul 2026 09:41AM UTC coverage: 74.898% (-0.05%) from 74.944%
30255006850

push

github

lballabio
Avoid deprecated Ubuntu runner

60031 of 80150 relevant lines covered (74.9%)

8637062.36 hits per line

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

92.06
/ql/prices.cpp
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2

3
/*
4
 Copyright (C) 2006, 2007 Ferdinando Ametrano
5
 Copyright (C) 2006 Katiuscia Manzoni
6
 Copyright (C) 2006 Joseph Wang
7

8
 This file is part of QuantLib, a free-software/open-source library
9
 for financial quantitative analysts and developers - http://quantlib.org/
10

11
 QuantLib is free software: you can redistribute it and/or modify it
12
 under the terms of the QuantLib license.  You should have received a
13
 copy of the license along with this program; if not, please email
14
 <quantlib-dev@lists.sf.net>. The license is also available online at
15
 <https://www.quantlib.org/license.shtml>.
16

17
 This program is distributed in the hope that it will be useful, but WITHOUT
18
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19
 FOR A PARTICULAR PURPOSE.  See the license for more details.
20
*/
21

22
#include <ql/prices.hpp>
23
#include <ql/errors.hpp>
24

25
namespace QuantLib {
26

27
    Real midEquivalent(const Real bid,
16✔
28
                       const Real ask,
29
                       const Real last,
30
                       const Real close)
31
    {
32
        if (bid != Null<Real>() && bid > 0.0) {
16✔
33
            if (ask != Null<Real>() && ask > 0.0) return ((bid+ask)/2.0);
8✔
34
            else                                  return bid;
35
        } else {
36
            if (ask != Null<Real>() && ask > 0.0)          return ask;
8✔
37
            else if (last != Null<Real>() && last > 0.0)   return last;
4✔
38
            else {
39
                QL_REQUIRE(close != Null<Real>() && close > 0.0,
4✔
40
                           "all input prices are invalid");
41
                return close;
42
            }
43
        }
44
    }
45

46
    Real midSafe(const Real bid,
4✔
47
                 const Real ask)
48
    {
49
        QL_REQUIRE(bid != Null<Real>() && bid > 0.0,
8✔
50
                   "invalid bid price");
51
        QL_REQUIRE(ask != Null<Real>() && ask > 0.0,
4✔
52
                   "invalid ask price");
53
        return (bid+ask)/2.0;
1✔
54
    }
55

56

57
    IntervalPrice::IntervalPrice()
17✔
58
    : open_(Null<Real>()), close_(Null<Real>()),
17✔
59
      high_(Null<Real>()), low_(Null<Real>()) {}
17✔
60

61
    IntervalPrice::IntervalPrice(Real open, Real close, Real high, Real low)
33✔
62
    : open_(open), close_(close), high_(high), low_(low) {}
33✔
63

64
    Real IntervalPrice::value(IntervalPrice::Type t) const {
104✔
65
        switch(t) {
104✔
66
          case Open:
26✔
67
            return open_;
26✔
68
          case Close:
26✔
69
            return close_;
26✔
70
          case High:
26✔
71
            return high_;
26✔
72
          case Low:
26✔
73
            return low_;
26✔
74
          default:
×
75
            QL_FAIL("Unknown price type");
×
76
        }
77
    }
78

79
    void IntervalPrice::setValue(Real value,
4✔
80
                                 IntervalPrice::Type t) {
81
        switch(t) {
4✔
82
          case Open:
1✔
83
            open_ = value;
1✔
84
            break;
1✔
85
          case Close:
1✔
86
            close_ = value;
1✔
87
            break;
1✔
88
          case High:
1✔
89
            high_ = value;
1✔
90
            break;
1✔
91
          case Low:
1✔
92
            low_ = value;
1✔
93
            break;
1✔
94
          default:
×
95
            QL_FAIL("Unknown price type");
×
96
        }
97
    }
4✔
98

99
    void IntervalPrice::setValues(Real open, Real close, Real high, Real low) {
1✔
100
        open_ = open; close_ = close; high_ = high; low_ = low;
1✔
101
    }
1✔
102

103

104
    TimeSeries<IntervalPrice> IntervalPrice::makeSeries(
6✔
105
                                               const std::vector<Date>& d,
106
                                               const std::vector<Real>& open,
107
                                               const std::vector<Real>& close,
108
                                               const std::vector<Real>& high,
109
                                               const std::vector<Real>& low) {
110
        Size dsize = d.size();
111
        QL_REQUIRE((open.size() == dsize && close.size() == dsize &&
6✔
112
                    high.size() == dsize && low.size() == dsize),
113
                   "size mismatch (" << dsize << ", "
114
                                     << open.size() << ", "
115
                                     << close.size() << ", "
116
                                     << high.size() << ", "
117
                                     << low.size() << ")");
118
        TimeSeries<IntervalPrice> retval;
119
        std::vector<Date>::const_iterator i;
120
        std::vector<Real>::const_iterator openi, closei, highi, lowi;
121
        openi = open.begin();
122
        closei = close.begin();
123
        highi = high.begin();
124
        lowi = low.begin();
125
        for (i = d.begin(); i != d.end(); ++i) {
23✔
126
            retval[*i] = IntervalPrice(*openi, *closei, *highi, *lowi);
17✔
127
            ++openi; ++closei; ++highi; ++lowi;
128
        }
129
        return retval;
6✔
130
    }
131

132
    std::vector<Real> IntervalPrice::extractValues(
4✔
133
                                           const TimeSeries<IntervalPrice>& ts,
134
                                           IntervalPrice::Type t)  {
135
        std::vector<Real> returnval;
136
        returnval.reserve(ts.size());
4✔
137
        for (const auto& i : ts) {
16✔
138
            returnval.push_back(i.second.value(t));
12✔
139
        }
140
        return returnval;
4✔
141
    }
×
142

143
    TimeSeries<Real> IntervalPrice::extractComponent(
4✔
144
                                          const TimeSeries<IntervalPrice>& ts,
145
                                          IntervalPrice::Type t) {
146
        std::vector<Date> dates = ts.dates();
4✔
147
        std::vector<Real> values = extractValues(ts, t);
4✔
148
        return TimeSeries<Real>(dates.begin(), dates.end(), values.begin());
8✔
149
    }
4✔
150

151
}
152

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

© 2026 Coveralls, Inc