1 /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 
3 /*
4  Copyright (C) 2003, 2004 StatPro Italia srl
5 
6  This file is part of QuantLib, a free-software/open-source library
7  for financial quantitative analysts and developers - http://quantlib.org/
8 
9  QuantLib is free software: you can redistribute it and/or modify it
10  under the terms of the QuantLib license.  You should have received a
11  copy of the license along with this program; if not, please email
12  <quantlib-dev@lists.sf.net>. The license is also available online at
13  <http://quantlib.org/license.shtml>.
14 
15  This program is distributed in the hope that it will be useful, but WITHOUT
16  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  FOR A PARTICULAR PURPOSE.  See the license for more details.
18 */
19 
20 #include "utilities.hpp"
21 #include <ql/instruments/payoffs.hpp>
22 #include <ql/indexes/indexmanager.hpp>
23 #include <ql/termstructures/yield/flatforward.hpp>
24 #include <ql/termstructures/volatility/equityfx/blackconstantvol.hpp>
25 #include <ql/time/calendars/nullcalendar.hpp>
26 
27 #define CHECK_DOWNCAST(Derived,Description) { \
28     ext::shared_ptr<Derived> hd = ext::dynamic_pointer_cast<Derived>(h); \
29     if (hd) \
30         return Description; \
31 }
32 
33 namespace QuantLib {
34 
payoffTypeToString(const ext::shared_ptr<Payoff> & h)35     std::string payoffTypeToString(const ext::shared_ptr<Payoff>& h) {
36 
37         CHECK_DOWNCAST(PlainVanillaPayoff, "plain-vanilla");
38         CHECK_DOWNCAST(CashOrNothingPayoff, "cash-or-nothing");
39         CHECK_DOWNCAST(AssetOrNothingPayoff, "asset-or-nothing");
40         CHECK_DOWNCAST(SuperSharePayoff, "super-share");
41         CHECK_DOWNCAST(SuperFundPayoff, "super-fund");
42         CHECK_DOWNCAST(PercentageStrikePayoff, "percentage-strike");
43         CHECK_DOWNCAST(GapPayoff, "gap");
44         CHECK_DOWNCAST(FloatingTypePayoff, "floating-type");
45 
46         QL_FAIL("unknown payoff type");
47     }
48 
49 
exerciseTypeToString(const ext::shared_ptr<Exercise> & h)50     std::string exerciseTypeToString(const ext::shared_ptr<Exercise>& h) {
51 
52         CHECK_DOWNCAST(EuropeanExercise, "European");
53         CHECK_DOWNCAST(AmericanExercise, "American");
54         CHECK_DOWNCAST(BermudanExercise, "Bermudan");
55 
56         QL_FAIL("unknown exercise type");
57     }
58 
59 
60     ext::shared_ptr<YieldTermStructure>
flatRate(const Date & today,const ext::shared_ptr<Quote> & forward,const DayCounter & dc)61     flatRate(const Date& today,
62              const ext::shared_ptr<Quote>& forward,
63              const DayCounter& dc) {
64         return ext::shared_ptr<YieldTermStructure>(
65                           new FlatForward(today, Handle<Quote>(forward), dc));
66     }
67 
68     ext::shared_ptr<YieldTermStructure>
flatRate(const Date & today,Rate forward,const DayCounter & dc)69     flatRate(const Date& today, Rate forward, const DayCounter& dc) {
70         return flatRate(
71                today, ext::shared_ptr<Quote>(new SimpleQuote(forward)), dc);
72     }
73 
74     ext::shared_ptr<YieldTermStructure>
flatRate(const ext::shared_ptr<Quote> & forward,const DayCounter & dc)75     flatRate(const ext::shared_ptr<Quote>& forward,
76              const DayCounter& dc) {
77         return ext::shared_ptr<YieldTermStructure>(
78               new FlatForward(0, NullCalendar(), Handle<Quote>(forward), dc));
79     }
80 
81     ext::shared_ptr<YieldTermStructure>
flatRate(Rate forward,const DayCounter & dc)82     flatRate(Rate forward, const DayCounter& dc) {
83         return flatRate(ext::shared_ptr<Quote>(new SimpleQuote(forward)),
84                         dc);
85     }
86 
87 
88     ext::shared_ptr<BlackVolTermStructure>
flatVol(const Date & today,const ext::shared_ptr<Quote> & vol,const DayCounter & dc)89     flatVol(const Date& today,
90             const ext::shared_ptr<Quote>& vol,
91             const DayCounter& dc) {
92         return ext::shared_ptr<BlackVolTermStructure>(new
93             BlackConstantVol(today, NullCalendar(), Handle<Quote>(vol), dc));
94     }
95 
96     ext::shared_ptr<BlackVolTermStructure>
flatVol(const Date & today,Volatility vol,const DayCounter & dc)97     flatVol(const Date& today, Volatility vol,
98             const DayCounter& dc) {
99         return flatVol(today,
100                        ext::shared_ptr<Quote>(new SimpleQuote(vol)),
101                        dc);
102     }
103 
104     ext::shared_ptr<BlackVolTermStructure>
flatVol(const ext::shared_ptr<Quote> & vol,const DayCounter & dc)105     flatVol(const ext::shared_ptr<Quote>& vol,
106             const DayCounter& dc) {
107         return ext::shared_ptr<BlackVolTermStructure>(new
108             BlackConstantVol(0, NullCalendar(), Handle<Quote>(vol), dc));
109     }
110 
111     ext::shared_ptr<BlackVolTermStructure>
flatVol(Volatility vol,const DayCounter & dc)112     flatVol(Volatility vol,
113             const DayCounter& dc) {
114         return flatVol(ext::shared_ptr<Quote>(new SimpleQuote(vol)), dc);
115     }
116 
117 
relativeError(Real x1,Real x2,Real reference)118     Real relativeError(Real x1, Real x2, Real reference) {
119         if (reference != 0.0)
120             return std::fabs(x1-x2)/reference;
121         else
122             // fall back to absolute error
123             return std::fabs(x1-x2);
124     }
125 
126 
IndexHistoryCleaner()127     IndexHistoryCleaner::IndexHistoryCleaner() {}
128 
~IndexHistoryCleaner()129     IndexHistoryCleaner::~IndexHistoryCleaner() {
130         IndexManager::instance().clearHistories();
131     }
132 
133 }
134