CMS Bonds in Python

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

CMS Bonds in Python

Lluis Pujol Bajador
Hello,

I am trying to create CMS Bonds in Python and thus I need to create the relate SWIG interface files.
As I am quite newbie to both SWIG and Quantlib, I am currently stuck with the CMS pricer.

I have succesfully been able to export (at least it builds without complains) :

1) The CMSCoupon:

using QuantLib::CmsCoupon;

typedef boost::shared_ptr<CashFlow> CmsCouponPtr;

%rename(CmsCoupon) CmsCouponPtr;
class CmsCouponPtr : public FloatingRateCouponPtr {
      public:
    %extend {
        CmsCouponPtr(const Date& paymentDate, Real nominal,
                  const Date& startDate, const Date& endDate,
                  Integer fixingDays, const boost::shared_ptr<SwapIndex>& index,
                  Real gearing = 1.0, Spread spread = 0.0,
                  const Date& refPeriodStart = Date(),
                  const Date& refPeriodEnd = Date(),
                  const DayCounter& dayCounter = DayCounter(),
                  bool isInArrears = false) {       
            const boost::shared_ptr<SwapIndex> swi =
          boost::dynamic_pointer_cast<SwapIndex>(index);
            return new CmsCouponPtr(
        new CmsCoupon(paymentDate,nominal,startDate,endDate,
                  fixingDays,swi,gearing,spread,
                  refPeriodStart, refPeriodEnd, dayCounter,isInArrears));
        }
   
     }   

};

2) The CMSBond:
using QuantLib::CmsRateBond;
typedef boost::shared_ptr<Instrument> CmsRateBondPtr;
%rename(CmsRateBond) CmsRateBondPtr;
class CmsRateBondPtr : public BondPtr {
    %feature("kwargs") CmsRateBondPtr;
      public:
    %extend {
        CmsRateBondPtr(Size settlementDays,
                    Real faceAmount,
                    const Schedule& schedule,
                    const SwapIndexPtr& index,
                    const DayCounter& paymentDayCounter,
                    BusinessDayConvention paymentConvention,
                    Natural fixingDays ,
                    const std::vector<Real>& gearings ,
                    const std::vector<Spread>& spreads ,
                    const std::vector<Rate>& caps ,
                    const std::vector<Rate>& floors,
                    bool inArrears = false,
                    Real redemption = 100.0,
                    const Date& issueDate = Date()){
            boost::shared_ptr<SwapIndex> swap = boost::dynamic_pointer_cast<SwapIndex>(index);
            return new CmsRateBondPtr(
                new CmsRateBond(settlementDays,
                                     faceAmount,
                                     schedule,
                                     swap,
                                     paymentDayCounter,
                                     paymentConvention,
                                     fixingDays,
                                     gearings,
                                     spreads,
                                     caps,
                                     floors,
                                     inArrears,
                                     redemption,
                                     issueDate));
        }
      }
    };


But when try to extend the current Pricers in (cashflows.i) as follows: (I paste all the pricers altough I am just addind the CMS pricer based on how the IborCouponPricer is defined).

%{
using QuantLib::FloatingRateCouponPricer;
using QuantLib::IborCouponPricer;
using QuantLib::BlackIborCouponPricer;
using QuantLib::CmsCouponPricer;
typedef boost::shared_ptr<IborCouponPricer> BlackIborCouponPricerPtr;
typedef boost::shared_ptr<FloatingRateCouponPricer> CmsCouponPricerPtr;
%}


%ignore IborCouponPricer;
class IborCouponPricer {
  public:
    Handle<OptionletVolatilityStructure> capletVolatility() const;
    void setCapletVolatility(const Handle<OptionletVolatilityStructure>& v =
                                      Handle<OptionletVolatilityStructure>());
};

%template(IborCouponPricer) boost::shared_ptr<IborCouponPricer>;
%template(FloatingRateCouponPricer) boost::shared_ptr<FloatingRateCouponPricer>;
void setCouponPricer(const Leg&, const boost::shared_ptr<IborCouponPricer>&);


%rename(BlackIborCouponPricer) BlackIborCouponPricerPtr;
class BlackIborCouponPricerPtr : public boost::shared_ptr<IborCouponPricer> {
  public:
    %extend {
        BlackIborCouponPricerPtr(const Handle<OptionletVolatilityStructure>& v =
                                     Handle<OptionletVolatilityStructure>()) {
            return new BlackIborCouponPricerPtr(new BlackIborCouponPricer(v));
        }
    }
};

%rename(CmsCouponPricer) CmsCouponPricerPtr;
class CmsCouponPricerPtr : public boost::shared_ptr<FloatingRateCouponPricer>{
      public:
       CmsCouponPricerPtr(const Handle<SwaptionVolatilityStructure>& v =
                                        Handle<SwaptionVolatilityStructure>()) {
            return new CmsCouponPricerPtr(new CmsCouponPricer(v));
           
   
       }
};

I get the following error (translated to english).

QuantLib/quantlib_wrap.cpp(127710) : error C2664: 'boost::shared_ptr<T>::shared_
ptr(const boost::shared_ptr<T> &)' : cannot convert parameter 1 from 'co
nst QuantLib::Handle<T>' to 'const boost::shared_ptr<T> &'
        with
        [
            T=QuantLib::FloatingRateCouponPricer
        ]
        and
        [
            T=QuantLib::SwaptionVolatilityStructure
        ]
        and
        [
            T=QuantLib::FloatingRateCouponPricer
        ]
        Reason: Cannot find the conversion from 'const QuantLib::Handle<T>'
 to 'const boost::shared_ptr<T>'
        with
        [
            T=QuantLib::SwaptionVolatilityStructure
        ]
        and
        [
            T=QuantLib::FloatingRateCouponPricer
        ]

It is not clear to me if I have also to export FloatingRateCouponPricer...

As I am quite lost with SWIG I don't know if someone could hint me a way to solve this or have any guidance on how to create the SWIG interface files for Quantlib.

Thanks in advance for your help.

Lluís

------------------------------------------------------------------------------
Simplify data backup and recovery for your virtual environment with vRanger.
Installation's a snap, and flexible recovery options mean your data is safe,
secure and there when you need it. Data protection magic?
Nope - It's vRanger. Get your free trial download today.
http://p.sf.net/sfu/quest-sfdev2dev
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: CMS Bonds in Python

Luigi Ballabio

Hi Lluis,
        from the code you pasted, it looks like you forgot to put the the
CmsCouponPricerPtr constructor inside %extend (you can copy how it's
done in the BlackIborCouponPricerPtr interface.)

However, I think you're making the wrong associations.  You're modeling
(CmsCouponPricer, FloatingRateCouponPricer) after
(BlackIborCouponPricer, IborCouponPricer).  Instead, CmsCouponPricer is
at the same level as IborCouponPricer, as they're both inherited from
FloatingRateCouponPricer; and in the role of BlackIborCouponPricer,
you'll have one of the CMS pricers defined in conundrumpricer.hpp.
Exporting CmsCouponPricer alone won't work, since it's an abstract
class.  What I'd suggest is something like:

