Skip to content

Commit b5ca919

Browse files
authored
Use a Python typemap for std::variant<Real, Handle<Quote>> (lballabio#744)
2 parents 5c68dcf + 56b835d commit b5ca919

3 files changed

Lines changed: 161 additions & 0 deletions

File tree

‎SWIG/defaultprobability.i‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,26 @@ namespace std {
196196
%shared_ptr(SpreadCdsHelper)
197197
class SpreadCdsHelper : public DefaultProbabilityHelper {
198198
public:
199+
#if defined(SWIGPYTHON)
200+
%feature("kwargs") SpreadCdsHelper;
201+
SpreadCdsHelper(
202+
const std::variant<Real, Handle<Quote>>& spread,
203+
const Period& tenor,
204+
Integer settlementDays,
205+
const Calendar& calendar,
206+
Frequency frequency,
207+
BusinessDayConvention convention,
208+
DateGeneration::Rule rule,
209+
const DayCounter& dayCounter,
210+
Real recoveryRate,
211+
const Handle<YieldTermStructure>& discountCurve,
212+
bool settlesAccrual = true,
213+
bool paysAtDefaultTime = true,
214+
const Date& startDate = Date(),
215+
const DayCounter& lastPeriodDayCounter = DayCounter(),
216+
bool rebatesAccrual = true,
217+
CreditDefaultSwap::PricingModel model = CreditDefaultSwap::Midpoint);
218+
#else
199219
SpreadCdsHelper(
200220
const Handle<Quote>& spread,
201221
const Period& tenor,
@@ -230,12 +250,35 @@ class SpreadCdsHelper : public DefaultProbabilityHelper {
230250
const DayCounter& lastPeriodDayCounter = DayCounter(),
231251
bool rebatesAccrual = true,
232252
CreditDefaultSwap::PricingModel model = CreditDefaultSwap::Midpoint);
253+
#endif
233254
};
234255

235256

236257
%shared_ptr(UpfrontCdsHelper)
237258
class UpfrontCdsHelper : public DefaultProbabilityHelper {
238259
public:
260+
#if defined(SWIGPYTHON)
261+
%feature("kwargs") UpfrontCdsHelper;
262+
UpfrontCdsHelper(
263+
const std::variant<Real, Handle<Quote>>& upfront,
264+
Rate spread,
265+
const Period& tenor,
266+
Integer settlementDays,
267+
const Calendar& calendar,
268+
Frequency frequency,
269+
BusinessDayConvention convention,
270+
DateGeneration::Rule rule,
271+
const DayCounter& dayCounter,
272+
Real recoveryRate,
273+
const Handle<YieldTermStructure>& discountCurve,
274+
Natural upfrontSettlementDays=0,
275+
bool settlesAccrual = true,
276+
bool paysAtDefaultTime = true,
277+
const Date& startDate = Date(),
278+
const DayCounter& lastPeriodDayCounter = DayCounter(),
279+
bool rebatesAccrual = true,
280+
CreditDefaultSwap::PricingModel model = CreditDefaultSwap::Midpoint);
281+
#else
239282
UpfrontCdsHelper(
240283
const Handle<Quote>& upfront,
241284
Rate spread,
@@ -274,6 +317,7 @@ class UpfrontCdsHelper : public DefaultProbabilityHelper {
274317
const DayCounter& lastPeriodDayCounter = DayCounter(),
275318
bool rebatesAccrual = true,
276319
CreditDefaultSwap::PricingModel model = CreditDefaultSwap::Midpoint);
320+
#endif
277321
};
278322

279323

‎SWIG/marketelements.i‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,62 @@ class Quote : public Observable {
4747

4848
RelinkableHandle<Quote> makeQuoteHandle(Real value);
4949

50+
#if defined(SWIGPYTHON)
51+
52+
%typecheck(SWIG_TYPECHECK_DOUBLE) std::variant<Real, Handle<Quote>>, const std::variant<Real, Handle<Quote>>& %{
53+
{
54+
int res = SWIG_AsVal_double($input, NULL);
55+
$1 = SWIG_CheckState(res);
56+
}
57+
if (!$1) {
58+
int res = SWIG_ConvertPtr($input, 0, $descriptor(Handle<Quote>*), SWIG_POINTER_NO_NULL | 0);
59+
$1 = SWIG_CheckState(res);;
60+
}
61+
%}
62+
63+
%typemap(in) std::variant<Real, Handle<Quote>> (int res, Real val, void * argp) %{
64+
res = SWIG_AsVal_double($input, &val);
65+
if (SWIG_IsOK(res)) {
66+
$1 = val;
67+
} else {
68+
res = SWIG_ConvertPtr($input, &argp, $descriptor(Handle<Quote>*), SWIG_POINTER_NO_NULL);
69+
if (!SWIG_IsOK(res)) {
70+
SWIG_exception_fail(SWIG_ArgError(res), "in method '$symname', argument $argnum of type '$type'");
71+
} else if (!argp) {
72+
SWIG_exception_fail(SWIG_ValueError, "invalid null reference in method '$symname', argument $argnum of type '$type'");
73+
} else {
74+
Handle< Quote > * h = reinterpret_cast< Handle< Quote > * >(argp);
75+
$1 = *h;
76+
if (SWIG_IsNewObj(res)) delete h;
77+
}
78+
}
79+
%}
80+
81+
%typemap(in) const std::variant<Real, Handle<Quote>>& (std::variant<Real, Handle<Quote>> temp, int res, Real val, void * argp) %{
82+
res = SWIG_AsVal_double($input, &val);
83+
if (SWIG_IsOK(res)) {
84+
temp = val;
85+
$1 = &temp;
86+
} else {
87+
res = SWIG_ConvertPtr($input, &argp, $descriptor(Handle<Quote>*), SWIG_POINTER_NO_NULL);
88+
if (!SWIG_IsOK(res)) {
89+
SWIG_exception_fail(SWIG_ArgError(res), "in method '$symname', argument $argnum of type '$type'");
90+
} else if (!argp) {
91+
SWIG_exception_fail(SWIG_ValueError, "invalid null reference in method '$symname', argument $argnum of type '$type'");
92+
} else {
93+
Handle< Quote > * h = reinterpret_cast< Handle< Quote > * >(argp);
94+
temp = *h;
95+
$1 = &temp;
96+
if (SWIG_IsNewObj(res)) delete h;
97+
}
98+
}
99+
%}
100+
101+
#endif
102+
103+
104+
105+
50106
// actual quotes
51107
%{
52108
using QuantLib::SimpleQuote;

‎SWIG/ratehelpers.i‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,66 @@ class OISRateHelper : public RateHelper {
359359
%feature("kwargs") forDates;
360360
#endif
361361
public:
362+
#if defined(SWIGPYTHON)
363+
OISRateHelper(
364+
Natural settlementDays,
365+
const Period& tenor,
366+
const std::variant<Real, Handle<Quote>>& rate,
367+
const ext::shared_ptr<OvernightIndex>& index,
368+
const Handle<YieldTermStructure>& discountingCurve = {},
369+
bool telescopicValueDates = false,
370+
Integer paymentLag = 0,
371+
BusinessDayConvention paymentConvention = Following,
372+
Frequency paymentFrequency = Annual,
373+
const Calendar& paymentCalendar = Calendar(),
374+
const Period& forwardStart = 0 * Days,
375+
const Spread overnightSpread = 0.0,
376+
Pillar::Choice pillar = Pillar::LastRelevantDate,
377+
Date customPillarDate = Date(),
378+
RateAveraging::Type averagingMethod = RateAveraging::Compound,
379+
ext::optional<bool> endOfMonth = ext::nullopt,
380+
ext::optional<Frequency> fixedPaymentFrequency = ext::nullopt,
381+
const Calendar& fixedCalendar = Calendar(),
382+
Natural lookbackDays = Null<Natural>(),
383+
Natural lockoutDays = 0,
384+
bool applyObservationShift = false,
385+
const ext::shared_ptr<FloatingRateCouponPricer>& pricer = {},
386+
DateGeneration::Rule rule = DateGeneration::Backward,
387+
const Calendar& overnightCalendar = Calendar());
388+
%extend {
389+
static ext::shared_ptr<OISRateHelper> forDates(
390+
const Date& startDate,
391+
const Date& endDate,
392+
const std::variant<Real, Handle<Quote>>& rate,
393+
const ext::shared_ptr<OvernightIndex>& index,
394+
const Handle<YieldTermStructure>& discountingCurve = {},
395+
bool telescopicValueDates = false,
396+
Integer paymentLag = 0,
397+
BusinessDayConvention paymentConvention = Following,
398+
Frequency paymentFrequency = Annual,
399+
const Calendar& paymentCalendar = Calendar(),
400+
Spread overnightSpread = 0.0,
401+
Pillar::Choice pillar = Pillar::LastRelevantDate,
402+
Date customPillarDate = Date(),
403+
RateAveraging::Type averagingMethod = RateAveraging::Compound,
404+
ext::optional<bool> endOfMonth = ext::nullopt,
405+
ext::optional<Frequency> fixedPaymentFrequency = ext::nullopt,
406+
const Calendar& fixedCalendar = Calendar(),
407+
Natural lookbackDays = Null<Natural>(),
408+
Natural lockoutDays = 0,
409+
bool applyObservationShift = false,
410+
const ext::shared_ptr<FloatingRateCouponPricer>& pricer = {},
411+
DateGeneration::Rule rule = DateGeneration::Backward,
412+
const Calendar& overnightCalendar = Calendar()) {
413+
return ext::make_shared<OISRateHelper>(
414+
startDate, endDate, rate, index, discountingCurve,
415+
telescopicValueDates, paymentLag, paymentConvention, paymentFrequency,
416+
paymentCalendar, overnightSpread, pillar, customPillarDate, averagingMethod,
417+
endOfMonth, fixedPaymentFrequency, fixedCalendar, lookbackDays, lockoutDays,
418+
applyObservationShift, pricer, rule, overnightCalendar);
419+
}
420+
}
421+
#else
362422
OISRateHelper(
363423
Natural settlementDays,
364424
const Period& tenor,
@@ -417,6 +477,7 @@ class OISRateHelper : public RateHelper {
417477
applyObservationShift, pricer, rule, overnightCalendar);
418478
}
419479
}
480+
#endif
420481
ext::shared_ptr<OvernightIndexedSwap> swap();
421482
};
422483

0 commit comments

Comments
 (0)