Exemplo n.º 1
0
        public static Tuple <Date, double> TryGetValue(this SortedDictionary <Date, double> historicalRates, Date fixingDate, ICalendar resetCalendar)
        {
            var returnTuple = Tuple.Create(fixingDate, 0.0);

            if (historicalRates != null && historicalRates.Count > 0)
            {
                var lastKeyPair  = historicalRates.ElementAt(historicalRates.Count - 1);
                var firstKeyPair = historicalRates.ElementAt(0);
                if (lastKeyPair.Key < fixingDate)
                {
                    returnTuple = Tuple.Create(lastKeyPair.Key, lastKeyPair.Value);
                }
                else if (firstKeyPair.Key <= fixingDate)
                {
                    while (!historicalRates.ContainsKey(fixingDate) ||
                           (historicalRates.ContainsKey(fixingDate) && double.IsNaN(historicalRates[fixingDate])))
                    {
                        fixingDate = resetCalendar.AddBizDays(fixingDate, -1);
                    }
                    var fixingRate = historicalRates[fixingDate];
                    returnTuple = Tuple.Create(fixingDate, fixingRate);
                }
            }
            return(returnTuple);
        }
Exemplo n.º 2
0
        public void TestCalendarAdjust()
        {
            var d1 = new Date(2014, 01, 30);
            var d2 = new Date(2014, 01, 31);
            var d3 = new Date(2014, 02, 06);
            var d4 = new Date(2014, 02, 12);

            Assert.AreEqual(new Date(2014, 02, 07), _cal.Adjust(d2, BusinessDayConvention.Following));
            Assert.AreEqual(new Date(2014, 01, 30), _cal.Adjust(d2, BusinessDayConvention.ModifiedFollowing));
            Assert.AreEqual(new Date(2014, 01, 30), _cal.Adjust(d3, BusinessDayConvention.Previous));
            Assert.AreEqual(new Date(2014, 02, 07), _cal.Adjust(d3, BusinessDayConvention.ModifiedPrevious));

            Assert.AreEqual(new Date(2014, 01, 31), _cal.AddDays(d1, 1));
            Assert.AreEqual(new Date(2014, 02, 07), _cal.AddBizDays(d1, 1));
            Assert.AreEqual(new Date(2014, 02, 07), _cal.AddDays(d1, 1, BusinessDayConvention.Following));
            Assert.AreEqual(new Date(2014, 01, 30), _cal.AddDays(d1, 1, BusinessDayConvention.ModifiedFollowing));
            Assert.AreEqual(new Date(2014, 02, 07), _cal.AddBizDays(d1, 1, BusinessDayConvention.Previous));
            Assert.AreEqual(new Date(2014, 02, 07), _cal.AddBizDays(d1, 1, BusinessDayConvention.ModifiedFollowing));

            Assert.AreEqual(new Date(2014, 02, 01), _cal.AddDays(d2, 1));
            Assert.AreEqual(new Date(2014, 02, 07), _cal.AddBizDays(d2, 1));
            Assert.AreEqual(new Date(2014, 02, 07), _cal.AddDays(d2, 1, BusinessDayConvention.Following));
            Assert.AreEqual(new Date(2014, 02, 07), _cal.AddDays(d2, 1, BusinessDayConvention.ModifiedFollowing));
            Assert.AreEqual(new Date(2014, 02, 10), _cal.AddBizDays(d2, 1, BusinessDayConvention.Following));
            Assert.AreEqual(new Date(2014, 02, 07), _cal.AddBizDays(d2, 1, BusinessDayConvention.Previous));

            Assert.AreEqual(false, _cal.BizDaysBetweenDates(d2, d3).Any());
            Assert.AreEqual(4, _cal.BizDaysBetweenDates(d1, d4).Count);
        }