%{
using QuantLib::CmsCouponPricer;
using QuantLib::AnalyticHaganPricer  // for instance
typedef boost::shared_ptr<CmsCouponPricer>
QuantLib::AnalyticHaganPricerPtr;
%}

%ignore QuantLib::CmsCouponPricer;
class CmsCouponPricer {
  public:
    Handle<SwaptionVolatilityStructure> swaptionVolatility() const;
    void setSwaptionVolatility(
        const Handle<SwaptionVolatilityStructure>& v =
            Handle<SwaptionVolatilityStructure>());
};

%template(CmsCouponPricer) boost::shared_ptr<CmsCouponPricer>;

%rename(AnalyticHaganPricer) AnalyticHaganPricerPtr;
class AnalyticHaganPricerPtr : public boost::shared_ptr<CmsCouponPricer>
{
  public:
    %extend {
        AnalyticHaganPricerPtr(
            const Handle<SwaptionVolatilityStructure>& v,
            GFunctionFactory::YieldCurveModel model,
            const Handle<Quote>& meanReversion) {
          return new AnalyticHaganPricerPtr(
              new AnalyticHaganPricer(v, model, meanReversion));
        }
    }
};

You'll also have to export the GFunctionFactory class before
AnalyticHaganPricer, and you'll need to overload setPricer so that it
also take a CmsCouponPricer.  Write again if you get stuck, and I'll try
to find some time and send a patch.

Luigi


On Thu, 2011-06-23 at 09:58 +0200, Lluis Pujol Bajador wrote:

> Hello,
>
> I am trying to create CMS Bonds in Python and thus I need to create
> the relate SWIG interface files.
> As I am quite newbie to both SWIG and Quantlib, I am currently stuck
> with the CMS pricer.
>
> I have succesfully been able to export (at least it builds without
> complains) :
>
> 1) The CMSCoupon:
>
> using QuantLib::CmsCoupon;
>
> typedef boost::shared_ptr<CashFlow> CmsCouponPtr;
>
> %rename(CmsCoupon) CmsCouponPtr;
> class CmsCouponPtr : public FloatingRateCouponPtr {
>       public:
>     %extend {
>         CmsCouponPtr(const Date& paymentDate, Real nominal,
>                   const Date& startDate, const Date& endDate,
>                   Integer fixingDays, const
> boost::shared_ptr<SwapIndex>& index,
>                   Real gearing = 1.0, Spread spread = 0.0,
>                   const Date& refPeriodStart = Date(),
>                   const Date& refPeriodEnd = Date(),
>                   const DayCounter& dayCounter = DayCounter(),
>                   bool isInArrears = false) {        
>             const boost::shared_ptr<SwapIndex> swi =
>           boost::dynamic_pointer_cast<SwapIndex>(index);
>             return new CmsCouponPtr(
>         new CmsCoupon(paymentDate,nominal,startDate,endDate,
>                   fixingDays,swi,gearing,spread,
>                   refPeriodStart, refPeriodEnd,
> dayCounter,isInArrears));
>         }
>    
>      }    
>
> };
>
> 2) The CMSBond:
> using QuantLib::CmsRateBond;
> typedef boost::shared_ptr<Instrument> CmsRateBondPtr;
> %rename(CmsRateBond) CmsRateBondPtr;
> class CmsRateBondPtr : public BondPtr {
>     %feature("kwargs") CmsRateBondPtr;
>       public:
>     %extend {
>         CmsRateBondPtr(Size settlementDays,
>                     Real faceAmount,
>                     const Schedule& schedule,
>                     const SwapIndexPtr& index,
>                     const DayCounter& paymentDayCounter,
>                     BusinessDayConvention paymentConvention,
>                     Natural fixingDays ,
>                     const std::vector<Real>& gearings ,
>                     const std::vector<Spread>& spreads ,
>                     const std::vector<Rate>& caps ,
>                     const std::vector<Rate>& floors,
>                     bool inArrears = false,
>                     Real redemption = 100.0,
>                     const Date& issueDate = Date()){
>             boost::shared_ptr<SwapIndex> swap =
> boost::dynamic_pointer_cast<SwapIndex>(index);
>             return new CmsRateBondPtr(
>                 new CmsRateBond(settlementDays,
>                                      faceAmount,
>                                      schedule,
>                                      swap,
>                                      paymentDayCounter,
>                                      paymentConvention,
>                                      fixingDays,
>                                      gearings,
>                                      spreads,
>                                      caps,
>                                      floors,
>                                      inArrears,
>                                      redemption,
>                                      issueDate));
>         }
>       }
>     };
>
>
> But when try to extend the current Pricers in (cashflows.i) as
> follows: (I paste all the pricers altough I am just addind the CMS
> pricer based on how the IborCouponPricer is defined).
>
> %{
> using QuantLib::FloatingRateCouponPricer;
> using QuantLib::IborCouponPricer;
> using QuantLib::BlackIborCouponPricer;
> using QuantLib::CmsCouponPricer;
> typedef boost::shared_ptr<IborCouponPricer> BlackIborCouponPricerPtr;
> typedef boost::shared_ptr<FloatingRateCouponPricer>
> CmsCouponPricerPtr;
> %}
>
>
> %ignore IborCouponPricer;
> class IborCouponPricer {
>   public:
>     Handle<OptionletVolatilityStructure> capletVolatility() const;
>     void setCapletVolatility(const
> Handle<OptionletVolatilityStructure>& v =
>
> Handle<OptionletVolatilityStructure>());
> };
>
> %template(IborCouponPricer) boost::shared_ptr<IborCouponPricer>;
> %template(FloatingRateCouponPricer)
> boost::shared_ptr<FloatingRateCouponPricer>;
> void setCouponPricer(const Leg&, const
> boost::shared_ptr<IborCouponPricer>&);
>
>
> %rename(BlackIborCouponPricer) BlackIborCouponPricerPtr;
> class BlackIborCouponPricerPtr : public
> boost::shared_ptr<IborCouponPricer> {
>   public:
>     %extend {
>         BlackIborCouponPricerPtr(const
> Handle<OptionletVolatilityStructure>& v =
>
> Handle<OptionletVolatilityStructure>()) {
>             return new BlackIborCouponPricerPtr(new
> BlackIborCouponPricer(v));
>         }
>     }
> };
>
> %rename(CmsCouponPricer) CmsCouponPricerPtr;
> class CmsCouponPricerPtr : public
> boost::shared_ptr<FloatingRateCouponPricer>{
>       public:
>        CmsCouponPricerPtr(const Handle<SwaptionVolatilityStructure>& v
> =
>
> Handle<SwaptionVolatilityStructure>()) {
>             return new CmsCouponPricerPtr(new CmsCouponPricer(v));
>            
>    
>        }
> };
>
> I get the following error (translated to english).
>
> QuantLib/quantlib_wrap.cpp(127710) : error C2664:
> 'boost::shared_ptr<T>::shared_
> ptr(const boost::shared_ptr<T> &)' : cannot convert parameter 1 from
> 'co
> nst QuantLib::Handle<T>' to 'const boost::shared_ptr<T> &'
>         with
>         [
>             T=QuantLib::FloatingRateCouponPricer
>         ]
>         and
>         [
>             T=QuantLib::SwaptionVolatilityStructure
>         ]
>         and
>         [
>             T=QuantLib::FloatingRateCouponPricer
>         ]
>         Reason: Cannot find the conversion from 'const
> QuantLib::Handle<T>'
>  to 'const boost::shared_ptr<T>'
>         with
>         [
>             T=QuantLib::SwaptionVolatilityStructure
>         ]
>         and
>         [
>             T=QuantLib::FloatingRateCouponPricer
>         ]
>
> It is not clear to me if I have also to export
> FloatingRateCouponPricer...
>
> As I am quite lost with SWIG I don't know if someone could hint me a
> way to solve this or have any guidance on how to create the SWIG
> interface files for Quantlib.
>
> Thanks in advance for your help.
>
> Lluís
> ------------------------------------------------------------------------------
> Simplify data backup and recovery for your virtual environment with vRanger.
> Installation's a snap, and flexible recovery options mean your data is safe,
> secure and there when you need it. Data protection magic?
> Nope - It's vRanger. Get your free trial download today.
> http://p.sf.net/sfu/quest-sfdev2dev
> _______________________________________________ QuantLib-users mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/quantlib-users

