forked from enthought/pyql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexercise.pyx
More file actions
104 lines (84 loc) · 2.99 KB
/
exercise.pyx
File metadata and controls
104 lines (84 loc) · 2.99 KB
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
from cython.operator cimport dereference as deref, preincrement as preinc
from libcpp cimport bool
from libcpp.vector cimport vector
from quantlib.handle cimport shared_ptr
from quantlib.time.date cimport Date, _pydate_from_qldate
from quantlib.time._date cimport Date as QlDate
cdef class Exercise:
"""Base exercise class
Attributes
----------
American
Bermudan
European
"""
American = Type.American
Bermudan = Type.Bermudan
European = Type.European
def __str__(self):
return "Exercise type: {}".format(self.type.name)
@property
def last_date(self):
return _pydate_from_qldate(self._thisptr.get().lastDate())
def dates(self):
""" all exercise dates"""
cdef vector[QlDate].const_iterator it = self._thisptr.get().dates().const_begin()
cdef list r = []
while it != self._thisptr.get().dates().end():
r.append(_pydate_from_qldate(deref(it)))
preinc(it)
@property
def type(self):
return self._thisptr.get().type()
cdef class EuropeanExercise(Exercise):
"""European exercise
A European option can only be exercised at one (expiry) date.
"""
def __init__(self, Date exercise_date not None):
self._thisptr.reset(
new _exercise.EuropeanExercise(
exercise_date._thisptr
)
)
cdef class AmericanExercise(Exercise):
"""American exercise
An American option can be exercised at any time between to
predefined dates; the first date might be amitted, in which
case the option can be exercied at any time before the expiry.
Parameters
----------
latest_exercise_date : :class:`~quantlib.time.date.Date`
Latest exercise date for the option
earliest_exercise_date : :class:`~quantlib.time.date.Date` | None
Earliest exercise date for the option
"""
def __init__(self, Date latest_exercise_date, Date earliest_exercise_date=None):
if earliest_exercise_date is not None:
self._thisptr = shared_ptr[_exercise.Exercise]( \
new _exercise.AmericanExercise(
earliest_exercise_date._thisptr,
latest_exercise_date._thisptr
)
)
else:
self._thisptr = shared_ptr[_exercise.Exercise]( \
new _exercise.AmericanExercise(
latest_exercise_date._thisptr
)
)
cdef class BermudanExercise(Exercise):
""" Bermudan exercise
A Bermudan option can only be exercised at a set of fixed dates.
Parameters
----------
dates : list of exercise dates
payoff_at_expiry : bool
"""
def __init__(self, list dates, bool payoff_at_expiry=False):
cdef vector[QlDate] c_dates
for d in dates:
c_dates.push_back((<Date?>d)._thisptr)
self._thisptr.reset(
new _exercise.BermudanExercise(c_dates,
payoff_at_expiry)
)