谁能提供一个示例,说明如何使用 QuantLib 复制 Excel/OpenOffice YIELD 和 PRICE 函数?
我有几个示例,但我还不太了解所有设置。当我尝试更改某些值时,我要么得到零,要么得到一些无意义的值。理想情况下,我想创建等效于 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_) );
}
最佳答案
如果你对OpenOffice中PRICE函数的实现感兴趣,可以看AnalysisAddIn::getPrice的实现中的代码.这是指getPrice_ 分析 helper.cxx 中的函数。也许你会发现那里发生了什么。
请注意,OpenGrok 似乎在这里配置错误,因此单击功能可能不起作用。但我想您可以在目录 /OOO340_m0/scaddins/source/analysis 中的文件中找到所需的一切。 .
关于c++ - QuantLib OpenOffice/Excel YIELD/PRICE 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5221428/
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
我正在尝试用ruby中的gsub函数替换字符串中的某些单词,但有时效果很好,在某些情况下会出现此错误?这种格式有什么问题吗NoMethodError(undefinedmethod`gsub!'fornil:NilClass):模型.rbclassTest"replacethisID1",WAY=>"replacethisID2andID3",DELTA=>"replacethisID4"}end另一个模型.rbclassCheck 最佳答案 啊,我找到了!gsub!是一个非常奇怪的方法。首先,它替换了字符串,所以它实际上修改了
我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin
如何在Ruby中按名称传递函数?(我使用Ruby才几个小时,所以我还在想办法。)nums=[1,2,3,4]#Thisworks,butismoreverbosethanI'dlikenums.eachdo|i|putsiend#InJS,Icouldjustdosomethinglike:#nums.forEach(console.log)#InF#,itwouldbesomethinglike:#List.iternums(printf"%A")#InRuby,IwishIcoulddosomethinglike:nums.eachputs在Ruby中能不能做到类似的简洁?我可以只
如何将send与+=一起使用?a=20;a.send"+=",10undefinedmethod`+='for20:Fixnuma=20;a+=10=>30 最佳答案 恐怕你不能。+=不是方法,而是语法糖。参见http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html它说Incommonwithmanyotherlanguages,Rubyhasasyntacticshortcut:a=a+2maybewrittenasa+=2.你能做的最好的事情是:
说在前面这部分我本来是合为一篇来写的,因为目的是一样的,都是通过独立按键来控制LED闪灭本质上是起到开关的作用,即调用函数和中断函数。但是写一篇太累了,我还是决定分为两篇写,这篇是调用函数篇。在本篇中你主要看到这些东西!!!1.调用函数的方法(主要讲语法和格式)2.独立按键如何控制LED亮灭3.程序中的一些细节(软件消抖等)1.调用函数的方法思路还是比较清晰地,就是通过按下按键来控制LED闪灭,即每按下一次,LED取反一次。重要的是,把按键与LED联系在一起。我打算用K1来作为开关,看了一下开发板原理图,K1连接的是单片机的P31口,当按下K1时,P31是与GND相连的,也就是说,当我按下去时
我需要一个通过输入字符串进行计算的方法,像这样function="(a/b)*100"a=25b=50function.something>>50有什么方法吗? 最佳答案 您可以使用instance_eval:function="(a/b)*100"a=25.0b=50instance_evalfunction#=>50.0请注意,使用eval本质上是不安全的,尤其是当您使用外部输入时,因为它可能包含注入(inject)的恶意代码。另请注意,a设置为25.0而不是25,因为如果它是整数a/b将导致0(整数)。
我对如何计算通过{%assignvar=0%}赋值的变量加一完全感到困惑。这应该是最简单的任务。到目前为止,这是我尝试过的:{%assignamount=0%}{%forvariantinproduct.variants%}{%assignamount=amount+1%}{%endfor%}Amount:{{amount}}结果总是0。也许我忽略了一些明显的东西。也许有更好的方法。我想要存档的只是获取运行的迭代次数。 最佳答案 因为{{incrementamount}}将输出您的变量值并且不会影响{%assign%}定义的变量,我
我需要从json记录中获取一些值并像下面这样提取curr_json_doc['title']['genre'].map{|s|s['name']}.join(',')但对于某些记录,curr_json_doc['title']['genre']可以为空。所以我想对map和join()使用try函数。我试过如下curr_json_doc['title']['genre'].try(:map,{|s|s['name']}).try(:join,(','))但是没用。 最佳答案 你没有正确传递block。block被传递给参数括号外的方法