--

The rule on staying alive as a forecaster is to give 'em a number or
give 'em a date, but never give 'em both at once.
-- Jane Bryant Quinn



------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2d-c2
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: CMS Bonds in Python

Lluis Pujol Bajador
Hi Luigi,

I believe that I've succesfully exposed to Python the pricer, and
GFunctionFactory and overloaded the setPricer.

In order to test it I am trying to port the cms test of the C++
testsuite. (probably too optimistic for me). I am currently stuck on the
SwaptionVolCube2 class.

I exported it like this:

%{
using QuantLib::SwaptionVolCube2;
typedef boost::shared_ptr<SwaptionVolatilityStructure>
     SwaptionVolCube2Ptr;
%}

%rename(SwaptionVolCube2) SwaptionVolCube2Ptr;
class SwaptionVolCube2Ptr
     : public boost::shared_ptr<SwaptionVolatilityStructure>  {
   public:
     %extend {
         SwaptionVolCube2Ptr(const Handle<SwaptionVolatilityStructure>&  atmVolStructure,
                             const std::vector<Period>&  optionTenors,
                             const std::vector<Period>&  swapTenors,
                             const std::vector<Spread>&  strikeSpreads,
                             const std::vector<std::vector<Handle<Quote>  >  >&  volSpreads,
                             const boost::shared_ptr<SwapIndex>&  swapIndexBase,
                             const boost::shared_ptr<SwapIndex>&  shortSwapIndexBase,
                             bool vegaWeightedSmileFit) {
             return new SwaptionVolCube2Ptr(
                 new SwaptionVolCube2(atmVolStructure,optionTenors,swapTenors,strikeSpreads,
                                        volSpreads, swapIndexBase, shortSwapIndexBase, vegaWeightedSmileFit));
         }
     }
};


It wraps and compiles without problem, but when I try to use it with a
Python list of lists of Handles, I get the following error complaining
about the vector of vectors of Handles

Traceback (most recent call last):
   File "D:\QuantLib\QuantLib-SWIG\Python\test\cms.py", line 114, in<module>
     vegaWeightedSmileFit)
   File "D:\Python26\Lib\site-packages\QuantLib\QuantLib.py", line 4432, in __init__
     this = _QuantLib.new_SwaptionVolCube2(*args)
TypeError: in method 'new_SwaptionVolCube2', argument 5 of type 'std::vector<  std::vector<  Handle<  Quote>,std::allocator<  Handle<  Quote>  >  >,std::allocator<  std::vector<  Handle<  Quote>,std::allocator<  Handle<  Quote>  >  >  >  >  const&'


How can I create a vector of vectors of Handles in Python?.
Any workaround?.


Thanks in advance.

Lluís.



El 27/06/2011 17:23, Luigi Ballabio escribió:

