Apama Capital Markets Foundation  10.0.0.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
QuantlibPlugin.hpp
1 /*
2  * \file QuantlibPlugin.hpp
3  * \brief Interface functions for the QuantlibPlugin
4  *
5  * \copyright
6  * Copyright (c) 2016 Software AG, Darmstadt, Germany and/or Software AG USA Inc., Reston, VA, USA, and/or its subsidiaries and/or its affiliates and/or their licensors.
7  * Use, reproduction, transfer, publication or disclosure is prohibited except as specifically provided for in your License Agreement with Software AG
8  */
9 
10 #ifndef _QUANTLIBPLUGIN_H_
11 #define _QUANTLIBPLUGIN_H_
12 
13 // the only header you need to use QuantLib
14 #include <ql/quantlib.hpp>
15 #include <ql/math/statistics/generalstatistics.hpp>
16 
17 #ifdef BOOST_MSVC
18 /* Uncomment the following lines to unmask floating-point
19  exceptions. Warning: unpredictable results can arise...
20 
21  See http://www.wilmott.com/messageview.cfm?catid=10&threadid=9481
22  Is there anyone with a definitive word about this?
23  */
24 // #include <float.h>
25 // namespace { unsigned int u = _controlfp(_EM_INEXACT, _MCW_EM); }
26 #endif
27 
28 #include <epl_plugin.hpp>
29 #include <boost/timer.hpp>
30 #include <boost/unordered_map.hpp>
31 #include <iostream>
32 #include <string>
33 using namespace com::apama::epl;
34 using namespace QuantLib;
35 
36 #if defined(QL_ENABLE_SESSIONS)
37 namespace QuantLib {
38 
39  Integer sessionId() { return 0; }
40 
41 }
42 #endif
43 
44 
45 #define BOND_COMPARE_EQUAL 0
46 #define BOND_MATURITYDATE_ERROR 1
47 #define BOND_ISSUEDATE_ERROR 2
48 #define BOND_SETTLEMENTDATE_ERROR 4
49 #define BOND_RATE_ERROR 8
50 #define BOND_FREQUENCY_ERROR 16
51 #define BOND_CREATED_NEW 255
52 
53 #define NO_UPDATE_BOND false
54 #define UPDATE_BOND true
55 
56 // local typedefs
57 typedef boost::shared_ptr<FixedRateBond> bond_ptr;
58 
66 class BondKey {
67 public:
71  BondKey(
72  const char* issueDate, const char* maturityDate, const char* settlementDate,
73  const Rate rate, const Frequency frequency, bool takeCopies = true);
77  BondKey(const BondKey& that);
81  BondKey& operator=(const BondKey& that);
82  ~BondKey();
86  const char *toString();
90  const char *getIssueDate() { return issueDate; }
94  const char *getMaturityDate() { return maturityDate; }
98  const char *getSettlementDate() { return settlementDate; }
102  Rate getRate() { return rate; }
106  Frequency getFrequency() { return frequency; }
107 
108 private:
109 
110  void cleanup();
111 
112  char* issueDate;
113  char* maturityDate;
114  char* settlementDate;
115  Rate rate;
116  Frequency frequency;
117  std::size_t hash;
118  bool ownStrings;
119 
120  friend bool operator<(const BondKey& lhs, const BondKey& rhs);
121  friend bool operator==(const BondKey& lhs, const BondKey& rhs);
122  friend int compare(BondKey& lhs, BondKey& rhs);
123  friend std::size_t hash_value(const BondKey& x) {
124  return x.hash;
125  }
126 };
127 
131 class YieldKey {
132 public:
136  YieldKey(bond_ptr& bond, Real price, const char* settlementDate, bool takeCopies = true);
140  YieldKey(const YieldKey& that);
144  YieldKey& operator=(const YieldKey& that);
145  ~YieldKey();
146 
147 private:
148  void cleanup();
149 
150  bond_ptr bond;
151  Real price;
152  char* settlementDate;
153  std::size_t hash;
154  bool ownStrings;
155 
156  friend bool operator<(const YieldKey& lhs, const YieldKey& rhs);
157  friend bool operator==(const YieldKey& lhs, const YieldKey& rhs);
158  friend std::size_t hash_value(const YieldKey& x) {
159  return x.hash;
160  }
161 };
162 
163 bool operator<(const BondKey& lhs, const BondKey& rhs);
164 bool operator==(const BondKey& lhs, const BondKey& rhs);
165 
166 // more local typedefs
167 typedef std::map< std::string, bond_ptr> stringbond_map;
168 //typedef boost::unordered_map< BondKey, bond_ptr > bond_map;
169 typedef boost::unordered_map< const char *, bond_ptr > bond_map;
170 typedef boost::unordered_map< const char *, BondKey > bondspec_map;
171 typedef boost::unordered_map< YieldKey, Real > yield_map;
172 
177 {
178 public:
182  OptionResultsChunk(int32_t errorNum, double npv, double implVol, double delta, double gamma, double theta, double vega, double rho, char* error);
190  void print() const;
194  int32_t errNum;
198  double npv;
202  double implVol;
206  double delta;
210  double gamma;
214  double theta;
218  double vega;
222  double rho;
226  char* error;
227 };
228 
230 class QuantlibPlugin : public EPLPlugin<QuantlibPlugin>
231 {
232 public:
236  QuantlibPlugin() :base_plugin_t("QuantlibPlugin"){}
237  ~QuantlibPlugin(){}
241  static void initialize(base_plugin_t::method_data_t &md);
256  custom_t<OptionResultsChunk>
257  modelPrice(double _underlying, double _strike, const char* _currentBussinessDate,
258  const char* _maturity, double _riskFreeRate, double _volatility,
259  double _dividendYield, const char* _t, const char* _modelName);
274  custom_t<OptionResultsChunk>
275  modelImplVol(double _price, double _underlying, double _strike,
276  const char* _currentBussinessDate, const char* _maturityDate,
277  double _riskFreeRate, double _dividendYield, const char* _t,
278  const char* _modelName);
293  custom_t<OptionResultsChunk>
294  modelGreeks(double _underlying, double _strike, const char* _currentBussinessDate,
295  const char* _maturity, double _riskFreeRate, double _volatility,
296  double _dividendYield, const char* _t, const char* _modelName);
312  custom_t<OptionResultsChunk>
313  btcoxrossrubinsteinGreeks(double _underlying, double _strike, const char* _currentBussinessDate,
314  const char* _maturity, double _riskFreeRate, double _volatility,
315  double _dividendYield, int64_t _timeSteps, const char* _t);
330  custom_t<OptionResultsChunk>
331  btcoxrossrubinsteinPrice(double _underlying, double _strike, const char* _currentBussinessDate,
332  const char* _maturity, double _riskFreeRate, double _volatility,
333  double _dividendYield, int64_t _timeSteps, const char* _t);
348  custom_t<OptionResultsChunk>
349  btcoxrossrubinsteinImplVol(double _price, double _underlying, double _strike,
350  const char* _currentBussinessDate, const char* _maturity,
351  double _riskFreeRate, double _dividendYield, int64_t _timeSteps,
352  const char* _t);
359  const char* error(const custom_t<OptionResultsChunk> &results);
366  double npv(const custom_t<OptionResultsChunk> &results);
373  double delta(const custom_t<OptionResultsChunk> &results);
380  double gamma(const custom_t<OptionResultsChunk> &results);
387  double theta(const custom_t<OptionResultsChunk> &results);
394  double vega(const custom_t<OptionResultsChunk> &results);
401  double rho(const custom_t<OptionResultsChunk> &results);
408  double implVol(const custom_t<OptionResultsChunk> &results);
421  double yield(const char* _symbol, double _price, const char* _issueDate,
422  const char* _maturityDate, const char* _settlementDate, double _rate,
423  int64_t _freq);
437  double dv01(const char* _symbol, double _yield, const char* _issueDate,
438  const char* _maturityDate, const char* _settlementDate, double _rate,
439  int64_t _freq);
452  double price(const char* _symbol, double _yield, const char* _issueDate,
453  const char* _maturityDate, const char* _settlementDate, double _rate,
454  int64_t _freq);
468  int64_t createBond(const char* _symbol, const char* _issueDate,
469  const char* _maturityDate, const char* _settlementDate,
470  double _rate, int64_t _frequency, bool _updateFlag);
477  void addToDataset(double _value, double _weight);
481  void resetDataset();
485  double mean();
489  double stddev();
493  double weightSum();
497  double variance();
501  double kurtosis();
505  double skew();
509  double min();
513  double max();
517  int64_t numSamples();
518 
519  static Logger* _logger;
527  static bond_ptr _getBond(
528  const char* symbol, const char* issueDate,
529  const char* maturityDate, const char* settlementDate,
530  Rate rate, Frequency frequency, bool updateFlag, int &ouputValue, std::string &discrepancyMessage);
534  static Rate _yield(
535  bond_ptr bond, const Real& price, const char* settlementDate);
539  static Real _dv01(
540  bond_ptr bond, const Rate& yield, const char* settlementDate);
544  static Real _price(
545  bond_ptr bond, const Rate& yield, const char* settlementDate);
549  static std::string getDiscrepancyErrorMessage(int outputValue, BondKey cachedBondKey, BondKey inputBondKey);
553  static void dumpCaches();
554 
555 private:
556  static bond_ptr _createBond(
557  const char* issueDate, const char* maturityDate, const char* settlementDate,
558  const Rate& rate, const Frequency& frequency);
559 };
560 
561 APAMA_DECLARE_EPL_PLUGIN(QuantlibPlugin)
562 #endif /*_QUANTLIBPLUGIN_H_ */
double npv
Definition: QuantlibPlugin.hpp:198
Rate getRate()
Definition: QuantlibPlugin.hpp:102
double implVol
Definition: QuantlibPlugin.hpp:202
char * error
Definition: QuantlibPlugin.hpp:226
static Logger * _logger
Definition: QuantlibPlugin.hpp:519
Definition: QuantlibPlugin.hpp:66
Definition: QuantlibPlugin.hpp:131
Definition: QuantlibPlugin.hpp:230
const char * getMaturityDate()
Definition: QuantlibPlugin.hpp:94
double gamma
Definition: QuantlibPlugin.hpp:210
Definition: QuantlibPlugin.hpp:176
double delta
Definition: QuantlibPlugin.hpp:206
Frequency getFrequency()
Definition: QuantlibPlugin.hpp:106
double vega
Definition: QuantlibPlugin.hpp:218
double theta
Definition: QuantlibPlugin.hpp:214
int32_t errNum
Definition: QuantlibPlugin.hpp:194
QuantlibPlugin()
Definition: QuantlibPlugin.hpp:236
const char * getSettlementDate()
Definition: QuantlibPlugin.hpp:98
const char * getIssueDate()
Definition: QuantlibPlugin.hpp:90
double rho
Definition: QuantlibPlugin.hpp:222