Lean  $LEAN_TAG$
SymbolRepresentation.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14 */
15 
16 using System;
17 using System.Linq;
18 using QuantConnect.Logging;
19 using System.Globalization;
21 using System.Collections.Generic;
25 using static QuantConnect.StringExtensions;
26 using System.Text.RegularExpressions;
28 
29 namespace QuantConnect
30 {
31  /// <summary>
32  /// Public static helper class that does parsing/generation of symbol representations (options, futures)
33  /// </summary>
34  public static class SymbolRepresentation
35  {
36  // Define the regex as a private readonly static field and compile it
37  private static readonly Regex _optionTickerRegex = new Regex(@"^([A-Z]+)\s*(\d{6})([CP])(\d{8})$", RegexOptions.IgnoreCase | RegexOptions.Compiled);
38 
39  /// <summary>
40  /// Class contains future ticker properties returned by ParseFutureTicker()
41  /// </summary>
43  {
44  /// <summary>
45  /// Underlying name
46  /// </summary>
47  public string Underlying { get; set; }
48 
49  /// <summary>
50  /// Short expiration year
51  /// </summary>
52  public int ExpirationYearShort { get; set; }
53 
54  /// <summary>
55  /// Short expiration year digits
56  /// </summary>
57  public int ExpirationYearShortLength { get; set; }
58 
59  /// <summary>
60  /// Expiration month
61  /// </summary>
62  public int ExpirationMonth { get; set; }
63 
64  /// <summary>
65  /// Expiration day
66  /// </summary>
67  public int ExpirationDay { get; set; }
68  }
69 
70  /// <summary>
71  /// Class contains option ticker properties returned by ParseOptionTickerIQFeed()
72  /// </summary>
74  {
75  /// <summary>
76  /// Underlying name
77  /// </summary>
78  public string Underlying { get; set; }
79 
80  /// <summary>
81  /// Option right
82  /// </summary>
83  public OptionRight OptionRight { get; set; }
84 
85  /// <summary>
86  /// Option strike
87  /// </summary>
88  public decimal OptionStrike { get; set; }
89 
90  /// <summary>
91  /// Expiration date
92  /// </summary>
93  public DateTime ExpirationDate { get; set; }
94  }
95 
96 
97  /// <summary>
98  /// Function returns underlying name, expiration year, expiration month, expiration day for the future contract ticker. Function detects if
99  /// the format used is either 1 or 2 digits year, and if day code is present (will default to 1rst day of month). Returns null, if parsing failed.
100  /// Format [Ticker][2 digit day code OPTIONAL][1 char month code][2/1 digit year code]
101  /// </summary>
102  /// <param name="ticker"></param>
103  /// <returns>Results containing 1) underlying name, 2) short expiration year, 3) expiration month</returns>
104  public static FutureTickerProperties ParseFutureTicker(string ticker)
105  {
106  var doubleDigitYear = char.IsDigit(ticker.Substring(ticker.Length - 2, 1)[0]);
107  var doubleDigitOffset = doubleDigitYear ? 1 : 0;
108 
109  var expirationDayOffset = 0;
110  var expirationDay = 1;
111  if (ticker.Length > 4 + doubleDigitOffset)
112  {
113  var potentialExpirationDay = ticker.Substring(ticker.Length - 4 - doubleDigitOffset, 2);
114  var containsExpirationDay = char.IsDigit(potentialExpirationDay[0]) && char.IsDigit(potentialExpirationDay[1]);
115  expirationDayOffset = containsExpirationDay ? 2 : 0;
116  if (containsExpirationDay && !int.TryParse(potentialExpirationDay, out expirationDay))
117  {
118  return null;
119  }
120  }
121 
122  var expirationYearString = ticker.Substring(ticker.Length - 1 - doubleDigitOffset, 1 + doubleDigitOffset);
123  var expirationMonthString = ticker.Substring(ticker.Length - 2 - doubleDigitOffset, 1);
124  var underlyingString = ticker.Substring(0, ticker.Length - 2 - doubleDigitOffset - expirationDayOffset);
125 
126  int expirationYearShort;
127 
128  if (!int.TryParse(expirationYearString, out expirationYearShort))
129  {
130  return null;
131  }
132 
133  if (!FuturesMonthCodeLookup.ContainsKey(expirationMonthString))
134  {
135  return null;
136  }
137 
138  var expirationMonth = FuturesMonthCodeLookup[expirationMonthString];
139 
140  return new FutureTickerProperties
141  {
142  Underlying = underlyingString,
143  ExpirationYearShort = expirationYearShort,
144  ExpirationYearShortLength = expirationYearString.Length,
145  ExpirationMonth = expirationMonth,
146  ExpirationDay = expirationDay
147  };
148  }
149 
150  /// <summary>
151  /// Helper method to parse and generate a future symbol from a given user friendly representation
152  /// </summary>
153  /// <param name="ticker">The future ticker, for example 'ESZ1'</param>
154  /// <param name="futureYear">Clarifies the year for the current future</param>
155  /// <returns>The future symbol or null if failed</returns>
156  public static Symbol ParseFutureSymbol(string ticker, int? futureYear = null)
157  {
158  var parsed = ParseFutureTicker(ticker);
159  if (parsed == null)
160  {
161  return null;
162  }
163 
164  var underlying = parsed.Underlying;
165  var expirationMonth = parsed.ExpirationMonth;
166  var expirationYear = GetExpirationYear(futureYear, parsed);
167 
168  if (!SymbolPropertiesDatabase.FromDataFolder().TryGetMarket(underlying, SecurityType.Future, out var market))
169  {
170  Log.Debug($@"SymbolRepresentation.ParseFutureSymbol(): {
171  Messages.SymbolRepresentation.FailedToGetMarketForTickerAndUnderlying(ticker, underlying)}");
172  return null;
173  }
174 
175  var expiryFunc = FuturesExpiryFunctions.FuturesExpiryFunction(Symbol.Create(underlying, SecurityType.Future, market));
176  var expiryDate = expiryFunc(new DateTime(expirationYear, expirationMonth, 1));
177 
178  return Symbol.CreateFuture(underlying, market, expiryDate);
179  }
180 
181  /// <summary>
182  /// Creates a future option Symbol from the provided ticker
183  /// </summary>
184  /// <param name="ticker">The future option ticker, for example 'ESZ0 P3590'</param>
185  /// <param name="strikeScale">Optional the future option strike scale factor</param>
186  public static Symbol ParseFutureOptionSymbol(string ticker, int strikeScale = 1)
187  {
188  var split = ticker.Split(' ');
189  if (split.Length != 2)
190  {
191  return null;
192  }
193 
194  var parsed = ParseFutureTicker(split[0]);
195  if (parsed == null)
196  {
197  return null;
198  }
199  ticker = parsed.Underlying;
200 
201  OptionRight right;
202  if (split[1][0] == 'P' || split[1][0] == 'p')
203  {
204  right = OptionRight.Put;
205  }
206  else if (split[1][0] == 'C' || split[1][0] == 'c')
207  {
208  right = OptionRight.Call;
209  }
210  else
211  {
212  return null;
213  }
214  var strike = split[1].Substring(1);
215 
216  if (parsed.ExpirationYearShort < 10)
217  {
218  parsed.ExpirationYearShort += 20;
219  }
220  var expirationYearParsed = 2000 + parsed.ExpirationYearShort;
221 
222  var expirationDate = new DateTime(expirationYearParsed, parsed.ExpirationMonth, 1);
223 
224  var strikePrice = decimal.Parse(strike, NumberStyles.Any, CultureInfo.InvariantCulture);
225  var futureTicker = FuturesOptionsSymbolMappings.MapFromOption(ticker);
226 
227  if (!SymbolPropertiesDatabase.FromDataFolder().TryGetMarket(futureTicker, SecurityType.Future, out var market))
228  {
229  Log.Debug($"SymbolRepresentation.ParseFutureOptionSymbol(): {Messages.SymbolRepresentation.NoMarketFound(futureTicker)}");
230  return null;
231  }
232 
233  var canonicalFuture = Symbol.Create(futureTicker, SecurityType.Future, market);
234  var futureExpiry = FuturesExpiryFunctions.FuturesExpiryFunction(canonicalFuture)(expirationDate);
235  var future = Symbol.CreateFuture(futureTicker, market, futureExpiry);
236 
238 
239  return Symbol.CreateOption(future,
240  market,
241  OptionStyle.American,
242  right,
243  strikePrice / strikeScale,
244  futureOptionExpiry);
245  }
246 
247  /// <summary>
248  /// Returns future symbol ticker from underlying and expiration date. Function can generate tickers of two formats: one and two digits year.
249  /// Format [Ticker][2 digit day code][1 char month code][2/1 digit year code], more information at http://help.tradestation.com/09_01/tradestationhelp/symbology/futures_symbology.htm
250  /// </summary>
251  /// <param name="underlying">String underlying</param>
252  /// <param name="expiration">Expiration date</param>
253  /// <param name="doubleDigitsYear">True if year should represented by two digits; False - one digit</param>
254  /// <param name="includeExpirationDate">True if expiration date should be included</param>
255  /// <returns>The user friendly future ticker</returns>
256  public static string GenerateFutureTicker(string underlying, DateTime expiration, bool doubleDigitsYear = true, bool includeExpirationDate = true)
257  {
258  var year = doubleDigitsYear ? expiration.Year % 100 : expiration.Year % 10;
259  var month = expiration.Month;
260 
261  var contractMonthDelta = FuturesExpiryUtilityFunctions.GetDeltaBetweenContractMonthAndContractExpiry(underlying, expiration.Date);
262  if (contractMonthDelta < 0)
263  {
264  // For futures that have an expiry after the contract month.
265  // This is for dairy contracts, which can and do expire after the contract month.
266  var expirationMonth = expiration.AddDays(-(expiration.Day - 1))
267  .AddMonths(contractMonthDelta);
268 
269  month = expirationMonth.Month;
270  year = doubleDigitsYear ? expirationMonth.Year % 100 : expirationMonth.Year % 10;
271  }
272  else {
273  // These futures expire in the month before or in the contract month
274  month += contractMonthDelta;
275 
276  // Get the month back into the allowable range, allowing for a wrap
277  // Below is a little algorithm for wrapping numbers with a certain bounds.
278  // In this case, were dealing with months, wrapping to years once we get to January
279  // As modulo works for [0, x), it's best to subtract 1 (as months are [1, 12] to convert to [0, 11]),
280  // do the modulo/integer division, then add 1 back on to get into the correct range again
281  month--;
282  year += month / 12;
283  month %= 12;
284  month++;
285  }
286 
287  var expirationDay = includeExpirationDate ? $"{expiration.Day:00}" : string.Empty;
288 
289  return $"{underlying}{expirationDay}{FuturesMonthLookup[month]}{year}";
290  }
291 
292  /// <summary>
293  /// Returns option symbol ticker in accordance with OSI symbology
294  /// More information can be found at http://www.optionsclearing.com/components/docs/initiatives/symbology/symbology_initiative_v1_8.pdf
295  /// </summary>
296  /// <param name="symbol">Symbol object to create OSI ticker from</param>
297  /// <returns>The OSI ticker representation</returns>
298  public static string GenerateOptionTickerOSI(this Symbol symbol)
299  {
300  if (!symbol.SecurityType.IsOption())
301  {
302  throw new ArgumentException(
304  }
305 
306  return GenerateOptionTickerOSI(symbol.Underlying.Value, symbol.ID.OptionRight, symbol.ID.StrikePrice, symbol.ID.Date);
307  }
308 
309  /// <summary>
310  /// Returns option symbol ticker in accordance with OSI symbology
311  /// More information can be found at http://www.optionsclearing.com/components/docs/initiatives/symbology/symbology_initiative_v1_8.pdf
312  /// </summary>
313  /// <param name="underlying">Underlying string</param>
314  /// <param name="right">Option right</param>
315  /// <param name="strikePrice">Option strike</param>
316  /// <param name="expiration">Option expiration date</param>
317  /// <returns>The OSI ticker representation</returns>
318  public static string GenerateOptionTickerOSI(string underlying, OptionRight right, decimal strikePrice, DateTime expiration)
319  {
320  if (underlying.Length > 5) underlying += " ";
321  return Invariant($"{underlying,-6}{expiration.ToStringInvariant(DateFormat.SixCharacter)}{right.ToStringPerformance()[0]}{(strikePrice * 1000m):00000000}");
322  }
323 
324  /// <summary>
325  /// Returns option symbol ticker in accordance with OSI symbology
326  /// More information can be found at http://www.optionsclearing.com/components/docs/initiatives/symbology/symbology_initiative_v1_8.pdf
327  /// </summary>
328  /// <param name="symbol">Symbol object to create OSI ticker from</param>
329  /// <returns>The OSI ticker representation</returns>
330  public static string GenerateOptionTickerOSICompact(this Symbol symbol)
331  {
332  // First, validate that the symbol is of the correct security type
333  if (!symbol.SecurityType.IsOption())
334  {
335  throw new ArgumentException(
337  }
338  return GenerateOptionTickerOSICompact(symbol.Underlying.Value, symbol.ID.OptionRight, symbol.ID.StrikePrice, symbol.ID.Date);
339  }
340 
341  /// <summary>
342  /// Returns option symbol ticker in accordance with OSI symbology
343  /// More information can be found at http://www.optionsclearing.com/components/docs/initiatives/symbology/symbology_initiative_v1_8.pdf
344  /// </summary>
345  /// <param name="underlying">Underlying string</param>
346  /// <param name="right">Option right</param>
347  /// <param name="strikePrice">Option strike</param>
348  /// <param name="expiration">Option expiration date</param>
349  /// <returns>The OSI ticker representation</returns>
350  public static string GenerateOptionTickerOSICompact(string underlying, OptionRight right, decimal strikePrice, DateTime expiration)
351  {
352  return Invariant($"{underlying}{expiration.ToStringInvariant(DateFormat.SixCharacter)}{right.ToStringPerformance()[0]}{(strikePrice * 1000m):00000000}");
353  }
354 
355  /// <summary>
356  /// Parses the specified OSI options ticker into a Symbol object
357  /// </summary>
358  /// <param name="ticker">The OSI compliant option ticker string</param>
359  /// <param name="securityType">The security type</param>
360  /// <param name="market">The associated market</param>
361  /// <returns>Symbol object for the specified OSI option ticker string</returns>
362  public static Symbol ParseOptionTickerOSI(string ticker, SecurityType securityType = SecurityType.Option, string market = Market.USA)
363  {
364  return ParseOptionTickerOSI(ticker, securityType, OptionStyle.American, market);
365  }
366 
367  /// <summary>
368  /// Parses the specified OSI options ticker into a Symbol object
369  /// </summary>
370  /// <param name="ticker">The OSI compliant option ticker string</param>
371  /// <param name="securityType">The security type</param>
372  /// <param name="market">The associated market</param>
373  /// <param name="optionStyle">The option style</param>
374  /// <returns>Symbol object for the specified OSI option ticker string</returns>
375  public static Symbol ParseOptionTickerOSI(string ticker, SecurityType securityType, OptionStyle optionStyle, string market)
376  {
377  if (!TryDecomposeOptionTickerOSI(ticker, out var optionTicker, out var expiry, out var right, out var strike))
378  {
379  throw new FormatException(Messages.SymbolRepresentation.InvalidOSITickerFormat(ticker));
380  }
381 
382  SecurityIdentifier underlyingSid;
383  string underlyingSymbolValue;
384  if (securityType == SecurityType.Option)
385  {
386  underlyingSid = SecurityIdentifier.GenerateEquity(optionTicker, market);
387  // We have the mapped symbol in the OSI ticker
388  underlyingSymbolValue = optionTicker;
389  // let it fallback to it's default handling, which include mapping
390  optionTicker = null;
391  }
392  else if(securityType == SecurityType.IndexOption)
393  {
394  underlyingSid = SecurityIdentifier.GenerateIndex(OptionSymbol.MapToUnderlying(optionTicker, securityType), market);
395  underlyingSymbolValue = underlyingSid.Symbol;
396  }
397  else
398  {
399  throw new NotImplementedException($"ParseOptionTickerOSI(): {Messages.SymbolRepresentation.SecurityTypeNotImplemented(securityType)}");
400  }
401  var sid = SecurityIdentifier.GenerateOption(expiry, underlyingSid, optionTicker, market, strike, right, optionStyle);
402  return new Symbol(sid, ticker, new Symbol(underlyingSid, underlyingSymbolValue));
403  }
404 
405  /// <summary>
406  /// Tries to decompose the specified OSI options ticker into its components
407  /// </summary>
408  /// <param name="ticker">The OSI option ticker</param>
409  /// <param name="optionTicker">The option ticker extracted from the OSI symbol</param>
410  /// <param name="expiry">The option contract expiry date</param>
411  /// <param name="right">The option contract right</param>
412  /// <param name="strike">The option contract strike price</param>
413  /// <returns>True if the OSI symbol was in the right format and could be decomposed</returns>
414  public static bool TryDecomposeOptionTickerOSI(string ticker, out string optionTicker, out DateTime expiry,
415  out OptionRight right, out decimal strike)
416  {
417  optionTicker = null;
418  expiry = default;
419  right = OptionRight.Call;
420  strike = decimal.Zero;
421 
422  if (string.IsNullOrEmpty(ticker))
423  {
424  return false;
425  }
426 
427  var match = _optionTickerRegex.Match(ticker);
428  if (!match.Success)
429  {
430  return false;
431  }
432 
433  optionTicker = match.Groups[1].Value;
434  expiry = DateTime.ParseExact(match.Groups[2].Value, DateFormat.SixCharacter, null);
435  right = match.Groups[3].Value.ToUpperInvariant() == "C" ? OptionRight.Call : OptionRight.Put;
436  strike = Parse.Decimal(match.Groups[4].Value) / 1000m;
437 
438  return true;
439  }
440 
441  /// <summary>
442  /// Tries to decompose the specified OSI options ticker into its components
443  /// </summary>
444  /// <param name="ticker">The OSI option ticker</param>
445  /// <param name="securityType">The option security type</param>
446  /// <param name="optionTicker">The option ticker extracted from the OSI symbol</param>
447  /// <param name="underlyingTicker">The underlying ticker</param>
448  /// <param name="expiry">The option contract expiry date</param>
449  /// <param name="right">The option contract right</param>
450  /// <param name="strike">The option contract strike price</param>
451  /// <returns>True if the OSI symbol was in the right format and could be decomposed</returns>
452  public static bool TryDecomposeOptionTickerOSI(string ticker, SecurityType securityType, out string optionTicker,
453  out string underlyingTicker, out DateTime expiry, out OptionRight right, out decimal strike)
454  {
455  optionTicker = null;
456  underlyingTicker = null;
457  expiry = default;
458  right = OptionRight.Call;
459  strike = decimal.Zero;
460 
461  if (!securityType.IsOption())
462  {
463  return false;
464  }
465 
466  var result = TryDecomposeOptionTickerOSI(ticker, out optionTicker, out expiry, out right, out strike);
467  underlyingTicker = securityType != SecurityType.IndexOption ? optionTicker : IndexOptionSymbol.MapToUnderlying(optionTicker);
468 
469  return result;
470  }
471 
472  /// <summary>
473  /// Function returns option ticker from IQFeed option ticker
474  /// For example CSCO1220V19 Cisco October Put at 19.00 Expiring on 10/20/12
475  /// Symbology details: http://www.iqfeed.net/symbolguide/index.cfm?symbolguide=guide&amp;displayaction=support%C2%A7ion=guide&amp;web=iqfeed&amp;guide=options&amp;web=IQFeed&amp;type=stock
476  /// </summary>
477  /// <param name="symbol">THe option symbol</param>
478  /// <returns>The option ticker</returns>
479  public static string GenerateOptionTicker(Symbol symbol)
480  {
481  var letter = _optionSymbology.Where(x => x.Value.Item2 == symbol.ID.OptionRight && x.Value.Item1 == symbol.ID.Date.Month).Select(x => x.Key).Single();
482  var twoYearDigit = symbol.ID.Date.ToString("yy");
483  return $"{SecurityIdentifier.Ticker(symbol.Underlying, symbol.ID.Date)}{twoYearDigit}{symbol.ID.Date.Day:00}{letter}{symbol.ID.StrikePrice.ToStringInvariant()}";
484  }
485 
486  /// <summary>
487  /// Function returns option contract parameters (underlying name, expiration date, strike, right) from IQFeed option ticker
488  /// Symbology details: http://www.iqfeed.net/symbolguide/index.cfm?symbolguide=guide&amp;displayaction=support%C2%A7ion=guide&amp;web=iqfeed&amp;guide=options&amp;web=IQFeed&amp;type=stock
489  /// </summary>
490  /// <param name="ticker">IQFeed option ticker</param>
491  /// <returns>Results containing 1) underlying name, 2) option right, 3) option strike 4) expiration date</returns>
492  public static OptionTickerProperties ParseOptionTickerIQFeed(string ticker)
493  {
494  var letterRange = _optionSymbology.Keys
495  .Select(x => x[0])
496  .ToArray();
497  var optionTypeDelimiter = ticker.LastIndexOfAny(letterRange);
498  var strikePriceString = ticker.Substring(optionTypeDelimiter + 1, ticker.Length - optionTypeDelimiter - 1);
499 
500  var lookupResult = _optionSymbology[ticker[optionTypeDelimiter].ToStringInvariant()];
501  var month = lookupResult.Item1;
502  var optionRight = lookupResult.Item2;
503 
504  var dayString = ticker.Substring(optionTypeDelimiter - 2, 2);
505  var yearString = ticker.Substring(optionTypeDelimiter - 4, 2);
506  var underlying = ticker.Substring(0, optionTypeDelimiter - 4);
507 
508  // if we cannot parse strike price, we ignore this contract, but log the information.
509  Decimal strikePrice;
510  if (!Decimal.TryParse(strikePriceString, NumberStyles.Any, CultureInfo.InvariantCulture, out strikePrice))
511  {
512  return null;
513  }
514 
515  int day;
516 
517  if (!int.TryParse(dayString, out day))
518  {
519  return null;
520  }
521 
522  int year;
523 
524  if (!int.TryParse(yearString, out year))
525  {
526  return null;
527  }
528 
529  var expirationDate = new DateTime(2000 + year, month, day);
530 
531  return new OptionTickerProperties
532  {
533  Underlying = underlying,
534  OptionRight = optionRight,
535  OptionStrike = strikePrice,
536  ExpirationDate = expirationDate
537  };
538  }
539 
540 
541  // This table describes IQFeed option symbology
542  private static Dictionary<string, Tuple<int, OptionRight>> _optionSymbology = new Dictionary<string, Tuple<int, OptionRight>>
543  {
544  { "A", Tuple.Create(1, OptionRight.Call) }, { "M", Tuple.Create(1, OptionRight.Put) },
545  { "B", Tuple.Create(2, OptionRight.Call) }, { "N", Tuple.Create(2, OptionRight.Put) },
546  { "C", Tuple.Create(3, OptionRight.Call) }, { "O", Tuple.Create(3, OptionRight.Put) },
547  { "D", Tuple.Create(4, OptionRight.Call) }, { "P", Tuple.Create(4, OptionRight.Put) },
548  { "E", Tuple.Create(5, OptionRight.Call) }, { "Q", Tuple.Create(5, OptionRight.Put) },
549  { "F", Tuple.Create(6, OptionRight.Call) }, { "R", Tuple.Create(6, OptionRight.Put) },
550  { "G", Tuple.Create(7, OptionRight.Call) }, { "S", Tuple.Create(7, OptionRight.Put) },
551  { "H", Tuple.Create(8, OptionRight.Call) }, { "T", Tuple.Create(8, OptionRight.Put) },
552  { "I", Tuple.Create(9, OptionRight.Call) }, { "U", Tuple.Create(9, OptionRight.Put) },
553  { "J", Tuple.Create(10, OptionRight.Call) }, { "V", Tuple.Create(10, OptionRight.Put) },
554  { "K", Tuple.Create(11, OptionRight.Call) }, { "W", Tuple.Create(11, OptionRight.Put) },
555  { "L", Tuple.Create(12, OptionRight.Call) }, { "X", Tuple.Create(12, OptionRight.Put) },
556 
557  };
558 
559 
560  /// <summary>
561  /// Provides a lookup dictionary for mapping futures month codes to their corresponding numeric values.
562  /// </summary>
563  public static IReadOnlyDictionary<string, int> FuturesMonthCodeLookup { get; } = new Dictionary<string, int>
564  {
565  { "F", 1 }, // January
566  { "G", 2 }, // February
567  { "H", 3 }, // March
568  { "J", 4 }, // April
569  { "K", 5 }, // May
570  { "M", 6 }, // June
571  { "N", 7 }, // July
572  { "Q", 8 }, // August
573  { "U", 9 }, // September
574  { "V", 10 }, // October
575  { "X", 11 }, // November
576  { "Z", 12 } // December
577  };
578 
579  /// <summary>
580  /// Provides a lookup dictionary for mapping numeric values to their corresponding futures month codes.
581  /// </summary>
582  public static IReadOnlyDictionary<int, string> FuturesMonthLookup { get; } = FuturesMonthCodeLookup.ToDictionary(kv => kv.Value, kv => kv.Key);
583 
584  /// <summary>
585  /// Get the expiration year from short year (two-digit integer).
586  /// Examples: NQZ23 and NQZ3 for Dec 2023
587  /// </summary>
588  /// <param name="futureYear">Clarifies the year for the current future</param>
589  /// <param name="parsed">Contains useful information about the future expiration year</param>
590  /// <remarks>Tickers from live trading may not provide the four-digit year.</remarks>
591  private static int GetExpirationYear(int? futureYear, FutureTickerProperties parsed)
592  {
593  if(futureYear.HasValue)
594  {
595  var referenceYear = 1900 + parsed.ExpirationYearShort;
596  while(referenceYear < futureYear.Value)
597  {
598  referenceYear += 10;
599  }
600 
601  return referenceYear;
602  }
603 
604  var currentYear = DateTime.UtcNow.Year;
605  if (parsed.ExpirationYearShortLength > 1)
606  {
607  // we are given a double digit year
608  return 2000 + parsed.ExpirationYearShort;
609  }
610 
611  var baseYear = ((int)Math.Round(currentYear / 10.0)) * 10 + parsed.ExpirationYearShort;
612  while (baseYear < currentYear)
613  {
614  baseYear += 10;
615  }
616  return baseYear;
617  }
618  }
619 }