> Hi Lluis,
> from the code you pasted, it looks like you forgot to put the the
> CmsCouponPricerPtr constructor inside %extend (you can copy how it's
> done in the BlackIborCouponPricerPtr interface.)
>
> However, I think you're making the wrong associations.  You're modeling
> (CmsCouponPricer, FloatingRateCouponPricer) after
> (BlackIborCouponPricer, IborCouponPricer).  Instead, CmsCouponPricer is
> at the same level as IborCouponPricer, as they're both inherited from
> FloatingRateCouponPricer; and in the role of BlackIborCouponPricer,
> you'll have one of the CMS pricers defined in conundrumpricer.hpp.
> Exporting CmsCouponPricer alone won't work, since it's an abstract
> class.  What I'd suggest is something like:
>
> %{
> using QuantLib::CmsCouponPricer;
> using QuantLib::AnalyticHaganPricer  // for instance
> typedef boost::shared_ptr<CmsCouponPricer>
> QuantLib::AnalyticHaganPricerPtr;
> %}
>
> %ignore QuantLib::CmsCouponPricer;
> class CmsCouponPricer {
>    public:
>      Handle<SwaptionVolatilityStructure>  swaptionVolatility() const;
>      void setSwaptionVolatility(
>          const Handle<SwaptionVolatilityStructure>&  v =
>              Handle<SwaptionVolatilityStructure>());
> };
>
> %template(CmsCouponPricer) boost::shared_ptr<CmsCouponPricer>;
>
> %rename(AnalyticHaganPricer) AnalyticHaganPricerPtr;
> class AnalyticHaganPricerPtr : public boost::shared_ptr<CmsCouponPricer>
> {
>    public:
>      %extend {
>          AnalyticHaganPricerPtr(
>              const Handle<SwaptionVolatilityStructure>&  v,
>              GFunctionFactory::YieldCurveModel model,
>              const Handle<Quote>&  meanReversion) {
>            return new AnalyticHaganPricerPtr(
>                new AnalyticHaganPricer(v, model, meanReversion));
>          }
>      }
> };
>
> You'll also have to export the GFunctionFactory class before
> AnalyticHaganPricer, and you'll need to overload setPricer so that it
> also take a CmsCouponPricer.  Write again if you get stuck, and I'll try
> to find some time and send a patch.
>
> Luigi
>
>
> On Thu, 2011-06-23 at 09:58 +0200, Lluis Pujol Bajador wrote:
>> Hello,
>>
>> I am trying to create CMS Bonds in Python and thus I need to create
>> the relate SWIG interface files.
>> As I am quite newbie to both SWIG and Quantlib, I am currently stuck
>> with the CMS pricer.
>>
>> I have succesfully been able to export (at least it builds without
>> complains) :
>>
>> 1) The CMSCoupon:
>>
>> using QuantLib::CmsCoupon;
>>
>> typedef boost::shared_ptr<CashFlow>  CmsCouponPtr;
>>
>> %rename(CmsCoupon) CmsCouponPtr;
>> class CmsCouponPtr : public FloatingRateCouponPtr {
>>        public:
>>      %extend {
>>          CmsCouponPtr(const Date&  paymentDate, Real nominal,
>>                    const Date&  startDate, const Date&  endDate,
>>                    Integer fixingDays, const
>> boost::shared_ptr<SwapIndex>&  index,
>>                    Real gearing = 1.0, Spread spread = 0.0,
>>                    const Date&  refPeriodStart = Date(),
>>                    const Date&  refPeriodEnd = Date(),
>>                    const DayCounter&  dayCounter = DayCounter(),
>>                    bool isInArrears = false) {
>>              const boost::shared_ptr<SwapIndex>  swi =
>>            boost::dynamic_pointer_cast<SwapIndex>(index);
>>              return new CmsCouponPtr(
>>          new CmsCoupon(paymentDate,nominal,startDate,endDate,
>>                    fixingDays,swi,gearing,spread,
>>                    refPeriodStart, refPeriodEnd,
>> dayCounter,isInArrears));
>>          }
>>
>>       }
>>
>> };
>>
>> 2) The CMSBond:
>> using QuantLib::CmsRateBond;
>> typedef boost::shared_ptr<Instrument>  CmsRateBondPtr;
>> %rename(CmsRateBond) CmsRateBondPtr;
>> class CmsRateBondPtr : public BondPtr {
>>      %feature("kwargs") CmsRateBondPtr;
>>        public:
>>      %extend {
>>          CmsRateBondPtr(Size settlementDays,
>>                      Real faceAmount,
>>                      const Schedule&  schedule,
>>                      const SwapIndexPtr&  index,
>>                      const DayCounter&  paymentDayCounter,
>>                      BusinessDayConvention paymentConvention,
>>                      Natural fixingDays ,
>>                      const std::vector<Real>&  gearings ,
>>                      const std::vector<Spread>&  spreads ,
>>                      const std::vector<Rate>&  caps ,
>>                      const std::vector<Rate>&  floors,
>>                      bool inArrears = false,
>>                      Real redemption = 100.0,
>>                      const Date&  issueDate = Date()){
>>              boost::shared_ptr<SwapIndex>  swap =
>> boost::dynamic_pointer_cast<SwapIndex>(index);
>>              return new CmsRateBondPtr(
>>                  new CmsRateBond(settlementDays,
>>                                       faceAmount,
>>                                       schedule,
>>                                       swap,
>>                                       paymentDayCounter,
>>                                       paymentConvention,
>>                                       fixingDays,
>>                                       gearings,
>>                                       spreads,
>>                                       caps,
>>                                       floors,
>>                                       inArrears,
>>                                       redemption,
>>                                       issueDate));
>>          }
>>        }
>>      };
>>
>>
>> But when try to extend the current Pricers in (cashflows.i) as
>> follows: (I paste all the pricers altough I am just addind the CMS
>> pricer based on how the IborCouponPricer is defined).
>>
>> %{
>> using QuantLib::FloatingRateCouponPricer;
>> using QuantLib::IborCouponPricer;
>> using QuantLib::BlackIborCouponPricer;
>> using QuantLib::CmsCouponPricer;
>> typedef boost::shared_ptr<IborCouponPricer>  BlackIborCouponPricerPtr;
>> typedef boost::shared_ptr<FloatingRateCouponPricer>
>> CmsCouponPricerPtr;
>> %}
>>
>>
>> %ignore IborCouponPricer;
>> class IborCouponPricer {
>>    public:
>>      Handle<OptionletVolatilityStructure>  capletVolatility() const;
>>      void setCapletVolatility(const
>> Handle<OptionletVolatilityStructure>&  v =
>>
>> Handle<OptionletVolatilityStructure>());
>> };
>>
>> %template(IborCouponPricer) boost::shared_ptr<IborCouponPricer>;
>> %template(FloatingRateCouponPricer)
>> boost::shared_ptr<FloatingRateCouponPricer>;
>> void setCouponPricer(const Leg&, const
>> boost::shared_ptr<IborCouponPricer>&);
>>
>>
>> %rename(BlackIborCouponPricer) BlackIborCouponPricerPtr;
>> class BlackIborCouponPricerPtr : public
>> boost::shared_ptr<IborCouponPricer>  {
>>    public:
>>      %extend {
>>          BlackIborCouponPricerPtr(const
>> Handle<OptionletVolatilityStructure>&  v =
>>
>> Handle<OptionletVolatilityStructure>()) {
>>              return new BlackIborCouponPricerPtr(new
>> BlackIborCouponPricer(v));
>>          }
>>      }
>> };
>>
>> %rename(CmsCouponPricer) CmsCouponPricerPtr;
>> class CmsCouponPricerPtr : public
>> boost::shared_ptr<FloatingRateCouponPricer>{
>>        public:
>>         CmsCouponPricerPtr(const Handle<SwaptionVolatilityStructure>&  v
>> =
>>
>> Handle<SwaptionVolatilityStructure>()) {
>>              return new CmsCouponPricerPtr(new CmsCouponPricer(v));
>>
>>
>>         }
>> };
>>
>> I get the following error (translated to english).
>>
>> QuantLib/quantlib_wrap.cpp(127710) : error C2664:
>> 'boost::shared_ptr<T>::shared_
>> ptr(const boost::shared_ptr<T>  &)' : cannot convert parameter 1 from
>> 'co
>> nst QuantLib::Handle<T>' to 'const boost::shared_ptr<T>  &'
>>          with
>>          [
>>              T=QuantLib::FloatingRateCouponPricer
>>          ]
>>          and
>>          [
>>              T=QuantLib::SwaptionVolatilityStructure
>>          ]
>>          and
>>          [
>>              T=QuantLib::FloatingRateCouponPricer
>>          ]
>>          Reason: Cannot find the conversion from 'const
>> QuantLib::Handle<T>'
>>   to 'const boost::shared_ptr<T>'
>>          with
>>          [
>>              T=QuantLib::SwaptionVolatilityStructure
>>          ]
>>          and
>>          [
>>              T=QuantLib::FloatingRateCouponPricer
>>          ]
>>
>> It is not clear to me if I have also to export
>> FloatingRateCouponPricer...
>>
>> As I am quite lost with SWIG I don't know if someone could hint me a
>> way to solve this or have any guidance on how to create the SWIG
>> interface files for Quantlib.
>>
>> Thanks in advance for your help.
>>
>> Lluís
>> ------------------------------------------------------------------------------
>> Simplify data backup and recovery for your virtual environment with vRanger.
>> Installation's a snap, and flexible recovery options mean your data is safe,
>> secure and there when you need it. Data protection magic?
>> Nope - It's vRanger. Get your free trial download today.
>> http://p.sf.net/sfu/quest-sfdev2dev
>> _______________________________________________ QuantLib-users mailing [hidden email]  https://lists.sourceforge.net/lists/listinfo/quantlib-users


