Lean  $LEAN_TAG$
FutureSymbol.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 
17 using QuantConnect.Logging;
18 
20 {
21  /// <summary>
22  /// Static class contains common utility methods specific to symbols representing the future contracts
23  /// </summary>
24  public static class FutureSymbol
25  {
26  /// <summary>
27  /// Determine if a given Futures contract is a standard contract.
28  /// </summary>
29  /// <param name="symbol">Future symbol</param>
30  /// <returns>True if symbol expiration matches standard expiration</returns>
31  public static bool IsStandard(Symbol symbol)
32  {
33  var contractExpirationDate = symbol.ID.Date.Date;
34 
35  try
36  {
37  // Use our FutureExpiryFunctions to determine standard contracts dates.
38  var expiryFunction = FuturesExpiryFunctions.FuturesExpiryFunction(symbol);
39  var monthsToAdd = FuturesExpiryUtilityFunctions.GetDeltaBetweenContractMonthAndContractExpiry(symbol.ID.Symbol, contractExpirationDate);
40  var contractMonth = contractExpirationDate.AddDays(-(contractExpirationDate.Day - 1))
41  .AddMonths(monthsToAdd);
42 
43  var standardExpirationDate = expiryFunction(contractMonth);
44 
45  // Return true if the dates match
46  return contractExpirationDate == standardExpirationDate.Date;
47  }
48  catch
49  {
50  Log.Error($"FutureSymbol.IsStandard(): Could not find standard date for {symbol}, will be classified as standard");
51  return true;
52  }
53  }
54 
55  /// <summary>
56  /// Returns true if the future contract is a weekly contract
57  /// </summary>
58  /// <param name="symbol">Future symbol</param>
59  /// <returns>True if symbol is non-standard contract</returns>
60  public static bool IsWeekly(Symbol symbol)
61  {
62  return !IsStandard(symbol);
63  }
64  }
65 }