public OISSpreadRepoGenerator(double onRate, string market, DateTime settle, DiscountCurve oisCurve) : base(onRate, market, settle) { this.oisCurve = oisCurve; country = (BondAnalytics.Country)Enum.Parse(typeof(BondAnalytics.Country), market); var startdate = CarbonModel.RollDate(settle, 1, DateUnit.Bd, BusinessDayConvention.Following, Calendar.Calendar.OTHolidayCode(country)); DateTime[] discDfDates = oisCurve.AsTuples().Select(x => x.Item1).ToArray(); double[] discDfs = oisCurve.AsTuples().Select(x => x.Item2).ToArray(); repoShift = onRate - BondAnalytics.CalcMMS(startdate, startdate, BondAnalytics.DayCountType.I30360, 12, 12, discDfDates, discDfs, discDfDates, discDfs, null, null, null, null, null, null, DateTime.MinValue, 0) * 10000; }
public USD3mLiborOISDCurveBuilder(DateTime valueDate, SwapCurveParameters_fc parameters, DiscountCurve discountCurve) : base(valueDate, parameters) { mDiscountCurve = discountCurve; }
public static double CalcMeasure(CalcMeasure Measure, double price, BondAnalytics.Country eCountry, //DateTime asof, DateTime settle, DiscountCurve discCurve, DiscountCurve fcstCurve, DateTime effectiveDate, DateTime maturity, DateTime firstCpnDate, double coupon, long bondCpnFreq, bool bOIS, BondAnalytics.DayCountType dct, long swapFixedFreq, long swapFloatFreq, List<DateTime> hols) //---------------------------------------------------------------------------------------- { double output = double.NaN; switch (Measure) { case Analytics.CalcMeasure.Price: { output = price; break; } case Analytics.CalcMeasure.Yield: { output = 10000.0*BondAnalytics.SolveYield(eCountry, settle, price, effectiveDate, firstCpnDate, maturity, coupon, bondCpnFreq)[0]; break; } case Analytics.CalcMeasure.TrueSpread: { DateTime[] dfDates = (bOIS ? discCurve.AsTuples().Select(x => x.Item1).ToArray() : fcstCurve.AsTuples().Select(x => x.Item1).ToArray()); double[] dfs = (bOIS ? discCurve.AsTuples().Select(x => x.Item2).ToArray() : fcstCurve.AsTuples().Select(x => x.Item2).ToArray()); // Check. Sometimes (holidays) the first date on the curve can be repeated, which will cause an exception in the pricer. if (dfDates[0] == dfDates[1]) { dfDates = dfDates.GetSubArray(1, dfDates.GetLength(0)).ToArray(); dfs = dfs.GetSubArray(1, dfs.GetLength(0)).ToArray(); } output = -10000.0*BondAnalytics.SolveZSpread(eCountry, settle, price, effectiveDate, firstCpnDate, maturity, coupon, bondCpnFreq, dfDates, dfs, hols); break; } case Analytics.CalcMeasure.MMS: { DateTime[] discDfDates = discCurve.AsTuples().Select(x => x.Item1).ToArray(); DateTime[] fcstDfDates = (bOIS ? discCurve.AsTuples().Select(x => x.Item1).ToArray() : fcstCurve.AsTuples().Select(x => x.Item1).ToArray()); double[] discDfs = discCurve.AsTuples().Select(x => x.Item2).ToArray(); double[] fcstDfs = (bOIS ? discCurve.AsTuples().Select(x => x.Item2).ToArray() : fcstCurve.AsTuples().Select(x => x.Item2).ToArray()); double mms = BondAnalytics.CalcMMS(settle, maturity, dct, swapFixedFreq, swapFloatFreq, discDfDates, discDfs, fcstDfDates, fcstDfs, hols, null, null, null, null, null, firstCpnDate, (bOIS ? 5 : 0)); output = 10000.0*mms; break; } case Analytics.CalcMeasure.Spread: { DateTime[] discDfDates = discCurve.AsTuples().Select(x => x.Item1).ToArray(); DateTime[] fcstDfDates = (bOIS ? discCurve.AsTuples().Select(x => x.Item1).ToArray() : fcstCurve.AsTuples().Select(x => x.Item1).ToArray()); double[] discDfs = discCurve.AsTuples().Select(x => x.Item2).ToArray(); double[] fcstDfs = (bOIS ? discCurve.AsTuples().Select(x => x.Item2).ToArray() : fcstCurve.AsTuples().Select(x => x.Item2).ToArray()); double mms = BondAnalytics.CalcMMS(settle, maturity, dct, swapFixedFreq, swapFloatFreq, discDfDates, discDfs, fcstDfDates, fcstDfs, hols, null, null, null, null, null, firstCpnDate, (bOIS ? 5 : 0)); double yield = BondAnalytics.SolveYield(eCountry, settle, price, effectiveDate, firstCpnDate, maturity, coupon, bondCpnFreq)[0]; output = 10000.0*(mms - yield); break; } } return output; }
public static IReadOnlyDictionary<DateTime, DiscountCurve> BulkGetHistoricEodDiscountCurvesFromCarbon(ICarbonClient client, List<DateTime> settleDates, string ccy, bool bOis) { var inputDateKind = settleDates.FirstOrDefault().Kind; var dict = new ConcurrentDictionary<DateTime, DiscountCurve>(); var dates = new List<DateTime>(); foreach (var settleDate in settleDates) { DiscountCurve data; var key = GetKeyForDiscountCurveCache(settleDate, ccy, bOis); if (DicountCurveDataCache.TryGetValue(key, out data)) { dict[settleDate] = data; } else if (settleDate <= DateTime.Today) { dates.Add(settleDate); } } if (dates.Count == 0) return dict.ToDictionary(kvp => kvp.Key, kvp => kvp.Value); var curveName = GetCurveName(ccy, bOis); if (string.IsNullOrEmpty(curveName)) return new Dictionary<DateTime, DiscountCurve>(); try { var start = dates.Min(); var end = dates.Max(); if (dates.Count > 16) { start = new DateTime(start.Year, start.Month, start.Day, 0, 0, 0, DateTimeKind.Utc); end = new DateTime(end.Year, end.Month, end.Day, 0, 0, 0, DateTimeKind.Utc); var datas = client.GetTimeSeriesData(curveName.ToUpperInvariant(), "interestcurves-ot", start, end, "dfs"); if (datas != null) { Parallel.ForEach(datas, messagePackObject => { try { var curve = messagePackObject.Value.ConvertMessagePackObjectToDiscountFactors(); if (curve == null) return; var dicountCurve = new DiscountCurve(new FinCADTable(curve, false)); var date = new DateTime(messagePackObject.Key.Year, messagePackObject.Key.Month, messagePackObject.Key.Day, 0, 0, 0, inputDateKind); dict.TryAdd(date, dicountCurve); var key = GetKeyForDiscountCurveCache(messagePackObject.Key, ccy, bOis); DicountCurveDataCache.TryAdd(key, dicountCurve); } catch (Exception ex) { Log.ErrorFormat("Failed to convert curve {0} : {1}", curveName, ex); } }); } } else { Parallel.ForEach(dates, date => { try { var curve = GetHistoricEodDiscountCurveFromCarbon(client, date, ccy, bOis); if (curve == null) return; dict.TryAdd(date, curve); var key = GetKeyForDiscountCurveCache(date, ccy, bOis); DicountCurveDataCache.TryAdd(key, curve); } catch (Exception ex) { SLog.log.ErrorFormat("Failed to get curve {0} : {1}", curveName, ex); } }); } } catch (Exception ex) { SLog.log.ErrorFormat("Failed to get curve {0} : {1}", curveName, ex); } return dict.ToDictionary(kvp => kvp.Key, kvp => kvp.Value); }
protected DiscountCurve InternalBuildUSD3mLiborOISDCurve(DiscountCurve discountCurve, string marketSnapCode_, string quotesourceCode_, CarbonClient carbonClient_, string futfixingsMarketSnapCode_ = null) { var p = new SwapCurveParameters_fc { Index = "US0003M Index", Currency = "USD", UseFutures = true, MaxNumFutures = 10, FuturesIMMOnly = true, FuturesMinTenor = Tenor.BusinessDays(3), FuturesMaxTenor = Tenor.Years(3), MarketSnapCode = marketSnapCode_, QuoteSource = quotesourceCode_, MarketSnapCode_Fix_MM_Fut = futfixingsMarketSnapCode_, SwapTenors = new List<string> { "3y", "4y", "5y", "6y", "7y", "8y", "9y", "10y", "12y", "15y", "20y", "25y", "30y", "40y", "50y", "60y" }, MoneyMarketIndices = new List<string> { "US0003M Index" }, BootstrapSettings = "Linear", SmoothingSettings = "None", CashConv = "CashUSD", FixedLegConv = "SwapUSDSemi30360", FloatingLegConv = "SwapUSD3mFloating", FloatingIndex = "LiborUSD3m", FuturesRateStartOffset = Tenor.BusinessDays(2) }; if (ValueDate <= new DateTime(2009, 8, 5)) { p.MaxNumFutures = 7; p.SwapTenors = new List<string> { "2y", "3y", "4y", "5y", "6y", "7y", "8y", "9y", "10y", "12y", "15y", "20y", "25y", "30y", "40y", "50y", "60y" }; } return CallCurveCalculator(new USD3mLiborOISDCurveBuilder(ValueDate, p, discountCurve), KnownCurve.USD3mLiborOISD, marketSnapCode_, quotesourceCode_, carbonClient_); }
protected DiscountCurve InternalBuildGBP6mLiborOISDCurve(DiscountCurve discountCurve, string marketSnapCode_, string quotesourceCode_, CarbonClient carbonClient_, string futfixingsMarketSnapCode_ = null) { return CallCurveCalculator(new GBP6mLiborOISDCurveBuilder(ValueDate, new SwapCurveParameters_fc { Index = "BP0006M Index", Currency = "GBP", UseFutures = false, MarketSnapCode = marketSnapCode_, MarketSnapCode_Fix_MM_Fut = futfixingsMarketSnapCode_, QuoteSource = quotesourceCode_, SwapTenors = new List<string> { "1y", "2y", "3y", "4y", "5y", "6y", "7y", "8y", "9y", "10y", "12y", "15y", "20y", "25y", "30y", "40y", "50y", "60y" }, MoneyMarketIndices = new List<string> { "BP00O/N Index", "BP0001M Index", "BP0002M Index", "BP0003M Index" }, BootstrapSettings = "Linear", SmoothingSettings = "Spline", FixedLegConv = "SwapGBPSemiFixed", FloatingLegConv = "SwapGBP6mFloating", FloatingIndex = "LiborGBP6m" }, discountCurve), KnownCurve.GBP6mLiborOISD, marketSnapCode_, quotesourceCode_, carbonClient_); }
//---------------------------------------------------------------------------------------- public static double calcMeasure( string Measure, double price, BondAnalytics.Country eCountry, DateTime asof, DateTime settle, DiscountCurve discCurve, DiscountCurve fcstCurve, BondStatic bondStatic, SpreadTimeSeriesConfigs configs, List<DateTime> hols) //---------------------------------------------------------------------------------------- { DateTime effectiveDate = DateTime.FromOADate( bondStatic.EffectiveDate); DateTime maturity = DateTime.FromOADate( bondStatic.Maturity); DateTime firstCpnDate = DateTime.FromOADate(bondStatic.FirstCoupon); double coupon = bondStatic.Coupon; long bondCpnFreq = configs.bndCouponFreq; bool bOIS = !configs.bForecastCurve; BondAnalytics.DayCountType dct = configs.dct; long fixedFreq = configs.swpfixfreq; long floatFreq = configs.swpfloatfreq; double output = double.NaN; switch (Measure) { case "Price": { output= price; break; } case "Yield": { output= 10000.0 * BondAnalytics.SolveYield(eCountry, settle, price, effectiveDate, firstCpnDate, maturity, coupon, bondCpnFreq)[0]; break; } case "TrueSpread": { DateTime[] dfDates = (bOIS ? discCurve.AsTuples().Select(x => x.Item1).ToArray() : fcstCurve.AsTuples().Select(x => x.Item1).ToArray()); double[] dfs = (bOIS ? discCurve.AsTuples().Select(x => x.Item2).ToArray() : fcstCurve.AsTuples().Select(x => x.Item2).ToArray()); // Check. Sometimes (holidays) the first date on the curve can be repeated, which will cause an exception in the pricer. if (dfDates[0] == dfDates[1]) { dfDates = dfDates.GetSubArray(1, dfDates.GetLength(0)).ToArray(); dfs = dfs.GetSubArray(1, dfs.GetLength(0)).ToArray(); } output= -10000.0 * BondAnalytics.SolveZSpread(eCountry, settle, price, effectiveDate, firstCpnDate, maturity, coupon, bondCpnFreq, dfDates, dfs,hols ); break; } case "MMS": { DateTime[] discDfDates = discCurve.AsTuples().Select(x => x.Item1).ToArray(); DateTime[] fcstDfDates = (bOIS ? discCurve.AsTuples().Select(x => x.Item1).ToArray() : fcstCurve.AsTuples().Select(x => x.Item1).ToArray()); double[] discDfs = discCurve.AsTuples().Select(x => x.Item2).ToArray(); double[] fcstDfs =(bOIS ? discCurve.AsTuples().Select(x => x.Item2).ToArray() : fcstCurve.AsTuples().Select(x => x.Item2).ToArray()); double mms = BondAnalytics.CalcMMS(settle, maturity, dct, fixedFreq, floatFreq, discDfDates, discDfs, fcstDfDates, fcstDfs, hols, null, null, null, null, null, firstCpnDate, (bOIS ? 5 : 0)); output= 10000.0 * mms; break; } case "Spread": { DateTime[] discDfDates = discCurve.AsTuples().Select(x => x.Item1).ToArray(); DateTime[] fcstDfDates = (bOIS ? discCurve.AsTuples().Select(x => x.Item1).ToArray() : fcstCurve.AsTuples().Select(x => x.Item1).ToArray()); double[] discDfs = discCurve.AsTuples().Select(x => x.Item2).ToArray(); double[] fcstDfs = (bOIS ? discCurve.AsTuples().Select(x => x.Item2).ToArray() : fcstCurve.AsTuples().Select(x => x.Item2).ToArray()); double mms = BondAnalytics.CalcMMS(settle, maturity, dct, fixedFreq, floatFreq, discDfDates, discDfs, fcstDfDates, fcstDfs, hols, null, null, null, null, null, firstCpnDate, (bOIS ? 5 : 0)); double yield = BondAnalytics.SolveYield(eCountry,settle ,price, effectiveDate, firstCpnDate, maturity, coupon, bondCpnFreq)[0]; output= 10000.0 * (mms - yield); break; } } return output; }