QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
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 <http://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
25namespace QuantLib {
26
28 const Real ask,
29 const Real last,
30 const Real close)
31 {
32 if (bid != Null<Real>() && bid > 0.0) {
33 if (ask != Null<Real>() && ask > 0.0) return ((bid+ask)/2.0);
34 else return bid;
35 } else {
36 if (ask != Null<Real>() && ask > 0.0) return ask;
37 else if (last != Null<Real>() && last > 0.0) return last;
38 else {
39 QL_REQUIRE(close != Null<Real>() && close > 0.0,
40 "all input prices are invalid");
41 return close;
42 }
43 }
44 }
45
46 Real midSafe(const Real bid,
47 const Real ask)
48 {
49 QL_REQUIRE(bid != Null<Real>() && bid > 0.0,
50 "invalid bid price");
51 QL_REQUIRE(ask != Null<Real>() && ask > 0.0,
52 "invalid ask price");
53 return (bid+ask)/2.0;
54 }
55
56
58 : open_(Null<Real>()), close_(Null<Real>()),
59 high_(Null<Real>()), low_(Null<Real>()) {}
60
62 : open_(open), close_(close), high_(high), low_(low) {}
63
65 switch(t) {
66 case Open:
67 return open_;
68 case Close:
69 return close_;
70 case High:
71 return high_;
72 case Low:
73 return low_;
74 default:
75 QL_FAIL("Unknown price type");
76 }
77 }
78
81 switch(t) {
82 case Open:
83 open_ = value;
84 break;
85 case Close:
86 close_ = value;
87 break;
88 case High:
89 high_ = value;
90 break;
91 case Low:
92 low_ = value;
93 break;
94 default:
95 QL_FAIL("Unknown price type");
96 }
97 }
98
100 open_ = open; close_ = close; high_ = high; low_ = low;
101 }
102
103
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 &&
112 high.size() == dsize && low.size() == dsize),
113 "size mismatch (" << dsize << ", "
114 << open.size() << ", "
115 << close.size() << ", "
116 << high.size() << ", "
117 << low.size() << ")");
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) {
126 retval[*i] = IntervalPrice(*openi, *closei, *highi, *lowi);
127 ++openi; ++closei; ++highi; ++lowi;
128 }
129 return retval;
130 }
131
135 std::vector<Real> returnval;
136 returnval.reserve(ts.size());
137 for (const auto& i : ts) {
138 returnval.push_back(i.second.value(t));
139 }
140 return returnval;
141 }
142
146 std::vector<Date> dates = ts.dates();
147 std::vector<Real> values = extractValues(ts, t);
148 return TimeSeries<Real>(dates.begin(), dates.end(), values.begin());
149 }
150
151}
152
void setValue(Real value, IntervalPrice::Type)
Definition: prices.cpp:79
static TimeSeries< Real > extractComponent(const TimeSeries< IntervalPrice > &, enum IntervalPrice::Type)
Definition: prices.cpp:143
Real low() const
Definition: prices.hpp:74
static std::vector< Real > extractValues(const TimeSeries< IntervalPrice > &, IntervalPrice::Type)
Definition: prices.cpp:132
Real open() const
Definition: prices.hpp:71
Real close() const
Definition: prices.hpp:72
Real high() const
Definition: prices.hpp:73
void setValues(Real open, Real close, Real high, Real low)
Definition: prices.cpp:99
Real value(IntervalPrice::Type) const
Definition: prices.cpp:64
static TimeSeries< IntervalPrice > makeSeries(const std::vector< Date > &d, const std::vector< Real > &open, const std::vector< Real > &close, const std::vector< Real > &high, const std::vector< Real > &low)
Definition: prices.cpp:104
template class providing a null value for a given type.
Definition: null.hpp:76
Container for historical data.
Definition: timeseries.hpp:51
std::vector< Date > dates() const
returns the dates for which historical data exist
Definition: timeseries.hpp:340
Size size() const
returns the number of historical data including null ones
Definition: timeseries.hpp:307
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
Real midSafe(const Real bid, const Real ask)
Definition: prices.cpp:46
Real midEquivalent(const Real bid, const Real ask, const Real last, const Real close)
Definition: prices.cpp:27
bool close(const Quantity &m1, const Quantity &m2, Size n)
Definition: quantity.cpp:163