Apama Capital Markets Foundation  10.5.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-2017 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 
231 class myIncrementalStatistics : public QuantLib::IncrementalStatistics
232 {
233 public:
235  void remove(Real value, Real weight);
236 };
237 
239 class QuantlibPlugin : public EPLPlugin<QuantlibPlugin>
240 {
241 public:
245  QuantlibPlugin() :base_plugin_t("QuantlibPlugin"){}
246  ~QuantlibPlugin(){}
250  static void initialize(base_plugin_t::method_data_t &md);
265  custom_t<OptionResultsChunk>
266  modelPrice(double _underlying, double _strike, const char* _currentBussinessDate,
267  const char* _maturity, double _riskFreeRate, double _volatility,
268  double _dividendYield, const char* _t, const char* _modelName);
283  custom_t<OptionResultsChunk>
284  modelImplVol(double _price, double _underlying, double _strike,
285  const char* _currentBussinessDate, const char* _maturityDate,
286  double _riskFreeRate, double _dividendYield, const char* _t,
287  const char* _modelName);
302  custom_t<OptionResultsChunk>
303  modelGreeks(double _underlying, double _strike, const char* _currentBussinessDate,
304  const char* _maturity, double _riskFreeRate, double _volatility,
305  double _dividendYield, const char* _t, const char* _modelName);
321  custom_t<OptionResultsChunk>
322  btcoxrossrubinsteinGreeks(double _underlying, double _strike, const char* _currentBussinessDate,
323  const char* _maturity, double _riskFreeRate, double _volatility,
324  double _dividendYield, int64_t _timeSteps, const char* _t);
339  custom_t<OptionResultsChunk>
340  btcoxrossrubinsteinPrice(double _underlying, double _strike, const char* _currentBussinessDate,
341  const char* _maturity, double _riskFreeRate, double _volatility,
342  double _dividendYield, int64_t _timeSteps, const char* _t);
357  custom_t<OptionResultsChunk>
358  btcoxrossrubinsteinImplVol(double _price, double _underlying, double _strike,
359  const char* _currentBussinessDate, const char* _maturity,
360  double _riskFreeRate, double _dividendYield, int64_t _timeSteps,
361  const char* _t);
368  const char* error(const custom_t<OptionResultsChunk> &results);
375  double npv(const custom_t<OptionResultsChunk> &results);
382  double delta(const custom_t<OptionResultsChunk> &results);
389  double gamma(const custom_t<OptionResultsChunk> &results);
396  double theta(const custom_t<OptionResultsChunk> &results);
403  double vega(const custom_t<OptionResultsChunk> &results);
410  double rho(const custom_t<OptionResultsChunk> &results);
417  double implVol(const custom_t<OptionResultsChunk> &results);
430  double yield(const char* _symbol, double _price, const char* _issueDate,
431  const char* _maturityDate, const char* _settlementDate, double _rate,
432  int64_t _freq);
446  double dv01(const char* _symbol, double _yield, const char* _issueDate,
447  const char* _maturityDate, const char* _settlementDate, double _rate,
448  int64_t _freq);
461  double price(const char* _symbol, double _yield, const char* _issueDate,
462  const char* _maturityDate, const char* _settlementDate, double _rate,
463  int64_t _freq);
477  int64_t createBond(const char* _symbol, const char* _issueDate,
478  const char* _maturityDate, const char* _settlementDate,
479  double _rate, int64_t _frequency, bool _updateFlag);
486  void addToDataset(double _value, double _weight);
493  void removeFromDataset(double _value, double _weight);
497  void resetDataset();
501  double mean();
505  double stddev();
509  double weightSum();
513  double variance();
517  double kurtosis();
521  double skew();
525  double min();
529  double max();
533  int64_t numSamples();
534 
535  static Logger* _logger;
543  static bond_ptr _getBond(
544  const char* symbol, const char* issueDate,
545  const char* maturityDate, const char* settlementDate,
546  Rate rate, Frequency frequency, bool updateFlag, int &ouputValue, std::string &discrepancyMessage);
550  static Rate _yield(
551  bond_ptr bond, const Real& price, const char* settlementDate);
555  static Real _dv01(
556  bond_ptr bond, const Rate& yield, const char* settlementDate);
560  static Real _price(
561  bond_ptr bond, const Rate& yield, const char* settlementDate);
565  static std::string getDiscrepancyErrorMessage(int outputValue, BondKey cachedBondKey, BondKey inputBondKey);
569  static void dumpCaches();
570 
571 private:
572  static bond_ptr _createBond(
573  const char* issueDate, const char* maturityDate, const char* settlementDate,
574  const Rate& rate, const Frequency& frequency);
575 };
576 
577 APAMA_DECLARE_EPL_PLUGIN(QuantlibPlugin)
578 #endif /*_QUANTLIBPLUGIN_H_ */
double npv
Definition: QuantlibPlugin.hpp:198
Rate getRate()
Definition: QuantlibPlugin.hpp:102
Definition: QuantlibPlugin.hpp:231
double implVol
Definition: QuantlibPlugin.hpp:202
char * error
Definition: QuantlibPlugin.hpp:226
static Logger * _logger
Definition: QuantlibPlugin.hpp:535
Definition: QuantlibPlugin.hpp:66
Definition: QuantlibPlugin.hpp:131
Definition: QuantlibPlugin.hpp:239
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:245
const char * getSettlementDate()
Definition: QuantlibPlugin.hpp:98
const char * getIssueDate()
Definition: QuantlibPlugin.hpp:90
double rho
Definition: QuantlibPlugin.hpp:222