------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2d-c2
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: CMS Bonds in Python

Francis Duffy
Hi Lluís,

I think that you should add the additional lines indicated below to the file marketelements.i.

Regards,
Francis.

namespace std {
    %template(QuoteVector) vector<boost::shared_ptr<Quote> >;
    %template(QuoteHandleVector) vector<Handle<Quote> >;
    %template(RelinkableQuoteHandleVector) vector<RelinkableHandle<Quote> >;
    
    // Additional lines required:
    // -----------------------------------
    %template(QuoteVVector) vector<vector<boost::shared_ptr<Quote> > >;
    %template(QuoteHandleVVector) vector<vector<Handle<Quote> > >;
    %template(RelinkableQuoteHandleVVector) vector<vector<RelinkableHandle<Quote> > >;
}

On Wed, Jul 6, 2011 at 12:03 AM, Lluis Pujol <[hidden email]> wrote:
Hi Luigi,

I believe that I've succesfully exposed to Python the pricer, and
GFunctionFactory and overloaded the setPricer.

In order to test it I am trying to port the cms test of the C++
testsuite. (probably too optimistic for me). I am currently stuck on the
SwaptionVolCube2 class.

I exported it like this:

%{
using QuantLib::SwaptionVolCube2;
typedef boost::shared_ptr<SwaptionVolatilityStructure>
    SwaptionVolCube2Ptr;
%}

%rename(SwaptionVolCube2) SwaptionVolCube2Ptr;
class SwaptionVolCube2Ptr
    : public boost::shared_ptr<SwaptionVolatilityStructure>  {
  public:
    %extend {
        SwaptionVolCube2Ptr(const Handle<SwaptionVolatilityStructure>&  atmVolStructure,
                            const std::vector<Period>&  optionTenors,
                            const std::vector<Period>&  swapTenors,
                            const std::vector<Spread>&  strikeSpreads,
                            const std::vector<std::vector<Handle<Quote>  >  >&  volSpreads,
                            const boost::shared_ptr<SwapIndex>&  swapIndexBase,
                            const boost::shared_ptr<SwapIndex>&  shortSwapIndexBase,
                            bool vegaWeightedSmileFit) {
            return new SwaptionVolCube2Ptr(
                new SwaptionVolCube2(atmVolStructure,optionTenors,swapTenors,strikeSpreads,
                                       volSpreads, swapIndexBase, shortSwapIndexBase, vegaWeightedSmileFit));
        }
    }
};


It wraps and compiles without problem, but when I try to use it with a
Python list of lists of Handles, I get the following error complaining
about the vector of vectors of Handles

Traceback (most recent call last):
  File "D:\QuantLib\QuantLib-SWIG\Python\test\cms.py", line 114, in<module>
    vegaWeightedSmileFit)
  File "D:\Python26\Lib\site-packages\QuantLib\QuantLib.py", line 4432, in __init__
    this = _QuantLib.new_SwaptionVolCube2(*args)
TypeError: in method 'new_SwaptionVolCube2', argument 5 of type 'std::vector<  std::vector<  Handle<  Quote>,std::allocator<  Handle<  Quote>  >  >,std::allocator<  std::vector<  Handle<  Quote>,std::allocator<  Handle<  Quote>  >  >  >  >  const&'


How can I create a vector of vectors of Handles in Python?.
Any workaround?.


Thanks in advance.

Lluís.



