Real CPICashFlow::amount() const { Real I0 = baseFixing(); Real I1; // what interpolation do we use? Index / flat / linear if (interpolation() == CPI::AsIndex ) { I1 = index()->fixing(fixingDate()); } else { // work out what it should be //std::cout << fixingDate() << " and " << frequency() << std::endl; //std::pair<Date,Date> dd = inflationPeriod(fixingDate(), frequency()); //std::cout << fixingDate() << " and " << dd.first << " " << dd.second << std::endl; // work out what it should be std::pair<Date,Date> dd = inflationPeriod(fixingDate(), frequency()); Real indexStart = index()->fixing(dd.first); if (interpolation() == CPI::Linear) { Real indexEnd = index()->fixing(dd.second+Period(1,Days)); // linear interpolation //std::cout << indexStart << " and " << indexEnd << std::endl; I1 = indexStart + (indexEnd - indexStart) * (fixingDate() - dd.first) / ( (dd.second+Period(1,Days)) - dd.first); // can't get to next period's value within current period } else { // no interpolation, i.e. flat = constant, so use start-of-period value I1 = indexStart; } } if (growthOnly()) return notional() * (I1 / I0 - 1.0); else return notional() * (I1 / I0); }
std::vector<boost::shared_ptr<CashFlow> > RiskyBond::expectedCashflows() { std::vector<boost::shared_ptr<CashFlow> > expected; std::vector<boost::shared_ptr<CashFlow> > cf = cashflows(); Date today = Settings::instance().evaluationDate(); Date npvDate = calendar_.advance(today, settlementDays_, Days); Date d1 = effectiveDate(); for (Size i = 0; i < cf.size(); i++) { Date d2 = cf[i]->date(); if (d2 > npvDate) { d1 = max(npvDate, d1); Date defaultDate = d1 + (d2-d1)/2; Real coupon = cf[i]->amount() * defaultTS_->survivalProbability(d2); Real recovery = notional(defaultDate) * recoveryRate_ * (defaultTS_->survivalProbability(d1) -defaultTS_->survivalProbability(d2)); boost::shared_ptr<CashFlow> flow1(new SimpleCashFlow(coupon, d2)); expected.push_back(flow1); boost::shared_ptr<CashFlow> flow2(new SimpleCashFlow(recovery, defaultDate)); expected.push_back(flow2); } d1 = d2; } return expected; }
Real CallableFixedRateBond::accrued(Date settlement) const { if (settlement == Date()) settlement = settlementDate(); const bool IncludeToday = false; for (Size i = 0; i<cashflows_.size(); ++i) { // the first coupon paying after d is the one we're after if (!cashflows_[i]->hasOccurred(settlement,IncludeToday)) { boost::shared_ptr<Coupon> coupon = boost::dynamic_pointer_cast<Coupon>(cashflows_[i]); if (coupon) // !!! return coupon->accruedAmount(settlement) / notional(settlement) * 100.0; else return 0.0; } } return 0.0; }
void RiskyBond::performCalculations() const { NPV_ = 0; Date today = Settings::instance().evaluationDate(); std::vector<boost::shared_ptr<CashFlow> > cf = cashflows(); Date d1 = effectiveDate(); for (Size i = 0; i < cf.size(); i++) { Date d2 = cf[i]->date(); if (d2 > today) { d1 = max(today , d1); Date defaultDate = d1 + (d2-d1)/2; // Mid-Point Rule Real coupon = cf[i]->amount() * defaultTS_->survivalProbability(d2); Real recovery = notional(defaultDate) * recoveryRate_ * (defaultTS_->survivalProbability(d1) -defaultTS_->survivalProbability(d2)); NPV_ += coupon * yieldTS()->discount(d2); NPV_ += recovery * yieldTS()->discount(defaultDate); } d1 = d2; } }
/* enregistre une jambe à taux flottant */ DLLEXPORT xloper * xlInitiateFloatLegUnitedStates (const char * objectID_, const double * effectiveDate_, xloper * firstCouponDate_, xloper * lastCouponDate_, const double * maturityDate_, xloper * notional_, const char * indexId_, xloper * frequency_, xloper * daycounter_, xloper * spread_, xloper * trigger_) { boost::shared_ptr<ObjectHandler::FunctionCall> functionCall( new ObjectHandler::FunctionCall("xlInitiatefloatLegUnitedStates")) ; try { QL_ENSURE(! functionCall->calledByFunctionWizard(), "") ; // vérification des codes erreur ObjectHandler::validateRange(trigger_, "trigger") ; ObjectHandler::validateRange(firstCouponDate_, "first Coupon Date") ; ObjectHandler::validateRange(lastCouponDate_, "last Coupon Date") ; ObjectHandler::validateRange(notional_, "notional") ; ObjectHandler::validateRange(frequency_, "frequency") ; ObjectHandler::validateRange(daycounter_, "daycounter") ; ObjectHandler::validateRange(spread_, "spread") ; // Création des oper ObjectHandler::ConvertOper myOper1(* firstCouponDate_), myOper2(* lastCouponDate_), myOper3(* notional_), myOper4(* frequency_), myOper5(* daycounter_), myOper6(* spread_) ; // création des dates et contrôles sur les dates intermédiaires QuantLib::Date effectiveDate(QuantLib::Date(static_cast<QuantLib::BigInteger>(* effectiveDate_))) ; QuantLib::Date maturityDate(QuantLib::Date(static_cast<QuantLib::BigInteger>(* maturityDate_))) ; QuantLib::Date firstCouponDate(myOper1.missing() ? QuantLib::Date() : QuantLib::Date(static_cast<QuantLib::BigInteger>(myOper1))) ; QuantLib::Date lastCouponDate(myOper2.missing() ? QuantLib::Date() : QuantLib::Date(static_cast<QuantLib::BigInteger>(myOper2))) ; QuantLib::Real notional(myOper3.missing() ? 100.0 : static_cast<QuantLib::Real>(myOper3)) ; QuantLib::Frequency frequency(myOper4.missing() ? QuantLib::Quarterly : ObjectHandler::frequencyFactory()(static_cast<std::string>(myOper4))) ; QuantLib::DayCounter daycounter(myOper5.missing() ? QuantLib::Actual360() : ObjectHandler::daycountFactory()(static_cast<std::string>(myOper5))) ; QuantLib::Spread spread(myOper6.missing() ? 0.0 : static_cast<QuantLib::Spread>(myOper6)) ; // on récupère l'index libor OH_GET_REFERENCE(iborIndexPtr, std::string(indexId_), QuantLibAddin::iborIndexObject, QuantLib::IborIndex) // Construction du value object boost::shared_ptr<QuantLibAddin::ValueObjects::floatLegUnitedStatesValueObject> myLegValueObject( new QuantLibAddin::ValueObjects::floatLegUnitedStatesValueObject(objectID_, true)) ; // instanciation de l'instrument boost::shared_ptr<QuantLibAddin::floatLegUnitedStatesObject> myLegObject( new QuantLibAddin::floatLegUnitedStatesObject(myLegValueObject, effectiveDate, firstCouponDate, lastCouponDate, maturityDate, iborIndexPtr, 2, frequency, daycounter, notional, spread, QuantLib::ModifiedFollowing, QuantLib::ModifiedFollowing, true, true)) ; // stockage de l'objet std::string returnValue = ObjectHandler::RepositoryXL::instance().storeObject(objectID_, myLegObject, true) ; // on force la réécriture static XLOPER returnOper ; ObjectHandler::scalarToOper(returnValue, returnOper) ; return & returnOper ; } catch (std::exception & e) { ObjectHandler::RepositoryXL::instance().logError(e.what(), functionCall) ; static XLOPER returnOper ; ObjectHandler::scalarToOper(e.what(), returnOper) ; return & returnOper ; } } ;