/* * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ using System; using Newtonsoft.Json; using ProtoBuf; using static QuantConnect.StringExtensions; namespace QuantConnect { /// /// Represents a unique security identifier. This is made of two components, /// the unique SID and the Value. The value is the current ticker symbol while /// the SID is constant over the life of a security /// [JsonConverter(typeof(SymbolJsonConverter))] [ProtoContract(SkipConstructor = true)] public sealed class Symbol : IEquatable, IComparable { // for performance we register how we compare with empty private bool? _isEmpty; /// /// Represents an unassigned symbol. This is intended to be used as an /// uninitialized, default value /// public static readonly Symbol Empty = new Symbol(SecurityIdentifier.Empty, string.Empty); /// /// Represents no symbol. This is intended to be used when no symbol is explicitly intended /// public static readonly Symbol None = new Symbol(SecurityIdentifier.None, "NONE"); /// /// Provides a convenience method for creating a Symbol for most security types. /// This method currently does not support Commodities /// /// The string ticker symbol /// The security type of the ticker. If securityType == Option, then a canonical symbol is created /// The market the ticker resides in /// An alias to be used for the symbol cache. Required when /// adding the same security from different markets /// Optional for and used for generating the base data SID /// A new Symbol object for the specified ticker public static Symbol Create(string ticker, SecurityType securityType, string market, string alias = null, Type baseDataType = null) { SecurityIdentifier sid; switch (securityType) { case SecurityType.Base: sid = SecurityIdentifier.GenerateBase(baseDataType, ticker, market); break; case SecurityType.Equity: sid = SecurityIdentifier.GenerateEquity(ticker, market); break; case SecurityType.Forex: sid = SecurityIdentifier.GenerateForex(ticker, market); break; case SecurityType.Cfd: sid = SecurityIdentifier.GenerateCfd(ticker, market); break; case SecurityType.Option: return CreateOption(ticker, market, default(OptionStyle), default(OptionRight), 0, SecurityIdentifier.DefaultDate); case SecurityType.Future: sid = SecurityIdentifier.GenerateFuture(SecurityIdentifier.DefaultDate, ticker, market); break; case SecurityType.Crypto: sid = SecurityIdentifier.GenerateCrypto(ticker, market); break; case SecurityType.FutureOption: throw new NotImplementedException("Cannot create future option Symbol using this method (insufficient information). Use `CreateOption(Symbol, ...)` instead."); case SecurityType.Commodity: default: throw new NotImplementedException(Invariant($"The security type has not been implemented yet: {securityType}")); } return new Symbol(sid, alias ?? ticker); } /// /// Creates a new Symbol for custom data. This method allows for the creation of a new Base Symbol /// using the first ticker and the first traded date from the provided underlying Symbol. This avoids /// the issue for mappable types, where the ticker is remapped supposing the provided ticker value is from today. /// See method /// The provided symbol is also set to so that it can be accessed using the custom data Symbol. /// This is useful for associating custom data Symbols to other asset classes so that it is possible to filter using custom data /// and place trades on the underlying asset based on the filtered custom data. /// /// Type of BaseData instance /// Underlying symbol to set for the Base Symbol /// Market /// New non-mapped Base Symbol that contains an Underlying Symbol public static Symbol CreateBase(Type baseType, Symbol underlying, string market) { // The SID Date is only defined for the following security types: base, equity, future, option. // Default to SecurityIdentifier.DefaultDate if there's no matching SecurityType var firstDate = underlying.SecurityType == SecurityType.Equity || underlying.SecurityType == SecurityType.Option || underlying.SecurityType == SecurityType.Future || underlying.SecurityType == SecurityType.FutureOption || underlying.SecurityType == SecurityType.Base ? underlying.ID.Date : (DateTime?)null; var sid = SecurityIdentifier.GenerateBase(baseType, underlying.ID.Symbol, market, mapSymbol: false, date: firstDate); return new Symbol(sid, underlying.Value, underlying); } /// /// Provides a convenience method for creating an option Symbol. /// /// The underlying ticker /// The market the underlying resides in /// The option style (American, European, ect..) /// The option right (Put/Call) /// The option strike price /// The option expiry date /// An alias to be used for the symbol cache. Required when /// adding the same security from different markets /// Specifies if symbol should be mapped using map file provider /// A new Symbol object for the specified option contract public static Symbol CreateOption(string underlying, string market, OptionStyle style, OptionRight right, decimal strike, DateTime expiry, string alias = null, bool mapSymbol = true) { var underlyingSid = SecurityIdentifier.GenerateEquity(underlying, market, mapSymbol); var underlyingSymbol = new Symbol(underlyingSid, underlying); return CreateOption(underlyingSymbol, market, style, right, strike, expiry, alias); } /// /// Provides a convenience method for creating an option Symbol using SecurityIdentifier. /// /// The underlying security symbol /// The market the underlying resides in /// The option style (American, European, ect..) /// The option right (Put/Call) /// The option strike price /// The option expiry date /// An alias to be used for the symbol cache. Required when /// adding the same security from diferent markets /// A new Symbol object for the specified option contract public static Symbol CreateOption(Symbol underlyingSymbol, string market, OptionStyle style, OptionRight right, decimal strike, DateTime expiry, string alias = null) { var sid = SecurityIdentifier.GenerateOption(expiry, underlyingSymbol.ID, market, strike, right, style); if (expiry == SecurityIdentifier.DefaultDate) { alias = alias ?? $"?{underlyingSymbol.Value.LazyToUpper()}"; } else { var sym = underlyingSymbol.Value; if (sid.Symbol != underlyingSymbol.ID.Symbol) { // If we have changed the SID and it does not match the underlying, // we've mapped a future into another Symbol. We want to have a value // representing the mapped ticker, not of the underlying. // e.g. we want: // OG C3200...|GC18Z20 // NOT // GC C3200...|GC18Z20 sym = sid.Symbol; } if (sym.Length > 5) sym += " "; alias = alias ?? SymbolRepresentation.GenerateOptionTickerOSI(sym, sid.OptionRight, sid.StrikePrice, sid.Date); } return new Symbol(sid, alias, underlyingSymbol); } /// /// Provides a convenience method for creating a future Symbol. /// /// The ticker /// The market the future resides in /// The future expiry date /// An alias to be used for the symbol cache. Required when /// adding the same security from different markets /// A new Symbol object for the specified future contract public static Symbol CreateFuture(string ticker, string market, DateTime expiry, string alias = null) { var sid = SecurityIdentifier.GenerateFuture(expiry, ticker, market); if (expiry == SecurityIdentifier.DefaultDate) { alias = alias ?? "/" + ticker.LazyToUpper(); } else { var sym = sid.Symbol; alias = alias ?? SymbolRepresentation.GenerateFutureTicker(sym, sid.Date); } return new Symbol(sid, alias); } /// /// Method returns true, if symbol is a derivative canonical symbol /// /// true, if symbol is a derivative canonical symbol public bool IsCanonical() { return (ID.SecurityType == SecurityType.Future || (ID.SecurityType == SecurityType.Option && HasUnderlying) || (ID.SecurityType == SecurityType.FutureOption && HasUnderlying)) && ID.Date == SecurityIdentifier.DefaultDate; } /// /// Determines if the specified is an underlying of this symbol instance /// /// The underlying to check for /// True if the specified is an underlying of this symbol instance public bool HasUnderlyingSymbol(Symbol symbol) { var current = this; while (current.HasUnderlying) { if (current.Underlying == symbol) { return true; } current = current.Underlying; } return false; } #region Properties /// /// Gets the current symbol for this ticker /// [ProtoMember(1)] public string Value { get; private set; } /// /// Gets the security identifier for this symbol /// [ProtoMember(2)] public SecurityIdentifier ID { get; private set; } /// /// Gets whether or not this is a derivative, /// that is, it has a valid property /// public bool HasUnderlying { get { return !ReferenceEquals(Underlying, null); } } /// /// Gets the security underlying symbol, if any /// [ProtoMember(3)] public Symbol Underlying { get; private set; } /// /// Gets the security type of the symbol /// public SecurityType SecurityType { get { return ID.SecurityType; } } #endregion #region Constructors /// /// Initializes a new instance of the class /// /// The security identifier for this symbol /// The current ticker symbol value public Symbol(SecurityIdentifier sid, string value) { if (value == null) { throw new ArgumentNullException(nameof(value)); } ID = sid; Value = value.LazyToUpper(); } /// /// Creates new symbol with updated mapped symbol. Symbol Mapping: When symbols change over time (e.g. CHASE-> JPM) need to update the symbol requested. /// Method returns newly created symbol /// public Symbol UpdateMappedSymbol(string mappedSymbol) { if (ID.SecurityType == SecurityType.Option) { var underlyingSymbol = new Symbol(Underlying.ID, mappedSymbol, null); var alias = Value; if (ID.Date != SecurityIdentifier.DefaultDate) { var sym = mappedSymbol; alias = SymbolRepresentation.GenerateOptionTickerOSI(sym, ID.OptionRight, ID.StrikePrice, ID.Date); } return new Symbol(ID, alias, underlyingSymbol); } if (ID.SecurityType == SecurityType.FutureOption) { throw new ArgumentException("Future Option can not be mapped."); } return new Symbol(ID, mappedSymbol, Underlying); } /// /// Determines the SecurityType based on the underlying Symbol's SecurityType /// /// Underlying Symbol of an option /// SecurityType of the option /// The provided underlying has no SecurityType able to represent it as an option public static SecurityType GetOptionTypeFromUnderlying(Symbol underlyingSymbol) { return GetOptionTypeFromUnderlying(underlyingSymbol.SecurityType); } /// /// Determines the SecurityType based on the underlying Symbol's SecurityType /// /// SecurityType of the underlying Symbol /// SecurityType of the option /// The provided underlying has no SecurityType able to represent it as an option public static SecurityType GetOptionTypeFromUnderlying(SecurityType securityType) { switch (securityType) { case SecurityType.Equity: return SecurityType.Option; case SecurityType.Future: return SecurityType.FutureOption; default: throw new ArgumentException($"No option type exists for underlying SecurityType: {securityType}"); } } /// /// Private constructor initializes a new instance of the class with underlying /// /// The security identifier for this symbol /// The current ticker symbol value /// The underlying symbol internal Symbol(SecurityIdentifier sid, string value, Symbol underlying) { if (value == null) { throw new ArgumentNullException(nameof(value)); } ID = sid; Value = value.LazyToUpper(); Underlying = underlying; } #endregion #region Overrides of Object /// /// Determines whether the specified is equal to the current . /// /// /// true if the specified object is equal to the current object; otherwise, false. /// /// The object to compare with the current object. 2 public override bool Equals(object obj) { if (ReferenceEquals(null, obj)) return false; if (ReferenceEquals(this, obj)) return true; // compare strings just as you would a symbol object var sidString = obj as string; if (sidString != null) { SecurityIdentifier sid; if (SecurityIdentifier.TryParse(sidString, out sid)) { return ID.Equals(sid); } } // compare a sid just as you would a symbol object if (obj is SecurityIdentifier) { return ID.Equals((SecurityIdentifier) obj); } if (obj.GetType() != GetType()) return false; return Equals((Symbol)obj); } /// /// Serves as a hash function for a particular type. /// /// /// A hash code for the current . /// /// 2 public override int GetHashCode() { // only SID is used for comparisons unchecked { return ID.GetHashCode(); } } /// /// Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object. /// /// /// A value that indicates the relative order of the objects being compared. The return value has these meanings: Value Meaning Less than zero This instance precedes in the sort order. Zero This instance occurs in the same position in the sort order as . Greater than zero This instance follows in the sort order. /// /// An object to compare with this instance. is not the same type as this instance. 2 public int CompareTo(object obj) { var str = obj as string; if (str != null) { return string.Compare(Value, str, StringComparison.OrdinalIgnoreCase); } var sym = obj as Symbol; if (sym != null) { return string.Compare(Value, sym.Value, StringComparison.OrdinalIgnoreCase); } throw new ArgumentException("Object must be of type Symbol or string."); } /// /// Returns a string that represents the current object. /// /// /// A string that represents the current object. /// /// 2 public override string ToString() { return SymbolCache.GetTicker(this); } #endregion #region Equality members /// /// Indicates whether the current object is equal to another object of the same type. /// /// /// true if the current object is equal to the parameter; otherwise, false. /// /// An object to compare with this object. public bool Equals(Symbol other) { if (ReferenceEquals(this, other)) return true; if (ReferenceEquals(other, null) || ReferenceEquals(other, Empty)) { // other is null or empty (equivalents) // so we need to know how We compare with Empty if (!_isEmpty.HasValue) { // for accuracy we compare IDs not references here _isEmpty = ID.Equals(Empty.ID); } return _isEmpty.Value; } // only SID is used for comparisons return ID.Equals(other.ID); } /// /// Equals operator /// /// The left operand /// The right operand /// True if both symbols are equal, otherwise false public static bool operator ==(Symbol left, Symbol right) { if (ReferenceEquals(left, right)) { // this is a performance shortcut return true; } if (ReferenceEquals(left, null) || left.Equals(Empty)) { return ReferenceEquals(right, null) || right.Equals(Empty); } return left.Equals(right); } /// /// Not equals operator /// /// The left operand /// The right operand /// True if both symbols are not equal, otherwise false public static bool operator !=(Symbol left, Symbol right) { return !(left == right); } #endregion #region Implicit operators /// /// Returns the symbol's string ticker /// /// The symbol /// The string ticker [Obsolete("Symbol implicit operator to string is provided for algorithm use only.")] public static implicit operator string(Symbol symbol) { return symbol.ToString(); } /// /// Creates symbol using string as sid /// /// The string /// The symbol [Obsolete("Symbol implicit operator from string is provided for algorithm use only.")] public static implicit operator Symbol(string ticker) { Symbol symbol; if (SymbolCache.TryGetSymbol(ticker, out symbol)) { return symbol; } SecurityIdentifier sid; if (SecurityIdentifier.TryParse(ticker, out sid)) { return new Symbol(sid, sid.Symbol); } return new Symbol(new SecurityIdentifier(ticker, 0), ticker); } #endregion #region String methods // in order to maintain better compile time backwards compatibility, // we'll redirect a few common string methods to Value, but mark obsolete #pragma warning disable 1591 [Obsolete("Symbol.Contains is a pass-through for Symbol.Value.Contains")] public bool Contains(string value) { return Value.Contains(value); } [Obsolete("Symbol.EndsWith is a pass-through for Symbol.Value.EndsWith")] public bool EndsWith(string value) { return Value.EndsWithInvariant(value); } [Obsolete("Symbol.StartsWith is a pass-through for Symbol.Value.StartsWith")] public bool StartsWith(string value) { return Value.StartsWithInvariant(value); } [Obsolete("Symbol.ToLower is a pass-through for Symbol.Value.ToLower")] public string ToLower() { return Value.ToLowerInvariant(); } [Obsolete("Symbol.ToUpper is a pass-through for Symbol.Value.ToUpper")] public string ToUpper() { return Value.LazyToUpper(); } #pragma warning restore 1591 #endregion } }