Esempio n. 1
0
        /// <summary>
        /// Converts an InteractiveBrokers symbol to a Lean symbol instance
        /// </summary>
        /// <param name="brokerageSymbol">The InteractiveBrokers symbol</param>
        /// <param name="securityType">The security type</param>
        /// <param name="market">The market</param>
        /// <param name="expirationDate">Expiration date of the security(if applicable)</param>
        /// <param name="strike">The strike of the security (if applicable)</param>
        /// <param name="optionRight">The option right of the security (if applicable)</param>
        /// <returns>A new Lean Symbol instance</returns>
        public Symbol GetLeanSymbol(string brokerageSymbol, SecurityType securityType, string market, DateTime expirationDate = default(DateTime), decimal strike = 0, OptionRight optionRight = 0)
        {
            if (string.IsNullOrWhiteSpace(brokerageSymbol))
            {
                throw new ArgumentException("Invalid symbol: " + brokerageSymbol);
            }

            if (securityType != SecurityType.Forex &&
                securityType != SecurityType.Equity &&
                securityType != SecurityType.Option &&
                securityType != SecurityType.Future &&
                securityType != SecurityType.FutureOption)
            {
                throw new ArgumentException("Invalid security type: " + securityType);
            }

            try
            {
                switch (securityType)
                {
                case SecurityType.Future:
                    return(Symbol.CreateFuture(GetLeanRootSymbol(brokerageSymbol), market, expirationDate));

                case SecurityType.Option:
                    return(Symbol.CreateOption(brokerageSymbol, market, OptionStyle.American, optionRight, strike, expirationDate));

                case SecurityType.FutureOption:
                    var canonicalFutureSymbol = Symbol.Create(GetLeanRootSymbol(brokerageSymbol), SecurityType.Future, market);
                    var futureContractMonth   = FuturesOptionsExpiryFunctions.GetFutureContractMonth(canonicalFutureSymbol, expirationDate);
                    var futureExpiry          = FuturesExpiryFunctions.FuturesExpiryFunction(canonicalFutureSymbol)(futureContractMonth);

                    return(Symbol.CreateOption(
                               Symbol.CreateFuture(
                                   brokerageSymbol,
                                   market,
                                   futureExpiry),
                               market,
                               OptionStyle.American,
                               optionRight,
                               strike,
                               expirationDate));

                case SecurityType.Equity:
                    brokerageSymbol = brokerageSymbol.Replace(" ", ".");
                    break;
                }

                return(Symbol.Create(brokerageSymbol, securityType, market));
            }
            catch (Exception)
            {
                throw new ArgumentException($"Invalid symbol: {brokerageSymbol}, security type: {securityType}, market: {market}.");
            }
        }
        public void FutureContractMonthDelta(string futureTicker, string market, int expiryMonth, int expectedDelta)
        {
            var contractMonth = new DateTime(2020, 12, 1);

            var future = Symbol.Create(futureTicker, SecurityType.Future, market);
            var option = Symbol.CreateOption(
                future,
                market,
                default(OptionStyle),
                default(OptionRight),
                default(decimal),
                SecurityIdentifier.DefaultDate);

            var futureOptionExpiry = FuturesOptionsExpiryFunctions.FuturesOptionExpiry(option, contractMonth);

            Assert.AreEqual(expectedDelta, contractMonth.Month - futureOptionExpiry.Month);
            Assert.AreEqual(new DateTime(2020, 12, 1), FuturesOptionsExpiryFunctions.GetFutureContractMonth(future, new DateTime(2020, expiryMonth, 1)));
        }