El 27/06/2011 17:23, Luigi Ballabio escribió:
> Hi Lluis,
>       from the code you pasted, it looks like you forgot to put the the
> CmsCouponPricerPtr constructor inside %extend (you can copy how it's
> done in the BlackIborCouponPricerPtr interface.)
>
> However, I think you're making the wrong associations.  You're modeling
> (CmsCouponPricer, FloatingRateCouponPricer) after
> (BlackIborCouponPricer, IborCouponPricer).  Instead, CmsCouponPricer is
> at the same level as IborCouponPricer, as they're both inherited from
> FloatingRateCouponPricer; and in the role of BlackIborCouponPricer,
> you'll have one of the CMS pricers defined in conundrumpricer.hpp.
> Exporting CmsCouponPricer alone won't work, since it's an abstract
> class.  What I'd suggest is something like:
>
> %{
> using QuantLib::CmsCouponPricer;
> using QuantLib::AnalyticHaganPricer  // for instance
> typedef boost::shared_ptr<CmsCouponPricer>
> QuantLib::AnalyticHaganPricerPtr;
> %}
>
> %ignore QuantLib::CmsCouponPricer;
> class CmsCouponPricer {
>    public:
>      Handle<SwaptionVolatilityStructure>  swaptionVolatility() const;
>      void setSwaptionVolatility(
>          const Handle<SwaptionVolatilityStructure>&  v =
>              Handle<SwaptionVolatilityStructure>());
> };
>
> %template(CmsCouponPricer) boost::shared_ptr<CmsCouponPricer>;
>
> %rename(AnalyticHaganPricer) AnalyticHaganPricerPtr;
> class AnalyticHaganPricerPtr : public boost::shared_ptr<CmsCouponPricer>
> {
>    public:
>      %extend {
>          AnalyticHaganPricerPtr(
>              const Handle<SwaptionVolatilityStructure>&  v,
>              GFunctionFactory::YieldCurveModel model,
>              const Handle<Quote>&  meanReversion) {
>            return new AnalyticHaganPricerPtr(
>                new AnalyticHaganPricer(v, model, meanReversion));
>          }
>      }
> };
>
> You'll also have to export the GFunctionFactory class before
> AnalyticHaganPricer, and you'll need to overload setPricer so that it
> also take a CmsCouponPricer.  Write again if you get stuck, and I'll try
> to find some time and send a patch.
>
> Luigi
>
>
> On Thu, 2011-06-23 at 09:58 +0200, Lluis Pujol Bajador wrote:
>> Hello,
>>
>> I am trying to create CMS Bonds in Python and thus I need to create
>> the relate SWIG interface files.
>> As I am quite newbie to both SWIG and Quantlib, I am currently stuck
>> with the CMS pricer.
>>
>> I have succesfully been able to export (at least it builds without
>> complains) :
>>
>> 1) The CMSCoupon:
>>
>> using QuantLib::CmsCoupon;
>>
>> typedef boost::shared_ptr<CashFlow>  CmsCouponPtr;
>>
>> %rename(CmsCoupon) CmsCouponPtr;
>> class CmsCouponPtr : public FloatingRateCouponPtr {
>>        public:
>>      %extend {
>>          CmsCouponPtr(const Date&  paymentDate, Real nominal,
>>                    const Date&  startDate, const Date&  endDate,
>>                    Integer fixingDays, const
>> boost::shared_ptr<SwapIndex>&  index,
>>                    Real gearing = 1.0, Spread spread = 0.0,
>>                    const Date&  refPeriodStart = Date(),
>>                    const Date&  refPeriodEnd = Date(),
>>                    const DayCounter&  dayCounter = DayCounter(),
>>                    bool isInArrears = false) {
>>              const boost::shared_ptr<SwapIndex>  swi =
>>            boost::dynamic_pointer_cast<SwapIndex>(index);
>>              return new CmsCouponPtr(
>>          new CmsCoupon(paymentDate,nominal,startDate,endDate,
>>                    fixingDays,swi,gearing,spread,
>>                    refPeriodStart, refPeriodEnd,
>> dayCounter,isInArrears));
>>          }
>>
>>       }
>>
>> };
>>
>> 2) The CMSBond:
>> using QuantLib::CmsRateBond;
>> typedef boost::shared_ptr<Instrument>  CmsRateBondPtr;
>> %rename(CmsRateBond) CmsRateBondPtr;
>> class CmsRateBondPtr : public BondPtr {
>>      %feature("kwargs") CmsRateBondPtr;
>>        public:
>>      %extend {
>>          CmsRateBondPtr(Size settlementDays,
>>                      Real faceAmount,
>>                      const Schedule&  schedule,
>>                      const SwapIndexPtr&  index,
>>                      const DayCounter&  paymentDayCounter,
>>                      BusinessDayConvention paymentConvention,
>>                      Natural fixingDays ,
>>                      const std::vector<Real>&  gearings ,
>>                      const std::vector<Spread>&  spreads ,
>>                      const std::vector<Rate>&  caps ,
>>                      const std::vector<Rate>&  floors,
>>                      bool inArrears = false,
>>                      Real redemption = 100.0,
>>                      const Date&  issueDate = Date()){
>>              boost::shared_ptr<SwapIndex>  swap =
>> boost::dynamic_pointer_cast<SwapIndex>(index);
>>              return new CmsRateBondPtr(
>>                  new CmsRateBond(settlementDays,
>>                                       faceAmount,
>>                                       schedule,
>>                                       swap,
>>                                       paymentDayCounter,
>>                                       paymentConvention,
>>                                       fixingDays,
>>                                       gearings,
>>                                       spreads,
>>                                       caps,
>>                                       floors,
>>                                       inArrears,
>>                                       redemption,
>>                                       issueDate));
>>          }
>>        }
>>      };
>>
>>
>> But when try to extend the current Pricers in (cashflows.i) as
>> follows: (I paste all the pricers altough I am just addind the CMS
>> pricer based on how the IborCouponPricer is defined).
>>
>> %{
>> using QuantLib::FloatingRateCouponPricer;
>> using QuantLib::IborCouponPricer;
>> using QuantLib::BlackIborCouponPricer;
>> using QuantLib::CmsCouponPricer;
>> typedef boost::shared_ptr<IborCouponPricer>  BlackIborCouponPricerPtr;
>> typedef boost::shared_ptr<FloatingRateCouponPricer>
>> CmsCouponPricerPtr;
>> %}
>>
>>
>> %ignore IborCouponPricer;
>> class IborCouponPricer {
>>    public:
>>      Handle<OptionletVolatilityStructure>  capletVolatility() const;
>>      void setCapletVolatility(const
>> Handle<OptionletVolatilityStructure>&  v =
>>
>> Handle<OptionletVolatilityStructure>());
>> };
>>
>> %template(IborCouponPricer) boost::shared_ptr<IborCouponPricer>;
>> %template(FloatingRateCouponPricer)
>> boost::shared_ptr<FloatingRateCouponPricer>;
>> void setCouponPricer(const Leg&, const
>> boost::shared_ptr<IborCouponPricer>&);
>>
>>
>> %rename(BlackIborCouponPricer) BlackIborCouponPricerPtr;
>> class BlackIborCouponPricerPtr : public
>> boost::shared_ptr<IborCouponPricer>  {
>>    public:
>>      %extend {
>>          BlackIborCouponPricerPtr(const
>> Handle<OptionletVolatilityStructure>&  v =
>>
>> Handle<OptionletVolatilityStructure>()) {
>>              return new BlackIborCouponPricerPtr(new
>> BlackIborCouponPricer(v));
>>          }
>>      }
>> };
>>
>> %rename(CmsCouponPricer) CmsCouponPricerPtr;
>> class CmsCouponPricerPtr : public
>> boost::shared_ptr<FloatingRateCouponPricer>{
>>        public:
>>         CmsCouponPricerPtr(const Handle<SwaptionVolatilityStructure>&  v
>> =
>>
>> Handle<SwaptionVolatilityStructure>()) {
>>              return new CmsCouponPricerPtr(new CmsCouponPricer(v));
>>
>>
>>         }
>> };
>>
>> I get the following error (translated to english).
>>
>> QuantLib/quantlib_wrap.cpp(127710) : error C2664:
>> 'boost::shared_ptr<T>::shared_
>> ptr(const boost::shared_ptr<T>  &)' : cannot convert parameter 1 from
>> 'co
>> nst QuantLib::Handle<T>' to 'const boost::shared_ptr<T>  &'
>>          with
>>          [
>>              T=QuantLib::FloatingRateCouponPricer
>>          ]
>>          and
>>          [
>>              T=QuantLib::SwaptionVolatilityStructure
>>          ]
>>          and
>>          [
>>              T=QuantLib::FloatingRateCouponPricer
>>          ]
>>          Reason: Cannot find the conversion from 'const
>> QuantLib::Handle<T>'
>>   to 'const boost::shared_ptr<T>'
>>          with
>>          [
>>              T=QuantLib::SwaptionVolatilityStructure
>>          ]
>>          and
>>          [
>>              T=QuantLib::FloatingRateCouponPricer
>>          ]
>>
>> It is not clear to me if I have also to export
>> FloatingRateCouponPricer...
>>
>> As I am quite lost with SWIG I don't know if someone could hint me a
>> way to solve this or have any guidance on how to create the SWIG
>> interface files for Quantlib.
>>
>> Thanks in advance for your help.
>>
>> Lluís
>> ------------------------------------------------------------------------------
>> Simplify data backup and recovery for your virtual environment with vRanger.
>> Installation's a snap, and flexible recovery options mean your data is safe,
>> secure and there when you need it. Data protection magic?
>> Nope - It's vRanger. Get your free trial download today.
>> http://p.sf.net/sfu/quest-sfdev2dev
>> _______________________________________________ QuantLib-users mailing [hidden email]  https://lists.sourceforge.net/lists/listinfo/quantlib-users


------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2d-c2
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users


------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2d-c2
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: CMS Bonds in Python

Lluis Pujol Bajador
In reply to this post by Lluis Pujol Bajador
Thank Francis.

It works.

I had also to cast the SwapIndex to Index.
 
Lluís

On Wed 06/07/11 19:21 , Francis Duffy <[hidden email]> wrote:

Hi Lluís,

I think that you should add the additional lines indicated below to the file marketelements.i.

Regards,
Francis.

