Lean  $LEAN_TAG$
AuxiliaryDataKey.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 
18 {
19  /// <summary>
20  /// Unique definition key for a collection of auxiliary data for a Market and SecurityType
21  /// </summary>
22  public class AuxiliaryDataKey
23  {
24  /// <summary>
25  /// USA equities market corporate actions key definition
26  /// </summary>
27  public static AuxiliaryDataKey EquityUsa { get; } = new (QuantConnect.Market.USA, SecurityType.Equity);
28 
29  /// <summary>
30  /// The market associated with these corporate actions
31  /// </summary>
32  public string Market { get; }
33 
34  /// <summary>
35  /// The associated security type
36  /// </summary>
37  public SecurityType SecurityType { get; }
38 
39  /// <summary>
40  /// Creates a new instance
41  /// </summary>
42  public AuxiliaryDataKey(string market, SecurityType securityType)
43  {
44  Market = market;
45  SecurityType = securityType;
46  }
47 
48  /// <summary>
49  /// Serves as a hash function for a particular type.
50  /// </summary>
51  public override int GetHashCode()
52  {
53  unchecked
54  {
55  var hashCode = Market.GetHashCode();
56  return (hashCode*397) ^ SecurityType.GetHashCode();
57  }
58  }
59 
60  /// <summary>
61  /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
62  /// </summary>
63  /// <returns>
64  /// true if the specified object is equal to the current object; otherwise, false.
65  /// </returns>
66  /// <param name="obj">The object to compare with the current object. </param><filterpriority>2</filterpriority>
67  public override bool Equals(object? obj)
68  {
69  if (ReferenceEquals(null, obj)) return false;
70  if (obj.GetType() != GetType()) return false;
71 
72  var other = (AuxiliaryDataKey)obj;
73 
74  return other.Market == Market
75  && other.SecurityType == SecurityType;
76  }
77 
78  /// <summary>
79  /// Returns a string containing the market and security type
80  /// </summary>
81  public override string ToString()
82  {
83  return $"{Market}:{SecurityType}";
84  }
85 
86  /// <summary>
87  /// Helper method to create a new instance from a Symbol
88  /// </summary>
89  public static AuxiliaryDataKey Create(Symbol symbol) => Create(symbol.HasUnderlying ? symbol.Underlying.ID : symbol.ID);
90 
91  /// <summary>
92  /// Helper method to create a new instance from a SecurityIdentifier
93  /// </summary>
94  public static AuxiliaryDataKey Create(SecurityIdentifier securityIdentifier)
95  {
96  securityIdentifier = securityIdentifier.HasUnderlying ? securityIdentifier.Underlying : securityIdentifier;
97  return new AuxiliaryDataKey(securityIdentifier.Market, securityIdentifier.SecurityType);
98  }
99  }
100 }