/src/quantlib/ql/prices.cpp
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) 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 | | |
25 | | namespace QuantLib { |
26 | | |
27 | | Real midEquivalent(const Real bid, |
28 | | const Real ask, |
29 | | const Real last, |
30 | | const Real close) |
31 | 0 | { |
32 | 0 | if (bid != Null<Real>() && bid > 0.0) { |
33 | 0 | if (ask != Null<Real>() && ask > 0.0) return ((bid+ask)/2.0); |
34 | 0 | else return bid; |
35 | 0 | } else { |
36 | 0 | if (ask != Null<Real>() && ask > 0.0) return ask; |
37 | 0 | else if (last != Null<Real>() && last > 0.0) return last; |
38 | 0 | else { |
39 | 0 | QL_REQUIRE(close != Null<Real>() && close > 0.0, |
40 | 0 | "all input prices are invalid"); |
41 | 0 | return close; |
42 | 0 | } |
43 | 0 | } |
44 | 0 | } |
45 | | |
46 | | Real midSafe(const Real bid, |
47 | | const Real ask) |
48 | 0 | { |
49 | 0 | QL_REQUIRE(bid != Null<Real>() && bid > 0.0, |
50 | 0 | "invalid bid price"); |
51 | 0 | QL_REQUIRE(ask != Null<Real>() && ask > 0.0, |
52 | 0 | "invalid ask price"); |
53 | 0 | return (bid+ask)/2.0; |
54 | 0 | } |
55 | | |
56 | | |
57 | | IntervalPrice::IntervalPrice() |
58 | 0 | : open_(Null<Real>()), close_(Null<Real>()), |
59 | 0 | high_(Null<Real>()), low_(Null<Real>()) {} |
60 | | |
61 | | IntervalPrice::IntervalPrice(Real open, Real close, Real high, Real low) |
62 | 0 | : open_(open), close_(close), high_(high), low_(low) {} |
63 | | |
64 | 0 | Real IntervalPrice::value(IntervalPrice::Type t) const { |
65 | 0 | switch(t) { |
66 | 0 | case Open: |
67 | 0 | return open_; |
68 | 0 | case Close: |
69 | 0 | return close_; |
70 | 0 | case High: |
71 | 0 | return high_; |
72 | 0 | case Low: |
73 | 0 | return low_; |
74 | 0 | default: |
75 | 0 | QL_FAIL("Unknown price type"); |
76 | 0 | } |
77 | 0 | } |
78 | | |
79 | | void IntervalPrice::setValue(Real value, |
80 | 0 | IntervalPrice::Type t) { |
81 | 0 | switch(t) { |
82 | 0 | case Open: |
83 | 0 | open_ = value; |
84 | 0 | break; |
85 | 0 | case Close: |
86 | 0 | close_ = value; |
87 | 0 | break; |
88 | 0 | case High: |
89 | 0 | high_ = value; |
90 | 0 | break; |
91 | 0 | case Low: |
92 | 0 | low_ = value; |
93 | 0 | break; |
94 | 0 | default: |
95 | 0 | QL_FAIL("Unknown price type"); |
96 | 0 | } |
97 | 0 | } |
98 | | |
99 | 0 | void IntervalPrice::setValues(Real open, Real close, Real high, Real low) { |
100 | 0 | open_ = open; close_ = close; high_ = high; low_ = low; |
101 | 0 | } |
102 | | |
103 | | |
104 | | TimeSeries<IntervalPrice> IntervalPrice::makeSeries( |
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 | 0 | const std::vector<Real>& low) { |
110 | 0 | Size dsize = d.size(); |
111 | 0 | QL_REQUIRE((open.size() == dsize && close.size() == dsize && |
112 | 0 | high.size() == dsize && low.size() == dsize), |
113 | 0 | "size mismatch (" << dsize << ", " |
114 | 0 | << open.size() << ", " |
115 | 0 | << close.size() << ", " |
116 | 0 | << high.size() << ", " |
117 | 0 | << low.size() << ")"); |
118 | 0 | TimeSeries<IntervalPrice> retval; |
119 | 0 | std::vector<Date>::const_iterator i; |
120 | 0 | std::vector<Real>::const_iterator openi, closei, highi, lowi; |
121 | 0 | openi = open.begin(); |
122 | 0 | closei = close.begin(); |
123 | 0 | highi = high.begin(); |
124 | 0 | lowi = low.begin(); |
125 | 0 | for (i = d.begin(); i != d.end(); ++i) { |
126 | 0 | retval[*i] = IntervalPrice(*openi, *closei, *highi, *lowi); |
127 | 0 | ++openi; ++closei; ++highi; ++lowi; |
128 | 0 | } |
129 | 0 | return retval; |
130 | 0 | } |
131 | | |
132 | | std::vector<Real> IntervalPrice::extractValues( |
133 | | const TimeSeries<IntervalPrice>& ts, |
134 | 0 | IntervalPrice::Type t) { |
135 | 0 | std::vector<Real> returnval; |
136 | 0 | returnval.reserve(ts.size()); |
137 | 0 | for (const auto& i : ts) { |
138 | 0 | returnval.push_back(i.second.value(t)); |
139 | 0 | } |
140 | 0 | return returnval; |
141 | 0 | } |
142 | | |
143 | | TimeSeries<Real> IntervalPrice::extractComponent( |
144 | | const TimeSeries<IntervalPrice>& ts, |
145 | 0 | IntervalPrice::Type t) { |
146 | 0 | std::vector<Date> dates = ts.dates(); |
147 | 0 | std::vector<Real> values = extractValues(ts, t); |
148 | 0 | return TimeSeries<Real>(dates.begin(), dates.end(), values.begin()); |
149 | 0 | } |
150 | | |
151 | | } |
152 | | |