namespace std {
    %template(QuoteVector) vector<boost::shared_ptr<Quote> >;
    %template(QuoteHandleVector) vector<Handle<Quote> >;
    %template(RelinkableQuoteHandleVector) vector<RelinkableHandle<Quote> >;
    
    // Additional lines required:
    // -----------------------------------
    %template(QuoteVVector) vector<vector<boost::shared_ptr<Quote> > >;
    %template(QuoteHandleVVector) vector<vector<Handle<Quote> > >;
    %template(RelinkableQuoteHandleVVector) vector<vector<RelinkableHandle<Quote> > >;
}

On Wed, Jul 6, 2011 at 12:03 AM, Lluis Pujol <<a href="javascript:top.opencompose('lpb@tinet.org','','','')">lpb@...> wrote:
Hi Luigi,

I believe that I've succesfully exposed to Python the pricer, and
GFunctionFactory and overloaded the setPricer.

In order to test it I am trying to port the cms test of the C++
testsuite. (probably too optimistic for me). I am currently stuck on the
SwaptionVolCube2 class.

I exported it like this:

%{
using QuantLib::SwaptionVolCube2;
typedef boost::shared_ptr<SwaptionVolatilityStructure>
    SwaptionVolCube2Ptr;
%}

%rename(SwaptionVolCube2) SwaptionVolCube2Ptr;
class SwaptionVolCube2Ptr
    : public boost::shared_ptr<SwaptionVolatilityStructure>  {
  public:
    %extend {
        SwaptionVolCube2Ptr(const Handle<SwaptionVolatilityStructure>&  atmVolStructure,
                            const std::vector<Period>&  optionTenors,
                            const std::vector<Period>&  swapTenors,
                            const std::vector<Spread>&  strikeSpreads,
                            const std::vector<std::vector<Handle<Quote>  >  >&  volSpreads,
                            const boost::shared_ptr<SwapIndex>&  swapIndexBase,
                            const boost::shared_ptr<SwapIndex>&  shortSwapIndexBase,
                            bool vegaWeightedSmileFit) {
            return new SwaptionVolCube2Ptr(
                new SwaptionVolCube2(atmVolStructure,optionTenors,swapTenors,strikeSpreads,
                                       volSpreads, swapIndexBase, shortSwapIndexBase, vegaWeightedSmileFit));
        }
    }
};


It wraps and compiles without problem, but when I try to use it with a
Python list of lists of Handles, I get the following error complaining
about the vector of vectors of Handles

Traceback (most recent call last):
  File "D:\QuantLib\QuantLib-SWIG\Python\test\cms.py", line 114, in<module>
    vegaWeightedSmileFit)
  File "D:\Python26\Lib\site-packages\QuantLib\QuantLib.py", line 4432, in __init__
    this = _QuantLib.new_SwaptionVolCube2(*args)
TypeError: in method 'new_SwaptionVolCube2', argument 5 of type 'std::vector<  std::vector<  Handle<  Quote>,std::allocator<  Handle<  Quote>  >  >,std::allocator<  std::vector<  Handle<  Quote>,std::allocator<  Handle<  Quote>  >  >  >  >  const&'


How can I create a vector of vectors of Handles in Python?.
Any workaround?.


Thanks in advance.

Lluís.



