PageRenderTime 51ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/quantlib/instruments/payoffs.pyx

https://github.com/phenaff/pyql
Cython | 104 lines | 74 code | 26 blank | 4 comment | 12 complexity | e85da5f46ec3a4194ebb8f8e6f03a877 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. include '../types.pxi'
  2. # cython imports
  3. cimport _option
  4. cimport _payoffs
  5. cdef public enum OptionType:
  6. Put = _option.Put
  7. Call = _option.Call
  8. PAYOFF_TO_STR = {Call:'Call', Put:'Put'}
  9. def str_to_option_type(name):
  10. if name.lower() == 'call':
  11. option_type = Call
  12. elif name.lower() == 'put':
  13. option_type = Put
  14. return option_type
  15. cdef class Payoff:
  16. def __cinit__(self):
  17. self._thisptr = NULL
  18. def __dealloc__(self):
  19. if self._thisptr is not NULL:
  20. del self._thisptr
  21. def __str__(self):
  22. if self._thisptr is not NULL:
  23. return 'Payoff: %s' % self._thisptr.get().name().c_str()
  24. cdef set_payoff(self, shared_ptr[_payoffs.Payoff] payoff):
  25. if self._thisptr != NULL:
  26. del self._thisptr
  27. self._thisptr = NULL
  28. if payoff.get() == NULL:
  29. raise ValueError('Setting the payoff with a null pointer.')
  30. self._thisptr = new shared_ptr[_payoffs.Payoff](payoff)
  31. cdef _payoffs.PlainVanillaPayoff* _get_payoff(PlainVanillaPayoff payoff):
  32. return <_payoffs.PlainVanillaPayoff*>payoff._thisptr.get()
  33. cdef class PlainVanillaPayoff(Payoff):
  34. """ Plain vanilla payoff.
  35. Parameters
  36. ----------
  37. option_type: int or str
  38. The type of option, can be either Call or Put
  39. strike: float
  40. The strike value
  41. from_qlpayoff: bool, optional
  42. For internal use only
  43. Properties
  44. ----------
  45. exercise: Exercise
  46. Read-only property that returns an Exercise instance
  47. payoff: PlainVanilaPayoff
  48. Read-only property that returns a PlainVanillaPayoff instance
  49. """
  50. def __init__(self, option_type, float strike, from_qlpayoff=False):
  51. if isinstance(option_type, basestring):
  52. option_type = str_to_option_type(option_type)
  53. if not from_qlpayoff:
  54. self._thisptr = new shared_ptr[_payoffs.Payoff]( \
  55. new _payoffs.PlainVanillaPayoff(
  56. <_option.Type>option_type, <Real>strike
  57. )
  58. )
  59. else:
  60. # instance is created based on a cpp QuantLib payoff
  61. # user is supposed to call the set_payoff method afterwards.
  62. # This can be dangerous as we use an instance with a NULL ptr ...
  63. pass
  64. def __str__(self):
  65. return 'Payoff: %s %s @ %f' % (
  66. _get_payoff(self).name().c_str(),
  67. PAYOFF_TO_STR[_get_payoff(self).optionType()],
  68. _get_payoff(self).strike()
  69. )
  70. property type:
  71. """ Exposes the internal option type.
  72. The type can be converted to str using the PAYOFF_TO_STR dictionnary.
  73. """
  74. def __get__(self):
  75. return _get_payoff(self).optionType()
  76. property strike:
  77. def __get__(self):
  78. return _get_payoff(self).strike()