QuantLib OpenOffice/Excel 产量/价格函数

发布于 2024-10-20 19:52:41 字数 4593 浏览 2 评论 0 原文

有人可以提供一个如何使用 QuantLib 复制 Excel/OpenOffice YIELDPRICE 函数的示例吗?

我有一些例子,但我还不太明白所有的设置。当我尝试更改某些值时,我要么得到零,要么得到一些无意义的值。理想情况下,我想创建与 YIELD/PRICE 函数等效的 C++。

在第一步中,我不需要复制 Excel 日期建模中的缺陷。我可以等到稍后再制作一个完全相同的副本。不过如果你知道怎么做那也很棒。


PRICE 例如,在 OpenOffice 中:

PRICE("2008-02-15","2010-11-15",5%,7%,100,2,1) = 95.068419616675

我的 QuantLib 代码能够获取 95.066759,这有点偏离。至少我有基本的价格函数,我现在想得到与结果完全匹配的结果。


我无法轻松包含所有包装代码,但基本代码如下。

#include <ql/time/calendar.hpp>
#include <ql/time/daycounters/actualactual.hpp>
#include <ql/time/daycounters/actual365fixed.hpp>
#include <ql/time/schedule.hpp>
#include <ql/time/calendars/unitedstates.hpp>
#include <ql/time/calendars/nullcalendar.hpp>

#include <ql/settings.hpp>
#include <ql/handle.hpp>
#include <ql/termstructures/yield/flatforward.hpp>
#include <ql/instruments/bonds/fixedratebond.hpp>

#include <ql/pricingengines/bond/discountingbondengine.hpp>
#include <ql/utilities/dataformatters.hpp>

#include <iostream>
#include <iomanip>

#include "boost/date_time/gregorian/gregorian.hpp"
using namespace QuantLib;

Date convert_date( boost::gregorian::date const & date )
{
    unsigned mon = date.month();
    return Date( date.day(), Month(mon), date.year() );
}

shared_ptr<Bond> create_bond( boost::gregorian::date const & settlement_, boost::gregorian::date const & maturity_,
    double coupon_, double yield_, double redemption_, unsigned frequency_ )
{
    // date set up
    //Calendar calendar = UnitedStates(UnitedStates::GovernmentBond);
    Calendar calendar = NullCalendar(); //small improvement

    Date settlementDate( convert_date( settlement_ ) );
    // the settlement date must be a business day
    settlementDate = calendar.adjust(settlementDate);

    Integer fixingDays = 0; //1;
    Natural settlementDays = 0; //1

    Date evalDate = calendar.advance(settlementDate, -fixingDays, Days);
    // Evaluation date (TODO: What should this actually be?)
    Settings::instance().evaluationDate() = evalDate;

    // bond set up
    Real faceAmount = 100;
    Real redemption = redemption_;
    Date issueDate( 1, January, 2001); //NOTE: shouldn't be relevant for price/yield calculations
    Date maturity( convert_date( maturity_ ) );
    Real couponRate = coupon_;
    Real yield = yield_;

    //ActualActual dayCounter( ActualActual::Bond );
    ActualActual dayCounter;
    //Actual365Fixed dayCounter;

    RelinkableHandle<YieldTermStructure> discountingTermStructure;
    boost::shared_ptr<YieldTermStructure> flatTermStructure(
        new FlatForward(
            settlementDate,
            yield,
            dayCounter,
            Compounded,
            Frequency( frequency_ ) ));
    discountingTermStructure.linkTo(flatTermStructure);

    boost::shared_ptr<PricingEngine> bondEngine(
        new DiscountingBondEngine(discountingTermStructure));

    Schedule fixedBondSchedule(
        issueDate,
        maturity,
        Period( Frequency( frequency_ ) ),
        calendar,
        Unadjusted,
        Unadjusted,
        DateGeneration::Backward,
        false /*EOM*/); //strangely makes no difference in our calculations

    boost::shared_ptr<Bond> fixedRateBond( new FixedRateBond(
        settlementDays,
        faceAmount,
        fixedBondSchedule,
        std::vector<Rate>(1, couponRate),
        dayCounter,
        Unadjusted,
        redemption) );

    fixedRateBond->setPricingEngine(bondEngine);
    return fixedRateBond;
}

//OpenOffice: PRICE("2008-02-15","2010-11-15",5%,7%,100,2,1)
double bond_price( boost::gregorian::date const & settlement_, boost::gregorian::date const & maturity_,
    double coupon_, double yield_, double redemption_, unsigned frequency_ )
{
    shared_ptr<Bond> bond( create_bond( settlement_, maturity_, coupon_, yield_, redemption_, frequency_ ) );
    return bond->cleanPrice();
}

//OpenOffice: PRICE("2008-02-15","2010-11-15",5%,7%,100,2,1)
double bond_yield( boost::gregorian::date const & settlement_, boost::gregorian::date const & maturity_,
    double coupon_, double price_, double redemption_, unsigned frequency_ )
{
    shared_ptr<Bond> bond( create_bond( settlement_, maturity_, coupon_, 0, redemption_, frequency_ ) );
    ActualActual dayCounter;
    return bond->yield( price_, dayCounter, Compounded, Frequency(frequency_) );
}

Can somebody provide an example of how to replicate the Excel/OpenOffice YIELD and PRICE functions using QuantLib?

I have a few examples but I don't quite understand all the setup yet. When I try to change some values I either get zeros out or some nonsensical values. Ideally I'd like to create the c++ equivalent to the YIELD/PRICE functions.

