Changeset 133
- Timestamp:
- 04/28/08 17:24:48 (2 months ago)
- Files:
-
- trunk/QLNet/QLNet/Cashflows/CappedFlooredCoupon.cs (added)
- trunk/QLNet/QLNet/Cashflows/Cashflowvectors.cs (modified) (6 diffs)
- trunk/QLNet/QLNet/Cashflows/CmsCoupon.cs (added)
- trunk/QLNet/QLNet/Cashflows/ConundrumPricer.cs (added)
- trunk/QLNet/QLNet/Cashflows/CouponPricer.cs (modified) (13 diffs)
- trunk/QLNet/QLNet/Cashflows/DigitalCmsCoupon.cs (added)
- trunk/QLNet/QLNet/Cashflows/DigitalCoupon.cs (added)
- trunk/QLNet/QLNet/Cashflows/DigitalIborCoupon.cs (added)
- trunk/QLNet/QLNet/Cashflows/FloatingRateCoupon.cs (modified) (7 diffs)
- trunk/QLNet/QLNet/Cashflows/Iborcoupon.cs (modified) (5 diffs)
- trunk/QLNet/QLNet/Cashflows/Replication.cs (added)
- trunk/QLNet/QLNet/Indexes/IBORIndex.cs (modified) (3 diffs)
- trunk/QLNet/QLNet/Indexes/InterestRateIndex.cs (modified) (5 diffs)
- trunk/QLNet/QLNet/Indexes/Swapindex.cs (modified) (3 diffs)
- trunk/QLNet/QLNet/Instruments/VanillaSwap.cs (modified) (6 diffs)
- trunk/QLNet/QLNet/Math/integrals/Integral.cs (modified) (1 diff)
- trunk/QLNet/QLNet/Math/integrals/Kronrodintegral.cs (added)
- trunk/QLNet/QLNet/Math/integrals/Segmentintegral.cs (modified) (1 diff)
- trunk/QLNet/QLNet/Pricingengines/Americanpayoffatexpiry.cs (modified) (2 diffs)
- trunk/QLNet/QLNet/Pricingengines/Americanpayoffathit.cs (modified) (3 diffs)
- trunk/QLNet/QLNet/QLNet.csproj (modified) (3 diffs)
- trunk/QLNet/QLNet/Termstructures/Volatility/swaption (added)
- trunk/QLNet/QLNet/Termstructures/Volatility/swaption/SwaptionVolatilityStructure.cs (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/QLNet/QLNet/Cashflows/Cashflowvectors.cs
r61 r133 1 1 /* 2 2 Copyright (C) 2008 Siarhei Novik (snovik@gmail.com) 3 3 Copyright (C) 2008 Toyin Akin (toyin_akin@hotmail.com) 4 * 4 5 This file is part of QLNet Project http://www.qlnet.org 5 6 … … 23 24 24 25 namespace QLNet { 26 27 public static partial class Utils { 28 public static double? toNullable(double val) { 29 if (val == double.MinValue) 30 return null; 31 else 32 return val; 33 } 34 } 35 25 36 public static class CashFlowVectors { 26 27 public static List<CashFlow> FloatingLeg<FloatingCouponType>(List<double> nominals, 37 public static List<CashFlow> FloatingLeg<InterestRateIndexType, FloatingCouponType, CappedFlooredCouponType>(List<double> nominals, 28 38 Schedule schedule, 29 InterestRateIndex index,39 InterestRateIndexType index, 30 40 DayCounter paymentDayCounter, 31 41 BusinessDayConvention paymentAdj, … … 37 47 bool isInArrears, 38 48 bool isZero) 39 where FloatingCouponType : FloatingRateCoupon, new() { 49 where InterestRateIndexType : InterestRateIndex, new() 50 where FloatingCouponType : FloatingRateCoupon, new() 51 where CappedFlooredCouponType : CappedFlooredCoupon, new() { 40 52 41 53 int n = schedule.Count; … … 59 71 60 72 Date refStart, start, refEnd, end; 61 Date lastPaymentDate = calendar.adjust(schedule[n -1], paymentAdj);62 63 for (int i =0; i<n-1; ++i) {73 Date lastPaymentDate = calendar.adjust(schedule[n - 1], paymentAdj); 74 75 for (int i = 0; i < n - 1; ++i) { 64 76 refStart = start = schedule[i]; 65 refEnd = end = schedule[i+1];77 refEnd = end = schedule[i + 1]; 66 78 Date paymentDate = isZero ? lastPaymentDate : calendar.adjust(end, paymentAdj); 67 if (i ==0 && !schedule.isRegular(i+1))79 if (i == 0 && !schedule.isRegular(i + 1)) 68 80 refStart = calendar.adjust(end - schedule.tenor(), schedule.businessDayConvention()); 69 if (i ==n-1 && !schedule.isRegular(i+1))81 if (i == n - 1 && !schedule.isRegular(i + 1)) 70 82 refEnd = calendar.adjust(start + schedule.tenor(), schedule.businessDayConvention()); 71 83 … … 73 85 leg.Add(new FixedRateCoupon(Utils.Get(nominals, i), 74 86 paymentDate, 75 Utils.effectiveFixedRate(spreads, caps, floors,i),87 Utils.effectiveFixedRate(spreads, caps, floors, i), 76 88 paymentDayCounter, 77 89 start, end, refStart, refEnd)); 78 } else { // floating coupon90 } else { 79 91 if (Utils.noOption(caps, floors, i)) { 80 92 leg.Add(new FloatingCouponType().factory(paymentDate, 81 Utils.Get(nominals, i),82 start, end,83 Utils.Get(fixingDays, i, 2),84 index,85 Utils.Get(gearings, i, 1),86 Utils.Get(spreads, i),87 refStart, refEnd, paymentDayCounter,88 isInArrears));93 Utils.Get(nominals, i), 94 start, end, 95 Utils.Get(fixingDays, i, 2), 96 index, 97 Utils.Get(gearings, i, 1), 98 Utils.Get(spreads, i), 99 refStart, refEnd, paymentDayCounter, 100 isInArrears)); 89 101 } else { 90 throw new NotImplementedException(); 102 leg.Add(new CappedFlooredCouponType().factory(paymentDate, 103 Utils.Get(nominals, i), 104 start, end, 105 Utils.Get(fixingDays, i, 2), 106 index, 107 Utils.Get(gearings, i, 1), 108 Utils.Get(spreads, i), 109 Utils.toNullable(Utils.Get(caps, i, Double.MinValue)), 110 Utils.toNullable(Utils.Get(floors, i, Double.MinValue)), 111 refStart, refEnd, paymentDayCounter, 112 isInArrears)); 91 113 } 92 114 } … … 94 116 return leg; 95 117 } 118 119 public static List<CashFlow> FloatingDigitalLeg<InterestRateIndexType, FloatingCouponType, DigitalCouponType>(List<double> nominals, 120 Schedule schedule, 121 InterestRateIndexType index, 122 DayCounter paymentDayCounter, 123 BusinessDayConvention paymentAdj, 124 List<int> fixingDays, 125 List<double> gearings, 126 List<double> spreads, 127 bool isInArrears, 128 List<double> callStrikes, 129 Position.Type callPosition, 130 bool isCallATMIncluded, 131 List<double> callDigitalPayoffs, 132 List<double> putStrikes, 133 Position.Type putPosition, 134 bool isPutATMIncluded, 135 List<double> putDigitalPayoffs, 136 DigitalReplication replication) 137 where InterestRateIndexType : InterestRateIndex, new() 138 where FloatingCouponType : FloatingRateCoupon, new() 139 where DigitalCouponType : DigitalCoupon, new() { 140 141 int n = schedule.Count; 142 if (nominals.Count == 0) throw new ArgumentException("no nominal given"); 143 if (nominals.Count > n) throw new ArgumentException( 144 "too many nominals (" + nominals.Count + "), only " + n + " required"); 145 if (gearings != null && gearings.Count > n) throw new ArgumentException( 146 "too many gearings (" + gearings.Count + "), only " + n + " required"); 147 if (spreads != null && spreads.Count > n) throw new ArgumentException( 148 "too many spreads (" + spreads.Count + "), only " + n + " required"); 149 if (callStrikes.Count > n) throw new ArgumentException( 150 "too many nominals (" + callStrikes.Count + "), only " + n + " required"); 151 if (putStrikes.Count > n) throw new ArgumentException( 152 "too many nominals (" + putStrikes.Count + "), only " + n + " required"); 153 154 155 List<CashFlow> leg = new List<CashFlow>(); 156 157 // the following is not always correct 158 Calendar calendar = schedule.calendar(); 159 160 Date refStart, start, refEnd, end; 161 Date paymentDate; 162 163 for (int i = 0; i < n; ++i) { 164 refStart = start = schedule.date(i); 165 refEnd = end = schedule.date(i + 1); 166 paymentDate = calendar.adjust(end, paymentAdj); 167 if (i == 0 && !schedule.isRegular(i + 1)) { 168 BusinessDayConvention bdc = schedule.businessDayConvention(); 169 refStart = calendar.adjust(end - schedule.tenor(), bdc); 170 } 171 if (i == n - 1 && !schedule.isRegular(i + 1)) { 172 BusinessDayConvention bdc = schedule.businessDayConvention(); 173 refEnd = calendar.adjust(start + schedule.tenor(), bdc); 174 } 175 if (Utils.Get(gearings, i, 1.0) == 0.0) { // fixed coupon 176 leg.Add(new 177 FixedRateCoupon(Utils.Get(nominals, i, 1.0), 178 paymentDate, 179 Utils.Get(spreads, i, 1.0), 180 paymentDayCounter, 181 start, end, refStart, refEnd)); 182 183 } else { // floating digital coupon 184 FloatingCouponType underlying = new FloatingCouponType().factory( 185 paymentDate, 186 Utils.Get(nominals, i, 1.0), 187 start, end, 188 Utils.Get(fixingDays, i, index.fixingDays()), 189 index, 190 Utils.Get(gearings, i, 1.0), 191 Utils.Get(spreads, i, 0.0), 192 refStart, refEnd, 193 paymentDayCounter, isInArrears) as FloatingCouponType; 194 195 DigitalCouponType digitalCoupon = new DigitalCouponType().factory( 196 underlying, 197 Utils.toNullable(Utils.Get(callStrikes, i, Double.MinValue)), 198 callPosition, 199 isCallATMIncluded, 200 Utils.toNullable(Utils.Get(callDigitalPayoffs, i, Double.MinValue)), 201 Utils.toNullable(Utils.Get(putStrikes, i, Double.MinValue)), 202 putPosition, 203 isPutATMIncluded, 204 Utils.toNullable(Utils.Get(putDigitalPayoffs, i, Double.MinValue)), 205 replication) as DigitalCouponType; 206 207 leg.Add(digitalCoupon); 208 } 209 } 210 return leg; 211 } 212 96 213 } 97 214 } trunk/QLNet/QLNet/Cashflows/CouponPricer.cs
r77 r133 1 1 /* 2 2 Copyright (C) 2008 Siarhei Novik (snovik@gmail.com) 3 Copyright (C) 2008 Toyin Akin (toyin_akin@hotmail.com) 3 4 4 5 This file is part of QLNet Project http://www.qlnet.org … … 23 24 namespace QLNet { 24 25 25 // Coupon pricers26 26 //! generic pricer for floating-rate coupons 27 27 public abstract class FloatingRateCouponPricer : IObservable, IObserver { 28 public abstract void initialize(FloatingRateCoupon coupon); 29 30 //! required interface 28 //! \name required interface 29 //@{ 31 30 public abstract double swapletPrice(); 32 31 public abstract double swapletRate(); … … 35 34 public abstract double floorletPrice(double effectiveFloor); 36 35 public abstract double floorletRate(double effectiveFloor); 37 36 public abstract void initialize(FloatingRateCoupon coupon); 38 37 protected abstract double optionletPrice(Option.Type optionType, double effStrike); 39 38 … … 50 49 51 50 // observer interface 52 public void update() { notifyObservers(); } 51 public void update() { notifyObservers(); } 53 52 #endregion 54 53 } … … 56 55 //! base pricer for capped/floored Ibor coupons 57 56 public abstract class IborCouponPricer : FloatingRateCouponPricer { 58 private Handle<OptionletVolatilityStructure> capletVol_; 59 public Handle<OptionletVolatilityStructure> capletVolatility() { return capletVol_; } 60 61 // public IborCouponPricer(OptionletVolatilityStructure v = OptionletVolatilityStructure>()) 57 public IborCouponPricer() 58 : this(new Handle<OptionletVolatilityStructure>()) { 59 } 62 60 public IborCouponPricer(Handle<OptionletVolatilityStructure> v) { 63 61 capletVol_ = v; 64 65 62 if (!capletVol_.empty()) 66 63 capletVol_.registerWith(update); 67 64 } 68 65 69 // public void setCapletVolatility(OptionletVolatilityStructure v = OptionletVolatilityStructure>()) { 66 public Handle<OptionletVolatilityStructure> capletVolatility() { 67 return capletVol_; 68 } 69 public void setCapletVolatility() { 70 setCapletVolatility(new Handle<OptionletVolatilityStructure>()); 71 } 70 72 public void setCapletVolatility(Handle<OptionletVolatilityStructure> v) { 71 73 capletVol_.unregisterWith(update); … … 76 78 update(); 77 79 } 78 } 79 80 81 //===========================================================================// 82 // BlackIborCouponPricer // 83 //===========================================================================// 80 private Handle<OptionletVolatilityStructure> capletVol_; 81 } 82 84 83 //! Black-formula pricer for capped/floored Ibor coupons 85 84 public class BlackIborCouponPricer : IborCouponPricer { 86 p rivate IborCoupon coupon_;87 private double discount_;88 private double gearing_;89 p rivate double spread_;90 private double spreadLegValue_;91 // private double accrualPeriod_; recheck92 93 public BlackIborCouponPricer() : this(new Handle<OptionletVolatilityStructure>()) { }94 public BlackIborCouponPricer(Handle<OptionletVolatilityStructure> v) : base(v) { }95 85 public BlackIborCouponPricer() 86 : this(new Handle<OptionletVolatilityStructure>()) { 87 } 88 public BlackIborCouponPricer(Handle<OptionletVolatilityStructure> v) 89 : base(v) { 90 } 91 92 //===========================================================================// 93 // BlackIborCouponPricer // 94 //===========================================================================// 96 95 97 96 public override void initialize(FloatingRateCoupon coupon) { 98 coupon_ = (IborCoupon)coupon;97 coupon_ = coupon as IborCoupon; 99 98 gearing_ = coupon_.gearing(); 100 99 spread_ = coupon_.spread(); 101 102 100 Date paymentDate = coupon_.date(); 103 101 InterestRateIndex index = coupon_.index(); … … 111 109 discount_ = 1.0; 112 110 113 // to be done in the future114 // accrualPeriod_ = coupon_.accrualPeriod();115 116 111 spreadLegValue_ = spread_ * coupon_.accrualPeriod() * discount_; 117 112 } 118 113 // 119 114 public override double swapletPrice() { 120 // past or future fixing is managed in InterestRateIndex.fixing() 115 // past or future fixing is managed in InterestRateIndex::fixing() 116 121 117 double swapletPrice = adjustedFixing() * coupon_.accrualPeriod() * discount_; 122 double result = gearing_ * swapletPrice + spreadLegValue_; 123 return result; 124 } 125 118 return gearing_ * swapletPrice + spreadLegValue_; 119 } 126 120 public override double swapletRate() { 127 double result = swapletPrice() / (coupon_.accrualPeriod() * discount_); 128 return result; 129 } 130 121 return swapletPrice() / (coupon_.accrualPeriod() * discount_); 122 } 131 123 public override double capletPrice(double effectiveCap) { 132 124 double capletPrice = optionletPrice(Option.Type.Call, effectiveCap); 133 125 return gearing_ * capletPrice; 134 126 } 135 136 127 public override double capletRate(double effectiveCap) { 137 return capletPrice(effectiveCap)/(coupon_.accrualPeriod()*discount_); 138 } 139 128 return capletPrice(effectiveCap) / (coupon_.accrualPeriod() * discount_); 129 } 140 130 public override double floorletPrice(double effectiveFloor) { 141 131 double floorletPrice = optionletPrice(Option.Type.Put, effectiveFloor); 142 132 return gearing_ * floorletPrice; 143 133 } 144 145 134 public override double floorletRate(double effectiveFloor) { 146 return floorletPrice(effectiveFloor) / (coupon_.accrualPeriod() *discount_);135 return floorletPrice(effectiveFloor) / (coupon_.accrualPeriod() * discount_); 147 136 } 148 137 … … 151 140 if (fixingDate <= Settings.evaluationDate()) { 152 141 // the amount is determined 153 double a, b; 142 double a; 143 double b; 154 144 if (optionType == Option.Type.Call) { 155 145 a = coupon_.indexFixing(); … … 161 151 return Math.Max(a - b, 0.0) * coupon_.accrualPeriod() * discount_; 162 152 } else { 163 throw new NotImplementedException();164 //QL_REQUIRE(!capletVolatility().empty(),165 // "missing optionlet volatility"); 166 // //not yet determined, use Black model167 //double variance = Math.Sqrt(capletVolatility().blackVariance(fixingDate, effStrike));168 //double fixing =blackFormula(optionType, effStrike, adjustedFixing(), variance);169 //return fixing * coupon_.accrualPeriod() * discount_;153 if (!(!capletVolatility().empty())) 154 throw new ApplicationException("missing optionlet volatility"); 155 156 // not yet determined, use Black model 157 double variance = Math.Sqrt(capletVolatility().link.blackVariance(fixingDate, effStrike)); 158 double fixing = Utils.blackFormula(optionType, effStrike, adjustedFixing(), variance); 159 return fixing * coupon_.accrualPeriod() * discount_; 170 160 } 171 161 } 172 162 173 163 private double adjustedFixing() { 164 174 165 double adjustement = 0.0; 175 166 … … 180 171 } else { 181 172 // see Hull, 4th ed., page 550 182 if ( capletVolatility().empty())173 if (!(!capletVolatility().empty())) 183 174 throw new ApplicationException("missing optionlet volatility"); 184 Date d1 = coupon_.fixingDate(), 185 referenceDate = capletVolatility().link.referenceDate(); 175 176 Date d1 = coupon_.fixingDate(); 177 Date referenceDate = capletVolatility().link.referenceDate(); 186 178 if (d1 <= referenceDate) { 187 179 adjustement = 0.0; … … 195 187 return fixing + adjustement; 196 188 } 197 } 198 189 190 private IborCoupon coupon_; 191 private double discount_; 192 private double gearing_; 193 private double spread_; 194 private double spreadLegValue_; 195 } 196 197 //! base pricer for vanilla CMS coupons 198 public abstract class CmsCouponPricer : FloatingRateCouponPricer { 199 public CmsCouponPricer() 200 : this(new Handle<SwaptionVolatilityStructure>()) { 201 } 202 public CmsCouponPricer(Handle<SwaptionVolatilityStructure> v) { 203 swaptionVol_ = v; 204 if (swaptionVol_ != null) 205 swaptionVol_.registerWith(update); 206 } 207 208 public Handle<SwaptionVolatilityStructure> swaptionVolatility() { 209 return swaptionVol_; 210 } 211 public void setSwaptionVolatility() { 212 setSwaptionVolatility(new Handle<SwaptionVolatilityStructure>()); 213 } 214 public void setSwaptionVolatility(Handle<SwaptionVolatilityStructure> v) { 215 if (swaptionVol_ != null) 216 swaptionVol_.unregisterWith(update); 217 swaptionVol_ = v; 218 if (swaptionVol_ != null) 219 swaptionVol_.registerWith(update); 220 update(); 221 } 222 private Handle<SwaptionVolatilityStructure> swaptionVol_; 223 } 224 225 226 //===========================================================================// 227 // CouponSelectorToSetPricer // 228 //===========================================================================// 199 229 200 230 public class PricerSetter : IAcyclicVisitor { 201 231 private FloatingRateCouponPricer pricer_; 202 203 232 public PricerSetter(FloatingRateCouponPricer pricer) { 204 233 pricer_ = pricer; … … 222 251 if (!(pricer_ is IborCouponPricer)) 223 252 throw new ApplicationException("pricer not compatible with Ibor coupon"); 224 c.setPricer(pricer_); 225 } 226 227 //public void visit(CappedFlooredIborCoupon c); 228 //public void visit(DigitalIborCoupon c); 229 //public void visit(CmsCoupon c); 230 //public void visit(CappedFlooredCmsCoupon c) 231 //public void visit(DigitalCmsCoupon c) 253 c.setPricer(pricer_ as IborCouponPricer); 254 } 255 public void visit(CappedFlooredIborCoupon c) { 256 if (!(pricer_ is IborCouponPricer)) 257 throw new ApplicationException("pricer not compatible with Ibor coupon"); 258 c.setPricer(pricer_ as IborCouponPricer); 259 } 260 public void visit(DigitalIborCoupon c) { 261 if (!(pricer_ is IborCouponPricer)) 262 throw new ApplicationException("pricer not compatible with Ibor coupon"); 263 c.setPricer(pricer_ as IborCouponPricer); 264 } 265 public void visit(CmsCoupon c) { 266 if (!(pricer_ is CmsCouponPricer)) 267 throw new ApplicationException("pricer not compatible with CMS coupon"); 268 c.setPricer(pricer_ as CmsCouponPricer); 269 } 270 public void visit(CappedFlooredCmsCoupon c) { 271 if (!(pricer_ is CmsCouponPricer)) 272 throw new ApplicationException("pricer not compatible with CMS coupon"); 273 c.setPricer(pricer_ as CmsCouponPricer); 274 } 275 public void visit(DigitalCmsCoupon c) { 276 if (!(pricer_ is CmsCouponPricer)) 277 throw new ApplicationException("pricer not compatible with CMS coupon"); 278 c.setPricer(pricer_ as CmsCouponPricer); 279 } 232 280 //public void visit(RangeAccrualFloatersCoupon c) 233 234 //void PricerSetter::visit(DigitalIborCoupon& c) { 235 // const boost::shared_ptr<IborCouponPricer> iborCouponPricer = 236 // boost::dynamic_pointer_cast<IborCouponPricer>(pricer_); 237 // QL_REQUIRE(iborCouponPricer, 238 // "pricer not compatible with Ibor coupon"); 239 // c.setPricer(iborCouponPricer); 281 //{ 282 // if (!(pricer_ is RangeAccrualPricer)) 283 // throw new ApplicationException("pricer not compatible with range-accrual coupon"); 284 // c.setPricer(pricer_ as RangeAccrualPricer); 240 285 //} 241 242 //void PricerSetter::visit(CappedFlooredIborCoupon& c) { 243 // const boost::shared_ptr<IborCouponPricer> iborCouponPricer = 244 // boost::dynamic_pointer_cast<IborCouponPricer>(pricer_); 245 // QL_REQUIRE(iborCouponPricer, 246 // "pricer not compatible with Ibor coupon"); 247 // c.setPricer(iborCouponPricer); 248 //} 249 250 //void PricerSetter::visit(CmsCoupon& c) { 251 // const boost::shared_ptr<CmsCouponPricer> cmsCouponPricer = 252 // boost::dynamic_pointer_cast<CmsCouponPricer>(pricer_); 253 // QL_REQUIRE(cmsCouponPricer, 254 // "pricer not compatible with CMS coupon"); 255 // c.setPricer(cmsCouponPricer); 256 //} 257 258 //void PricerSetter::visit(CappedFlooredCmsCoupon& c) { 259 // const boost::shared_ptr<CmsCouponPricer> cmsCouponPricer = 260 // boost::dynamic_pointer_cast<CmsCouponPricer>(pricer_); 261 // QL_REQUIRE(cmsCouponPricer, 262 // "pricer not compatible with CMS coupon"); 263 // c.setPricer(cmsCouponPricer); 264 //} 265 266 //void PricerSetter::visit(DigitalCmsCoupon& c) { 267 // const boost::shared_ptr<CmsCouponPricer> cmsCouponPricer = 268 // boost::dynamic_pointer_cast<CmsCouponPricer>(pricer_); 269 // QL_REQUIRE(cmsCouponPricer, 270 // "pricer not compatible with CMS coupon"); 271 // c.setPricer(cmsCouponPricer); 272 //} 273 274 //void PricerSetter::visit(RangeAccrualFloatersCoupon& c) { 275 // const boost::shared_ptr<RangeAccrualPricer> rangeAccrualPricer = 276 // boost::dynamic_pointer_cast<RangeAccrualPricer>(pricer_); 277 // QL_REQUIRE(rangeAccrualPricer, 278 // "pricer not compatible with range-accrual coupon"); 279 // c.setPricer(rangeAccrualPricer); 280 //} 281 } 282 286 } 283 287 284 288 partial class Utils { 285 289 public static void setCouponPricer(List<CashFlow> leg, FloatingRateCouponPricer pricer) { 286 290 PricerSetter setter = new PricerSetter(pricer); 287 foreach (CashFlow cf in leg) {291 foreach (CashFlow cf in leg) { 288 292 cf.accept(setter); 289 293 } … … 292 296 public static void setCouponPricers(List<CashFlow> leg, List<FloatingRateCouponPricer> pricers) { 293 297 throw new NotImplementedException(); 294 //Size nCashFlows = leg.size(); 295 //QL_REQUIRE(nCashFlows>0, "no cashflows"); 296 297 //Size nPricers = pricers.size(); 298 //QL_REQUIRE(nCashFlows >= nPricers, 299 // "mismatch between leg size (" << nCashFlows << 300 // ") and number of pricers (" << nPricers << ")"); 301 302 //for (Size i=0; i<nCashFlows; ++i) { 303 // PricerSetter setter(i<nPricers ? pricers[i] : pricers[nPricers-1]); 304 // leg[i]->accept(setter); 298 //int nCashFlows = leg.Count; 299 //if (!(nCashFlows > 0)) 300 // throw new ApplicationException("no cashflows"); 301 302 //int nPricers = pricers.Count; 303 //if (!(nCashFlows >= nPricers)) 304 // throw new ApplicationException("mismatch between leg size (" + nCashFlows + ") and number of pricers (" + nPricers + ")"); 305 306 //for (int i = 0; i < nCashFlows; ++i) 307 //{ 308 // PricerSetter[] setter = new PricerSetter[i](i < nPricers ? pricers : pricers[nPricers - 1]); 309 // leg[i].accept(setter); 305 310 //} 306 311 } trunk/QLNet/QLNet/Cashflows/FloatingRateCoupon.cs
r62 r133 1 1 /* 2 2 Copyright (C) 2008 Siarhei Novik (snovik@gmail.com) 3 3 Copyright (C) 2008 Toyin Akin (toyin_akin@hotmail.com) 4 * 4 5 This file is part of QLNet Project http://www.qlnet.org 5 6 … … 21 22 namespace QLNet { 22 23 public class FloatingRateCoupon : Coupon, IObserver { 23 protected InterestRateIndex index_;24 protected InterestRateIndex index_; 24 25 protected DayCounter dayCounter_; 25 26 protected int fixingDays_; … … 29 30 protected FloatingRateCouponPricer pricer_; 30 31 31 // constructors32 // constructors 32 33 //private FloatingRateCoupon() { } 33 34 //public FloatingRateCoupon(Date paymentDate, double nominal, Date startDate, Date endDate, int fixingDays, InterestRateIndex index) : … … 47 48 public FloatingRateCoupon(Date paymentDate, double nominal, Date startDate, Date endDate, int fixingDays, InterestRateIndex index, 48 49 double gearing, double spread, Date refPeriodStart, Date refPeriodEnd, DayCounter dayCounter, bool isInArrears) : 49 base(nominal, paymentDate, startDate, endDate, refPeriodStart, refPeriodEnd) { 50 index_ = index; 51 dayCounter_ = dayCounter; 52 fixingDays_ = fixingDays == default(int) ? index.fixingDays() : fixingDays; 53 gearing_ = gearing; 54 spread_ = spread; 55 isInArrears_ = isInArrears; 56 57 if (gearing_ == 0) throw new ArgumentException("Null gearing not allowed"); 50 base(nominal, paymentDate, startDate, endDate, refPeriodStart, refPeriodEnd) { 51 index_ = index; 52 dayCounter_ = dayCounter; 53 fixingDays_ = fixingDays == default(int) ? index.fixingDays() : fixingDays; 54 gearing_ = gearing; 55 spread_ = spread; 56 isInArrears_ = isInArrears; 58 57 59 if (dayCounter_ == null) 60 dayCounter_ = index_.dayCounter(); 58 if (gearing_ == 0) throw new ArgumentException("Null gearing not allowed"); 59 60 if (dayCounter_ == null) 61 dayCounter_ = index_.dayCounter(); 61 62 62 63 // add as observer … … 68 69 public FloatingRateCoupon() { } 69 70 70 public v oid setPricer(FloatingRateCouponPricer pricer) {71 public virtual void setPricer(FloatingRateCouponPricer pricer) { 71 72 if (pricer_ != null) // remove from the old observable 72 73 pricer_.unregisterWith(update); … … 137 138 public double price(YieldTermStructure yts) { 138 139 return amount() * yts.discount(date()); 139 }140 141 //! convexity adjustment for the given index fixing140 } 141 142 //! convexity adjustment for the given index fixing 142 143 protected double convexityAdjustmentImpl(double f) { 143 return (gearing() == 0 ? 0 : adjustedFixing-f);144 }144 return (gearing() == 0 ? 0 : adjustedFixing - f); 145 } 145 146 146 147 //! convexity adjustment … … 157 158 index, gearing, spread, refPeriodStart, refPeriodEnd, dayCounter, isInArrears); 158 159 } 159 }160 } 160 161 } trunk/QLNet/QLNet/Cashflows/Iborcoupon.cs
r61 r133 1 1 /* 2 2 Copyright (C) 2008 Siarhei Novik (snovik@gmail.com) 3 3 Copyright (C) 2008 Toyin Akin (toyin_akin@hotmail.com) 4 * 4 5 This file is part of QLNet Project http://www.qlnet.org 5 6 … … 42 43 //! Implemented in order to manage the case of par coupon 43 44 public override double indexFixing() { 44 #if QL_USE_INDEXED_COUPON45 #if QL_USE_INDEXED_COUPON 45 46 return index_->fixing(fixingDate()); 46 #else47 #else 47 48 if (isInArrears()) { 48 49 return index_.fixing(fixingDate()); … … 65 66 if (pastFixing != default(double)) 66 67 return pastFixing; 67 } 68 catch { 68 } catch { 69 69 // fall through and forecast 70 70 } … … 83 83 return (startDiscount / endDiscount - 1.0) / spanningTime; 84 84 } 85 #endif85 #endif 86 86 } 87 87 … … 192 192 193 193 public List<CashFlow> value() { 194 List<CashFlow> cashflows = CashFlowVectors.FloatingLeg<Ibor Coupon>(194 List<CashFlow> cashflows = CashFlowVectors.FloatingLeg<IborIndex, IborCoupon, CappedFlooredIborCoupon>( 195 195 notionals_, schedule_, index_, paymentDayCounter_, 196 196 paymentAdjustment_, fixingDays_, gearings_, spreads_, trunk/QLNet/QLNet/Indexes/IBORIndex.cs
r61 r133 2 2 Copyright (C) 2008 Siarhei Novik (snovik@gmail.com) 3 3 Copyright (C) 2008 Andrea Maggiulli 4 4 Copyright (C) 2008 Toyin Akin (toyin_akin@hotmail.com) 5 * 5 6 This file is part of QLNet Project http://www.qlnet.org 6 7 … … 25 26 namespace QLNet { 26 27 //! base class for Inter-Bank-Offered-Rate indexes (e.g. %Libor, etc.) 27 public class IborIndex : InterestRateIndex 28 { 28 public class IborIndex : InterestRateIndex { 29 29 protected BusinessDayConvention convention_; 30 30 public BusinessDayConvention businessDayConvention() { return convention_; } … … 37 37 public bool endOfMonth() { return endOfMonth_; } 38 38 39 // need by CashFlowVectors 40 public IborIndex() { } 41 39 42 public IborIndex(string familyName, Period tenor, int settlementDays, Currency currency, 40 43 Calendar fixingCalendar, BusinessDayConvention convention, bool endOfMonth, 41 DayCounter dayCounter) : this(familyName, tenor, settlementDays, currency, 42 fixingCalendar,convention,endOfMonth, 43 dayCounter, new Handle<YieldTermStructure>()) {} 44 DayCounter dayCounter) 45 : this(familyName, tenor, settlementDays, currency, 46 fixingCalendar, convention, endOfMonth, 47 dayCounter, new Handle<YieldTermStructure>()) { } 44 48 45 public IborIndex(string familyName, Period tenor, int settlementDays, Currency currency,46 Calendar fixingCalendar, BusinessDayConvention convention, bool endOfMonth,47 DayCounter dayCounter, Handle<YieldTermStructure> h) :49 public IborIndex(string familyName, Period tenor, int settlementDays, Currency currency, 50 Calendar fixingCalendar, BusinessDayConvention convention, bool endOfMonth, 51 DayCounter dayCounter, Handle<YieldTermStructure> h) : 48 52 base(familyName, tenor, settlementDays, currency, fixingCalendar, dayCounter) { 49 53 convention_ = convention; trunk/QLNet/QLNet/Indexes/InterestRateIndex.cs
r61 r133 1 1 /* 2 2 Copyright (C) 2008 Siarhei Novik (snovik@gmail.com) 3 3 Copyright (C) 2008 Toyin Akin (toyin_akin@hotmail.com) 4 4 5 This file is part of QLNet Project http://www.qlnet.org 5 6 … … 41 42 42 43 protected DayCounter dayCounter_; 43 public DayCounter dayCounter() { return dayCounter_; }