Exemplo n.º 3
0
        public static double GetAverageIndex(this IDictionary <Date, double> floatingIndexRates,
                                             Date fixingDate,
                                             ICalendar resetCalendar,
                                             int period,
                                             int?fixingRateDigits = null)
        {
            var    indexRates = floatingIndexRates.ToSortedDictionary();
            double sum        = 0.0;
            var    n          = period;
            var    temp       = new List <Tuple <Date, double> >();

            //var t1D = new Term("1D");
            while (indexRates != null && indexRates.Count > 0)
            {
                if (resetCalendar.IsHoliday(fixingDate))
                {
                    fixingDate = resetCalendar.AddBizDays(fixingDate, -1);
                    continue;
                }

                double value;
                var    fixingTruple = indexRates.TryGetValue(fixingDate, resetCalendar);
                fixingDate = fixingTruple.Item1;
                value      = fixingTruple.Item2;
                sum       += value;
                temp.Add(Tuple.Create(fixingDate, value));

                fixingDate = resetCalendar.AddBizDays(fixingDate, -1);
                if (--n == 0)
                {
                    break;
                }
            }

            return((sum / period).Round((fixingRateDigits ?? 13) + 2));
        }
        public static double tradeVolLinearInterp(Date valuationDate, double tradeOpenVol, double tradeCloseVol, Date startDate, Date maturityDate,
                                                  int numOfSmoothingDays, DayCountMode dayCountMode, ICalendar calendar)
        {
            Date   smoothingEndDate;
            double T, t;

            switch (dayCountMode)
            {
            case DayCountMode.CalendarDay:
                smoothingEndDate = startDate.AddDays(numOfSmoothingDays);
                if (smoothingEndDate > maturityDate)
                {
                    smoothingEndDate = maturityDate;
                }
                T = smoothingEndDate - startDate;
                t = valuationDate - startDate;

                break;

            default:
                smoothingEndDate = calendar.AddBizDays(startDate, numOfSmoothingDays);
                if (smoothingEndDate > maturityDate)
                {
                    smoothingEndDate = maturityDate;
                }
                Date valuationTradingDate = calendar.IsBizDay(valuationDate) ? valuationDate : calendar.PrevBizDay(valuationDate);
                T = calendar.BizDaysBetweenDatesExcluStartDay(startDate, smoothingEndDate).Count;
                t = calendar.BizDaysBetweenDatesExcluStartDay(startDate, valuationTradingDate).Count;
                break;
            }

            if (valuationDate >= smoothingEndDate)
            {
                return(tradeCloseVol);
            }
            if (valuationDate < smoothingEndDate & valuationDate >= startDate)
            {
                double slope = (tradeCloseVol - tradeOpenVol) / T;
                return(tradeOpenVol + slope * t);
            }
            else
            {
                throw new PricingBaseException("AnalyticalOptionTradeVolInterp error: valuationDate < startDate");
            }
        }
Exemplo n.º 5
0
        public double GetResetRate(
            IYieldCurve fixingCurve,
            Date fixingDate,
            ICalendar resetCalendar,
            ITerm fixingTenor,
            int period,
            IDictionary <Date, double> historicalRates,
            Compound indexCompound,
            IDayCount indexDayCount
            )
        {
            var refDate = fixingCurve != null ? fixingCurve.ReferenceDate : fixingDate;
            var t1D     = new Term("1D");

            while (resetCalendar.IsHoliday(fixingDate))
            {
                fixingDate = t1D.Prev(fixingDate);
            }

            IDictionary <Date, double> indexRates = new SortedDictionary <Date, double>();

            if (fixingDate <= refDate)
            {
                indexRates = historicalRates;
            }
            else
            {
                for (var i = 0; i < period; i++)
                {
                    var    resetFixingDate = resetCalendar.AddBizDays(fixingDate, -i);
                    double reseFixingRate  = fixingCurve.GetForwardRate(resetFixingDate < refDate ? refDate : resetFixingDate, fixingTenor, indexCompound, indexDayCount);
                    indexRates.Add(resetFixingDate, reseFixingRate);
                }
            }

            return(indexRates.GetAverageIndex(fixingDate, resetCalendar, period, _resetRateDigits));
        }
Exemplo n.º 6
0
 /// <summary>
 /// 根据日历和基准日期得到目标日期
 /// </summary>
 /// <param name="calendar">日历</param>
 /// <param name="date">基准日期</param>
 /// <returns>目标日期</returns>
 public Date Get(ICalendar calendar, Date date)
 {
     return(IsBizDay ?
            calendar.AddBizDays(date, Offset, BusinessDayConvention)
                         : calendar.AddDays(date, Offset, BusinessDayConvention));
 }