| 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 | { |
| 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 | |
| 57 | IntervalPrice::IntervalPrice() |
| 58 | : open_(Null<Real>()), close_(Null<Real>()), |
| 59 | high_(Null<Real>()), low_(Null<Real>()) {} |
| 60 | |
| 61 | IntervalPrice::IntervalPrice(Real open, Real close, Real high, Real low) |
| 62 | : open_(open), close_(close), high_(high), low_(low) {} |
| 63 | |
| 64 | Real IntervalPrice::value(IntervalPrice::Type t) const { |
| 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 | |
| 79 | void IntervalPrice::setValue(Real value, |
| 80 | IntervalPrice::Type t) { |
| 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 | |
| 99 | void IntervalPrice::setValues(Real open, Real close, Real high, Real low) { |
| 100 | open_ = open; close_ = close; high_ = high; low_ = low; |
| 101 | } |
| 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 | 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() << ")" ); |
| 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) { |
| 126 | retval[*i] = IntervalPrice(*openi, *closei, *highi, *lowi); |
| 127 | ++openi; ++closei; ++highi; ++lowi; |
| 128 | } |
| 129 | return retval; |
| 130 | } |
| 131 | |
| 132 | std::vector<Real> IntervalPrice::( |
| 133 | const TimeSeries<IntervalPrice>& ts, |
| 134 | IntervalPrice::Type t) { |
| 135 | std::vector<Real> returnval; |
| 136 | returnval.reserve(n: ts.size()); |
| 137 | for (const auto& i : ts) { |
| 138 | returnval.push_back(x: i.second.value(t)); |
| 139 | } |
| 140 | return returnval; |
| 141 | } |
| 142 | |
| 143 | TimeSeries<Real> IntervalPrice::( |
| 144 | const TimeSeries<IntervalPrice>& ts, |
| 145 | IntervalPrice::Type t) { |
| 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 | |
| 153 | |