Ejemplo n.º 1
0
        public void BacktestingOptionChainProviderReturnsMultipleContractsForZipFileContainingMultipleContracts(
            OptionRight right,
            int strike,
            int year,
            int month,
            int day)
        {
            var underlying     = Symbol.CreateFuture("GC", Market.COMEX, new DateTime(2020, 4, 28));
            var expiry         = new DateTime(year, month, day);
            var expectedOption = Symbol.CreateOption(
                underlying,
                Market.COMEX,
                OptionStyle.American,
                right,
                strike,
                expiry);

            var provider  = new BacktestingOptionChainProvider();
            var contracts = provider.GetOptionContractList(underlying, new DateTime(2020, 1, 5))
                            .ToHashSet();

            Assert.IsTrue(
                contracts.Contains(expectedOption),
                $"Failed to find contract {expectedOption} in: [{string.Join(", ", contracts.Select(s => s.ToString()))}");
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets <see cref="OptionHistory"/> object for a given symbol, date and resolution
        /// </summary>
        /// <param name="symbol">The symbol to retrieve historical option data for</param>
        /// <param name="date">Date of the data</param>
        /// <param name="resolution">The resolution to request</param>
        /// <returns>A <see cref="OptionHistory"/> object that contains historical option data.</returns>
        public OptionHistory GetOptionHistory(Symbol symbol, DateTime date, Resolution?resolution = null)
        {
            SetStartDate(date.AddDays(1));
            var option     = _algorithm.Securities[symbol] as Option;
            var underlying = AddEquity(symbol.Underlying.Value, option.Resolution);

            var provider   = new BacktestingOptionChainProvider();
            var allSymbols = provider.GetOptionContractList(symbol.Underlying, date);

            var requests = _algorithm.History(symbol.Underlying, TimeSpan.FromDays(1), resolution)
                           .SelectMany(x => option.ContractFilter.Filter(new OptionFilterUniverse(allSymbols, x)))
                           .Distinct()
                           .Select(x =>
                                   new HistoryRequest(date.AddDays(-1),
                                                      date,
                                                      typeof(QuoteBar),
                                                      x,
                                                      resolution ?? option.Resolution,
                                                      underlying.Exchange.Hours,
                                                      MarketHoursDatabase.FromDataFolder().GetDataTimeZone(underlying.Symbol.ID.Market, underlying.Symbol, underlying.Type),
                                                      Resolution.Minute,
                                                      underlying.IsExtendedMarketHours,
                                                      underlying.IsCustomData(),
                                                      DataNormalizationMode.Raw,
                                                      LeanData.GetCommonTickTypeForCommonDataTypes(typeof(QuoteBar), underlying.Type))
                                   );

            requests = requests.Union(new[] { new HistoryRequest(underlying.Subscriptions.FirstOrDefault(), underlying.Exchange.Hours, date.AddDays(-1), date) });

            return(new OptionHistory(_algorithm.HistoryProvider.GetHistory(requests.OrderByDescending(x => x.Symbol.SecurityType), _algorithm.TimeZone).Memoize()));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Gets <see cref="OptionHistory"/> object for a given symbol, date and resolution
        /// </summary>
        /// <param name="symbol">The symbol to retrieve historical option data for</param>
        /// <param name="date">Date of the data</param>
        /// <param name="resolution">The resolution to request</param>
        /// <returns>A <see cref="OptionHistory"/> object that contains historical option data.</returns>
        public OptionHistory GetOptionHistory(Symbol symbol, DateTime date, Resolution?resolution = null)
        {
            SetStartDate(date.AddDays(1));
            var option     = _algorithm.Securities[symbol] as Option;
            var underlying = AddEquity(symbol.Underlying.Value, option.Resolution);

            var provider   = new BacktestingOptionChainProvider();
            var allSymbols = provider.GetOptionContractList(symbol.Underlying, date);

            var requests = _algorithm.History(symbol.Underlying, TimeSpan.FromDays(1), resolution)
                           .SelectMany(x => option.ContractFilter.Filter(new OptionFilterUniverse(allSymbols, x)))
                           .Distinct()
                           .Select(x =>
                                   new HistoryRequest()
            {
                Symbol       = x,
                StartTimeUtc = date.AddDays(-1),
                EndTimeUtc   = date,
                Resolution   = resolution ?? option.Resolution,
                DataType     = typeof(QuoteBar)
            }
                                   );

            requests = requests.Union(new[] { new HistoryRequest(underlying.Subscriptions.FirstOrDefault(), underlying.Exchange.Hours, date.AddDays(-1), date) });

            return(new OptionHistory(_algorithm.HistoryProvider.GetHistory(requests.OrderByDescending(x => x.Symbol.SecurityType), _algorithm.TimeZone).Memoize()));
        }
Ejemplo n.º 4
0
        public void BacktestingOptionChainProviderLoadsEquityOptionChain()
        {
            var provider       = new BacktestingOptionChainProvider();
            var twxOptionChain = provider.GetOptionContractList(Symbol.Create("TWX", SecurityType.Equity, Market.USA), new DateTime(2014, 6, 5))
                                 .ToList();

            Assert.AreEqual(184, twxOptionChain.Count);
            Assert.AreEqual(23m, twxOptionChain.OrderBy(s => s.ID.StrikePrice).First().ID.StrikePrice);
            Assert.AreEqual(105m, twxOptionChain.OrderBy(s => s.ID.StrikePrice).Last().ID.StrikePrice);
        }
Ejemplo n.º 5
0
        public void BacktestingOptionChainProviderLoadsFutureOptionChain()
        {
            var provider      = new BacktestingOptionChainProvider();
            var esOptionChain = provider.GetOptionContractList(
                Symbol.CreateFuture(
                    QuantConnect.Securities.Futures.Indices.SP500EMini,
                    Market.CME,
                    new DateTime(2020, 6, 19)),
                new DateTime(2020, 1, 5))
                                .ToList();

            Assert.AreEqual(107, esOptionChain.Count);
            Assert.AreEqual(2900m, esOptionChain.OrderBy(s => s.ID.StrikePrice).First().ID.StrikePrice);
            Assert.AreEqual(3500m, esOptionChain.OrderBy(s => s.ID.StrikePrice).Last().ID.StrikePrice);
        }
Ejemplo n.º 6
0
        public void BacktestingOptionChainProviderUsesPreviousTradableDateChain()
        {
            // the 7th is a saturday should fetch fridays data instead
            var date = new DateTime(2014, 6, 7);

            Assert.AreEqual(DayOfWeek.Saturday, date.DayOfWeek);

            var provider       = new BacktestingOptionChainProvider(TestGlobals.DataCacheProvider, TestGlobals.MapFileProvider);
            var twxOptionChain = provider.GetOptionContractList(Symbol.Create("TWX", SecurityType.Equity, Market.USA), date)
                                 .ToList();

            Assert.AreEqual(184, twxOptionChain.Count);
            Assert.AreEqual(23m, twxOptionChain.OrderBy(s => s.ID.StrikePrice).First().ID.StrikePrice);
            Assert.AreEqual(105m, twxOptionChain.OrderBy(s => s.ID.StrikePrice).Last().ID.StrikePrice);
        }
Ejemplo n.º 7
0
        public void ValidateAAPLOptionChainSecurityIdentifiers()
        {
            var chainProvider  = new BacktestingOptionChainProvider();
            var aapl           = Symbol.Create("AAPL", SecurityType.Equity, Market.USA);
            var chains         = new HashSet <Symbol>();
            var expectedChains = File.ReadAllLines("TestData/aapl_chain.csv")
                                 .Where(x => !string.IsNullOrWhiteSpace(x))
                                 .ToDictionary(x => x, _ => false);

            Assert.AreNotEqual(0, expectedChains.Count);

            var start = new DateTime(2020, 1, 1);
            var end   = new DateTime(2020, 7, 1);

            foreach (var date in Time.EachDay(start, end))
            {
                if (USHoliday.Dates.Contains(date) || date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday)
                {
                    continue;
                }

                foreach (var symbol in chainProvider.GetOptionContractList(aapl, date))
                {
                    chains.Add(symbol);
                }
            }

            var fails = new HashSet <Symbol>();

            foreach (var chain in chains)
            {
                if (expectedChains.ContainsKey(chain.ID.ToString()))
                {
                    expectedChains[chain.ID.ToString()] = true;
                    continue;
                }

                fails.Add(chain);
            }

            Assert.AreEqual(0, fails.Count, $"The following option Symbols were not found in the expected chain:    \n{string.Join("\n", fails.Select(x => x.ID.ToString()))}");
            Assert.IsTrue(expectedChains.All(kvp => kvp.Value), $"The following option Symbols were not loaded:    \n{string.Join("\n", expectedChains.Where(kvp => !kvp.Value).Select(x => x.Key))}");
        }
Ejemplo n.º 8
0
        public void BacktestingOptionChainProviderResolvesSymbolMapping()
        {
            var ticker   = "GOOCV"; // Old ticker, should resolve and fetch GOOG
            var provider = new BacktestingOptionChainProvider(TestGlobals.DataCacheProvider, TestGlobals.MapFileProvider);

            var underlyingSymbol = QuantConnect.Symbol.Create(ticker, SecurityType.Equity, Market.USA);
            var alias            = "?" + underlyingSymbol.Value;
            var optionSymbol     = Symbol.CreateOption(
                underlyingSymbol,
                underlyingSymbol.ID.Market,
                Symbol.GetOptionTypeFromUnderlying(underlyingSymbol).DefaultOptionStyle(),
                default(OptionRight),
                0,
                SecurityIdentifier.DefaultDate,
                alias);

            var googOptionChain = provider.GetOptionContractList(optionSymbol.Underlying, new DateTime(2015, 12, 23))
                                  .ToList();

            Assert.AreEqual(118, googOptionChain.Count);
            Assert.AreEqual(600m, googOptionChain.OrderBy(s => s.ID.StrikePrice).First().ID.StrikePrice);
            Assert.AreEqual(800m, googOptionChain.OrderBy(s => s.ID.StrikePrice).Last().ID.StrikePrice);
        }