El 27/06/2011 17:23, Luigi Ballabio escribió:
> Hi Lluis,
>       from the code you pasted, it looks like you forgot to put the the
> CmsCouponPricerPtr constructor inside %extend (you can copy how it's
> done in the BlackIborCouponPricerPtr interface.)
>
> However, I think you're making the wrong associations.  You're modeling
> (CmsCouponPricer, FloatingRateCouponPricer) after
> (BlackIborCouponPricer, IborCouponPricer).  Instead, CmsCouponPricer is
> at the same level as IborCouponPricer, as they're both inherited from
> FloatingRateCouponPricer; and in the role of BlackIborCouponPricer,
> you'll have one of the CMS pricers defined in conundrumpricer.hpp.
> Exporting CmsCouponPricer alone won't work, since it's an abstract
> class.  What I'd suggest is something like:
>
> %{
> using QuantLib::CmsCouponPricer;
> using QuantLib::AnalyticHaganPricer  // for instance
> typedef boost::shared_ptr<CmsCouponPricer>
> QuantLib::AnalyticHaganPricerPtr;
> %}
>
> %ignore QuantLib::CmsCouponPricer;
> class CmsCouponPricer {
>    public:
>      Handle<SwaptionVolatilityStructure>  swaptionVolatility() const;
>      void setSwaptionVolatility(
>          const Handle<SwaptionVolatilityStructure>&  v =
>              Handle<SwaptionVolatilityStructure>());
> };
>
> %template(CmsCouponPricer) boost::shared_ptr<CmsCouponPricer>;
>
> %rename(AnalyticHaganPricer) AnalyticHaganPricerPtr;
> class AnalyticHaganPricerPtr : public boost::shared_ptr<CmsCouponPricer>
> {
>    public:
>      %extend {
>          AnalyticHaganPricerPtr(
>              const Handle<SwaptionVolatilityStructure>&  v,
>              GFunctionFactory::YieldCurveModel model,
>              const Handle<Quote>&  meanReversion) {
>            return new AnalyticHaganPricerPtr(
>                new AnalyticHaganPricer(v, model, meanReversion));
>          }
>      }
> };
>
> You'll also have to export the GFunctionFactory class before
> AnalyticHaganPricer, and you'll need to overload setPricer so that it
> also take a CmsCouponPricer.  Write again if you get stuck, and I'll try
> to find some time and send a patch.
>
> Luigi
>
>
> On Thu, 2011-06-23 at 09:58 +0200, Lluis Pujol Bajador wrote:
>> Hello,
>>
>> I am trying to create CMS Bonds in Python and thus I need to create
>> the relate SWIG interface files.
>> As I am quite newbie to both SWIG and Quantlib, I am currently stuck
>> with the CMS pricer.
>>
>> I have succesfully been able to export (at least it builds without
>> complains) :
>>
>> 1) The CMSCoupon:
>>
>> using QuantLib::CmsCoupon;
>>
>> typedef boost::shared_ptr<CashFlow>  CmsCouponPtr;
>>
>> %rename(CmsCoupon) CmsCouponPtr;
>> class CmsCouponPtr : public FloatingRateCouponPtr {
>>        public:
>>      %extend {
>>          CmsCouponPtr(const Date&  paymentDate, Real nominal,
>>                    const Date&  startDate, const Date&  endDate,
>>                    Integer fixingDays, const
>> boost::shared_ptr<SwapIndex>&  index,
>>                    Real gearing = 1.0, Spread spread = 0.0,
>>                    const Date&  refPeriodStart = Date(),
>>                    const Date&  refPeriodEnd = Date(),
>>                    const DayCounter&  dayCounter = DayCounter(),
>>                    bool isInArrears = false) {
>>              const boost::shared_ptr<SwapIndex>  swi =
>>            boost::dynamic_pointer_cast<SwapIndex>(index);
>>              return new CmsCouponPtr(
>>          new CmsCoupon(paymentDate,nominal,startDate,endDate,
>>                    fixingDays,swi,gearing,spread,
>>                    refPeriodStart, refPeriodEnd,
>> dayCounter,isInArrears));
>>          }
>>
>>       }
>>
>> };
>>
>> 2) The CMSBond:
>> using QuantLib::CmsRateBond;
>> typedef boost::shared_ptr<Instrument>  CmsRateBondPtr;
>> %rename(CmsRateBond) CmsRateBondPtr;
>> class CmsRateBondPtr : public BondPtr {
>>      %feature("kwargs") CmsRateBondPtr;
>>        public:
>>      %extend {
>>          CmsRateBondPtr(Size settlementDays,
>>                      Real faceAmount,
>>                      const Schedule&  schedule,
>>                      const SwapIndexPtr&  index,
>>                      const DayCounter&  paymentDayCounter,
>>                      BusinessDayConvention paymentConvention,
>>                      Natural fixingDays ,
>>                      const std::vector<Real>&  gearings ,
>>                      const std::vector<Spread>&  spreads ,
>>                      const std::vector<Rate>&  caps ,
>>                      const std::vector<Rate>&  floors,
>>                      bool inArrears = false,
>>                      Real redemption = 100.0,
>>                      const Date&  issueDate = Date()){
>>              boost::shared_ptr<SwapIndex>  swap =
>> boost::dynamic_pointer_cast<SwapIndex>(index);
>>              return new CmsRateBondPtr(
>>                  new CmsRateBond(settlementDays,
>>                                       faceAmount,
>>                                       schedule,
>>                                       swap,
>>                                       paymentDayCounter,
>>                                       paymentConvention,
>>                                       fixingDays,
>>                                       gearings,
>>                                       spreads,
>>                                       caps,
>>                                       floors,
>>                                       inArrears,
>>                                       redemption,
>>                                       issueDate));
>>          }
>>        }
>>      };
>>
>>
>> But when try to extend the current Pricers in (cashflows.i) as
>> follows: (I paste all the pricers altough I am just addind the CMS
>> pricer based on how the IborCouponPricer is defined).
>>
>> %{
>> using QuantLib::FloatingRateCouponPricer;
>> using QuantLib::IborCouponPricer;
>> using QuantLib::BlackIborCouponPricer;
>> using QuantLib::CmsCouponPricer;
>> typedef boost::shared_ptr<IborCouponPricer>  BlackIborCouponPricerPtr;
>> typedef boost::shared_ptr<FloatingRateCouponPricer>
>> CmsCouponPricerPtr;
>> %}
>>
>>
>> %ignore IborCouponPricer;
>> class IborCouponPricer {
>>    public:
>>      Handle<OptionletVolatilityStructure>  capletVolatility() const;
>>      void setCapletVolatility(const
>> Handle<OptionletVolatilityStructure>&  v =
>>
>> Handle<OptionletVolatilityStructure>());
>> };
>>
>> %template(IborCouponPricer) boost::shared_ptr<IborCouponPricer>;
>> %template(FloatingRateCouponPricer)
>> boost::shared_ptr<FloatingRateCouponPricer>;
>> void setCouponPricer(const Leg&, const
>> boost::shared_ptr<IborCouponPricer>&);
>>
>>
>> %rename(BlackIborCouponPricer) BlackIborCouponPricerPtr;
>> class BlackIborCouponPricerPtr : public
>> boost::shared_ptr<IborCouponPricer>  {
>>    public:
>>      %extend {
>>          BlackIborCouponPricerPtr(const
>> Handle<OptionletVolatilityStructure>&  v =
>>
>> Handle<OptionletVolatilityStructure>()) {
>>              return new BlackIborCouponPricerPtr(new
>> BlackIborCouponPricer(v));
>>          }
>>      }
>> };
>>
>> %rename(CmsCouponPricer) CmsCouponPricerPtr;
>> class CmsCouponPricerPtr : public
>> boost::shared_ptr<FloatingRateCouponPricer>{
>>        public:
>>         CmsCouponPricerPtr(const Handle<SwaptionVolatilityStructure>&  v
>> =
>>
>> Handle<SwaptionVolatilityStructure>()) {
>>              return new CmsCouponPricerPtr(new CmsCouponPricer(v));
>>
>>
>>         }
>> };
>>
>> I get the following error (translated to english).
>>
>> QuantLib/quantlib_wrap.cpp(127710) : error C2664:
>> 'boost::shared_ptr<T>::shared_
>> ptr(const boost::shared_ptr<T>  &)' : cannot convert parameter 1 from
>> 'co
>> nst QuantLib::Handle<T>' to 'const boost::shared_ptr<T>  &'
>>          with
>>          [
>>              T=QuantLib::FloatingRateCouponPricer
>>          ]
>>          and
>>          [
>>              T=QuantLib::SwaptionVolatilityStructure
>>          ]
>>          and
>>          [
>>              T=QuantLib::FloatingRateCouponPricer
>>          ]
>>          Reason: Cannot find the conversion from 'const
>> QuantLib::Handle<T>'
>>   to 'const boost::shared_ptr<T>'
>>          with
>>          [
>>              T=QuantLib::SwaptionVolatilityStructure
>>          ]
>>          and
>>          [
>>              T=QuantLib::FloatingRateCouponPricer
>>          ]
>>
>> It is not clear to me if I have also to export
>> FloatingRateCouponPricer...
>>
>> As I am quite lost with SWIG I don't know if someone could hint me a
>> way to solve this or have any guidance on how to create the SWIG
>> interface files for Quantlib.
>>
>> Thanks in advance for your help.
>>
>> Lluís
>> ------------------------------------------------------------------------------
>> Simplify data backup and recovery for your virtual environment with vRanger.
>> Installation's a snap, and flexible recovery options mean your data is safe,
>> secure and there when you need it. Data protection magic?
>> Nope - It's vRanger. Get your free trial download today.
>> http://p.sf.net/sfu/quest-sfdev2dev
>> _______________________________________________ QuantLib-users mailing <a href="javascript:top.opencompose('listQuantLib-users@lists.sourceforge.net','','','')">listQuantLib-users@...  https://lists.sourceforge.net/lists/listinfo/quantlib-users


------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2d-c2
_______________________________________________
QuantLib-users mailing list
<a href="javascript:top.opencompose('QuantLib-users@lists.sourceforge.net','','','')">QuantLib-users@...
https://lists.sourceforge.net/lists/listinfo/quantlib-users


------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2d-c2
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: CMS Bonds in Python

Luigi Ballabio
On Wed, 2011-07-06 at 22:42 +0200, Lluis Pujol Bajador wrote:
> Thank Francis.
>
> It works.
>
> I had also to cast the SwapIndex to Index.

Lluis,
        once you have a working version, just post it here and I'll be happy to
include it in the library.

Luigi


--

Brady's First Law of Problem Solving:
When confronted by a difficult problem, you can solve it more
easily by reducing it to the question, "How would the Lone
Ranger have handled this?"



------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2d-c2
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users