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/*! \file prices.hpp
23 \brief price classes
24*/
25
26#ifndef quantlib_prices_hpp
27#define quantlib_prices_hpp
28
29#include <ql/timeseries.hpp>
30#include <ql/utilities/null.hpp>
31
32namespace QuantLib {
33
34 //! Price types
35 enum PriceType {
36 Bid, /*!< Bid price. */
37 Ask, /*!< Ask price. */
38 Last, /*!< Last price. */
39 Close, /*!< Close price. */
40 Mid, /*!< Mid price, calculated as the arithmetic
41 average of bid and ask prices. */
42 MidEquivalent, /*!< Mid equivalent price, calculated as
43 a) the arithmetic average of bid and ask prices
44 when both are available; b) either the bid or the
45 ask price if any of them is available;
46 c) the last price; or d) the close price. */
47 MidSafe /*!< Safe Mid price, returns the mid price only if
48 both bid and ask are available. */
49 };
50
51 /*! return the MidEquivalent price, i.e. the mid if available,
52 or a suitable substitute if the proper mid is not available
53 */
54 Real midEquivalent(Real bid, Real ask, Real last, Real close);
55
56 /*! return the MidSafe price, i.e. the mid only if
57 both bid and ask prices are available
58 */
59 Real midSafe(Real bid, Real ask);
60
61 //! interval price
62 class IntervalPrice {
63 public:
64 enum Type { Open, Close, High, Low };
65
66 IntervalPrice();
67 IntervalPrice(Real open, Real close, Real high, Real low);
68
69 //! \name Inspectors
70 //@{
71 Real open() const { return open_; }
72 Real close() const { return close_; }
73 Real high() const { return high_; }
74 Real low() const { return low_; }
75 Real value(IntervalPrice::Type) const;
76 //@}
77
78 //! \name Modifiers
79 //@{
80 void setValue(Real value, IntervalPrice::Type);
81 void setValues(Real open, Real close, Real high, Real low);
82 //@}
83
84 //! \name Helper functions
85 //@{
86 static TimeSeries<IntervalPrice> makeSeries(
87 const std::vector<Date>& d,
88 const std::vector<Real>& open,
89 const std::vector<Real>& close,
90 const std::vector<Real>& high,
91 const std::vector<Real>& low);
92 static std::vector<Real> extractValues(
93 const TimeSeries<IntervalPrice>&,
94 IntervalPrice::Type);
95 static TimeSeries<Real> extractComponent(
96 const TimeSeries<IntervalPrice>&,
97 enum IntervalPrice::Type);
98 //@}
99 private:
100 Real open_, close_, high_, low_;
101 };
102
103 #ifdef QL_NULL_AS_FUNCTIONS
104
105 template <>
106 inline IntervalPrice Null<IntervalPrice>() {
107 return {};
108 };
109
110 #else
111
112 template <>
113 class Null<IntervalPrice>
114 {
115 public:
116 Null() = default;
117 operator IntervalPrice() const { return {}; }
118 };
119
120 #endif
121
122}
123
124#endif
125

source code of quantlib/ql/prices.hpp