Login  Register

Re: writing SWIG file for forward/FRA

Posted by Francis Duffy on Oct 07, 2012; 9:19am
URL: http://quantlib.414.s1.nabble.com/writing-SWIG-file-for-forward-FRA-tp12526p12527.html

Hi Tawanda,

I amended your interface file a little as shown below to export the class ForwardRateAgreement. I do not export the class Forward because I do not think that you need to. I think that your main issue is the use of  boost::shared_ptr<IborIndex>& in the argument list of the constructor. I think that this should be IborIndexPtr& like it is in the constructor of VanillaSwap in the file swap.i.

I have attached a short java example that I used to check that the constructor works. I am not sure what language you are targeting though so this may not be helpful.

Regards,
Francis.

forwardrateagreement.i

-------------------------------

#ifndef quantlib_forwardrateagreement_i
#define quantlib_forwardrateagreement_i
 
%include instruments.i
%include termstructures.i
%include interestrate.i

 

%{
using QuantLib::Position;
using QuantLib::ForwardRateAgreement;
typedef boost::shared_ptr<Instrument> ForwardRateAgreementPtr;
%}
 
struct Position {
    enum Type {Long, Short};
};

 

%rename(ForwardRateAgreement) ForwardRateAgreementPtr;
class ForwardRateAgreementPtr : public boost::shared_ptr<Instrument> {
   public:
     %extend {
         ForwardRateAgreementPtr(const Date& valueDate,
                 const Date& maturityDate,
                 Position::Type type,
                 Rate strikeForwardRate,
                 Real notionalAmount,
                 const IborIndexPtr& index,
                 const Handle<YieldTermStructure>& discountCurve = Handle<YieldTermStructure>()) {
            
             boost::shared_ptr<IborIndex> libor =
                 boost::dynamic_pointer_cast<IborIndex>(index);

 

             return new ForwardRateAgreementPtr(
                     new ForwardRateAgreement(valueDate,
                         maturityDate,
                         type,
                         strikeForwardRate,
                         notionalAmount,
                         libor,
                         discountCurve));
         }
        
         Real spotIncome(const Handle<YieldTermStructure>& incomeDiscountCurve) const {
             return boost::dynamic_pointer_cast<ForwardRateAgreement>(*self)
                 ->spotIncome(incomeDiscountCurve);
         }

 

         Real spotValue() const {
             return boost::dynamic_pointer_cast<ForwardRateAgreement>(*self)
                 ->spotValue();
         }

 

         InterestRate forwardRate() const {
             return boost::dynamic_pointer_cast<ForwardRateAgreement>(*self)
                 ->forwardRate();
         }
     }
};

 

#endif

 

On Sat, Oct 6, 2012 at 4:35 PM, Tawanda Gwena <[hidden email]> wrote:
I am trying to expose the FRA instrument. I have managed to write the swig file and it compiles. However, I cannot create the object. It consistently produces the error:


Wrong arguments for overloaded function 'new_ForwardRateAgreement'
 Possible C/C++ prototypes are:
   ForwardRateAgreementPtr::ForwardRateAgreementPtr(Date const &,Date const &,Position::Type,Rate,Real,boost::shared_ptr< IborIndex > const &,QuantLib::Handle< YieldTermStructure > const &)
   ForwardRateAgreementPtr::ForwardRateAgreementPtr(Date const &,Date const &,Position::Type,Rate,Real,boost::shared_ptr< IborIndex > const &)

What am I doing wrong? Below are the contents of the swig file:



#ifndef quantlib_forwards_i
#define quantlib_forwards_i

%include instruments.i
%include termstructures.i
%include cashflows.i
%include grid.i
%include stl.i
%{
using QuantLib::Position;
using QuantLib::Forward;
using QuantLib::ForwardRateAgreement;
using QuantLib::Seasonality;
typedef boost::shared_ptr<Instrument> ForwardPtr;
typedef boost::shared_ptr<Instrument> ForwardRateAgreementPtr;
%}

struct Position {
    enum Type { Long, Short};
};

%rename(Forward) ForwardPtr;
class ForwardPtr : public boost::shared_ptr<Instrument> {
  public:
    %extend {

        Date settlementDate() {
            return boost::dynamic_pointer_cast<Forward>(*self)->settlementDate();
        }
        BusinessDayConvention businessDayConvention() {
            return boost::dynamic_pointer_cast<Forward>(*self)->businessDayConvention();
        }
        Rate spotValue() {
            return boost::dynamic_pointer_cast<Forward>(*self)->spotValue();
        }
        Rate forwardValue() {
            return boost::dynamic_pointer_cast<Forward>(*self)->forwardValue();
        }
        InterestRate impliedYield(Real underlyingSpotValue,
                                  Real forwardValue,
                                  Date settlementDate,
                                  Compounding compoundingConvention,
                                  DayCounter dayCounter) {
           return boost::dynamic_pointer_cast<Forward>(*self)->impliedYield(
                                  underlyingSpotValue,
                                  forwardValue,
                                  settlementDate,
                                  compoundingConvention,
                                  dayCounter);
        }
        }
  protected: /* added this later, but did not help*/
        Forward(const DayCounter& dayCounter,
                const Calendar& calendar,
                BusinessDayConvention businessDayConvention,
                Natural settlementDays,
                const boost::shared_ptr<Payoff>& payoff,
                const Date& valueDate,
                const Date& maturityDate,
                const Handle<YieldTermStructure>& discountCurve =
                                                Handle<YieldTermStructure>()) {
                return new ForwardPtr(new Forward(dayCounter,
                        calendar,
                        businessDayConvention,
                        settlementDays,
                        payoff,
                        valueDate,
                        maturityDate,
                        discountCurve));
        }

};

%rename(ForwardRateAgreement) ForwardRateAgreementPtr;
class ForwardRateAgreementPtr : public ForwardPtr {
  public:
    %extend {
        ForwardRateAgreementPtr(
                const Date& valueDate,
                const Date& maturityDate,
                Position::Type type,
                Rate strikeForwardRate,
                Real notionalAmount,
                const boost::shared_ptr<IborIndex>& index,
                const Handle<YieldTermStructure>& discountCurve =
                                                 Handle<YieldTermStructure>()) {
                            return new ForwardRateAgreementPtr(
                                         new ForwardRateAgreement(valueDate,
                                                                  maturityDate,
                                                                  type,
                                                                  strikeForwardRate,
                                                                  notionalAmount,
                                                                  index,
                                                                  discountCurve));
            }


       Real spotIncome(const Handle<YieldTermStructure>& incomeDiscountCurve) const {
          return boost::dynamic_pointer_cast<ForwardRateAgreement>(*self)-> spotIncome(
                            incomeDiscountCurve);
           }
      /*Real spotValue() {
        return boost::dynamic_pointer_cast<ForwardRateAgreement>(*self)->spotValue();
      }*/
    }
};


#endif
------------------------------------------------------------------------------
Don't let slow site performance ruin your business. Deploy New Relic APM
Deploy New Relic app performance management and know exactly
what is happening inside your Ruby, Python, PHP, Java, and .NET app
Try New Relic at no cost today and get our sweet Data Nerd shirt too!
http://p.sf.net/sfu/newrelic-dev2dev
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev


------------------------------------------------------------------------------
Don't let slow site performance ruin your business. Deploy New Relic APM
Deploy New Relic app performance management and know exactly
what is happening inside your Ruby, Python, PHP, Java, and .NET app
Try New Relic at no cost today and get our sweet Data Nerd shirt too!
http://p.sf.net/sfu/newrelic-dev2dev
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev

FRA.java (2K) Download Attachment