public static RiskyFlySurface GetSurfaceForCode(string nymexSymbol, string nymexOptionFilename, string qwackCode, BasicPriceCurve priceCurve, ICalendarProvider calendarProvider, ICurrencyProvider currency, IFutureSettingsProvider futureSettingsProvider) { var parsed = NYMEXOptionParser.Instance.Parse(nymexOptionFilename).Where(r => r.Symbol == nymexSymbol); var(optionExerciseType, optionMarginingType) = OptionTypeFromCode(nymexSymbol); var origin = DateTime.ParseExact(parsed.First().TradeDate, "MM/dd/yyyy", CultureInfo.InvariantCulture); var q = parsed.Where(x => x.Settle > 0).Select(x => new ListedOptionSettlementRecord { CallPut = x.PutCall == "C"?OptionType.C:OptionType.P, ExerciseType = optionExerciseType, MarginType = optionMarginingType, PV = x.Settle, Strike = x.Strike, UnderlyingFuturesCode = Year2to1(x.Contract.Split(' ')[0].Replace(nymexSymbol, qwackCode)), ExpiryDate = OptionExpiryFromNymexRecord(x, calendarProvider), ValDate = origin }).Where(z => z.ExpiryDate > origin).ToList(); var priceDict = priceCurve.PillarLabels.ToDictionary(x => x, x => priceCurve.GetPriceForDate(priceCurve.PillarDatesForLabel(x))); ListedSurfaceHelper.ImplyVols(q, priceDict, new ConstantRateIrCurve(0.0, origin, "dummy", currency.GetCurrency("USD"))); var smiles = ListedSurfaceHelper.ToDeltaSmiles(q, priceDict); var allOptionExpiries = new List <DateTime>(); var lastDate = q.Max(x => x.ExpiryDate); var dummyFutureCode = $"{qwackCode}Z{DateExtensions.SingleDigitYear(DateTime.Today.Year + 2)}"; var c = new FutureCode(dummyFutureCode, DateTime.Today.Year - 2, futureSettingsProvider); var contract = c.GetFrontMonth(origin, false); var lastContract = c.GetFrontMonth(lastDate, false); while (contract != lastContract) { var cc = new FutureCode(contract, origin.Year, futureSettingsProvider); var exp = ListedUtils.FuturesCodeToDateTime(contract); var record = new NYMEXOptionRecord { ContractMonth = exp.Month, ContractYear = exp.Year, Symbol = nymexSymbol }; var optExpiry = OptionExpiryFromNymexRecord(record, calendarProvider); if (optExpiry > origin) { allOptionExpiries.Add(optExpiry); } contract = cc.GetNextCode(false); } var surface = ListedSurfaceHelper.ToRiskyFlySurfaceStepFlat(smiles, origin, priceCurve, allOptionExpiries, currency); return(surface); }
public BlackFuturesCurve(IATMVolSurface volSurface, DateTime startDate, DateTime expiryDate, int nTimeSteps, Func <DateTime, double> forwardCurve, string name, IFutureSettingsProvider futureSettingsProvider, Dictionary <DateTime, double> pastFixings = null) { _surface = volSurface; _startDate = startDate; _expiryDate = expiryDate; _numberOfSteps = nTimeSteps; _name = name; _forwardCurve = forwardCurve; _pastFixings = pastFixings ?? (new Dictionary <DateTime, double>()); _futureSettingsProvider = futureSettingsProvider; if (startDate > expiryDate) { throw new Exception("Start date must be before expiry date"); } _codes = new List <string>(); _futuresExpiries = new List <DateTime>(); var fCode = new FutureCode(name, _futureSettingsProvider); var currentCode = fCode.GetFrontMonth(startDate); _codes.Add(currentCode); fCode = new FutureCode(currentCode, DateTime.Today.Year - 2, _futureSettingsProvider); _futuresExpiries.Add(fCode.GetRollDate()); fCode = new FutureCode(currentCode, DateTime.Today.Year - 2, _futureSettingsProvider); var targetCode = fCode.GetFrontMonth(expiryDate); while (currentCode != targetCode) { currentCode = fCode.GetNextCode(false); _futuresExpiries.Add(fCode.GetRollDate()); _codes.Add(currentCode); } }
public static object FuturesGetFrontMonth( [ExcelArgument(Description = "Value date")] DateTime ValueDate, [ExcelArgument(Description = "Futures code root, e.g. CL")] string FuturesCodeRoot, [ExcelArgument(Description = "Use expiry rather than roll date")] bool UseExpiryRatherThanRoll) { return(ExcelHelper.Execute(_logger, () => { var dummyFutureCode = $"{FuturesCodeRoot}Z{DateExtensions.SingleDigitYear(DateTime.Today.Year + 2)}"; var c = new FutureCode(dummyFutureCode, DateTime.Today.Year - 2, ContainerStores.SessionContainer.GetService <IFutureSettingsProvider>()); return c.GetFrontMonth(ValueDate, UseExpiryRatherThanRoll); })); }
public static object CreateFixingDictionaryForRollingFuture( [ExcelArgument(Description = "Object name")] string ObjectName, [ExcelArgument(Description = "Asset Id")] string AssetId, [ExcelArgument(Description = "Futures code, e.g. QS or CO")] string FuturesCode, [ExcelArgument(Description = "1st month fixings array, 1st column dates / 2nd column fixings")] object[,] Fixings1m, [ExcelArgument(Description = "2nd month fixings array, 1st column dates / 2nd column fixings")] object[,] Fixings2m) { return(ExcelHelper.Execute(_logger, () => { var dict = new FixingDictionary { Name = AssetId, AssetId = AssetId, FixingDictionaryType = FixingDictionaryType.Asset }; var futuresProvider = ContainerStores.SessionContainer.GetRequiredService <IFutureSettingsProvider>(); var dictData1m = Fixings1m.RangeToDictionary <DateTime, double>(); var dictData2m = Fixings2m.RangeToDictionary <DateTime, double>(); var fc = new FutureCode(FuturesCode, futuresProvider); fc.YearBeforeWhich2DigitDatesAreUsed = DateTime.Today.Year - 2; foreach (var kv in dictData1m) { var currentFM = fc.GetFrontMonth(kv.Key, true); var cfm = new FutureCode(currentFM, DateTime.Today.Year - 2, futuresProvider); if (kv.Key <= cfm.GetRollDate()) { dict.Add(kv.Key, kv.Value); } else { dict.Add(kv.Key, dictData2m[kv.Key]); } } return ExcelHelper.PushToCache <IFixingDictionary>(dict, ObjectName); })); }
public void Finish(IFeatureCollection collection) { if (!_timesteps.IsComplete) { return; } //vols... _vols = new double[_timesteps.TimeStepCount][]; for (var t = 0; t < _vols.Length; t++) { _vols[t] = new double[_codes.Count]; for (var c = 0; c < _vols[t].Length; c++) { _vols[t][c] = _surface.GetVolForDeltaStrike(0.5, _futuresExpiries[c], 1.0); } } //work out which futures are front-month var fCode = new FutureCode(_name, _futureSettingsProvider); var codesForDate = _timesteps.Dates.Select(d => fCode.GetFrontMonth(d)); var mappingFeature = collection.GetFeature <IPathMappingFeature>(); _frontMonthFactors = codesForDate.Select(c => mappingFeature.GetDimension(c)).ToList(); _isComplete = true; }