In my first step I don't need to replicate the defects in the Excel date modelling. I can wait until later to produce an exact duplicate. Though if you know how that is also great.


PRICE example, in OpenOffice:

PRICE("2008-02-15","2010-11-15",5%,7%,100,2,1) = 95.068419616675

My QuantLib code is capable of getting 95.066759 which is a bit off. At least I have the basic price function, I'd like to get an exact match for the results now.


I can't easily include all the wrapping code, but the essential code is as follows.

#include <ql/time/calendar.hpp>
#include <ql/time/daycounters/actualactual.hpp>
#include <ql/time/daycounters/actual365fixed.hpp>
#include <ql/time/schedule.hpp>
#include <ql/time/calendars/unitedstates.hpp>
#include <ql/time/calendars/nullcalendar.hpp>

#include <ql/settings.hpp>
#include <ql/handle.hpp>
#include <ql/termstructures/yield/flatforward.hpp>
#include <ql/instruments/bonds/fixedratebond.hpp>

#include <ql/pricingengines/bond/discountingbondengine.hpp>
#include <ql/utilities/dataformatters.hpp>

#include <iostream>
#include <iomanip>

#include "boost/date_time/gregorian/gregorian.hpp"
using namespace QuantLib;

Date convert_date( boost::gregorian::date const & date )
{
    unsigned mon = date.month();
    return Date( date.day(), Month(mon), date.year() );
}

shared_ptr<Bond> create_bond( boost::gregorian::date const & settlement_, boost::gregorian::date const & maturity_,
    double coupon_, double yield_, double redemption_, unsigned frequency_ )
{
    // date set up
    //Calendar calendar = UnitedStates(UnitedStates::GovernmentBond);
    Calendar calendar = NullCalendar(); //small improvement

    Date settlementDate( convert_date( settlement_ ) );
    // the settlement date must be a business day
    settlementDate = calendar.adjust(settlementDate);

    Integer fixingDays = 0; //1;
    Natural settlementDays = 0; //1

    Date evalDate = calendar.advance(settlementDate, -fixingDays, Days);
    // Evaluation date (TODO: What should this actually be?)
    Settings::instance().evaluationDate() = evalDate;

    // bond set up
    Real faceAmount = 100;
    Real redemption = redemption_;
    Date issueDate( 1, January, 2001); //NOTE: shouldn't be relevant for price/yield calculations
    Date maturity( convert_date( maturity_ ) );
    Real couponRate = coupon_;
    Real yield = yield_;

    //ActualActual dayCounter( ActualActual::Bond );
    ActualActual dayCounter;
    //Actual365Fixed dayCounter;

    RelinkableHandle<YieldTermStructure> discountingTermStructure;
    boost::shared_ptr<YieldTermStructure> flatTermStructure(
        new FlatForward(
            settlementDate,
            yield,
            dayCounter,
            Compounded,
            Frequency( frequency_ ) ));
    discountingTermStructure.linkTo(flatTermStructure);

    boost::shared_ptr<PricingEngine> bondEngine(
        new DiscountingBondEngine(discountingTermStructure));

    Schedule fixedBondSchedule(
        issueDate,
        maturity,
        Period( Frequency( frequency_ ) ),
        calendar,
        Unadjusted,
        Unadjusted,
        DateGeneration::Backward,
        false /*EOM*/); //strangely makes no difference in our calculations

    boost::shared_ptr<Bond> fixedRateBond( new FixedRateBond(
        settlementDays,
        faceAmount,
        fixedBondSchedule,
        std::vector<Rate>(1, couponRate),
        dayCounter,
        Unadjusted,
        redemption) );

    fixedRateBond->setPricingEngine(bondEngine);
    return fixedRateBond;
}

//OpenOffice: PRICE("2008-02-15","2010-11-15",5%,7%,100,2,1)
double bond_price( boost::gregorian::date const & settlement_, boost::gregorian::date const & maturity_,
    double coupon_, double yield_, double redemption_, unsigned frequency_ )
{
    shared_ptr<Bond> bond( create_bond( settlement_, maturity_, coupon_, yield_, redemption_, frequency_ ) );
    return bond->cleanPrice();
}

//OpenOffice: PRICE("2008-02-15","2010-11-15",5%,7%,100,2,1)
double bond_yield( boost::gregorian::date const & settlement_, boost::gregorian::date const & maturity_,
    double coupon_, double price_, double redemption_, unsigned frequency_ )
{
    shared_ptr<Bond> bond( create_bond( settlement_, maturity_, coupon_, 0, redemption_, frequency_ ) );
    ActualActual dayCounter;
    return bond->yield( price_, dayCounter, Compounded, Frequency(frequency_) );
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乞讨 2024-10-27 19:52:41

如果您对OpenOffice中PRICE函数的实现感兴趣,可以查看AnalysisAddIn::getPrice。这是指 analysis helper.cxx 中的 getPrice_ 函数。也许你会发现那里发生了什么。

请注意,OpenGrok 似乎在这里配置错误,因此单击功能可能不起作用。但我想您可以在目录 /OOO340_m0/scaddins/source/analysis

If you are interested in the implementation of the PRICE function in OpenOffice, you can see the code in the implementation of AnalysisAddIn::getPrice. This refers to the getPrice_ function in analysis helper.cxx. Maybe you find out what's happening there.

Note, that OpenGrok seems to be misconfigured here, so clicking on functions may not work. But I suppose you find all you need in the files in the directory /OOO340_m0/scaddins/source/analysis.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文