예제 #1
0
파일: Symbol.cs 프로젝트: nika90426/Lean
#pragma warning restore 1591

        #endregion

        /// <summary>
        /// Centralized helper method to resolve alias for a symbol
        /// </summary>
        public static string GetAlias(SecurityIdentifier securityIdentifier, Symbol underlying = null)
        {
            string sym;

            switch (securityIdentifier.SecurityType)
            {
            case SecurityType.FutureOption:
            case SecurityType.Option:
            case SecurityType.IndexOption:
                if (securityIdentifier.Date == SecurityIdentifier.DefaultDate)
                {
                    return($"?{underlying.Value.LazyToUpper()}");
                }
                sym = underlying.Value;
                if (securityIdentifier.Symbol != underlying.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 = securityIdentifier.Symbol;
                }

                if (sym.Length > 5)
                {
                    sym += " ";
                }

                return(SymbolRepresentation.GenerateOptionTickerOSI(sym, securityIdentifier.OptionRight, securityIdentifier.StrikePrice, securityIdentifier.Date));

            case SecurityType.Future:
                sym = securityIdentifier.Symbol;
                if (securityIdentifier.Date == SecurityIdentifier.DefaultDate)
                {
                    return($"/{sym}");
                }
                return(SymbolRepresentation.GenerateFutureTicker(sym, securityIdentifier.Date));

            default:
                return(null);
            }
        }
예제 #2
0
        /// <summary>
        /// Provides a convenience method for creating a futures Symbol.
        /// </summary>
        /// <param name="underlying">The underlying ticker</param>
        /// <param name="market">The market the underlying resides in</param>
        /// <param name="expiry">The option expiry date</param>
        /// <param name="alias">An alias to be used for the symbol cache. Required when
        /// adding the same security from different markets</param>
        /// <returns>A new Symbol object for the specified option contract</returns>
        public static Symbol CreateFuture(string underlying, string market, DateTime expiry, string alias = null)
        {
            var underlyingSid    = SecurityIdentifier.GenerateBase(underlying, market);
            var sid              = SecurityIdentifier.GenerateFuture(expiry, underlyingSid, market);
            var underlyingSymbol = new Symbol(underlyingSid, underlying);

            if (expiry == SecurityIdentifier.DefaultDate)
            {
                alias = alias ?? "/" + underlying.ToUpper();
            }
            else
            {
                var sym = sid.Symbol;
                alias = alias ?? SymbolRepresentation.GenerateFutureTicker(sym, sid.Date);
            }

            return(new Symbol(sid, alias, underlyingSymbol));
        }
예제 #3
0
        /// <summary>
        /// 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
        /// </summary>
        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));
            }
            else
            {
                return(new Symbol(ID, mappedSymbol, Underlying));
            }
        }
예제 #4
0
        /// <summary>
        /// Provides a convenience method for creating an option Symbol using SecurityIdentifier.
        /// </summary>
        /// <param name="underlyingSymbol">The underlying security symbol</param>
        /// <param name="market">The market the underlying resides in</param>
        /// <param name="style">The option style (American, European, ect..)</param>
        /// <param name="right">The option right (Put/Call)</param>
        /// <param name="strike">The option strike price</param>
        /// <param name="expiry">The option expiry date</param>
        /// <param name="alias">An alias to be used for the symbol cache. Required when
        /// adding the same security from diferent markets</param>
        /// <returns>A new Symbol object for the specified option contract</returns>
        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.ToUpper();
            }
            else
            {
                var sym = underlyingSymbol.Value;
                if (sym.Length > 5)
                {
                    sym += " ";
                }

                alias = alias ?? SymbolRepresentation.GenerateOptionTickerOSI(sym, sid.OptionRight, sid.StrikePrice, sid.Date);
            }

            return(new Symbol(sid, alias, underlyingSymbol));
        }
예제 #5
0
파일: Symbol.cs 프로젝트: nika90426/Lean
        /// <summary>
        /// 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
        /// </summary>
        public Symbol UpdateMappedSymbol(string mappedSymbol)
        {
            // Throw for any option SecurityType that is not for equities, we don't support mapping for them (FOPs and Index Options)
            if (ID.SecurityType.IsOption() && SecurityType != SecurityType.Option)
            {
                throw new ArgumentException($"SecurityType {ID.SecurityType} can not be mapped.");
            }

            // Avoid updating the current instance's underlying Symbol.
            var underlyingSymbol = Underlying;

            // Some universe Symbols, such as Constituent ETF universe Symbols and mapped custom data Symbols, have an
            // underlying equity ETF Symbol as their underlying. When we're checking to see if a specific BaseData
            // instance requires mapping, only the parent Symbol will be updated, which might not even need to be mapped
            // (e.g. universe symbols with no equity ticker in symbol value).
            // This will ensure that we map all of the underlying Symbol(s) that also require mapping updates.
            if (HasUnderlying)
            {
                underlyingSymbol = Underlying.UpdateMappedSymbol(mappedSymbol);
            }

            // If this Symbol is not a custom data type, and the security type does not support mapping,
            // then we know for a fact that this Symbol should not be mapped.
            // Custom data types should be mapped, especially if this method is called on them because
            // they can have an underlying that is also mapped.
            if (SecurityType != SecurityType.Base && !SecurityType.RequiresMapping())
            {
                return(new Symbol(ID, Value, underlyingSymbol));
            }

            if (SecurityType == SecurityType.Option)
            {
                mappedSymbol = !IsCanonical()
                    ? SymbolRepresentation.GenerateOptionTickerOSI(mappedSymbol, ID.OptionRight, ID.StrikePrice, ID.Date)
                    : Value;
            }

            return(new Symbol(ID, mappedSymbol, underlyingSymbol));
        }
예제 #6
0
        /// <summary>
        /// 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
        /// </summary>
        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));
            }
            // Throw for the rest of our option types, we don't support mapping for them (FOPs and Index Options)
            if (ID.SecurityType.IsOption())
            {
                throw new ArgumentException($"SecurityType {ID.SecurityType} can not be mapped.");
            }

            return(new Symbol(ID, mappedSymbol, Underlying));
        }