File: Options.i

package info (click to toggle)
quantlib-ruby 0.2.1.cvs20020322-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 972 kB
  • ctags: 1,309
  • sloc: cpp: 16,141; ruby: 2,578; makefile: 59
file content (156 lines) | stat: -rw-r--r-- 4,315 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156


/*
 Copyright (C) 2000, 2001, 2002 RiskMap srl

 This file is part of QuantLib, a free-software/open-source library
 for financial quantitative analysts and developers - http://quantlib.org/

 QuantLib is free software: you can redistribute it and/or modify it under the
 terms of the QuantLib license.  You should have received a copy of the
 license along with this program; if not, please email ferdinando@ametrano.net
 The license is also available online at http://quantlib.org/html/license.html

 This program is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 FOR A PARTICULAR PURPOSE.  See the license for more details.
*/

// $Id: Options.i,v 1.6 2002/01/16 14:51:31 nando Exp $

#ifndef quantlib_options_i
#define quantlib_options_i

%include Instruments.i
%include MarketElements.i
%include TermStructures.i
%include Types.i

// typemap option types to corresponding strings

%{
using QuantLib::Option;
typedef Option::Type OptionType;
using QuantLib::StringFormatter;
%}

%typemap(ruby,in) OptionType (OptionType temp),
                  const OptionType & (OptionType temp) {
    if (TYPE($source) == T_STRING) {
        std::string s(STR2CSTR($source));
        s = StringFormatter::toLowercase(s);
        if (s == "c" || s == "call")          temp = Option::Call;
        else if (s == "p" || s == "put")      temp = Option::Put;
        else if (s == "s" || s == "straddle") temp = Option::Straddle;
        else {
            rb_raise(rb_eTypeError,"unknown option type");
        }
    } else {
        rb_raise(rb_eTypeError,"not an option type");
    }
    $target = &temp;
};

%typemap(ruby,out) OptionType, const OptionType & {
    switch (*$source) {
      case Option::Call:     $target = rb_str_new2("Call");     break;
      case Option::Put:      $target = rb_str_new2("Put");      break;
      case Option::Straddle: $target = rb_str_new2("Straddle"); break;
    }
};

%typemap(ruby,ret) OptionType {
    delete $source;
}


// handles to pricing engines

%{
using QuantLib::OptionPricingEngine;
typedef Handle<OptionPricingEngine> OptionEngineHandle;
%}

class OptionEngineHandle {
  private:
    // abstract class - no constructor
    OptionEngineHandle();
  public:
    ~OptionEngineHandle();
};


// plain options and engines

%{
using QuantLib::Instruments::PlainOption;
typedef Handle<PlainOption> PlainOptionHandle;
%}

// fake inheritance
%name(PlainOption) class PlainOptionHandle : public InstrumentHandle {
  public:
    // constructor redefined below
    ~PlainOptionHandle();
};

%addmethods PlainOptionHandle {
    void crash() {}
    PlainOptionHandle(OptionType type,
        MarketElementRelinkableHandle underlying, double strike,
        TermStructureRelinkableHandle dividendYield,
        TermStructureRelinkableHandle riskFreeRate,
        Date exerciseDate,
        MarketElementRelinkableHandle volatility,
        OptionEngineHandle engine) {
            return new PlainOptionHandle(new PlainOption(type,underlying,
                strike,dividendYield,riskFreeRate,exerciseDate,volatility,
                engine,"","option"));
    }
    double delta() {
        return (*self)->delta();
    }
    double gamma() {
        return (*self)->gamma();
    }
    double theta() {
        return (*self)->theta();
    }
    double vega() {
        return (*self)->vega();
    }
    double rho() {
        return (*self)->rho();
    }
    double dividendRho() {
        return (*self)->dividendRho();
    }
    double impliedVolatility(double targetValue, double accuracy = 1.0e-4,
                             size_t maxEvaluations = 100,
                             double minVol = 1.0e-4, double maxVol = 4.0) {
        return (*self)->impliedVolatility(
            targetValue,accuracy,maxEvaluations,minVol,maxVol);
    }
}

%{
using QuantLib::Pricers::EuropeanEngine;
typedef Handle<EuropeanEngine> EuropeanEngineHandle;
%}

// fake inheritance
%name(EuropeanEngine) class EuropeanEngineHandle : public OptionEngineHandle {
  public:
    // constructor redefined below
    ~EuropeanEngineHandle();
};

%addmethods EuropeanEngineHandle {
    void crash() {}
    EuropeanEngineHandle() {
        return new EuropeanEngineHandle(new EuropeanEngine);
    }
}


#endif