CashFlow Model
Posted by dhoorens on
URL: http://quantlib.414.s1.nabble.com/CashFlow-Model-tp7236.html
Hi
I try to make two things in the same time (which is, obviously, impossible since I'm a man...)
*make a cashflow model
*use Quantlib
I would like to put every cashflow I have in my asset portfolio, during a specific period of time, in a std::list<> with the following code
std::list<boost::shared_ptr<CashFlow> > Portfolio::listCashFlow(Date d, Period period){
std::list<boost::shared_ptr<CashFlow> > listCF;
for (const_iterator i=components_.begin(); i!=components_.end(); ++i) {
if (boost::shared_ptr<Bond> iTemp = boost::dynamic_pointer_cast<Bond> (i->first.first)){
//iTemp is now a bond => I can use the cashflow() method
Leg cf = iTemp->cashflows();
for (int j = 0; j<cf.size(); j++){
if (d <= (cf[j])->date() && (cf[j])->date() <= d+period)
listCF.push_back(cf[j]);
}
}
else if (boost::shared_ptr<Swap> iTemp =boost::dynamic_pointer_cast<Swap> (i->first.first)){
//iTemp is now a swap => I can use the leg(size) method
Leg cf1 = iTemp->leg(0);
for (int j = 0; j<cf1.size(); j++){
if (d <= (cf1[j])->date() && (cf1[j])->date() <= d+period)
listCF.push_back(cf1[j]);
}
Leg cf2 = iTemp->leg(1);
for (int j = 0; j<cf2.size(); j++){
if (d <= (cf2[j])->date() && (cf2[j])->date() <= d+period)
listCF.push_back(cf2[j]);
}
}
else if (boost::shared_ptr<VanillaOption> iTemp =boost::dynamic_pointer_cast<VanillaOption> (i->first.first)){
//iTemp is now a VanillaOption => i can use ...???
Date exerciseDate = iTemp->exercise()->lastDate();
if (d <= exerciseDate && exerciseDate <= d+period){
boost::shared_ptr<Payoff> p = iTemp->payoff();
///////////HERE IS THE PROBLEM////////////////////////
Real underlyingPrice = ???????????;
//////////////////////////////////////////////////////
Real amount = p->operator()(underlyingPrice);
boost::shared_ptr<CashFlow> cf =
boost::shared_ptr<CashFlow>(
new SimpleCashFlow(
amount,
exerciseDate));
listCF.push_back(cf);
}
}
}
return listCF;
}
the data member components_ is defined by
typedef std::pair< std::pair<boost::shared_ptr<QuantLib::Instrument>,
boost::shared_ptr<QuantLib::Instrument> >,
QuantLib::Real> component;
typedef std::list<component>::iterator iterator;
typedef std::list<component>::const_iterator const_iterator;
the pair QL::Instrument, QL::instrument is used for (marketValue, bookValue)
Does a magic fonction exist at Instrument level which will give me all the cashflows of a specific instrument?
For the moment, I have to use polymorphic dynamic_cast to check if the instrument provides cashflows and after i have to get those CF...
It is not a nice solution :-/
The second problem I have is in the "vanillaOption" part.
How to get my plainVanillaPayoff cashFlow?
Because for a Call (for example), my payoff cashflow is max(0, Underlying-strike) at maturity, but I don't have information about underlying at maturity....
Did I miss smthg?
Tks
Regards
David