Assembla home | Assembla project page
 

Changeset 133

Show
Ignore:
Timestamp:
04/28/08 17:24:48 (2 months ago)
Author:
snovik
Message:

New: Update from Toyin Akin

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/QLNet/QLNet/Cashflows/Cashflowvectors.cs

    r61 r133  
    11/* 
    22 Copyright (C) 2008 Siarhei Novik (snovik@gmail.com) 
    3    
     3 Copyright (C) 2008 Toyin Akin (toyin_akin@hotmail.com) 
     4 *  
    45 This file is part of QLNet Project http://www.qlnet.org 
    56 
     
    2324 
    2425namespace 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 
    2536    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, 
    2838                                                Schedule schedule, 
    29                                                 InterestRateIndex index, 
     39                                                InterestRateIndexType index, 
    3040                                                DayCounter paymentDayCounter, 
    3141                                                BusinessDayConvention paymentAdj, 
     
    3747                                                bool isInArrears, 
    3848                                                bool isZero) 
    39                 where FloatingCouponType : FloatingRateCoupon, new() { 
     49            where InterestRateIndexType : InterestRateIndex, new() 
     50            where FloatingCouponType : FloatingRateCoupon, new() 
     51            where CappedFlooredCouponType : CappedFlooredCoupon, new() { 
    4052 
    4153            int n = schedule.Count; 
     
    5971 
    6072            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) { 
    6476                refStart = start = schedule[i]; 
    65                 refEnd   =   end = schedule[i+1]; 
     77                refEnd = end = schedule[i + 1]; 
    6678                Date paymentDate = isZero ? lastPaymentDate : calendar.adjust(end, paymentAdj); 
    67                 if (i==0   && !schedule.isRegular(i+1)) 
     79                if (i == 0 && !schedule.isRegular(i + 1)) 
    6880                    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)) 
    7082                    refEnd = calendar.adjust(start + schedule.tenor(), schedule.businessDayConvention()); 
    7183 
     
    7385                    leg.Add(new FixedRateCoupon(Utils.Get(nominals, i), 
    7486                                                paymentDate, 
    75                                                 Utils.effectiveFixedRate(spreads,caps, floors,i), 
     87                                                Utils.effectiveFixedRate(spreads, caps, floors, i), 
    7688                                                paymentDayCounter, 
    7789                                                start, end, refStart, refEnd)); 
    78                 } else {                                                            // floating coupon 
     90                } else { 
    7991                    if (Utils.noOption(caps, floors, i)) { 
    8092                        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)); 
    89101                    } 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)); 
    91113                    } 
    92114                } 
     
    94116            return leg; 
    95117        } 
     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 
    96213    } 
    97214} 
  • trunk/QLNet/QLNet/Cashflows/CouponPricer.cs

    r77 r133  
    11/* 
    22 Copyright (C) 2008 Siarhei Novik (snovik@gmail.com) 
     3 Copyright (C) 2008 Toyin Akin (toyin_akin@hotmail.com) 
    34   
    45 This file is part of QLNet Project http://www.qlnet.org 
     
    2324namespace QLNet { 
    2425 
    25     // Coupon pricers 
    2626    //! generic pricer for floating-rate coupons 
    2727    public abstract class FloatingRateCouponPricer : IObservable, IObserver { 
    28         public abstract void initialize(FloatingRateCoupon coupon); 
    29  
    30         //! required interface 
     28        //! \name required interface 
     29        //@{ 
    3130        public abstract double swapletPrice(); 
    3231        public abstract double swapletRate(); 
     
    3534        public abstract double floorletPrice(double effectiveFloor); 
    3635        public abstract double floorletRate(double effectiveFloor); 
    37  
     36        public abstract void initialize(FloatingRateCoupon coupon); 
    3837        protected abstract double optionletPrice(Option.Type optionType, double effStrike); 
    3938 
     
    5049 
    5150        // observer interface 
    52         public void update() { notifyObservers(); }  
     51        public void update() { notifyObservers(); } 
    5352        #endregion 
    5453    } 
     
    5655    //! base pricer for capped/floored Ibor coupons 
    5756    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        } 
    6260        public IborCouponPricer(Handle<OptionletVolatilityStructure> v) { 
    6361            capletVol_ = v; 
    64  
    6562            if (!capletVol_.empty()) 
    6663                capletVol_.registerWith(update); 
    6764        } 
    6865 
    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        } 
    7072        public void setCapletVolatility(Handle<OptionletVolatilityStructure> v) { 
    7173            capletVol_.unregisterWith(update); 
     
    7678            update(); 
    7779        } 
    78     } 
    79  
    80  
    81     //===========================================================================// 
    82     //                              BlackIborCouponPricer                        // 
    83     //===========================================================================// 
     80        private Handle<OptionletVolatilityStructure> capletVol_; 
     81    } 
     82 
    8483    //! Black-formula pricer for capped/floored Ibor coupons 
    8584    public class BlackIborCouponPricer : IborCouponPricer { 
    86         private IborCoupon coupon_; 
    87         private double discount_; 
    88         private double gearing_; 
    89         private double spread_; 
    90         private double spreadLegValue_; 
    91         // private double accrualPeriod_;       recheck 
    92  
    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        //===========================================================================// 
    9695 
    9796        public override void initialize(FloatingRateCoupon coupon) { 
    98             coupon_ = (IborCoupon)coupon; 
     97            coupon_ = coupon as IborCoupon; 
    9998            gearing_ = coupon_.gearing(); 
    10099            spread_ = coupon_.spread(); 
    101  
    102100            Date paymentDate = coupon_.date(); 
    103101            InterestRateIndex index = coupon_.index(); 
     
    111109                discount_ = 1.0; 
    112110 
    113             // to be done in the future 
    114             // accrualPeriod_ = coupon_.accrualPeriod(); 
    115  
    116111            spreadLegValue_ = spread_ * coupon_.accrualPeriod() * discount_; 
    117112        } 
    118  
     113        //  
    119114        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 
    121117            double swapletPrice = adjustedFixing() * coupon_.accrualPeriod() * discount_; 
    122             double result = gearing_ * swapletPrice + spreadLegValue_; 
    123             return result; 
    124         } 
    125  
     118            return gearing_ * swapletPrice + spreadLegValue_; 
     119        } 
    126120        public override double swapletRate() { 
    127             double result = swapletPrice() / (coupon_.accrualPeriod() * discount_); 
    128             return result; 
    129         } 
    130  
     121            return swapletPrice() / (coupon_.accrualPeriod() * discount_); 
     122        } 
    131123        public override double capletPrice(double effectiveCap) { 
    132124            double capletPrice = optionletPrice(Option.Type.Call, effectiveCap); 
    133125            return gearing_ * capletPrice; 
    134126        } 
    135  
    136127        public override double capletRate(double effectiveCap) { 
    137             return capletPrice(effectiveCap)/(coupon_.accrualPeriod()*discount_); 
    138         } 
    139  
     128            return capletPrice(effectiveCap) / (coupon_.accrualPeriod() * discount_); 
     129        } 
    140130        public override double floorletPrice(double effectiveFloor) { 
    141131            double floorletPrice = optionletPrice(Option.Type.Put, effectiveFloor); 
    142132            return gearing_ * floorletPrice; 
    143133        } 
    144  
    145134        public override double floorletRate(double effectiveFloor) { 
    146             return floorletPrice(effectiveFloor) / (coupon_.accrualPeriod()*discount_); 
     135            return floorletPrice(effectiveFloor) / (coupon_.accrualPeriod() * discount_); 
    147136        } 
    148137 
     
    151140            if (fixingDate <= Settings.evaluationDate()) { 
    152141                // the amount is determined 
    153                 double a, b; 
     142                double a; 
     143                double b; 
    154144                if (optionType == Option.Type.Call) { 
    155145                    a = coupon_.indexFixing(); 
     
    161151                return Math.Max(a - b, 0.0) * coupon_.accrualPeriod() * discount_; 
    162152            } else { 
    163                 throw new NotImplementedException(); 
    164                 //QL_REQUIRE(!capletVolatility().empty(), 
    165                 //           "missing optionlet volatility"); 
    166                 //// not yet determined, use Black model 
    167                 //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_; 
    170160            } 
    171161        } 
    172162 
    173163        private double adjustedFixing() { 
     164 
    174165            double adjustement = 0.0; 
    175166 
     
    180171            } else { 
    181172                // see Hull, 4th ed., page 550 
    182                 if (capletVolatility().empty()) 
     173                if (!(!capletVolatility().empty())) 
    183174                    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(); 
    186178                if (d1 <= referenceDate) { 
    187179                    adjustement = 0.0; 
     
    195187            return fixing + adjustement; 
    196188        } 
    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    //===========================================================================// 
    199229 
    200230    public class PricerSetter : IAcyclicVisitor { 
    201231        private FloatingRateCouponPricer pricer_; 
    202  
    203232        public PricerSetter(FloatingRateCouponPricer pricer) { 
    204233            pricer_ = pricer; 
     
    222251            if (!(pricer_ is IborCouponPricer)) 
    223252                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        } 
    232280        //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); 
    240285        //} 
    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    } 
    283287 
    284288    partial class Utils { 
    285289        public static void setCouponPricer(List<CashFlow> leg, FloatingRateCouponPricer pricer) { 
    286290            PricerSetter setter = new PricerSetter(pricer); 
    287             foreach(CashFlow cf in leg) { 
     291            foreach (CashFlow cf in leg) { 
    288292                cf.accept(setter); 
    289293            } 
     
    292296        public static void setCouponPricers(List<CashFlow> leg, List<FloatingRateCouponPricer> pricers) { 
    293297            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); 
    305310            //} 
    306311        } 
  • trunk/QLNet/QLNet/Cashflows/FloatingRateCoupon.cs

    r62 r133  
    11/* 
    22 Copyright (C) 2008 Siarhei Novik (snovik@gmail.com) 
    3    
     3 Copyright (C) 2008 Toyin Akin (toyin_akin@hotmail.com) 
     4 *  
    45 This file is part of QLNet Project http://www.qlnet.org 
    56 
     
    2122namespace QLNet { 
    2223    public class FloatingRateCoupon : Coupon, IObserver { 
    23            protected InterestRateIndex index_; 
     24        protected InterestRateIndex index_; 
    2425        protected DayCounter dayCounter_; 
    2526        protected int fixingDays_; 
     
    2930        protected FloatingRateCouponPricer pricer_; 
    3031 
    31                // constructors 
     32        // constructors 
    3233        //private FloatingRateCoupon() { } 
    3334        //public FloatingRateCoupon(Date paymentDate, double nominal, Date startDate, Date endDate, int fixingDays, InterestRateIndex index) : 
     
    4748        public FloatingRateCoupon(Date paymentDate, double nominal, Date startDate, Date endDate, int fixingDays, InterestRateIndex index, 
    4849                                  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; 
    5857 
    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(); 
    6162 
    6263            // add as observer 
     
    6869        public FloatingRateCoupon() { } 
    6970 
    70         public void setPricer(FloatingRateCouponPricer pricer) { 
     71        public virtual void setPricer(FloatingRateCouponPricer pricer) { 
    7172            if (pricer_ != null)   // remove from the old observable 
    7273                pricer_.unregisterWith(update); 
     
    137138        public double price(YieldTermStructure yts) { 
    138139            return amount() * yts.discount(date()); 
    139                
    140                  
    141                //! convexity adjustment for the given index fixing 
     140       
     141 
     142        //! convexity adjustment for the given index fixing 
    142143        protected double convexityAdjustmentImpl(double f) { 
    143                        return (gearing() == 0 ? 0 : adjustedFixing-f); 
    144                
     144            return (gearing() == 0 ? 0 : adjustedFixing - f); 
     145       
    145146 
    146147        //! convexity adjustment 
     
    157158                       index, gearing, spread, refPeriodStart, refPeriodEnd, dayCounter, isInArrears); 
    158159        } 
    159        
     160   
    160161} 
  • trunk/QLNet/QLNet/Cashflows/Iborcoupon.cs

    r61 r133  
    11/* 
    22 Copyright (C) 2008 Siarhei Novik (snovik@gmail.com) 
    3    
     3 Copyright (C) 2008 Toyin Akin (toyin_akin@hotmail.com) 
     4 *  
    45 This file is part of QLNet Project http://www.qlnet.org 
    56 
     
    4243        //! Implemented in order to manage the case of par coupon 
    4344        public override double indexFixing() { 
    44         #if QL_USE_INDEXED_COUPON 
     45#if QL_USE_INDEXED_COUPON 
    4546            return index_->fixing(fixingDate()); 
    46         #else 
     47#else 
    4748            if (isInArrears()) { 
    4849                return index_.fixing(fixingDate()); 
     
    6566                        if (pastFixing != default(double)) 
    6667                            return pastFixing; 
    67                     } 
    68                     catch { 
     68                    } catch { 
    6969                        // fall through and forecast 
    7070                    } 
     
    8383                return (startDiscount / endDiscount - 1.0) / spanningTime; 
    8484            } 
    85         #endif 
     85#endif 
    8686        } 
    8787 
     
    192192 
    193193        public List<CashFlow> value() { 
    194             List<CashFlow> cashflows = CashFlowVectors.FloatingLeg<IborCoupon>( 
     194            List<CashFlow> cashflows = CashFlowVectors.FloatingLeg<IborIndex, IborCoupon, CappedFlooredIborCoupon>( 
    195195                                    notionals_, schedule_, index_, paymentDayCounter_, 
    196196                                    paymentAdjustment_, fixingDays_, gearings_, spreads_, 
  • trunk/QLNet/QLNet/Indexes/IBORIndex.cs

    r61 r133  
    22 Copyright (C) 2008 Siarhei Novik (snovik@gmail.com) 
    33 Copyright (C) 2008 Andrea Maggiulli 
    4    
     4 Copyright (C) 2008 Toyin Akin (toyin_akin@hotmail.com) 
     5 *  
    56 This file is part of QLNet Project http://www.qlnet.org 
    67 
     
    2526namespace QLNet { 
    2627    //! base class for Inter-Bank-Offered-Rate indexes (e.g. %Libor, etc.) 
    27     public class IborIndex : InterestRateIndex  
    28     { 
     28    public class IborIndex : InterestRateIndex { 
    2929        protected BusinessDayConvention convention_; 
    3030        public BusinessDayConvention businessDayConvention() { return convention_; } 
     
    3737        public bool endOfMonth() { return endOfMonth_; } 
    3838 
     39        // need by CashFlowVectors 
     40        public IborIndex() { } 
     41 
    3942        public IborIndex(string familyName, Period tenor, int settlementDays, Currency currency, 
    4043                 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>()) { } 
    4448 
    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) : 
    4852            base(familyName, tenor, settlementDays, currency, fixingCalendar, dayCounter) { 
    4953            convention_ = convention; 
  • trunk/QLNet/QLNet/Indexes/InterestRateIndex.cs

    r61 r133  
    11/* 
    22 Copyright (C) 2008 Siarhei Novik (snovik@gmail.com) 
    3    
     3 Copyright (C) 2008 Toyin Akin (toyin_akin@hotmail.com) 
     4 
    45 This file is part of QLNet Project http://www.qlnet.org 
    56 
     
    4142 
    4243        protected DayCounter dayCounter_; 
    43         public DayCounter dayCounter() { return dayCounter_; } </