/*
* (c) Copyright Christian P. Fries, Germany. All rights reserved. Contact: email@christian-fries.de.
*
* Created on 26.11.2012
*/
package net.finmath.marketdata.products;
import net.finmath.marketdata.model.AnalyticModelInterface;
import net.finmath.marketdata.model.curves.DiscountCurveInterface;
import net.finmath.marketdata.model.curves.ForwardCurveInterface;
import net.finmath.time.ScheduleInterface;
/**
* Implements the valuation of a swap leg using curves (discount curve, forward curve).
* The swap leg valuation supports distinct discounting and forward curves.
* Support for day counting is limited to the capabilities of
* TimeDiscretizationInterface.
*
* @author Christian Fries
*/
public class SwapLeg extends AbstractAnalyticProduct implements AnalyticProductInterface {
private final ScheduleInterface legSchedule;
private final String forwardCurveName;
private final double spread;
private final String discountCurveName;
private boolean isNotionalExchanged = false;
/**
* Creates a swap leg. The swap leg has a unit notional of 1.
*
* @param legSchedule Schedule of the leg.
* @param forwardCurveName Name of the forward curve, leave empty if this is a fix leg.
* @param spread Fixed spread on the forward or fix rate.
* @param discountCurveName Name of the discount curve for the leg.
* @param isNotionalExchanged If true, the leg will pay notional at the beginning of each swap period and receive notional at the end of the swap period. Note that the cash flow date for the notional is periodStart and periodEnd (not fixingDate and paymentDate).
*/
public SwapLeg(ScheduleInterface legSchedule, String forwardCurveName, double spread, String discountCurveName, boolean isNotionalExchanged) {
super();
this.legSchedule = legSchedule;
this.forwardCurveName = forwardCurveName;
this.spread = spread;
this.discountCurveName = discountCurveName;
this.isNotionalExchanged = isNotionalExchanged;
}
/**
* Creates a swap leg (without notional exchange). The swap leg has a unit notional of 1.
*
* @param legSchedule Schedule of the leg.
* @param forwardCurveName Name of the forward curve, leave empty if this is a fix leg.
* @param spread Fixed spread on the forward or fix rate.
* @param discountCurveName Name of the discount curve for the leg.
*/
public SwapLeg(ScheduleInterface legSchedule, String forwardCurveName, double spread, String discountCurveName) {
this(legSchedule, forwardCurveName, spread, discountCurveName, false);
}
@Override
public double getValue(double evaluationTime, AnalyticModelInterface model) {
ForwardCurveInterface forwardCurve = model.getForwardCurve(forwardCurveName);
DiscountCurveInterface discountCurve = model.getDiscountCurve(discountCurveName);
DiscountCurveInterface discountCurveForForward = null;
if(forwardCurve == null && forwardCurveName != null && forwardCurveName.length() > 0) {
// User might like to get forward from discount curve.
discountCurveForForward = model.getDiscountCurve(forwardCurveName);
if(discountCurveForForward == null) {
// User specified a name for the forward curve, but no curve was found.
throw new IllegalArgumentException("No curve of the name " + forwardCurveName + " was found in the model.");
}
}
double value = 0.0;
for(int periodIndex=0; periodIndex evaluationTime ? discountCurve.getDiscountFactor(model, paymentDate) : 0.0;
value += forward * periodLength * discountFactor;
if(isNotionalExchanged) {
double periodEnd = legSchedule.getPeriodEnd(periodIndex);
value += periodEnd > evaluationTime ? discountCurve.getDiscountFactor(model, periodEnd) : 0.0;
double periodStart = legSchedule.getPeriodStart(periodIndex);
value -= periodStart > evaluationTime ? discountCurve.getDiscountFactor(model, periodStart) : 0.0;
}
}
return value / discountCurve.getDiscountFactor(model, evaluationTime);
}
public ScheduleInterface getSchedule() {
return legSchedule;
}
public String getForwardCurveName() {
return forwardCurveName;
}
public double getSpread() {
return spread;
}
public String getDiscountCurveName() {
return discountCurveName;
}
public boolean isNotionalExchanged() {
return isNotionalExchanged;
}
@Override
public String toString() {
return "SwapLeg [legSchedule=" + legSchedule + ", forwardCurveName="
+ forwardCurveName + ", spread=" + spread
+ ", discountCurveName=" + discountCurveName
+ ", isNotionalExchanged=" + isNotionalExchanged + "]";
}
}
Ads help maintain this website.