From ab48f3302841eab65fae23a4ab6776e8fc80aaa4 Mon Sep 17 00:00:00 2001 From: Jhonathan Abreu Date: Tue, 10 Sep 2024 16:25:55 -0400 Subject: [PATCH 01/10] Add new QCAlgorithm.OptionChain method to get full data option chain --- Algorithm/QCAlgorithm.cs | 107 +++++++++++++++++++++++- Tests/Algorithm/AlgorithmChainsTests.cs | 79 +++++++++++++++++ 2 files changed, 185 insertions(+), 1 deletion(-) create mode 100644 Tests/Algorithm/AlgorithmChainsTests.cs diff --git a/Algorithm/QCAlgorithm.cs b/Algorithm/QCAlgorithm.cs index 1ee6e0692b55..ce591a83c14f 100644 --- a/Algorithm/QCAlgorithm.cs +++ b/Algorithm/QCAlgorithm.cs @@ -53,6 +53,9 @@ using QuantConnect.Securities.CryptoFuture; using QuantConnect.Algorithm.Framework.Alphas.Analysis; using QuantConnect.Algorithm.Framework.Portfolio.SignalExports; +using QuantConnect.Data.Auxiliary; +using Python.Runtime; +using QuantConnect.Python; namespace QuantConnect.Algorithm { @@ -467,6 +470,9 @@ public IAlgorithmSettings Settings /// Gets the option chain provider, used to get the list of option contracts for an underlying symbol /// [DocumentationAttribute(AddingData)] + [Obsolete("OptionChainProvider property is will soon be deprecated. " + + "The new OptionChain() method should be used to fetch equity and index option chains, " + + "which will contain additional data per contract, like daily price data, implied volatility and greeks.")] public IOptionChainProvider OptionChainProvider { get; private set; } /// @@ -2281,7 +2287,7 @@ public IndexOption AddIndexOption(string underlying, string targetOption, Resolu { throw new KeyNotFoundException($"No default market set for underlying security type: {SecurityType.Index}"); } - + return AddIndexOption( QuantConnect.Symbol.Create(underlying, SecurityType.Index, market), targetOption, resolution, fillForward); @@ -3325,6 +3331,105 @@ public List Fundamentals(List symbols) return symbols.Select(symbol => Fundamentals(symbol)).ToList(); } + /// + /// Get the option chain for the specified symbol at the current time () + /// + /// + /// The symbol for which the option chain is asked for. + /// It can be either the canonical option or the underlying symbol. + /// + /// + /// The option chain as an enumerable of , + /// each containing the contract symbol along with additional data, including daily price data, + /// implied volatility and greeks. + /// + /// + /// As of 2024/09/10, this method only support equity and index options. + /// Future options support will be added in the future and, in the meantime, + /// the should be used for that security type. + /// + [DocumentationAttribute(AddingData)] + public DataHistory OptionChain(Symbol symbol) + { + if (symbol.SecurityType == SecurityType.Future || symbol.SecurityType == SecurityType.FutureOption) + { + Log($"Warning: QCAlgorithm.{nameof(OptionChain)} method cannot be used to get future options chains yet. " + + $"Until support is added, please fall back to the {nameof(OptionChainProvider)}."); + var data = Enumerable.Empty(); + return new DataHistory(data, new Lazy(() => new PandasConverter().GetDataFrame(data))); + } + + var canonicalSymbol = GetCanonicalSymbol(symbol, Time); + var marketHoursEntry = MarketHoursDatabase.GetEntry(canonicalSymbol.ID.Market, canonicalSymbol, canonicalSymbol.SecurityType); + + var previousTradingDate = QuantConnect.Time.GetStartTimeForTradeBars(marketHoursEntry.ExchangeHours, Time, QuantConnect.Time.OneDay, 1, + extendedMarketHours: false, marketHoursEntry.DataTimeZone); + previousTradingDate = previousTradingDate.ConvertTo(marketHoursEntry.DataTimeZone, TimeZone); + var history = History(canonicalSymbol, previousTradingDate, Time, Resolution.Daily); + var optionChain = history?.SingleOrDefault()?.Data?.Cast(); + + if (optionChain == null) + { + optionChain = Enumerable.Empty(); + } + + return new DataHistory(optionChain, new Lazy(() => new PandasConverter().GetDataFrame(history))); + } + + private static Symbol GetCanonicalSymbol(Symbol symbol, DateTime date) + { + Symbol canonicalSymbol; + if (!symbol.SecurityType.HasOptions()) + { + // we got an option + if (symbol.SecurityType.IsOption() && symbol.Underlying != null) + { + // Resolve any mapping before requesting option contract list for equities + // Needs to be done in order for the data file key to be accurate + if (symbol.Underlying.RequiresMapping()) + { + var mappedUnderlyingSymbol = MapUnderlyingSymbol(symbol.Underlying, date); + + canonicalSymbol = QuantConnect.Symbol.CreateCanonicalOption(mappedUnderlyingSymbol); + } + else + { + canonicalSymbol = symbol.Canonical; + } + } + else + { + throw new NotSupportedException($"QCAlgorithm.GetCanonicalSymbol(): " + + $"{nameof(SecurityType.Equity)}, {nameof(SecurityType.Future)}, or {nameof(SecurityType.Index)} is expected but was {symbol.SecurityType}"); + } + } + else + { + // we got the underlying + var mappedUnderlyingSymbol = MapUnderlyingSymbol(symbol, date); + canonicalSymbol = QuantConnect.Symbol.CreateCanonicalOption(mappedUnderlyingSymbol); + } + + return canonicalSymbol; + } + + private static Symbol MapUnderlyingSymbol(Symbol underlying, DateTime date) + { + if (underlying.RequiresMapping()) + { + var mapFileProvider = Composer.Instance.GetPart(); + + var mapFileResolver = mapFileProvider.Get(AuxiliaryDataKey.Create(underlying)); + var mapFile = mapFileResolver.ResolveMapFile(underlying); + var ticker = mapFile.GetMappedSymbol(date, underlying.Value); + return underlying.UpdateMappedSymbol(ticker); + } + else + { + return underlying; + } + } + /// /// Set the properties and exchange hours for a given key into our databases /// diff --git a/Tests/Algorithm/AlgorithmChainsTests.cs b/Tests/Algorithm/AlgorithmChainsTests.cs new file mode 100644 index 000000000000..94671fd2a6b8 --- /dev/null +++ b/Tests/Algorithm/AlgorithmChainsTests.cs @@ -0,0 +1,79 @@ +/* + * 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 System.Linq; +using NUnit.Framework; +using QuantConnect.Algorithm; +using QuantConnect.Data; +using QuantConnect.Interfaces; +using QuantConnect.Lean.Engine.DataFeeds; +using QuantConnect.Tests.Engine.DataFeeds; +using QuantConnect.Util; + +namespace QuantConnect.Tests.Algorithm +{ + [TestFixture] + public class AlgorithmChainsTest + { + private QCAlgorithm _algorithm; + private BacktestingOptionChainProvider _optionChainProvider; + + [OneTimeSetUp] + public void OneTimeSetUp() + { + var historyProvider = Composer.Instance.GetExportedValueByTypeName("SubscriptionDataReaderHistoryProvider", true); + var parameters = new HistoryProviderInitializeParameters(null, null, TestGlobals.DataProvider, TestGlobals.DataCacheProvider, + TestGlobals.MapFileProvider, TestGlobals.FactorFileProvider, (_) => { }, true, new DataPermissionManager(), null, + new AlgorithmSettings()); + historyProvider.Initialize(parameters); + + _algorithm = new QCAlgorithm(); + _algorithm.SetHistoryProvider(historyProvider); + _algorithm.SubscriptionManager.SetDataManager(new DataManagerStub(_algorithm)); + + _optionChainProvider = new BacktestingOptionChainProvider(TestGlobals.DataCacheProvider, TestGlobals.MapFileProvider); + } + + private static TestCaseData[] OptionChainTestCases = new TestCaseData[] + { + // By underlying + new(Symbols.AAPL, new DateTime(2014, 06, 06)), + new(Symbols.SPX, new DateTime(2021, 01, 04)), + // By canonical + new(Symbol.CreateCanonicalOption(Symbols.AAPL), new DateTime(2014, 06, 06)), + new(Symbol.CreateCanonicalOption(Symbols.SPX), new DateTime(2021, 01, 04)) + }; + + [TestCaseSource(nameof(OptionChainTestCases))] + public void GetsFullDataOptionChain(Symbol symbol, DateTime date) + { + _algorithm.SetDateTime(date.ConvertToUtc(_algorithm.TimeZone)); + var optionContractsData = _algorithm.OptionChain(symbol).ToList(); + + var optionContractsSymbols = _optionChainProvider.GetOptionContractList(symbol, date).ToList(); + + CollectionAssert.AreEquivalent(optionContractsSymbols, optionContractsData.Select(x => x.Symbol)); + } + + [Test] + public void CannotGetFutureOptionsChain() + { + var result = _algorithm.OptionChain(Symbols.ES_Future_Chain).ToList(); + Assert.IsEmpty(result); + Assert.IsTrue(_algorithm.LogMessages.Any(x => x.Contains($"Warning: QCAlgorithm.{nameof(QCAlgorithm.OptionChain)} method cannot be used to get future options chains yet. Until support is added, please fall back to the {nameof(QCAlgorithm.OptionChainProvider)}.", StringComparison.InvariantCulture))); + } + } +} From 6b58b6734207ff2d82f64e068aa0f8f7191e8a98 Mon Sep 17 00:00:00 2001 From: Jhonathan Abreu Date: Tue, 10 Sep 2024 16:37:55 -0400 Subject: [PATCH 02/10] Add extension method to get canonical symbol --- Algorithm/QCAlgorithm.cs | 56 +---------------- Common/Extensions.cs | 60 +++++++++++++++++++ .../BacktestingOptionChainProvider.cs | 56 +---------------- 3 files changed, 63 insertions(+), 109 deletions(-) diff --git a/Algorithm/QCAlgorithm.cs b/Algorithm/QCAlgorithm.cs index ce591a83c14f..4a346077a0f6 100644 --- a/Algorithm/QCAlgorithm.cs +++ b/Algorithm/QCAlgorithm.cs @@ -3359,7 +3359,7 @@ public DataHistory OptionChain(Symbol symbol) return new DataHistory(data, new Lazy(() => new PandasConverter().GetDataFrame(data))); } - var canonicalSymbol = GetCanonicalSymbol(symbol, Time); + var canonicalSymbol = symbol.GetCanonical(Time); var marketHoursEntry = MarketHoursDatabase.GetEntry(canonicalSymbol.ID.Market, canonicalSymbol, canonicalSymbol.SecurityType); var previousTradingDate = QuantConnect.Time.GetStartTimeForTradeBars(marketHoursEntry.ExchangeHours, Time, QuantConnect.Time.OneDay, 1, @@ -3376,60 +3376,6 @@ public DataHistory OptionChain(Symbol symbol) return new DataHistory(optionChain, new Lazy(() => new PandasConverter().GetDataFrame(history))); } - private static Symbol GetCanonicalSymbol(Symbol symbol, DateTime date) - { - Symbol canonicalSymbol; - if (!symbol.SecurityType.HasOptions()) - { - // we got an option - if (symbol.SecurityType.IsOption() && symbol.Underlying != null) - { - // Resolve any mapping before requesting option contract list for equities - // Needs to be done in order for the data file key to be accurate - if (symbol.Underlying.RequiresMapping()) - { - var mappedUnderlyingSymbol = MapUnderlyingSymbol(symbol.Underlying, date); - - canonicalSymbol = QuantConnect.Symbol.CreateCanonicalOption(mappedUnderlyingSymbol); - } - else - { - canonicalSymbol = symbol.Canonical; - } - } - else - { - throw new NotSupportedException($"QCAlgorithm.GetCanonicalSymbol(): " + - $"{nameof(SecurityType.Equity)}, {nameof(SecurityType.Future)}, or {nameof(SecurityType.Index)} is expected but was {symbol.SecurityType}"); - } - } - else - { - // we got the underlying - var mappedUnderlyingSymbol = MapUnderlyingSymbol(symbol, date); - canonicalSymbol = QuantConnect.Symbol.CreateCanonicalOption(mappedUnderlyingSymbol); - } - - return canonicalSymbol; - } - - private static Symbol MapUnderlyingSymbol(Symbol underlying, DateTime date) - { - if (underlying.RequiresMapping()) - { - var mapFileProvider = Composer.Instance.GetPart(); - - var mapFileResolver = mapFileProvider.Get(AuxiliaryDataKey.Create(underlying)); - var mapFile = mapFileResolver.ResolveMapFile(underlying); - var ticker = mapFile.GetMappedSymbol(date, underlying.Value); - return underlying.UpdateMappedSymbol(ticker); - } - else - { - return underlying; - } - } - /// /// Set the properties and exchange hours for a given key into our databases /// diff --git a/Common/Extensions.cs b/Common/Extensions.cs index d04dfdf0d6cf..76014b5e04a8 100644 --- a/Common/Extensions.cs +++ b/Common/Extensions.cs @@ -1851,6 +1851,66 @@ public static bool IsMarketOpen(this Symbol symbol, DateTime utcTime, bool exten return exchangeHours.IsOpen(time, extendedMarketHours); } + /// + /// Helper method to get the canonical symbol for a specified symbol, taking care of mapping in the case of equity options + /// + /// The symbol to get the canonical for. It can be an underlying or an option + /// The date of the request, in order to resolve mappings when necessary + /// The optional map file provider + /// The canonical symbol + public static Symbol GetCanonical(this Symbol symbol, DateTime date, IMapFileProvider mapFileProvider = null) + { + Symbol canonicalSymbol; + if (!symbol.SecurityType.HasOptions()) + { + // we got an option + if (symbol.SecurityType.IsOption() && symbol.Underlying != null) + { + // Resolve any mapping before requesting option contract list for equities + // Needs to be done in order for the data file key to be accurate + if (symbol.Underlying.RequiresMapping()) + { + var mappedUnderlyingSymbol = MapUnderlyingSymbol(symbol.Underlying, date, mapFileProvider); + + canonicalSymbol = Symbol.CreateCanonicalOption(mappedUnderlyingSymbol); + } + else + { + canonicalSymbol = symbol.Canonical; + } + } + else + { + throw new NotSupportedException($"QCAlgorithm.GetCanonicalSymbol(): " + + $"{nameof(SecurityType.Equity)}, {nameof(SecurityType.Future)}, or {nameof(SecurityType.Index)} is expected but was {symbol.SecurityType}"); + } + } + else + { + // we got the underlying + var mappedUnderlyingSymbol = MapUnderlyingSymbol(symbol, date, mapFileProvider); + canonicalSymbol = Symbol.CreateCanonicalOption(mappedUnderlyingSymbol); + } + + return canonicalSymbol; + } + + private static Symbol MapUnderlyingSymbol(Symbol underlying, DateTime date, IMapFileProvider mapFileProvider = null) + { + if (underlying.RequiresMapping()) + { + mapFileProvider ??= Composer.Instance.GetPart(); + var mapFileResolver = mapFileProvider.Get(AuxiliaryDataKey.Create(underlying)); + var mapFile = mapFileResolver.ResolveMapFile(underlying); + var ticker = mapFile.GetMappedSymbol(date, underlying.Value); + return underlying.UpdateMappedSymbol(ticker); + } + else + { + return underlying; + } + } + /// /// Extension method to round a datetime to the nearest unit timespan. /// diff --git a/Engine/DataFeeds/BacktestingOptionChainProvider.cs b/Engine/DataFeeds/BacktestingOptionChainProvider.cs index a456f39f34d6..24da32354b6f 100644 --- a/Engine/DataFeeds/BacktestingOptionChainProvider.cs +++ b/Engine/DataFeeds/BacktestingOptionChainProvider.cs @@ -42,64 +42,12 @@ public BacktestingOptionChainProvider(IDataCacheProvider dataCacheProvider, IMap /// Gets the list of option contracts for a given underlying symbol /// /// The option or the underlying symbol to get the option chain for. - /// Providing the option allows targetting an option ticker different than the default e.g. SPXW + /// Providing the option allows targeting an option ticker different than the default e.g. SPXW /// The date for which to request the option chain (only used in backtesting) /// The list of option contracts public virtual IEnumerable GetOptionContractList(Symbol symbol, DateTime date) { - Symbol canonicalSymbol; - if (!symbol.SecurityType.HasOptions()) - { - // we got an option - if (symbol.SecurityType.IsOption() && symbol.Underlying != null) - { - canonicalSymbol = GetCanonical(symbol, date); - } - else - { - throw new NotSupportedException($"BacktestingOptionChainProvider.GetOptionContractList(): " + - $"{nameof(SecurityType.Equity)}, {nameof(SecurityType.Future)}, or {nameof(SecurityType.Index)} is expected but was {symbol.SecurityType}"); - } - } - else - { - // we got the underlying - var mappedUnderlyingSymbol = MapUnderlyingSymbol(symbol, date); - canonicalSymbol = Symbol.CreateCanonicalOption(mappedUnderlyingSymbol); - } - - return GetSymbols(canonicalSymbol, date); - } - - private Symbol GetCanonical(Symbol optionSymbol, DateTime date) - { - // Resolve any mapping before requesting option contract list for equities - // Needs to be done in order for the data file key to be accurate - if (optionSymbol.Underlying.RequiresMapping()) - { - var mappedUnderlyingSymbol = MapUnderlyingSymbol(optionSymbol.Underlying, date); - - return Symbol.CreateCanonicalOption(mappedUnderlyingSymbol); - } - else - { - return optionSymbol.Canonical; - } - } - - private Symbol MapUnderlyingSymbol(Symbol underlying, DateTime date) - { - if (underlying.RequiresMapping()) - { - var mapFileResolver = _mapFileProvider.Get(AuxiliaryDataKey.Create(underlying)); - var mapFile = mapFileResolver.ResolveMapFile(underlying); - var ticker = mapFile.GetMappedSymbol(date, underlying.Value); - return underlying.UpdateMappedSymbol(ticker); - } - else - { - return underlying; - } + return GetSymbols(symbol.GetCanonical(date), date); } } } From 564c86703e8949d43fe7f1c973888c1fbb477afd Mon Sep 17 00:00:00 2001 From: Jhonathan Abreu Date: Tue, 10 Sep 2024 17:46:56 -0400 Subject: [PATCH 03/10] Support future options in new OptionChain method --- Algorithm/QCAlgorithm.cs | 52 ++++++++++------ Common/Extensions.cs | 60 ------------------- .../BacktestingOptionChainProvider.cs | 56 ++++++++++++++++- Tests/Algorithm/AlgorithmChainsTests.cs | 22 +++---- 4 files changed, 97 insertions(+), 93 deletions(-) diff --git a/Algorithm/QCAlgorithm.cs b/Algorithm/QCAlgorithm.cs index 4a346077a0f6..789cff1d6b3a 100644 --- a/Algorithm/QCAlgorithm.cs +++ b/Algorithm/QCAlgorithm.cs @@ -3344,36 +3344,52 @@ public List Fundamentals(List symbols) /// implied volatility and greeks. /// /// - /// As of 2024/09/10, this method only support equity and index options. - /// Future options support will be added in the future and, in the meantime, - /// the should be used for that security type. + /// As of 2024/09/10, future options chain will not contain any additional data (e.g. daily price data, implied volatility and greeks), + /// it will be populated with the contract symbol only. This is expected to change in the future. /// [DocumentationAttribute(AddingData)] public DataHistory OptionChain(Symbol symbol) { - if (symbol.SecurityType == SecurityType.Future || symbol.SecurityType == SecurityType.FutureOption) + var canonicalSymbol = GetCanonicalOptionSymbol(symbol); + IEnumerable optionChain; + + if (canonicalSymbol.SecurityType != SecurityType.FutureOption) + { + var marketHoursEntry = MarketHoursDatabase.GetEntry(canonicalSymbol.ID.Market, canonicalSymbol, canonicalSymbol.SecurityType); + var previousTradingDate = QuantConnect.Time.GetStartTimeForTradeBars(marketHoursEntry.ExchangeHours, Time, QuantConnect.Time.OneDay, 1, + extendedMarketHours: false, marketHoursEntry.DataTimeZone); + previousTradingDate = previousTradingDate.ConvertTo(marketHoursEntry.DataTimeZone, TimeZone); + + var history = History(canonicalSymbol, previousTradingDate, Time, Resolution.Daily); + optionChain = history?.SingleOrDefault()?.Data?.Cast() ?? Enumerable.Empty(); + } + else { - Log($"Warning: QCAlgorithm.{nameof(OptionChain)} method cannot be used to get future options chains yet. " + - $"Until support is added, please fall back to the {nameof(OptionChainProvider)}."); - var data = Enumerable.Empty(); - return new DataHistory(data, new Lazy(() => new PandasConverter().GetDataFrame(data))); + optionChain = OptionChainProvider.GetOptionContractList(canonicalSymbol, Time) + .Select(contractSymbol => new OptionUniverse() + { + Symbol = contractSymbol, + EndTime = Time.Date + }); } - var canonicalSymbol = symbol.GetCanonical(Time); - var marketHoursEntry = MarketHoursDatabase.GetEntry(canonicalSymbol.ID.Market, canonicalSymbol, canonicalSymbol.SecurityType); + return new DataHistory(optionChain, new Lazy(() => new PandasConverter().GetDataFrame(optionChain))); + } - var previousTradingDate = QuantConnect.Time.GetStartTimeForTradeBars(marketHoursEntry.ExchangeHours, Time, QuantConnect.Time.OneDay, 1, - extendedMarketHours: false, marketHoursEntry.DataTimeZone); - previousTradingDate = previousTradingDate.ConvertTo(marketHoursEntry.DataTimeZone, TimeZone); - var history = History(canonicalSymbol, previousTradingDate, Time, Resolution.Daily); - var optionChain = history?.SingleOrDefault()?.Data?.Cast(); + private static Symbol GetCanonicalOptionSymbol(Symbol symbol) + { + // We got the underlying + if (symbol.SecurityType.HasOptions()) + { + return QuantConnect.Symbol.CreateCanonicalOption(symbol); + } - if (optionChain == null) + if (symbol.SecurityType.IsOption()) { - optionChain = Enumerable.Empty(); + return symbol.Canonical; } - return new DataHistory(optionChain, new Lazy(() => new PandasConverter().GetDataFrame(history))); + throw new ArgumentException($"The symbol {symbol} is not an option or an underlying symbol."); } /// diff --git a/Common/Extensions.cs b/Common/Extensions.cs index 76014b5e04a8..d04dfdf0d6cf 100644 --- a/Common/Extensions.cs +++ b/Common/Extensions.cs @@ -1851,66 +1851,6 @@ public static bool IsMarketOpen(this Symbol symbol, DateTime utcTime, bool exten return exchangeHours.IsOpen(time, extendedMarketHours); } - /// - /// Helper method to get the canonical symbol for a specified symbol, taking care of mapping in the case of equity options - /// - /// The symbol to get the canonical for. It can be an underlying or an option - /// The date of the request, in order to resolve mappings when necessary - /// The optional map file provider - /// The canonical symbol - public static Symbol GetCanonical(this Symbol symbol, DateTime date, IMapFileProvider mapFileProvider = null) - { - Symbol canonicalSymbol; - if (!symbol.SecurityType.HasOptions()) - { - // we got an option - if (symbol.SecurityType.IsOption() && symbol.Underlying != null) - { - // Resolve any mapping before requesting option contract list for equities - // Needs to be done in order for the data file key to be accurate - if (symbol.Underlying.RequiresMapping()) - { - var mappedUnderlyingSymbol = MapUnderlyingSymbol(symbol.Underlying, date, mapFileProvider); - - canonicalSymbol = Symbol.CreateCanonicalOption(mappedUnderlyingSymbol); - } - else - { - canonicalSymbol = symbol.Canonical; - } - } - else - { - throw new NotSupportedException($"QCAlgorithm.GetCanonicalSymbol(): " + - $"{nameof(SecurityType.Equity)}, {nameof(SecurityType.Future)}, or {nameof(SecurityType.Index)} is expected but was {symbol.SecurityType}"); - } - } - else - { - // we got the underlying - var mappedUnderlyingSymbol = MapUnderlyingSymbol(symbol, date, mapFileProvider); - canonicalSymbol = Symbol.CreateCanonicalOption(mappedUnderlyingSymbol); - } - - return canonicalSymbol; - } - - private static Symbol MapUnderlyingSymbol(Symbol underlying, DateTime date, IMapFileProvider mapFileProvider = null) - { - if (underlying.RequiresMapping()) - { - mapFileProvider ??= Composer.Instance.GetPart(); - var mapFileResolver = mapFileProvider.Get(AuxiliaryDataKey.Create(underlying)); - var mapFile = mapFileResolver.ResolveMapFile(underlying); - var ticker = mapFile.GetMappedSymbol(date, underlying.Value); - return underlying.UpdateMappedSymbol(ticker); - } - else - { - return underlying; - } - } - /// /// Extension method to round a datetime to the nearest unit timespan. /// diff --git a/Engine/DataFeeds/BacktestingOptionChainProvider.cs b/Engine/DataFeeds/BacktestingOptionChainProvider.cs index 24da32354b6f..a456f39f34d6 100644 --- a/Engine/DataFeeds/BacktestingOptionChainProvider.cs +++ b/Engine/DataFeeds/BacktestingOptionChainProvider.cs @@ -42,12 +42,64 @@ public BacktestingOptionChainProvider(IDataCacheProvider dataCacheProvider, IMap /// Gets the list of option contracts for a given underlying symbol /// /// The option or the underlying symbol to get the option chain for. - /// Providing the option allows targeting an option ticker different than the default e.g. SPXW + /// Providing the option allows targetting an option ticker different than the default e.g. SPXW /// The date for which to request the option chain (only used in backtesting) /// The list of option contracts public virtual IEnumerable GetOptionContractList(Symbol symbol, DateTime date) { - return GetSymbols(symbol.GetCanonical(date), date); + Symbol canonicalSymbol; + if (!symbol.SecurityType.HasOptions()) + { + // we got an option + if (symbol.SecurityType.IsOption() && symbol.Underlying != null) + { + canonicalSymbol = GetCanonical(symbol, date); + } + else + { + throw new NotSupportedException($"BacktestingOptionChainProvider.GetOptionContractList(): " + + $"{nameof(SecurityType.Equity)}, {nameof(SecurityType.Future)}, or {nameof(SecurityType.Index)} is expected but was {symbol.SecurityType}"); + } + } + else + { + // we got the underlying + var mappedUnderlyingSymbol = MapUnderlyingSymbol(symbol, date); + canonicalSymbol = Symbol.CreateCanonicalOption(mappedUnderlyingSymbol); + } + + return GetSymbols(canonicalSymbol, date); + } + + private Symbol GetCanonical(Symbol optionSymbol, DateTime date) + { + // Resolve any mapping before requesting option contract list for equities + // Needs to be done in order for the data file key to be accurate + if (optionSymbol.Underlying.RequiresMapping()) + { + var mappedUnderlyingSymbol = MapUnderlyingSymbol(optionSymbol.Underlying, date); + + return Symbol.CreateCanonicalOption(mappedUnderlyingSymbol); + } + else + { + return optionSymbol.Canonical; + } + } + + private Symbol MapUnderlyingSymbol(Symbol underlying, DateTime date) + { + if (underlying.RequiresMapping()) + { + var mapFileResolver = _mapFileProvider.Get(AuxiliaryDataKey.Create(underlying)); + var mapFile = mapFileResolver.ResolveMapFile(underlying); + var ticker = mapFile.GetMappedSymbol(date, underlying.Value); + return underlying.UpdateMappedSymbol(ticker); + } + else + { + return underlying; + } } } } diff --git a/Tests/Algorithm/AlgorithmChainsTests.cs b/Tests/Algorithm/AlgorithmChainsTests.cs index 94671fd2a6b8..af02e9aafd9b 100644 --- a/Tests/Algorithm/AlgorithmChainsTests.cs +++ b/Tests/Algorithm/AlgorithmChainsTests.cs @@ -20,6 +20,7 @@ using QuantConnect.Data; using QuantConnect.Interfaces; using QuantConnect.Lean.Engine.DataFeeds; +using QuantConnect.Securities; using QuantConnect.Tests.Engine.DataFeeds; using QuantConnect.Util; @@ -45,16 +46,18 @@ public void OneTimeSetUp() _algorithm.SubscriptionManager.SetDataManager(new DataManagerStub(_algorithm)); _optionChainProvider = new BacktestingOptionChainProvider(TestGlobals.DataCacheProvider, TestGlobals.MapFileProvider); + _algorithm.SetOptionChainProvider(_optionChainProvider); } private static TestCaseData[] OptionChainTestCases = new TestCaseData[] { // By underlying - new(Symbols.AAPL, new DateTime(2014, 06, 06)), - new(Symbols.SPX, new DateTime(2021, 01, 04)), + new(Symbols.AAPL, new DateTime(2014, 06, 06, 12, 0, 0)), + new(Symbols.SPX, new DateTime(2021, 01, 04, 12, 0, 0)), // By canonical - new(Symbol.CreateCanonicalOption(Symbols.AAPL), new DateTime(2014, 06, 06)), - new(Symbol.CreateCanonicalOption(Symbols.SPX), new DateTime(2021, 01, 04)) + new(Symbol.CreateCanonicalOption(Symbols.AAPL), new DateTime(2014, 06, 06, 12, 0, 0)), + new(Symbol.CreateCanonicalOption(Symbols.SPX), new DateTime(2021, 01, 04, 12, 0, 0)), + new(Symbol.CreateFuture(Futures.Indices.SP500EMini, Market.CME, new DateTime(2020, 6, 19)), new DateTime(2020, 01, 05, 12, 0, 0)), }; [TestCaseSource(nameof(OptionChainTestCases))] @@ -62,18 +65,11 @@ public void GetsFullDataOptionChain(Symbol symbol, DateTime date) { _algorithm.SetDateTime(date.ConvertToUtc(_algorithm.TimeZone)); var optionContractsData = _algorithm.OptionChain(symbol).ToList(); + Assert.IsNotEmpty(optionContractsData); - var optionContractsSymbols = _optionChainProvider.GetOptionContractList(symbol, date).ToList(); + var optionContractsSymbols = _optionChainProvider.GetOptionContractList(symbol, date.Date).ToList(); CollectionAssert.AreEquivalent(optionContractsSymbols, optionContractsData.Select(x => x.Symbol)); } - - [Test] - public void CannotGetFutureOptionsChain() - { - var result = _algorithm.OptionChain(Symbols.ES_Future_Chain).ToList(); - Assert.IsEmpty(result); - Assert.IsTrue(_algorithm.LogMessages.Any(x => x.Contains($"Warning: QCAlgorithm.{nameof(QCAlgorithm.OptionChain)} method cannot be used to get future options chains yet. Until support is added, please fall back to the {nameof(QCAlgorithm.OptionChainProvider)}.", StringComparison.InvariantCulture))); - } } } From ff69ba93285d6084aa98157e00d8381741497a01 Mon Sep 17 00:00:00 2001 From: Jhonathan Abreu Date: Tue, 10 Sep 2024 18:09:30 -0400 Subject: [PATCH 04/10] Replace option chain provider with OptionChain method in some regression algorithms --- ...nContractFromUniverseRegressionAlgorithm.cs | 18 +++++++++--------- ...onBuySellCallIntradayRegressionAlgorithm.cs | 8 ++++---- ...reOptionCallOTMExpiryRegressionAlgorithm.cs | 9 +++++---- ...ionShortCallITMExpiryRegressionAlgorithm.cs | 9 +++++---- ...tionShortPutITMExpiryRegressionAlgorithm.cs | 9 +++++---- ...nContractFromUniverseRegressionAlgorithm.py | 6 +++--- ...onBuySellCallIntradayRegressionAlgorithm.py | 7 +++---- ...reOptionCallOTMExpiryRegressionAlgorithm.py | 3 ++- ...ionShortCallITMExpiryRegressionAlgorithm.py | 4 ++-- ...tionShortPutITMExpiryRegressionAlgorithm.py | 6 ++++-- 10 files changed, 42 insertions(+), 37 deletions(-) diff --git a/Algorithm.CSharp/AddOptionContractFromUniverseRegressionAlgorithm.cs b/Algorithm.CSharp/AddOptionContractFromUniverseRegressionAlgorithm.cs index fa6cdb6fc3cf..ca342c973db6 100644 --- a/Algorithm.CSharp/AddOptionContractFromUniverseRegressionAlgorithm.cs +++ b/Algorithm.CSharp/AddOptionContractFromUniverseRegressionAlgorithm.cs @@ -13,12 +13,12 @@ * limitations under the License. */ -using System; -using System.Collections.Generic; -using System.Linq; using QuantConnect.Data; using QuantConnect.Data.UniverseSelection; using QuantConnect.Interfaces; +using System; +using System.Collections.Generic; +using System.Linq; namespace QuantConnect.Algorithm.CSharp { @@ -110,14 +110,14 @@ public override void OnSecuritiesChanged(SecurityChanges changes) foreach (var addedSecurity in changes.AddedSecurities) { - var option = OptionChainProvider.GetOptionContractList(addedSecurity.Symbol, Time) - .OrderBy(symbol => symbol.ID.Symbol) - .First(optionContract => optionContract.ID.Date == _expiration - && optionContract.ID.OptionRight == OptionRight.Call - && optionContract.ID.OptionStyle == OptionStyle.American); + var option = OptionChain(addedSecurity.Symbol) + .OrderBy(contractData => contractData.Symbol.ID.Symbol) + .First(optionContract => optionContract.Symbol.ID.Date == _expiration + && optionContract.Symbol.ID.OptionRight == OptionRight.Call + && optionContract.Symbol.ID.OptionStyle == OptionStyle.American); AddOptionContract(option); - foreach (var symbol in new[] { option, option.Underlying }) + foreach (var symbol in new[] { option.Symbol, option.Underlying.Symbol }) { var config = SubscriptionManager.SubscriptionDataConfigService.GetSubscriptionDataConfigs(symbol).ToList(); diff --git a/Algorithm.CSharp/FutureOptionBuySellCallIntradayRegressionAlgorithm.cs b/Algorithm.CSharp/FutureOptionBuySellCallIntradayRegressionAlgorithm.cs index ec9de2233f86..42a5a13e8806 100644 --- a/Algorithm.CSharp/FutureOptionBuySellCallIntradayRegressionAlgorithm.cs +++ b/Algorithm.CSharp/FutureOptionBuySellCallIntradayRegressionAlgorithm.cs @@ -58,10 +58,10 @@ public override void Initialize() Resolution.Minute).Symbol; // Select a future option expiring ITM, and adds it to the algorithm. - var esOptions = OptionChainProvider.GetOptionContractList(es20m20, Time) - .Concat(OptionChainProvider.GetOptionContractList(es20h20, Time)) - .Where(x => x.ID.StrikePrice == 3200m && x.ID.OptionRight == OptionRight.Call) - .Select(x => AddFutureOptionContract(x, Resolution.Minute).Symbol) + var esOptions = OptionChain(es20m20) + .Concat(OptionChain(es20h20)) + .Where(contractData => contractData.Symbol.ID.StrikePrice == 3200m && contractData.Symbol.ID.OptionRight == OptionRight.Call) + .Select(contractData => AddFutureOptionContract(contractData.Symbol, Resolution.Minute).Symbol) .ToList(); var expectedContracts = new[] diff --git a/Algorithm.CSharp/FutureOptionCallOTMExpiryRegressionAlgorithm.cs b/Algorithm.CSharp/FutureOptionCallOTMExpiryRegressionAlgorithm.cs index 53a192780aaf..d212d808c4df 100644 --- a/Algorithm.CSharp/FutureOptionCallOTMExpiryRegressionAlgorithm.cs +++ b/Algorithm.CSharp/FutureOptionCallOTMExpiryRegressionAlgorithm.cs @@ -59,11 +59,12 @@ public override void Initialize() Resolution.Minute).Symbol; // Select a future option call expiring OTM, and adds it to the algorithm. - _esOption = AddFutureOptionContract(OptionChainProvider.GetOptionContractList(_es19m20, Time) - .Where(x => x.ID.StrikePrice >= 3300m && x.ID.OptionRight == OptionRight.Call) - .OrderBy(x => x.ID.StrikePrice) + _esOption = AddFutureOptionContract(OptionChain(_es19m20) + .Where(contractData => contractData.Symbol.ID.StrikePrice >= 3300m && contractData.Symbol.ID.OptionRight == OptionRight.Call) + .OrderBy(contractData => contractData.Symbol.ID.StrikePrice) .Take(1) - .Single(), Resolution.Minute).Symbol; + .Single() + .Symbol, Resolution.Minute).Symbol; _expectedContract = QuantConnect.Symbol.CreateOption(_es19m20, Market.CME, OptionStyle.American, OptionRight.Call, 3300m, new DateTime(2020, 6, 19)); if (_esOption != _expectedContract) diff --git a/Algorithm.CSharp/IndexOptionShortCallITMExpiryRegressionAlgorithm.cs b/Algorithm.CSharp/IndexOptionShortCallITMExpiryRegressionAlgorithm.cs index 702c96164e2e..1e7f7d8fb8c9 100644 --- a/Algorithm.CSharp/IndexOptionShortCallITMExpiryRegressionAlgorithm.cs +++ b/Algorithm.CSharp/IndexOptionShortCallITMExpiryRegressionAlgorithm.cs @@ -60,11 +60,12 @@ public override void Initialize() _spx = AddIndex("SPX", Resolution.Minute).Symbol; // Select a index option expiring ITM, and adds it to the algorithm. - _esOption = AddIndexOptionContract(OptionChainProvider.GetOptionContractList(_spx, Time) - .Where(x => x.ID.StrikePrice <= 3200m && x.ID.OptionRight == OptionRight.Call && x.ID.Date.Year == 2021 && x.ID.Date.Month == 1) - .OrderByDescending(x => x.ID.StrikePrice) + _esOption = AddIndexOptionContract(OptionChain(_spx) + .Where(contractData => contractData.Symbol.ID.StrikePrice <= 3200m && contractData.Symbol.ID.OptionRight == OptionRight.Call && contractData.Symbol.ID.Date.Year == 2021 && contractData.Symbol.ID.Date.Month == 1) + .OrderByDescending(contractData => contractData.Symbol.ID.StrikePrice) .Take(1) - .Single(), Resolution.Minute).Symbol; + .Single() + .Symbol, Resolution.Minute).Symbol; _expectedContract = QuantConnect.Symbol.CreateOption(_spx, Market.USA, OptionStyle.European, OptionRight.Call, 3200m, new DateTime(2021, 1, 15)); if (_esOption != _expectedContract) diff --git a/Algorithm.CSharp/IndexOptionShortPutITMExpiryRegressionAlgorithm.cs b/Algorithm.CSharp/IndexOptionShortPutITMExpiryRegressionAlgorithm.cs index bde4f77e7e34..08576db310e8 100644 --- a/Algorithm.CSharp/IndexOptionShortPutITMExpiryRegressionAlgorithm.cs +++ b/Algorithm.CSharp/IndexOptionShortPutITMExpiryRegressionAlgorithm.cs @@ -59,11 +59,12 @@ public override void Initialize() _spx = AddIndex("SPX", Resolution.Minute).Symbol; // Select a index option expiring ITM, and adds it to the algorithm. - _spxOption = AddIndexOptionContract(OptionChainProvider.GetOptionContractList(_spx, Time) - .Where(x => x.ID.StrikePrice <= 4200m && x.ID.OptionRight == OptionRight.Put && x.ID.Date.Year == 2021 && x.ID.Date.Month == 1) - .OrderByDescending(x => x.ID.StrikePrice) + _spxOption = AddIndexOptionContract(OptionChain(_spx) + .Where(contractData => contractData.Symbol.ID.StrikePrice <= 4200m && contractData.Symbol.ID.OptionRight == OptionRight.Put && contractData.Symbol.ID.Date.Year == 2021 && contractData.Symbol.ID.Date.Month == 1) + .OrderByDescending(contractData => contractData.Symbol.ID.StrikePrice) .Take(1) - .Single(), Resolution.Minute).Symbol; + .Single() + .Symbol, Resolution.Minute).Symbol; _expectedContract = QuantConnect.Symbol.CreateOption(_spx, Market.USA, OptionStyle.European, OptionRight.Put, 4200m, new DateTime(2021, 1, 15)); if (_spxOption != _expectedContract) diff --git a/Algorithm.Python/AddOptionContractFromUniverseRegressionAlgorithm.py b/Algorithm.Python/AddOptionContractFromUniverseRegressionAlgorithm.py index 9ae4482ddfae..d8b69a69b27c 100644 --- a/Algorithm.Python/AddOptionContractFromUniverseRegressionAlgorithm.py +++ b/Algorithm.Python/AddOptionContractFromUniverseRegressionAlgorithm.py @@ -67,8 +67,8 @@ def on_securities_changed(self, changes): return for addedSecurity in changes.added_securities: - options = self.option_chain_provider.get_option_contract_list(addedSecurity.symbol, self.time) - options = sorted(options, key=lambda x: x.id.symbol) + options = self.option_chain(addedSecurity.symbol) + options = sorted(options, key=lambda x: x.symbol.id.symbol) option = next((option for option in options @@ -76,7 +76,7 @@ def on_securities_changed(self, changes): option.id.option_right == OptionRight.CALL and option.id.option_style == OptionStyle.AMERICAN), None) - self.add_option_contract(option) + self.add_option_contract(option.symbol) # just keep the first we got if self._option == None: diff --git a/Algorithm.Python/FutureOptionBuySellCallIntradayRegressionAlgorithm.py b/Algorithm.Python/FutureOptionBuySellCallIntradayRegressionAlgorithm.py index 37e47c3dc427..4256568cd1d9 100644 --- a/Algorithm.Python/FutureOptionBuySellCallIntradayRegressionAlgorithm.py +++ b/Algorithm.Python/FutureOptionBuySellCallIntradayRegressionAlgorithm.py @@ -51,10 +51,9 @@ def initialize(self): # Select a future option expiring ITM, and adds it to the algorithm. self.es_options = [ - self.add_future_option_contract(i, Resolution.MINUTE).symbol - for i in (self.option_chain_provider.get_option_contract_list(self.es19m20, self.time) + - self.option_chain_provider.get_option_contract_list(self.es20h20, self.time)) - if i.id.strike_price == 3200.0 and i.id.option_right == OptionRight.CALL + self.add_future_option_contract(i.symbol, Resolution.MINUTE).symbol + for i in (list(self.option_chain(self.es19m20)) + list(self.option_chain(self.es20h20))) + if i.symbol.id.strike_price == 3200.0 and i.symbol.id.option_right == OptionRight.CALL ] self.expected_contracts = [ diff --git a/Algorithm.Python/FutureOptionCallOTMExpiryRegressionAlgorithm.py b/Algorithm.Python/FutureOptionCallOTMExpiryRegressionAlgorithm.py index 548ffe36f655..87626a681ea0 100644 --- a/Algorithm.Python/FutureOptionCallOTMExpiryRegressionAlgorithm.py +++ b/Algorithm.Python/FutureOptionCallOTMExpiryRegressionAlgorithm.py @@ -45,7 +45,8 @@ def initialize(self): self.es_option = self.add_future_option_contract( list( sorted( - [x for x in self.option_chain_provider.get_option_contract_list(self.es19m20, self.time) if x.id.strike_price >= 3300.0 and x.id.option_right == OptionRight.CALL], + [x.symbol for x in self.option_chain(self.es19m20) + if x.symbol.id.strike_price >= 3300.0 and x.symbol.id.option_right == OptionRight.CALL], key=lambda x: x.id.strike_price ) )[0], Resolution.MINUTE).symbol diff --git a/Algorithm.Python/IndexOptionShortCallITMExpiryRegressionAlgorithm.py b/Algorithm.Python/IndexOptionShortCallITMExpiryRegressionAlgorithm.py index 975311ac67fe..c842382fbce7 100644 --- a/Algorithm.Python/IndexOptionShortCallITMExpiryRegressionAlgorithm.py +++ b/Algorithm.Python/IndexOptionShortCallITMExpiryRegressionAlgorithm.py @@ -38,8 +38,8 @@ def initialize(self): self.spx = self.add_index("SPX", Resolution.MINUTE).symbol # Select a index option expiring ITM, and adds it to the algorithm. - self.spx_option = list(self.option_chain_provider.get_option_contract_list(self.spx, self.time)) - self.spx_option = [i for i in self.spx_option if i.id.strike_price <= 3200 and i.id.option_right == OptionRight.CALL and i.id.date.year == 2021 and i.id.date.month == 1] + self.spx_option = list(self.option_chain(self.spx)) + self.spx_option = [i.symbol for i in self.spx_option if i.symbol.id.strike_price <= 3200 and i.symbol.id.option_right == OptionRight.CALL and i.symbol.id.date.year == 2021 and i.symbol.id.date.month == 1] self.spx_option = list(sorted(self.spx_option, key=lambda x: x.id.strike_price, reverse=True))[0] self.spx_option = self.add_index_option_contract(self.spx_option, Resolution.MINUTE).symbol diff --git a/Algorithm.Python/IndexOptionShortPutITMExpiryRegressionAlgorithm.py b/Algorithm.Python/IndexOptionShortPutITMExpiryRegressionAlgorithm.py index 04e672786542..33e0a86a7ded 100644 --- a/Algorithm.Python/IndexOptionShortPutITMExpiryRegressionAlgorithm.py +++ b/Algorithm.Python/IndexOptionShortPutITMExpiryRegressionAlgorithm.py @@ -38,8 +38,10 @@ def initialize(self): self.spx = self.add_index("SPX", Resolution.MINUTE).symbol # Select a index option expiring ITM, and adds it to the algorithm. - self.spx_option = list(self.option_chain_provider.get_option_contract_list(self.spx, self.time)) - self.spx_option = [i for i in self.spx_option if i.id.strike_price <= 4200 and i.id.option_right == OptionRight.PUT and i.id.date.year == 2021 and i.id.date.month == 1] + self.spx_option = list(self.option_chain(self.spx)) + self.spx_option = [i.symbol + for i in self.spx_option + if i.symbol.id.strike_price <= 4200 and i.symbol.id.option_right == OptionRight.PUT and i.symbol.id.date.year == 2021 and i.symbol.id.date.month == 1] self.spx_option = list(sorted(self.spx_option, key=lambda x: x.id.strike_price, reverse=True))[0] self.spx_option = self.add_index_option_contract(self.spx_option, Resolution.MINUTE).symbol From f440da7f1417cf527e66bb81c2a53ae1cfb4e43b Mon Sep 17 00:00:00 2001 From: Jhonathan Abreu Date: Tue, 10 Sep 2024 18:33:51 -0400 Subject: [PATCH 05/10] Add new regression algorithms for OptionChain method --- .../OptionChainFullDataRegressionAlgorithm.cs | 127 ++++++++++++++++++ .../OptionChainFullDataRegressionAlgorithm.py | 45 +++++++ 2 files changed, 172 insertions(+) create mode 100644 Algorithm.CSharp/OptionChainFullDataRegressionAlgorithm.cs create mode 100644 Algorithm.Python/OptionChainFullDataRegressionAlgorithm.py diff --git a/Algorithm.CSharp/OptionChainFullDataRegressionAlgorithm.cs b/Algorithm.CSharp/OptionChainFullDataRegressionAlgorithm.cs new file mode 100644 index 000000000000..4ec5a0cd7c0e --- /dev/null +++ b/Algorithm.CSharp/OptionChainFullDataRegressionAlgorithm.cs @@ -0,0 +1,127 @@ +/* + * 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 System.Collections.Generic; +using System.Linq; +using QuantConnect.Data; +using QuantConnect.Interfaces; +using QuantConnect.Securities; + +namespace QuantConnect.Algorithm.CSharp +{ + /// + /// Regression algorithm illustrating the usage of the method + /// to get an option chain, which contains additional data besides the symbols, including prices, implied volatility and greeks. + /// It also shows how this data can be used to filter the contracts based on certain criteria. + /// + public class OptionChainFullDataRegressionAlgorithm : QCAlgorithm, IRegressionAlgorithmDefinition + { + private Symbol _optionContract; + + public override void Initialize() + { + SetStartDate(2015, 12, 24); + SetEndDate(2015, 12, 24); + SetCash(100000); + + var goog = AddEquity("GOOG").Symbol; + + _optionContract = OptionChain(goog) + // Get contracts expiring within 10 days, with an implied volatility greater than 0.5 and a delta less than 0.5 + .Where(contractData => contractData.Symbol.ID.Date - Time <= TimeSpan.FromDays(10) && + contractData.ImpliedVolatility > 0.5m && + contractData.Greeks.Delta < 0.5m) + // Get the contract with the latest expiration date + .OrderByDescending(x => x.ID.Date) + .First(); + + AddOptionContract(_optionContract); + } + + public override void OnData(Slice slice) + { + // Do some trading with the selected contract for sample purposes + if (!Portfolio.Invested) + { + MarketOrder(_optionContract, 1); + } + else + { + Liquidate(); + } + } + + /// + /// This is used by the regression test system to indicate if the open source Lean repository has the required data to run this algorithm. + /// + public bool CanRunLocally { get; } = true; + + /// + /// This is used by the regression test system to indicate which languages this algorithm is written in. + /// + public virtual List Languages { get; } = new() { Language.CSharp, Language.Python }; + + /// + /// Data Points count of all timeslices of algorithm + /// + public long DataPoints => 1057; + + /// + /// Data Points count of the algorithm history + /// + public int AlgorithmHistoryDataPoints => 1; + + /// + /// Final status of the algorithm + /// + public AlgorithmStatus AlgorithmStatus => AlgorithmStatus.Completed; + + /// + /// This is used by the regression test system to indicate what the expected statistics are from running the algorithm + /// + public Dictionary ExpectedStatistics => new Dictionary + { + {"Total Orders", "210"}, + {"Average Win", "0%"}, + {"Average Loss", "0%"}, + {"Compounding Annual Return", "0%"}, + {"Drawdown", "0%"}, + {"Expectancy", "0"}, + {"Start Equity", "100000"}, + {"End Equity", "96041"}, + {"Net Profit", "0%"}, + {"Sharpe Ratio", "0"}, + {"Sortino Ratio", "0"}, + {"Probabilistic Sharpe Ratio", "0%"}, + {"Loss Rate", "0%"}, + {"Win Rate", "0%"}, + {"Profit-Loss Ratio", "0"}, + {"Alpha", "0"}, + {"Beta", "0"}, + {"Annual Standard Deviation", "0"}, + {"Annual Variance", "0"}, + {"Information Ratio", "0"}, + {"Tracking Error", "0"}, + {"Treynor Ratio", "0"}, + {"Total Fees", "$209.00"}, + {"Estimated Strategy Capacity", "$0"}, + {"Lowest Capacity Asset", "GOOCV W6U7PD1F2WYU|GOOCV VP83T1ZUHROL"}, + {"Portfolio Turnover", "85.46%"}, + {"OrderListHash", "a7ab1a9e64fe9ba76ea33a40a78a4e3b"} + }; + } +} diff --git a/Algorithm.Python/OptionChainFullDataRegressionAlgorithm.py b/Algorithm.Python/OptionChainFullDataRegressionAlgorithm.py new file mode 100644 index 000000000000..5abde4ee1137 --- /dev/null +++ b/Algorithm.Python/OptionChainFullDataRegressionAlgorithm.py @@ -0,0 +1,45 @@ +# 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. + +from AlgorithmImports import * + +### +### Regression algorithm illustrating the usage of the method +### to get an option chain, which contains additional data besides the symbols, including prices, implied volatility and greeks. +### It also shows how this data can be used to filter the contracts based on certain criteria. +### +class OptionChainFullDataRegressionAlgorithm(QCAlgorithm): + + def initialize(self): + self.set_start_date(2015, 12, 24) + self.set_end_date(2015, 12, 24) + self.set_cash(100000) + + goog = self.add_equity("GOOG").symbol + + # Get contracts expiring within 10 days, with an implied volatility greater than 0.5 and a delta less than 0.5 + contracts = [ + contract_data.symbol + for contract_data in self.option_chain(goog) + if contract_data.symbol.id.date - self.time <= timedelta(days=10) and contract_data.implied_volatility > 0.5 and contract_data.greeks.delta < 0.5] + # Get the contract with the latest expiration date + self._option_contract = sorted(contracts, key=lambda x: x.id.date, reverse=True)[0] + + self.add_option_contract(self._option_contract) + + def on_data(self, data): + # Do some trading with the selected contract for sample purposes + if not self.portfolio.invested: + self.market_order(self._option_contract, 1) + else: + self.liquidate() From db3250b2a5ad9589cafcc2ca60b9a2d92cb7963b Mon Sep 17 00:00:00 2001 From: Jhonathan Abreu Date: Wed, 11 Sep 2024 09:48:14 -0400 Subject: [PATCH 06/10] Replace option chain provider with OptionChain method in some regression algorithms --- ...RemoveOptionContractRegressionAlgorithm.cs | 8 ++++---- ...moveSecuritySameLoopRegressionAlgorithm.cs | 8 ++++---- ...tractFromFutureChainRegressionAlgorithm.cs | 2 +- ...ptionContractExpiresRegressionAlgorithm.cs | 10 +++++----- ...dOptionContractTwiceRegressionAlgorithm.cs | 9 +++++---- ...oveOneOptionContractRegressionAlgorithm.cs | 9 +++++---- ...mentAfterManualSecurityRemovalAlgorithm.cs | 9 +++++---- .../DelistedIndexOptionDivestedRegression.cs | 6 ++---- ...eOptionCallITMExpiryRegressionAlgorithm.cs | 9 +++++---- ...nCallITMGreeksExpiryRegressionAlgorithm.cs | 9 +++++---- .../FutureOptionDailyRegressionAlgorithm.cs | 6 +++--- ...tureOptionIndicatorsRegressionAlgorithm.cs | 9 +++++---- ...reOptionPutITMExpiryRegressionAlgorithm.cs | 9 +++++---- ...reOptionPutOTMExpiryRegressionAlgorithm.cs | 9 +++++---- ...onShortCallITMExpiryRegressionAlgorithm.cs | 9 +++++---- ...onShortCallOTMExpiryRegressionAlgorithm.cs | 9 +++++---- ...ionShortPutITMExpiryRegressionAlgorithm.cs | 9 +++++---- ...ionShortPutOTMExpiryRegressionAlgorithm.cs | 9 +++++---- ...nBuySellCallIntradayRegressionAlgorithm.cs | 6 +++--- ...xOptionCallITMExpiryRegressionAlgorithm.cs | 9 +++++---- ...nCallITMGreeksExpiryRegressionAlgorithm.cs | 9 +++++---- ...xOptionCallOTMExpiryRegressionAlgorithm.cs | 9 +++++---- ...exOptionPutITMExpiryRegressionAlgorithm.cs | 9 +++++---- ...exOptionPutOTMExpiryRegressionAlgorithm.cs | 9 +++++---- ...onShortCallOTMExpiryRegressionAlgorithm.cs | 9 +++++---- ...ionShortPutOTMExpiryRegressionAlgorithm.cs | 9 +++++---- ...ForAutomaticExerciseRegressionAlgorithm.cs | 2 +- .../OptionAssignmentRegressionAlgorithm.cs | 2 +- ...AssignmentStatisticsRegressionAlgorithm.cs | 2 +- ...iryOrderHasZeroPriceRegressionAlgorithm.cs | 9 +++++---- ...ptionSymbolCanonicalRegressionAlgorithm.cs | 2 +- .../OptionTimeSliceRegressionAlgorithm.cs | 11 +++++----- ...lectionSymbolCacheRemovalRegressionTest.cs | 4 ++-- ...ptionContractExpiresRegressionAlgorithm.py | 12 +++++------ ...eOptionCallITMExpiryRegressionAlgorithm.py | 5 ++++- .../FutureOptionDailyRegressionAlgorithm.py | 4 +++- ...reOptionPutITMExpiryRegressionAlgorithm.py | 5 ++++- ...reOptionPutOTMExpiryRegressionAlgorithm.py | 4 ++-- ...onShortCallITMExpiryRegressionAlgorithm.py | 2 +- ...onShortCallOTMExpiryRegressionAlgorithm.py | 2 +- ...ionShortPutITMExpiryRegressionAlgorithm.py | 2 +- ...ionShortPutOTMExpiryRegressionAlgorithm.py | 2 +- ...nBuySellCallIntradayRegressionAlgorithm.py | 8 ++++---- ...xOptionCallITMExpiryRegressionAlgorithm.py | 12 ++++++----- ...nCallITMGreeksExpiryRegressionAlgorithm.py | 8 +++++--- ...xOptionCallOTMExpiryRegressionAlgorithm.py | 18 ++++++++--------- ...exOptionPutITMExpiryRegressionAlgorithm.py | 6 ++++-- ...exOptionPutOTMExpiryRegressionAlgorithm.py | 20 ++++++++++--------- ...onShortCallOTMExpiryRegressionAlgorithm.py | 6 ++++-- ...ionShortPutOTMExpiryRegressionAlgorithm.py | 6 ++++-- 50 files changed, 205 insertions(+), 166 deletions(-) diff --git a/Algorithm.CSharp/AddAndRemoveOptionContractRegressionAlgorithm.cs b/Algorithm.CSharp/AddAndRemoveOptionContractRegressionAlgorithm.cs index 0a8cce770c88..85073972a3c2 100644 --- a/Algorithm.CSharp/AddAndRemoveOptionContractRegressionAlgorithm.cs +++ b/Algorithm.CSharp/AddAndRemoveOptionContractRegressionAlgorithm.cs @@ -40,10 +40,10 @@ public override void Initialize() var aapl = QuantConnect.Symbol.Create("AAPL", SecurityType.Equity, Market.USA); - _contract = OptionChainProvider.GetOptionContractList(aapl, Time) - .OrderBy(symbol => symbol.ID.Symbol) - .FirstOrDefault(optionContract => optionContract.ID.OptionRight == OptionRight.Call - && optionContract.ID.OptionStyle == OptionStyle.American); + _contract = OptionChain(aapl) + .OrderBy(x => x.Symbol.ID.Symbol) + .FirstOrDefault(optionContract => optionContract.Symbol.ID.OptionRight == OptionRight.Call + && optionContract.Symbol.ID.OptionStyle == OptionStyle.American)?.Symbol; AddOptionContract(_contract); } diff --git a/Algorithm.CSharp/AddAndRemoveSecuritySameLoopRegressionAlgorithm.cs b/Algorithm.CSharp/AddAndRemoveSecuritySameLoopRegressionAlgorithm.cs index bcb0030789f8..afa1a1066a5e 100644 --- a/Algorithm.CSharp/AddAndRemoveSecuritySameLoopRegressionAlgorithm.cs +++ b/Algorithm.CSharp/AddAndRemoveSecuritySameLoopRegressionAlgorithm.cs @@ -39,10 +39,10 @@ public override void Initialize() var aapl = AddEquity("AAPL").Symbol; - _contract = OptionChainProvider.GetOptionContractList(aapl, Time) - .OrderBy(symbol => symbol.ID.Symbol) - .FirstOrDefault(optionContract => optionContract.ID.OptionRight == OptionRight.Call - && optionContract.ID.OptionStyle == OptionStyle.American); + _contract = OptionChain(aapl) + .OrderBy(x => x.Symbol.ID.Symbol) + .FirstOrDefault(optionContract => optionContract.Symbol.ID.OptionRight == OptionRight.Call + && optionContract.Symbol.ID.OptionStyle == OptionStyle.American); } public override void OnData(Slice slice) diff --git a/Algorithm.CSharp/AddFutureOptionContractFromFutureChainRegressionAlgorithm.cs b/Algorithm.CSharp/AddFutureOptionContractFromFutureChainRegressionAlgorithm.cs index f735b4f83384..57b3b03f728a 100644 --- a/Algorithm.CSharp/AddFutureOptionContractFromFutureChainRegressionAlgorithm.cs +++ b/Algorithm.CSharp/AddFutureOptionContractFromFutureChainRegressionAlgorithm.cs @@ -49,7 +49,7 @@ public override void OnData(Slice slice) { foreach (var contract in futuresContracts) { - var option_contract_symbols = OptionChainProvider.GetOptionContractList(contract.Symbol, Time).ToList(); + var option_contract_symbols = OptionChain(contract.Symbol).Select(x => x.Symbol).ToList(); if(option_contract_symbols.Count == 0) { continue; diff --git a/Algorithm.CSharp/AddOptionContractExpiresRegressionAlgorithm.cs b/Algorithm.CSharp/AddOptionContractExpiresRegressionAlgorithm.cs index f772559aa43d..ab7f6d6d53bc 100644 --- a/Algorithm.CSharp/AddOptionContractExpiresRegressionAlgorithm.cs +++ b/Algorithm.CSharp/AddOptionContractExpiresRegressionAlgorithm.cs @@ -46,11 +46,11 @@ public override void OnData(Slice slice) { if (_option == null) { - var option = OptionChainProvider.GetOptionContractList(_twx, Time) - .OrderBy(symbol => symbol.ID.Symbol) - .FirstOrDefault(optionContract => optionContract.ID.Date == _expiration - && optionContract.ID.OptionRight == OptionRight.Call - && optionContract.ID.OptionStyle == OptionStyle.American); + var option = OptionChain(_twx) + .OrderBy(x => x.Symbol.ID.Symbol) + .FirstOrDefault(optionContract => optionContract.Symbol.ID.Date == _expiration + && optionContract.Symbol.ID.OptionRight == OptionRight.Call + && optionContract.Symbol.ID.OptionStyle == OptionStyle.American)?.Symbol; if (option != null) { _option = AddOptionContract(option).Symbol; diff --git a/Algorithm.CSharp/AddOptionContractTwiceRegressionAlgorithm.cs b/Algorithm.CSharp/AddOptionContractTwiceRegressionAlgorithm.cs index 3b2e387a0431..ecf3c31f32ca 100644 --- a/Algorithm.CSharp/AddOptionContractTwiceRegressionAlgorithm.cs +++ b/Algorithm.CSharp/AddOptionContractTwiceRegressionAlgorithm.cs @@ -43,10 +43,11 @@ public override void Initialize() var aapl = QuantConnect.Symbol.Create("AAPL", SecurityType.Equity, Market.USA); - _contract = OptionChainProvider.GetOptionContractList(aapl, Time) - .OrderBy(symbol => symbol.ID.StrikePrice) - .FirstOrDefault(optionContract => optionContract.ID.OptionRight == OptionRight.Call - && optionContract.ID.OptionStyle == OptionStyle.American); + _contract = OptionChain(aapl) + .OrderBy(x => x.Symbol.ID.StrikePrice) + .FirstOrDefault(optionContract => optionContract.Symbol.ID.OptionRight == OptionRight.Call + && optionContract.Symbol.ID.OptionStyle == OptionStyle.American) + .Symbol; AddOptionContract(_contract); } diff --git a/Algorithm.CSharp/AddTwoAndRemoveOneOptionContractRegressionAlgorithm.cs b/Algorithm.CSharp/AddTwoAndRemoveOneOptionContractRegressionAlgorithm.cs index 2ac31c437381..237b90c15bd0 100644 --- a/Algorithm.CSharp/AddTwoAndRemoveOneOptionContractRegressionAlgorithm.cs +++ b/Algorithm.CSharp/AddTwoAndRemoveOneOptionContractRegressionAlgorithm.cs @@ -41,11 +41,12 @@ public override void Initialize() var aapl = QuantConnect.Symbol.Create("AAPL", SecurityType.Equity, Market.USA); - var contracts = OptionChainProvider.GetOptionContractList(aapl, Time) - .OrderBy(symbol => symbol.ID.StrikePrice) - .Where(optionContract => optionContract.ID.OptionRight == OptionRight.Call - && optionContract.ID.OptionStyle == OptionStyle.American) + var contracts = OptionChain(aapl) + .OrderBy(x => x.Symbol.ID.StrikePrice) + .Where(optionContract => optionContract.Symbol.ID.OptionRight == OptionRight.Call + && optionContract.Symbol.ID.OptionStyle == OptionStyle.American) .Take(2) + .Select(x => x.Symbol) .ToList(); _contract1 = contracts[0]; diff --git a/Algorithm.CSharp/DelayedSettlementAfterManualSecurityRemovalAlgorithm.cs b/Algorithm.CSharp/DelayedSettlementAfterManualSecurityRemovalAlgorithm.cs index 18e1a78e4405..fd27d8c14e13 100644 --- a/Algorithm.CSharp/DelayedSettlementAfterManualSecurityRemovalAlgorithm.cs +++ b/Algorithm.CSharp/DelayedSettlementAfterManualSecurityRemovalAlgorithm.cs @@ -37,10 +37,11 @@ public override void Initialize() var equity = AddEquity("GOOG"); - _optionSymbol = OptionChainProvider.GetOptionContractList(equity.Symbol, Time) - .OrderBy(symbol => symbol.ID.StrikePrice) - .ThenByDescending(symbol => symbol.ID.Date) - .First(optionContract => optionContract.ID.OptionRight == OptionRight.Call); + _optionSymbol = OptionChain(equity.Symbol) + .OrderBy(x => x.Symbol.ID.StrikePrice) + .ThenByDescending(x => x.Symbol.ID.Date) + .First(optionContract => optionContract.Symbol.ID.OptionRight == OptionRight.Call) + .Symbol; var option = AddOptionContract(_optionSymbol); option.SetSettlementModel(new DelayedSettlementModel(Option.DefaultSettlementDays, Option.DefaultSettlementTime)); diff --git a/Algorithm.CSharp/DelistedIndexOptionDivestedRegression.cs b/Algorithm.CSharp/DelistedIndexOptionDivestedRegression.cs index 9665b5f47a4f..61556acfe751 100644 --- a/Algorithm.CSharp/DelistedIndexOptionDivestedRegression.cs +++ b/Algorithm.CSharp/DelistedIndexOptionDivestedRegression.cs @@ -51,10 +51,8 @@ public override void OnData(Slice slice) if (_addOption) { - var contracts = OptionChainProvider.GetOptionContractList(_spx, Time); - contracts = contracts.Where(x => - x.ID.OptionRight == OptionRight.Put && - x.ID.Date.Date == new DateTime(2021, 1, 15)); + var contracts = OptionChain(_spx) + .Where(x => x.Symbol.ID.OptionRight == OptionRight.Put && x.Symbol.ID.Date.Date == new DateTime(2021, 1, 15)); var option = AddIndexOptionContract(contracts.First(), Resolution.Minute); _optionExpiry = option.Expiry; diff --git a/Algorithm.CSharp/FutureOptionCallITMExpiryRegressionAlgorithm.cs b/Algorithm.CSharp/FutureOptionCallITMExpiryRegressionAlgorithm.cs index a511f150aad3..7ba533fb4ba1 100644 --- a/Algorithm.CSharp/FutureOptionCallITMExpiryRegressionAlgorithm.cs +++ b/Algorithm.CSharp/FutureOptionCallITMExpiryRegressionAlgorithm.cs @@ -53,11 +53,12 @@ public override void Initialize() Resolution.Minute).Symbol; // Select a future option expiring ITM, and adds it to the algorithm. - _esOption = AddFutureOptionContract(OptionChainProvider.GetOptionContractList(_es19m20, Time) - .Where(x => x.ID.StrikePrice <= 3200m && x.ID.OptionRight == OptionRight.Call) - .OrderByDescending(x => x.ID.StrikePrice) + _esOption = AddFutureOptionContract(OptionChain(_es19m20) + .Where(x => x.Symbol.ID.StrikePrice <= 3200m && x.Symbol.ID.OptionRight == OptionRight.Call) + .OrderByDescending(x => x.Symbol.ID.StrikePrice) .Take(1) - .Single(), Resolution.Minute).Symbol; + .Single() + .Symbol, Resolution.Minute).Symbol; _expectedOptionContract = QuantConnect.Symbol.CreateOption(_es19m20, Market.CME, OptionStyle.American, OptionRight.Call, 3200m, new DateTime(2020, 6, 19)); if (_esOption != _expectedOptionContract) diff --git a/Algorithm.CSharp/FutureOptionCallITMGreeksExpiryRegressionAlgorithm.cs b/Algorithm.CSharp/FutureOptionCallITMGreeksExpiryRegressionAlgorithm.cs index d77a6d33e1a2..de2ba17ed5b5 100644 --- a/Algorithm.CSharp/FutureOptionCallITMGreeksExpiryRegressionAlgorithm.cs +++ b/Algorithm.CSharp/FutureOptionCallITMGreeksExpiryRegressionAlgorithm.cs @@ -55,11 +55,12 @@ public override void Initialize() TimeSpan.FromMinutes(1)); // Select a future option expiring ITM, and adds it to the algorithm. - _esOption = AddFutureOptionContract(OptionChainProvider.GetOptionContractList(_es19m20.Symbol, new DateTime(2020, 1, 5)) - .Where(x => x.ID.StrikePrice <= 3200m && x.ID.OptionRight == OptionRight.Call) - .OrderByDescending(x => x.ID.StrikePrice) + _esOption = AddFutureOptionContract(OptionChain(_es19m20.Symbol) + .Where(x => x.Symbol.ID.StrikePrice <= 3200m && x.Symbol.ID.OptionRight == OptionRight.Call) + .OrderByDescending(x => x.Symbol.ID.StrikePrice) .Take(1) - .Single(), Resolution.Minute); + .Single() + .Symbol, Resolution.Minute); _esOption.PriceModel = OptionPriceModels.BjerksundStensland(); diff --git a/Algorithm.CSharp/FutureOptionDailyRegressionAlgorithm.cs b/Algorithm.CSharp/FutureOptionDailyRegressionAlgorithm.cs index dd868d446f14..c3fe3eca3f77 100644 --- a/Algorithm.CSharp/FutureOptionDailyRegressionAlgorithm.cs +++ b/Algorithm.CSharp/FutureOptionDailyRegressionAlgorithm.cs @@ -47,9 +47,9 @@ public override void Initialize() Resolution).Symbol; // Attempt to fetch a specific future option contract - DcOption = OptionChainProvider.GetOptionContractList(dc, Time) - .Where(x => x.ID.StrikePrice == 17m && x.ID.OptionRight == OptionRight.Call) - .Select(x => AddFutureOptionContract(x, Resolution).Symbol) + DcOption = OptionChain(dc) + .Where(x => x.Symbol.ID.StrikePrice == 17m && x.Symbol.ID.OptionRight == OptionRight.Call) + .Select(x => AddFutureOptionContract(x.Symbol, Resolution).Symbol) .FirstOrDefault(); // Validate it is the expected contract diff --git a/Algorithm.CSharp/FutureOptionIndicatorsRegressionAlgorithm.cs b/Algorithm.CSharp/FutureOptionIndicatorsRegressionAlgorithm.cs index 6cfea60abec0..1a24bb695310 100644 --- a/Algorithm.CSharp/FutureOptionIndicatorsRegressionAlgorithm.cs +++ b/Algorithm.CSharp/FutureOptionIndicatorsRegressionAlgorithm.cs @@ -32,11 +32,12 @@ public override void Initialize() var underlying = AddFutureContract(QuantConnect.Symbol.CreateFuture(Futures.Indices.SP500EMini, Market.CME, new DateTime(2020, 3, 20)), Resolution.Minute).Symbol; - var option = AddFutureOptionContract(OptionChainProvider.GetOptionContractList(underlying, Time) - .Where(x => x.ID.StrikePrice <= 3200m && x.ID.OptionRight == OptionRight.Call) - .OrderByDescending(x => x.ID.StrikePrice) + var option = AddFutureOptionContract(OptionChain(underlying) + .Where(x => x.Symbol.ID.StrikePrice <= 3200m && x.Symbol.ID.OptionRight == OptionRight.Call) + .OrderByDescending(x => x.Symbol.ID.StrikePrice) .Take(1) - .Single(), Resolution.Minute).Symbol; + .Single() + .Symbol, Resolution.Minute).Symbol; InitializeIndicators(option); } diff --git a/Algorithm.CSharp/FutureOptionPutITMExpiryRegressionAlgorithm.cs b/Algorithm.CSharp/FutureOptionPutITMExpiryRegressionAlgorithm.cs index b32e72d62fb2..f733f53f1e93 100644 --- a/Algorithm.CSharp/FutureOptionPutITMExpiryRegressionAlgorithm.cs +++ b/Algorithm.CSharp/FutureOptionPutITMExpiryRegressionAlgorithm.cs @@ -54,11 +54,12 @@ public override void Initialize() Resolution.Minute).Symbol; // Select a future option expiring ITM, and adds it to the algorithm. - _esOption = AddFutureOptionContract(OptionChainProvider.GetOptionContractList(_es19m20, Time) - .Where(x => x.ID.StrikePrice >= 3300m && x.ID.OptionRight == OptionRight.Put) - .OrderBy(x => x.ID.StrikePrice) + _esOption = AddFutureOptionContract(OptionChain(_es19m20) + .Where(x => x.Symbol.ID.StrikePrice >= 3300m && x.Symbol.ID.OptionRight == OptionRight.Put) + .OrderBy(x => x.Symbol.ID.StrikePrice) .Take(1) - .Single(), Resolution.Minute).Symbol; + .Single() + .Symbol, Resolution.Minute).Symbol; _expectedContract = QuantConnect.Symbol.CreateOption(_es19m20, Market.CME, OptionStyle.American, OptionRight.Put, 3300m, new DateTime(2020, 6, 19)); if (_esOption != _expectedContract) diff --git a/Algorithm.CSharp/FutureOptionPutOTMExpiryRegressionAlgorithm.cs b/Algorithm.CSharp/FutureOptionPutOTMExpiryRegressionAlgorithm.cs index 0077f4e64a05..1303e44e449d 100644 --- a/Algorithm.CSharp/FutureOptionPutOTMExpiryRegressionAlgorithm.cs +++ b/Algorithm.CSharp/FutureOptionPutOTMExpiryRegressionAlgorithm.cs @@ -58,11 +58,12 @@ public override void Initialize() Resolution.Minute).Symbol; // Select a future option expiring ITM, and adds it to the algorithm. - _esOption = AddFutureOptionContract(OptionChainProvider.GetOptionContractList(_es19m20, Time) - .Where(x => x.ID.StrikePrice <= 3150m && x.ID.OptionRight == OptionRight.Put) - .OrderByDescending(x => x.ID.StrikePrice) + _esOption = AddFutureOptionContract(OptionChain(_es19m20) + .Where(x => x.Symbol.ID.StrikePrice <= 3150m && x.Symbol.ID.OptionRight == OptionRight.Put) + .OrderByDescending(x => x.Symbol.ID.StrikePrice) .Take(1) - .Single(), Resolution.Minute).Symbol; + .Single() + .Symbol, Resolution.Minute).Symbol; _expectedContract = QuantConnect.Symbol.CreateOption(_es19m20, Market.CME, OptionStyle.American, OptionRight.Put, 3150m, new DateTime(2020, 6, 19)); if (_esOption != _expectedContract) diff --git a/Algorithm.CSharp/FutureOptionShortCallITMExpiryRegressionAlgorithm.cs b/Algorithm.CSharp/FutureOptionShortCallITMExpiryRegressionAlgorithm.cs index f8e8601e961c..8588c91f1a03 100644 --- a/Algorithm.CSharp/FutureOptionShortCallITMExpiryRegressionAlgorithm.cs +++ b/Algorithm.CSharp/FutureOptionShortCallITMExpiryRegressionAlgorithm.cs @@ -54,11 +54,12 @@ public override void Initialize() Resolution.Minute).Symbol; // Select a future option expiring ITM, and adds it to the algorithm. - _esOption = AddFutureOptionContract(OptionChainProvider.GetOptionContractList(_es19m20, Time) - .Where(x => x.ID.StrikePrice <= 3100m && x.ID.OptionRight == OptionRight.Call) - .OrderByDescending(x => x.ID.StrikePrice) + _esOption = AddFutureOptionContract(OptionChain(_es19m20) + .Where(x => x.Symbol.ID.StrikePrice <= 3100m && x.Symbol.ID.OptionRight == OptionRight.Call) + .OrderByDescending(x => x.Symbol.ID.StrikePrice) .Take(1) - .Single(), Resolution.Minute).Symbol; + .Single() + .Symbol, Resolution.Minute).Symbol; _expectedContract = QuantConnect.Symbol.CreateOption(_es19m20, Market.CME, OptionStyle.American, OptionRight.Call, 3100m, new DateTime(2020, 6, 19)); if (_esOption != _expectedContract) diff --git a/Algorithm.CSharp/FutureOptionShortCallOTMExpiryRegressionAlgorithm.cs b/Algorithm.CSharp/FutureOptionShortCallOTMExpiryRegressionAlgorithm.cs index ec3224197a55..442c14b18a3f 100644 --- a/Algorithm.CSharp/FutureOptionShortCallOTMExpiryRegressionAlgorithm.cs +++ b/Algorithm.CSharp/FutureOptionShortCallOTMExpiryRegressionAlgorithm.cs @@ -55,11 +55,12 @@ public override void Initialize() Resolution.Minute).Symbol; // Select a future option expiring ITM, and adds it to the algorithm. - _esOption = AddFutureOptionContract(OptionChainProvider.GetOptionContractList(_es19m20, Time) - .Where(x => x.ID.StrikePrice >= 3400m && x.ID.OptionRight == OptionRight.Call) - .OrderBy(x => x.ID.StrikePrice) + _esOption = AddFutureOptionContract(OptionChain(_es19m20) + .Where(x => x.Symbol.ID.StrikePrice >= 3400m && x.Symbol.ID.OptionRight == OptionRight.Call) + .OrderBy(x => x.Symbol.ID.StrikePrice) .Take(1) - .Single(), Resolution.Minute).Symbol; + .Single() + .Symbol, Resolution.Minute).Symbol; _expectedContract = QuantConnect.Symbol.CreateOption(_es19m20, Market.CME, OptionStyle.American, OptionRight.Call, 3400m, new DateTime(2020, 6, 19)); if (_esOption != _expectedContract) diff --git a/Algorithm.CSharp/FutureOptionShortPutITMExpiryRegressionAlgorithm.cs b/Algorithm.CSharp/FutureOptionShortPutITMExpiryRegressionAlgorithm.cs index fdc2c27e247e..94107fc3f96f 100644 --- a/Algorithm.CSharp/FutureOptionShortPutITMExpiryRegressionAlgorithm.cs +++ b/Algorithm.CSharp/FutureOptionShortPutITMExpiryRegressionAlgorithm.cs @@ -54,11 +54,12 @@ public override void Initialize() Resolution.Minute).Symbol; // Select a future option expiring ITM, and adds it to the algorithm. - _esOption = AddFutureOptionContract(OptionChainProvider.GetOptionContractList(_es19m20, Time) - .Where(x => x.ID.StrikePrice <= 3400m && x.ID.OptionRight == OptionRight.Put) - .OrderByDescending(x => x.ID.StrikePrice) + _esOption = AddFutureOptionContract(OptionChain(_es19m20) + .Where(x => x.Symbol.ID.StrikePrice <= 3400m && x.Symbol.ID.OptionRight == OptionRight.Put) + .OrderByDescending(x => x.Symbol.ID.StrikePrice) .Take(1) - .Single(), Resolution.Minute).Symbol; + .Single() + .Symbol, Resolution.Minute).Symbol; _expectedContract = QuantConnect.Symbol.CreateOption(_es19m20, Market.CME, OptionStyle.American, OptionRight.Put, 3400m, new DateTime(2020, 6, 19)); if (_esOption != _expectedContract) diff --git a/Algorithm.CSharp/FutureOptionShortPutOTMExpiryRegressionAlgorithm.cs b/Algorithm.CSharp/FutureOptionShortPutOTMExpiryRegressionAlgorithm.cs index c079df2a0eb2..6b5fda32c9b2 100644 --- a/Algorithm.CSharp/FutureOptionShortPutOTMExpiryRegressionAlgorithm.cs +++ b/Algorithm.CSharp/FutureOptionShortPutOTMExpiryRegressionAlgorithm.cs @@ -55,11 +55,12 @@ public override void Initialize() Resolution.Minute).Symbol; // Select a future option expiring ITM, and adds it to the algorithm. - _esOption = AddFutureOptionContract(OptionChainProvider.GetOptionContractList(_es19m20, Time) - .Where(x => x.ID.StrikePrice <= 3000m && x.ID.OptionRight == OptionRight.Put) - .OrderByDescending(x => x.ID.StrikePrice) + _esOption = AddFutureOptionContract(OptionChain(_es19m20) + .Where(x => x.Symbol.ID.StrikePrice <= 3000m && x.Symbol.ID.OptionRight == OptionRight.Put) + .OrderByDescending(x => x.Symbol.ID.StrikePrice) .Take(1) - .Single(), Resolution.Minute).Symbol; + .Single() + .Symbol, Resolution.Minute).Symbol; _expectedContract = QuantConnect.Symbol.CreateOption(_es19m20, Market.CME, OptionStyle.American, OptionRight.Put, 3000m, new DateTime(2020, 6, 19)); if (_esOption != _expectedContract) diff --git a/Algorithm.CSharp/IndexOptionBuySellCallIntradayRegressionAlgorithm.cs b/Algorithm.CSharp/IndexOptionBuySellCallIntradayRegressionAlgorithm.cs index 27127b8515d0..35601b325a63 100644 --- a/Algorithm.CSharp/IndexOptionBuySellCallIntradayRegressionAlgorithm.cs +++ b/Algorithm.CSharp/IndexOptionBuySellCallIntradayRegressionAlgorithm.cs @@ -46,9 +46,9 @@ public override void Initialize() var spx = AddIndex("SPX", Resolution.Minute).Symbol; // Select a index option expiring ITM, and adds it to the algorithm. - var spxOptions = OptionChainProvider.GetOptionContractList(spx, Time) - .Where(x => (x.ID.StrikePrice == 3700m || x.ID.StrikePrice == 3800m) && x.ID.OptionRight == OptionRight.Call && x.ID.Date.Year == 2021 && x.ID.Date.Month == 1) - .Select(x => AddIndexOptionContract(x, Resolution.Minute).Symbol) + var spxOptions = OptionChain(spx) + .Where(x => (x.Symbol.ID.StrikePrice == 3700m || x.Symbol.ID.StrikePrice == 3800m) && x.Symbol.ID.OptionRight == OptionRight.Call && x.Symbol.ID.Date.Year == 2021 && x.Symbol.ID.Date.Month == 1) + .Select(x => AddIndexOptionContract(x.Symbol, Resolution.Minute).Symbol) .OrderBy(x => x.ID.StrikePrice) .ToList(); diff --git a/Algorithm.CSharp/IndexOptionCallITMExpiryRegressionAlgorithm.cs b/Algorithm.CSharp/IndexOptionCallITMExpiryRegressionAlgorithm.cs index ed604b2615e9..7d1da74da4b8 100644 --- a/Algorithm.CSharp/IndexOptionCallITMExpiryRegressionAlgorithm.cs +++ b/Algorithm.CSharp/IndexOptionCallITMExpiryRegressionAlgorithm.cs @@ -51,11 +51,12 @@ public override void Initialize() _spx = AddIndex("SPX", Resolution).Symbol; // Select an index option expiring ITM, and adds it to the algorithm. - _spxOption = AddIndexOptionContract(OptionChainProvider.GetOptionContractList(_spx, Time) - .Where(x => x.ID.StrikePrice <= 3200m && x.ID.OptionRight == OptionRight.Call && x.ID.Date.Year == 2021 && x.ID.Date.Month == 1) - .OrderByDescending(x => x.ID.StrikePrice) + _spxOption = AddIndexOptionContract(OptionChain(_spx) + .Where(x => x.Symbol.ID.StrikePrice <= 3200m && x.Symbol.ID.OptionRight == OptionRight.Call && x.Symbol.ID.Date.Year == 2021 && x.Symbol.ID.Date.Month == 1) + .OrderByDescending(x => x.Symbol.ID.StrikePrice) .Take(1) - .Single(), Resolution).Symbol; + .Single() + .Symbol, Resolution).Symbol; _expectedOptionContract = QuantConnect.Symbol.CreateOption(_spx, Market.USA, OptionStyle.European, OptionRight.Call, 3200m, new DateTime(2021, 1, 15)); if (_spxOption != _expectedOptionContract) diff --git a/Algorithm.CSharp/IndexOptionCallITMGreeksExpiryRegressionAlgorithm.cs b/Algorithm.CSharp/IndexOptionCallITMGreeksExpiryRegressionAlgorithm.cs index c46bd8533015..39b3a637f886 100644 --- a/Algorithm.CSharp/IndexOptionCallITMGreeksExpiryRegressionAlgorithm.cs +++ b/Algorithm.CSharp/IndexOptionCallITMGreeksExpiryRegressionAlgorithm.cs @@ -45,11 +45,12 @@ public override void Initialize() _spx = spx.Symbol; // Select an index option expiring ITM, and adds it to the algorithm. - _spxOption = AddIndexOptionContract(OptionChainProvider.GetOptionContractList(_spx, Time) - .Where(x => x.ID.StrikePrice <= 3200m && x.ID.OptionRight == OptionRight.Call && x.ID.Date.Year == 2021 && x.ID.Date.Month == 1) - .OrderByDescending(x => x.ID.StrikePrice) + _spxOption = AddIndexOptionContract(OptionChain(_spx) + .Where(x => x.Symbol.ID.StrikePrice <= 3200m && x.Symbol.ID.OptionRight == OptionRight.Call && x.Symbol.ID.Date.Year == 2021 && x.Symbol.ID.Date.Month == 1) + .OrderByDescending(x => x.Symbol.ID.StrikePrice) .Take(1) - .Single(), Resolution.Minute); + .Single() + .Symbol, Resolution.Minute); _spxOption.PriceModel = OptionPriceModels.BlackScholes(); diff --git a/Algorithm.CSharp/IndexOptionCallOTMExpiryRegressionAlgorithm.cs b/Algorithm.CSharp/IndexOptionCallOTMExpiryRegressionAlgorithm.cs index 4dd82cf71fdf..e40c19fcf4e8 100644 --- a/Algorithm.CSharp/IndexOptionCallOTMExpiryRegressionAlgorithm.cs +++ b/Algorithm.CSharp/IndexOptionCallOTMExpiryRegressionAlgorithm.cs @@ -56,11 +56,12 @@ public override void Initialize() _spx = AddIndex("SPX", Resolution).Symbol; // Select a index option call expiring OTM, and adds it to the algorithm. - _spxOption = AddIndexOptionContract(OptionChainProvider.GetOptionContractList(_spx, Time) - .Where(x => x.ID.StrikePrice >= 4250m && x.ID.OptionRight == OptionRight.Call && x.ID.Date.Year == 2021 && x.ID.Date.Month == 1) - .OrderBy(x => x.ID.StrikePrice) + _spxOption = AddIndexOptionContract(OptionChain(_spx) + .Where(x => x.Symbol.ID.StrikePrice >= 4250m && x.Symbol.ID.OptionRight == OptionRight.Call && x.Symbol.ID.Date.Year == 2021 && x.Symbol.ID.Date.Month == 1) + .OrderBy(x => x.Symbol.ID.StrikePrice) .Take(1) - .Single(), Resolution).Symbol; + .Single() + .Symbol, Resolution).Symbol; _expectedContract = QuantConnect.Symbol.CreateOption(_spx, Market.USA, OptionStyle.European, OptionRight.Call, 4250m, new DateTime(2021, 1, 15)); if (_spxOption != _expectedContract) diff --git a/Algorithm.CSharp/IndexOptionPutITMExpiryRegressionAlgorithm.cs b/Algorithm.CSharp/IndexOptionPutITMExpiryRegressionAlgorithm.cs index 6a0f654dc2f8..69415a3f6867 100644 --- a/Algorithm.CSharp/IndexOptionPutITMExpiryRegressionAlgorithm.cs +++ b/Algorithm.CSharp/IndexOptionPutITMExpiryRegressionAlgorithm.cs @@ -48,11 +48,12 @@ public override void Initialize() _spx = AddIndex("SPX", Resolution.Minute).Symbol; // Select a index option expiring ITM, and adds it to the algorithm. - _spxOption = AddIndexOptionContract(OptionChainProvider.GetOptionContractList(_spx, Time) - .Where(x => x.ID.StrikePrice >= 4200m && x.ID.OptionRight == OptionRight.Put && x.ID.Date.Year == 2021 && x.ID.Date.Month == 1) - .OrderBy(x => x.ID.StrikePrice) + _spxOption = AddIndexOptionContract(OptionChain(_spx) + .Where(x => x.Symbol.ID.StrikePrice >= 4200m && x.Symbol.ID.OptionRight == OptionRight.Put && x.Symbol.ID.Date.Year == 2021 && x.Symbol.ID.Date.Month == 1) + .OrderBy(x => x.Symbol.ID.StrikePrice) .Take(1) - .Single(), Resolution.Minute).Symbol; + .Single() + .Symbol, Resolution.Minute).Symbol; _expectedContract = QuantConnect.Symbol.CreateOption(_spx, Market.USA, OptionStyle.European, OptionRight.Put, 4200m, new DateTime(2021, 1, 15)); if (_spxOption != _expectedContract) diff --git a/Algorithm.CSharp/IndexOptionPutOTMExpiryRegressionAlgorithm.cs b/Algorithm.CSharp/IndexOptionPutOTMExpiryRegressionAlgorithm.cs index 968ba699559d..6e86b14e5188 100644 --- a/Algorithm.CSharp/IndexOptionPutOTMExpiryRegressionAlgorithm.cs +++ b/Algorithm.CSharp/IndexOptionPutOTMExpiryRegressionAlgorithm.cs @@ -53,11 +53,12 @@ public override void Initialize() _spx = AddIndex("SPX", Resolution.Minute).Symbol; // Select a index option expiring ITM, and adds it to the algorithm. - _spxOption = AddIndexOptionContract(OptionChainProvider.GetOptionContractList(_spx, Time) - .Where(x => x.ID.StrikePrice <= 3200m && x.ID.OptionRight == OptionRight.Put && x.ID.Date.Year == 2021 && x.ID.Date.Month == 1) - .OrderByDescending(x => x.ID.StrikePrice) + _spxOption = AddIndexOptionContract(OptionChain(_spx) + .Where(x => x.Symbol.ID.StrikePrice <= 3200m && x.Symbol.ID.OptionRight == OptionRight.Put && x.Symbol.ID.Date.Year == 2021 && x.Symbol.ID.Date.Month == 1) + .OrderByDescending(x => x.Symbol.ID.StrikePrice) .Take(1) - .Single(), Resolution.Minute).Symbol; + .Single() + .Symbol, Resolution.Minute).Symbol; _expectedContract = QuantConnect.Symbol.CreateOption(_spx, Market.USA, OptionStyle.European, OptionRight.Put, 3200m, new DateTime(2021, 1, 15)); if (_spxOption != _expectedContract) diff --git a/Algorithm.CSharp/IndexOptionShortCallOTMExpiryRegressionAlgorithm.cs b/Algorithm.CSharp/IndexOptionShortCallOTMExpiryRegressionAlgorithm.cs index 03158317a59b..0cba7712bc24 100644 --- a/Algorithm.CSharp/IndexOptionShortCallOTMExpiryRegressionAlgorithm.cs +++ b/Algorithm.CSharp/IndexOptionShortCallOTMExpiryRegressionAlgorithm.cs @@ -50,11 +50,12 @@ public override void Initialize() _spx = AddIndex("SPX", Resolution.Minute).Symbol; // Select a index option expiring ITM, and adds it to the algorithm. - _spxOption = AddIndexOptionContract(OptionChainProvider.GetOptionContractList(_spx, Time) - .Where(x => x.ID.StrikePrice >= 4250m && x.ID.OptionRight == OptionRight.Call && x.ID.Date.Year == 2021 && x.ID.Date.Month == 1) - .OrderBy(x => x.ID.StrikePrice) + _spxOption = AddIndexOptionContract(OptionChain(_spx) + .Where(x => x.Symbol.ID.StrikePrice >= 4250m && x.Symbol.ID.OptionRight == OptionRight.Call && x.Symbol.ID.Date.Year == 2021 && x.Symbol.ID.Date.Month == 1) + .OrderBy(x => x.Symbol.ID.StrikePrice) .Take(1) - .Single(), Resolution.Minute).Symbol; + .Single() + .Symbol, Resolution.Minute).Symbol; _expectedContract = QuantConnect.Symbol.CreateOption(_spx, Market.USA, OptionStyle.European, OptionRight.Call, 4250m, new DateTime(2021, 1, 15)); if (_spxOption != _expectedContract) diff --git a/Algorithm.CSharp/IndexOptionShortPutOTMExpiryRegressionAlgorithm.cs b/Algorithm.CSharp/IndexOptionShortPutOTMExpiryRegressionAlgorithm.cs index b425ef5a67d6..c143b0a39b28 100644 --- a/Algorithm.CSharp/IndexOptionShortPutOTMExpiryRegressionAlgorithm.cs +++ b/Algorithm.CSharp/IndexOptionShortPutOTMExpiryRegressionAlgorithm.cs @@ -50,11 +50,12 @@ public override void Initialize() _spx = AddIndex("SPX", Resolution.Minute).Symbol; // Select a index option expiring ITM, and adds it to the algorithm. - _spxOption = AddIndexOptionContract(OptionChainProvider.GetOptionContractList(_spx, Time) - .Where(x => x.ID.StrikePrice <= 3200m && x.ID.OptionRight == OptionRight.Put && x.ID.Date.Year == 2021 && x.ID.Date.Month == 1) - .OrderByDescending(x => x.ID.StrikePrice) + _spxOption = AddIndexOptionContract(OptionChain(_spx) + .Where(x => x.Symbol.ID.StrikePrice <= 3200m && x.Symbol.ID.OptionRight == OptionRight.Put && x.Symbol.ID.Date.Year == 2021 && x.Symbol.ID.Date.Month == 1) + .OrderByDescending(x => x.Symbol.ID.StrikePrice) .Take(1) - .Single(), Resolution.Minute).Symbol; + .Single() + .Symbol, Resolution.Minute).Symbol; _expectedContract = QuantConnect.Symbol.CreateOption(_spx, Market.USA, OptionStyle.European, OptionRight.Put, 3200m, new DateTime(2021, 1, 15)); if (_spxOption != _expectedContract) diff --git a/Algorithm.CSharp/InsufficientBuyingPowerForAutomaticExerciseRegressionAlgorithm.cs b/Algorithm.CSharp/InsufficientBuyingPowerForAutomaticExerciseRegressionAlgorithm.cs index 6571d5e534a2..c718b5646d6e 100644 --- a/Algorithm.CSharp/InsufficientBuyingPowerForAutomaticExerciseRegressionAlgorithm.cs +++ b/Algorithm.CSharp/InsufficientBuyingPowerForAutomaticExerciseRegressionAlgorithm.cs @@ -44,7 +44,7 @@ public override void Initialize() _stock = AddEquity("GOOG").Symbol; - var contracts = OptionChainProvider.GetOptionContractList(_stock, UtcTime).ToList(); + var contracts = OptionChain(_stock).Select(x => x.Symbol).ToList(); _option = contracts .Where(c => c.ID.OptionRight == OptionRight.Put) .OrderBy(c => c.ID.Date) diff --git a/Algorithm.CSharp/OptionAssignmentRegressionAlgorithm.cs b/Algorithm.CSharp/OptionAssignmentRegressionAlgorithm.cs index 00adbd3f630b..42d5a541f565 100644 --- a/Algorithm.CSharp/OptionAssignmentRegressionAlgorithm.cs +++ b/Algorithm.CSharp/OptionAssignmentRegressionAlgorithm.cs @@ -46,7 +46,7 @@ public override void Initialize() SetCash(100000); Stock = AddEquity("GOOG", Resolution.Minute); - var contracts = OptionChainProvider.GetOptionContractList(Stock.Symbol, UtcTime).ToList(); + var contracts = OptionChain(Stock.Symbol).Select(x => x.Symbol).ToList(); PutOptionSymbol = contracts .Where(c => c.ID.OptionRight == OptionRight.Put) diff --git a/Algorithm.CSharp/OptionAssignmentStatisticsRegressionAlgorithm.cs b/Algorithm.CSharp/OptionAssignmentStatisticsRegressionAlgorithm.cs index 36eb30ec531f..57026b06d8c1 100644 --- a/Algorithm.CSharp/OptionAssignmentStatisticsRegressionAlgorithm.cs +++ b/Algorithm.CSharp/OptionAssignmentStatisticsRegressionAlgorithm.cs @@ -48,7 +48,7 @@ public override void Initialize() _goog = AddEquity("GOOG", Resolution.Minute); - var contracts = OptionChainProvider.GetOptionContractList(_goog.Symbol, UtcTime).ToList(); + var contracts = OptionChain(_goog.Symbol).ToList(); _googCall600Symbol = contracts .Where(c => c.ID.OptionRight == OptionRight.Call) diff --git a/Algorithm.CSharp/OptionOTMExpiryOrderHasZeroPriceRegressionAlgorithm.cs b/Algorithm.CSharp/OptionOTMExpiryOrderHasZeroPriceRegressionAlgorithm.cs index 890343d44067..12a9068704f9 100644 --- a/Algorithm.CSharp/OptionOTMExpiryOrderHasZeroPriceRegressionAlgorithm.cs +++ b/Algorithm.CSharp/OptionOTMExpiryOrderHasZeroPriceRegressionAlgorithm.cs @@ -58,11 +58,12 @@ public override void Initialize() Resolution.Minute).Symbol; // Select a future option call expiring OTM, and adds it to the algorithm. - _esOption = AddFutureOptionContract(OptionChainProvider.GetOptionContractList(_es19m20, Time) - .Where(x => x.ID.StrikePrice >= 3300m && x.ID.OptionRight == OptionRight.Call) - .OrderBy(x => x.ID.StrikePrice) + _esOption = AddFutureOptionContract(OptionChain(_es19m20) + .Where(x => x.Symbol.ID.StrikePrice >= 3300m && x.Symbol.ID.OptionRight == OptionRight.Call) + .OrderBy(x => x.Symbol.ID.StrikePrice) .Take(1) - .Single(), Resolution.Minute).Symbol; + .Single() + .Symbol, Resolution.Minute).Symbol; _expectedContract = QuantConnect.Symbol.CreateOption(_es19m20, Market.CME, OptionStyle.American, OptionRight.Call, 3300m, new DateTime(2020, 6, 19)); if (_esOption != _expectedContract) diff --git a/Algorithm.CSharp/OptionSymbolCanonicalRegressionAlgorithm.cs b/Algorithm.CSharp/OptionSymbolCanonicalRegressionAlgorithm.cs index 677147453982..f91cd5ddb9ed 100644 --- a/Algorithm.CSharp/OptionSymbolCanonicalRegressionAlgorithm.cs +++ b/Algorithm.CSharp/OptionSymbolCanonicalRegressionAlgorithm.cs @@ -36,7 +36,7 @@ public override void Initialize() SetEndDate(2014, 06, 09); var equitySymbol = AddEquity("TWX").Symbol; - var contracts = OptionChainProvider.GetOptionContractList(equitySymbol, UtcTime).ToList(); + var contracts = OptionChain(equitySymbol).Select(x => x.Symbol).ToList(); var callOptionSymbol = contracts .Where(c => c.ID.OptionRight == OptionRight.Call) diff --git a/Algorithm.CSharp/OptionTimeSliceRegressionAlgorithm.cs b/Algorithm.CSharp/OptionTimeSliceRegressionAlgorithm.cs index 74a79c9e097b..4725a74a6eda 100644 --- a/Algorithm.CSharp/OptionTimeSliceRegressionAlgorithm.cs +++ b/Algorithm.CSharp/OptionTimeSliceRegressionAlgorithm.cs @@ -54,10 +54,11 @@ public override void OnData(Slice slice) _lastSliceTime = Time; var underlyingPrice = Securities[_symbol].Price; - var contractSymbol = OptionChainProvider.GetOptionContractList(_symbol, Time) - .Where(x => x.ID.StrikePrice - underlyingPrice > 0) - .OrderBy(x => x.ID.Date) - .FirstOrDefault(); + var contractSymbol = OptionChain(_symbol) + .Where(x => x.Symbol.ID.StrikePrice - underlyingPrice > 0) + .OrderBy(x => x.Symbol.ID.Date) + .FirstOrDefault() + ?.Symbol; if (contractSymbol != null) { @@ -91,7 +92,7 @@ public override void OnEndOfAlgorithm() /// /// Data Points count of the algorithm history /// - public int AlgorithmHistoryDataPoints => 3; + public int AlgorithmHistoryDataPoints => 787; /// /// Final status of the algorithm diff --git a/Algorithm.CSharp/UniverseSelectionSymbolCacheRemovalRegressionTest.cs b/Algorithm.CSharp/UniverseSelectionSymbolCacheRemovalRegressionTest.cs index 9c22e90f6d8b..66707df6ffd4 100644 --- a/Algorithm.CSharp/UniverseSelectionSymbolCacheRemovalRegressionTest.cs +++ b/Algorithm.CSharp/UniverseSelectionSymbolCacheRemovalRegressionTest.cs @@ -1,4 +1,4 @@ - + /* * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. @@ -43,7 +43,7 @@ public override void Initialize() AddEquity("AAPL", Resolution.Daily); _equitySymbol = AddEquity("TWX", Resolution.Minute).Symbol; - var contracts = OptionChainProvider.GetOptionContractList(_equitySymbol, UtcTime).ToList(); + var contracts = OptionChain(_equitySymbol).Select(x => x.Symbol).ToList(); var callOptionSymbol = contracts .Where(c => c.ID.OptionRight == OptionRight.Call) diff --git a/Algorithm.Python/AddOptionContractExpiresRegressionAlgorithm.py b/Algorithm.Python/AddOptionContractExpiresRegressionAlgorithm.py index 20f5496e4cd4..9b4318be719d 100644 --- a/Algorithm.Python/AddOptionContractExpiresRegressionAlgorithm.py +++ b/Algorithm.Python/AddOptionContractExpiresRegressionAlgorithm.py @@ -43,14 +43,14 @@ def on_data(self, data): data: Slice object keyed by symbol containing the stock data ''' if self._option == None: - options = self.option_chain_provider.get_option_contract_list(self._twx, self.time) - options = sorted(options, key=lambda x: x.id.symbol) + options = self.option_chain(self._twx) + options = sorted(options, key=lambda x: x.symbol.id.symbol) - option = next((option + option = next((option.symbol for option in options - if option.id.date == self._expiration and - option.id.option_right == OptionRight.CALL and - option.id.option_style == OptionStyle.AMERICAN), None) + if option.symbol.id.date == self._expiration and + option.symbol.id.option_right == OptionRight.CALL and + option.symbol.id.option_style == OptionStyle.AMERICAN), None) if option != None: self._option = self.add_option_contract(option).symbol diff --git a/Algorithm.Python/FutureOptionCallITMExpiryRegressionAlgorithm.py b/Algorithm.Python/FutureOptionCallITMExpiryRegressionAlgorithm.py index 10164f439e04..b4d1b31d181a 100644 --- a/Algorithm.Python/FutureOptionCallITMExpiryRegressionAlgorithm.py +++ b/Algorithm.Python/FutureOptionCallITMExpiryRegressionAlgorithm.py @@ -41,7 +41,10 @@ def initialize(self): # Select a future option expiring ITM, and adds it to the algorithm. self.es_option = self.add_future_option_contract( list( - sorted([x for x in self.option_chain_provider.get_option_contract_list(self.es19m20, self.time) if x.id.strike_price <= 3200.0 and x.id.option_right == OptionRight.CALL], key=lambda x: x.id.strike_price, reverse=True) + sorted([x.symbol + for x in self.option_chain(self.es19m20) + if x.symbol.id.strike_price <= 3200.0 and x.symbol.id.option_right == OptionRight.CALL], + key=lambda x: x.id.strike_price, reverse=True) )[0], Resolution.MINUTE).symbol self.expected_contract = Symbol.create_option(self.es19m20, Market.CME, OptionStyle.AMERICAN, OptionRight.CALL, 3200.0, datetime(2020, 6, 19)) diff --git a/Algorithm.Python/FutureOptionDailyRegressionAlgorithm.py b/Algorithm.Python/FutureOptionDailyRegressionAlgorithm.py index a82004978c72..9e9916d965fd 100644 --- a/Algorithm.Python/FutureOptionDailyRegressionAlgorithm.py +++ b/Algorithm.Python/FutureOptionDailyRegressionAlgorithm.py @@ -34,7 +34,9 @@ def initialize(self): # Attempt to fetch a specific ITM future option contract dc_options = [ - self.add_future_option_contract(x, resolution).symbol for x in (self.option_chain_provider.get_option_contract_list(self.dc, self.time)) if x.id.strike_price == 17 and x.id.option_right == OptionRight.CALL + self.add_future_option_contract(x.symbol, resolution).symbol + for x in self.option_chain(self.dc) + if x.symbol.id.strike_price == 17 and x.symbol.id.option_right == OptionRight.CALL ] self.dc_option = dc_options[0] diff --git a/Algorithm.Python/FutureOptionPutITMExpiryRegressionAlgorithm.py b/Algorithm.Python/FutureOptionPutITMExpiryRegressionAlgorithm.py index bc365833c6e3..0e8cfb577122 100644 --- a/Algorithm.Python/FutureOptionPutITMExpiryRegressionAlgorithm.py +++ b/Algorithm.Python/FutureOptionPutITMExpiryRegressionAlgorithm.py @@ -40,7 +40,10 @@ def initialize(self): # Select a future option expiring ITM, and adds it to the algorithm. self.es_option = self.add_future_option_contract( list( - sorted([x for x in self.option_chain_provider.get_option_contract_list(self.es19m20, self.time) if x.id.strike_price >= 3300.0 and x.id.option_right == OptionRight.PUT], key=lambda x: x.id.strike_price) + sorted([x.symbol + for x in self.option_chain(self.es19m20) + if x.symbol.id.strike_price >= 3300.0 and x.symbol.id.option_right == OptionRight.PUT], + key=lambda x: x.id.strike_price) )[0], Resolution.MINUTE).symbol self.expected_contract = Symbol.create_option(self.es19m20, Market.CME, OptionStyle.AMERICAN, OptionRight.PUT, 3300.0, datetime(2020, 6, 19)) diff --git a/Algorithm.Python/FutureOptionPutOTMExpiryRegressionAlgorithm.py b/Algorithm.Python/FutureOptionPutOTMExpiryRegressionAlgorithm.py index 52a6b85041fe..ad2073998d9c 100644 --- a/Algorithm.Python/FutureOptionPutOTMExpiryRegressionAlgorithm.py +++ b/Algorithm.Python/FutureOptionPutOTMExpiryRegressionAlgorithm.py @@ -45,7 +45,7 @@ def initialize(self): self.es_option = self.add_future_option_contract( list( sorted( - [x for x in self.option_chain_provider.get_option_contract_list(self.es19m20, self.time) if x.id.strike_price <= 3150.0 and x.id.option_right == OptionRight.PUT], + [x.symbol for x in self.option_chain(self.es19m20) if x.symbol.id.strike_price <= 3150.0 and x.symbol.id.option_right == OptionRight.PUT], key=lambda x: x.id.strike_price, reverse=True ) @@ -71,7 +71,7 @@ def on_data(self, data: Slice): if delisting.type == DelistingType.DELISTED: if delisting.time != datetime(2020, 6, 20): raise AssertionError(f"Delisting happened at unexpected date: {delisting.time}") - + def on_order_event(self, order_event: OrderEvent): if order_event.status != OrderStatus.FILLED: # There's lots of noise with OnOrderEvent, but we're only interested in fills. diff --git a/Algorithm.Python/FutureOptionShortCallITMExpiryRegressionAlgorithm.py b/Algorithm.Python/FutureOptionShortCallITMExpiryRegressionAlgorithm.py index c1a37a2156c9..bc624db42e13 100644 --- a/Algorithm.Python/FutureOptionShortCallITMExpiryRegressionAlgorithm.py +++ b/Algorithm.Python/FutureOptionShortCallITMExpiryRegressionAlgorithm.py @@ -41,7 +41,7 @@ def initialize(self): self.es_option = self.add_future_option_contract( list( sorted( - [x for x in self.option_chain_provider.get_option_contract_list(self.es19m20, self.time) if x.id.strike_price <= 3100.0 and x.id.option_right == OptionRight.CALL], + [x.symbol for x in self.option_chain(self.es19m20) if x.symbol.id.strike_price <= 3100.0 and x.symbol.id.option_right == OptionRight.CALL], key=lambda x: x.id.strike_price, reverse=True ) diff --git a/Algorithm.Python/FutureOptionShortCallOTMExpiryRegressionAlgorithm.py b/Algorithm.Python/FutureOptionShortCallOTMExpiryRegressionAlgorithm.py index 0290c7f50705..4a63f7aad03d 100644 --- a/Algorithm.Python/FutureOptionShortCallOTMExpiryRegressionAlgorithm.py +++ b/Algorithm.Python/FutureOptionShortCallOTMExpiryRegressionAlgorithm.py @@ -42,7 +42,7 @@ def initialize(self): self.es_option = self.add_future_option_contract( list( sorted( - [x for x in self.option_chain_provider.get_option_contract_list(self.es19m20, self.time) if x.id.strike_price >= 3400.0 and x.id.option_right == OptionRight.CALL], + [x.symbol for x in self.option_chain(self.es19m20) if x.symbol.id.strike_price >= 3400.0 and x.symbol.id.option_right == OptionRight.CALL], key=lambda x: x.id.strike_price ) )[0], Resolution.MINUTE).symbol diff --git a/Algorithm.Python/FutureOptionShortPutITMExpiryRegressionAlgorithm.py b/Algorithm.Python/FutureOptionShortPutITMExpiryRegressionAlgorithm.py index 07733699b55a..5022a24fdd12 100644 --- a/Algorithm.Python/FutureOptionShortPutITMExpiryRegressionAlgorithm.py +++ b/Algorithm.Python/FutureOptionShortPutITMExpiryRegressionAlgorithm.py @@ -41,7 +41,7 @@ def initialize(self): self.es_option = self.add_future_option_contract( list( sorted( - [x for x in self.option_chain_provider.get_option_contract_list(self.es19m20, self.time) if x.id.strike_price <= 3400.0 and x.id.option_right == OptionRight.PUT], + [x.symbol for x in self.option_chain(self.es19m20) if x.symbol.id.strike_price <= 3400.0 and x.symbol.id.option_right == OptionRight.PUT], key=lambda x: x.id.strike_price, reverse=True ) diff --git a/Algorithm.Python/FutureOptionShortPutOTMExpiryRegressionAlgorithm.py b/Algorithm.Python/FutureOptionShortPutOTMExpiryRegressionAlgorithm.py index fca36816b119..3bf486ecaa6b 100644 --- a/Algorithm.Python/FutureOptionShortPutOTMExpiryRegressionAlgorithm.py +++ b/Algorithm.Python/FutureOptionShortPutOTMExpiryRegressionAlgorithm.py @@ -42,7 +42,7 @@ def initialize(self): self.es_option = self.add_future_option_contract( list( sorted( - [x for x in self.option_chain_provider.get_option_contract_list(self.es19m20, self.time) if x.id.strike_price <= 3000.0 and x.id.option_right == OptionRight.PUT], + [x.symbol for x in self.option_chain(self.es19m20) if x.symbol.id.strike_price <= 3000.0 and x.symbol.id.option_right == OptionRight.PUT], key=lambda x: x.id.strike_price, reverse=True ) diff --git a/Algorithm.Python/IndexOptionBuySellCallIntradayRegressionAlgorithm.py b/Algorithm.Python/IndexOptionBuySellCallIntradayRegressionAlgorithm.py index 7420cff78cc9..1132a86ffc7c 100644 --- a/Algorithm.Python/IndexOptionBuySellCallIntradayRegressionAlgorithm.py +++ b/Algorithm.Python/IndexOptionBuySellCallIntradayRegressionAlgorithm.py @@ -37,9 +37,9 @@ def initialize(self): # Select a index option expiring ITM, and adds it to the algorithm. spx_options = list(sorted([ - self.add_index_option_contract(i, Resolution.MINUTE).symbol \ - for i in self.option_chain_provider.get_option_contract_list(spx, self.time)\ - if (i.id.strike_price == 3700 or i.id.strike_price == 3800) and i.id.option_right == OptionRight.CALL and i.id.date.year == 2021 and i.id.date.month == 1], + self.add_index_option_contract(i.symbol, Resolution.MINUTE).symbol \ + for i in self.option_chain(spx)\ + if (i.symbol.id.strike_price == 3700 or i.symbol.id.strike_price == 3800) and i.symbol.id.option_right == OptionRight.CALL and i.symbol.id.date.year == 2021 and i.symbol.id.date.month == 1], key=lambda x: x.id.strike_price )) @@ -66,7 +66,7 @@ def initialize(self): if spx_options[0] != expectedContract3700: raise Exception(f"Contract {expectedContract3700} was not found in the chain, found instead: {spx_options[0]}") - + if spx_options[1] != expectedContract3800: raise Exception(f"Contract {expectedContract3800} was not found in the chain, found instead: {spx_options[1]}") diff --git a/Algorithm.Python/IndexOptionCallITMExpiryRegressionAlgorithm.py b/Algorithm.Python/IndexOptionCallITMExpiryRegressionAlgorithm.py index 38179fef8fe1..f2fa710a44eb 100644 --- a/Algorithm.Python/IndexOptionCallITMExpiryRegressionAlgorithm.py +++ b/Algorithm.Python/IndexOptionCallITMExpiryRegressionAlgorithm.py @@ -33,8 +33,10 @@ def initialize(self): self.spx = self.add_index("SPX", Resolution.MINUTE).symbol # Select an index option expiring ITM, and adds it to the algorithm. - self.spx_option = list(self.option_chain_provider.get_option_contract_list(self.spx, self.time)) - self.spx_option = [i for i in self.spx_option if i.id.strike_price <= 3200 and i.id.option_right == OptionRight.CALL and i.id.date.year == 2021 and i.id.date.month == 1] + self.spx_option = list(self.option_chain(self.spx)) + self.spx_option = [i.symbol + for i in self.spx_option + if i.symbol.id.strike_price <= 3200 and i.symbol.id.option_right == OptionRight.CALL and i.symbol.id.date.year == 2021 and i.symbol.id.date.month == 1] self.spx_option = list(sorted(self.spx_option, key=lambda x: x.id.strike_price, reverse=True))[0] self.spx_option = self.add_index_option_contract(self.spx_option, Resolution.MINUTE).symbol @@ -43,8 +45,8 @@ def initialize(self): raise Exception(f"Contract {self.expected_option_contract} was not found in the chain") self.schedule.on( - self.date_rules.tomorrow, - self.time_rules.after_market_open(self.spx, 1), + self.date_rules.tomorrow, + self.time_rules.after_market_open(self.spx, 1), lambda: self.market_order(self.spx_option, 1) ) @@ -55,7 +57,7 @@ def on_data(self, data: Slice): if delisting.type == DelistingType.WARNING: if delisting.time != datetime(2021, 1, 15): raise Exception(f"Delisting warning issued at unexpected date: {delisting.time}") - + if delisting.type == DelistingType.DELISTED: if delisting.time != datetime(2021, 1, 16): raise Exception(f"Delisting happened at unexpected date: {delisting.time}") diff --git a/Algorithm.Python/IndexOptionCallITMGreeksExpiryRegressionAlgorithm.py b/Algorithm.Python/IndexOptionCallITMGreeksExpiryRegressionAlgorithm.py index 793a12e80ea1..ba691dab1560 100644 --- a/Algorithm.Python/IndexOptionCallITMGreeksExpiryRegressionAlgorithm.py +++ b/Algorithm.Python/IndexOptionCallITMGreeksExpiryRegressionAlgorithm.py @@ -30,8 +30,10 @@ def initialize(self): self.spx = spx.symbol # Select a index option call expiring ITM, and adds it to the algorithm. - self.spx_option = list(self.option_chain_provider.get_option_contract_list(self.spx, self.time)) - self.spx_option = [i for i in self.spx_option if i.id.strike_price <= 3200 and i.id.option_right == OptionRight.CALL and i.id.date.year == 2021 and i.id.date.month == 1] + self.spx_option = list(self.option_chain(self.spx)) + self.spx_option = [i.symbol + for i in self.spx_option + if i.symbol.id.strike_price <= 3200 and i.symbol.id.option_right == OptionRight.CALL and i.symbol.id.date.year == 2021 and i.symbol.id.date.month == 1] self.spx_option = list(sorted(self.spx_option, key=lambda x: x.id.strike_price, reverse=True))[0] self.spx_option = self.add_index_option_contract(self.spx_option, Resolution.MINUTE) @@ -81,7 +83,7 @@ def on_data(self, data: Slice): if any([i for i in rho if i == 0]): raise Exception("Option contract Rho was equal to zero") - + if any([i for i in theta if i == 0]): raise Exception("Option contract Theta was equal to zero") diff --git a/Algorithm.Python/IndexOptionCallOTMExpiryRegressionAlgorithm.py b/Algorithm.Python/IndexOptionCallOTMExpiryRegressionAlgorithm.py index fe2a16dfbf5b..f4cc1d24bd52 100644 --- a/Algorithm.Python/IndexOptionCallOTMExpiryRegressionAlgorithm.py +++ b/Algorithm.Python/IndexOptionCallOTMExpiryRegressionAlgorithm.py @@ -38,17 +38,17 @@ def initialize(self): self.spx = self.add_index("SPX", Resolution.MINUTE).symbol # Select a index option call expiring OTM, and adds it to the algorithm. - self.spx_option = list(self.option_chain_provider.get_option_contract_list(self.spx, self.time)) - self.spx_option = [i for i in self.spx_option if i.id.strike_price >= 4250 and i.id.option_right == OptionRight.CALL and i.id.date.year == 2021 and i.id.date.month == 1] + self.spx_option = list(self.option_chain(self.spx)) + self.spx_option = [i.symbol for i in self.spx_option if i.symbol.id.strike_price >= 4250 and i.symbol.id.option_right == OptionRight.CALL and i.symbol.id.date.year == 2021 and i.symbol.id.date.month == 1] self.spx_option = list(sorted(self.spx_option, key=lambda x: x.id.strike_price))[0] self.spx_option = self.add_index_option_contract(self.spx_option, Resolution.MINUTE).symbol self.expected_contract = Symbol.create_option( - self.spx, - Market.USA, - OptionStyle.EUROPEAN, - OptionRight.CALL, - 4250, + self.spx, + Market.USA, + OptionStyle.EUROPEAN, + OptionRight.CALL, + 4250, datetime(2021, 1, 15) ) @@ -56,8 +56,8 @@ def initialize(self): raise Exception(f"Contract {self.expected_contract} was not found in the chain") self.schedule.on( - self.date_rules.tomorrow, - self.time_rules.after_market_open(self.spx, 1), + self.date_rules.tomorrow, + self.time_rules.after_market_open(self.spx, 1), lambda: self.market_order(self.spx_option, 1) ) diff --git a/Algorithm.Python/IndexOptionPutITMExpiryRegressionAlgorithm.py b/Algorithm.Python/IndexOptionPutITMExpiryRegressionAlgorithm.py index 2a241f79daf6..92ebe8a0703d 100644 --- a/Algorithm.Python/IndexOptionPutITMExpiryRegressionAlgorithm.py +++ b/Algorithm.Python/IndexOptionPutITMExpiryRegressionAlgorithm.py @@ -32,8 +32,10 @@ def initialize(self): self.spx = self.add_index("SPX", Resolution.MINUTE).symbol # Select a index option expiring ITM, and adds it to the algorithm. - self.spx_option = list(self.option_chain_provider.get_option_contract_list(self.spx, self.time)) - self.spx_option = [i for i in self.spx_option if i.id.strike_price >= 4200 and i.id.option_right == OptionRight.PUT and i.id.date.year == 2021 and i.id.date.month == 1] + self.spx_option = list(self.option_chain(self.spx)) + self.spx_option = [i.symbol + for i in self.spx_option + if i.symbol.id.strike_price >= 4200 and i.symbol.id.option_right == OptionRight.PUT and i.symbol.id.date.year == 2021 and i.symbol.id.date.month == 1] self.spx_option = list(sorted(self.spx_option, key=lambda x: x.id.strike_price))[0] self.spx_option = self.add_index_option_contract(self.spx_option, Resolution.MINUTE).symbol diff --git a/Algorithm.Python/IndexOptionPutOTMExpiryRegressionAlgorithm.py b/Algorithm.Python/IndexOptionPutOTMExpiryRegressionAlgorithm.py index 43d6c648bfdc..fbf59017ddde 100644 --- a/Algorithm.Python/IndexOptionPutOTMExpiryRegressionAlgorithm.py +++ b/Algorithm.Python/IndexOptionPutOTMExpiryRegressionAlgorithm.py @@ -38,17 +38,19 @@ def initialize(self): self.spx = self.add_index("SPX", Resolution.MINUTE).symbol # Select a index option call expiring OTM, and adds it to the algorithm. - self.spx_option = list(self.option_chain_provider.get_option_contract_list(self.spx, self.time)) - self.spx_option = [i for i in self.spx_option if i.id.strike_price <= 3200 and i.id.option_right == OptionRight.PUT and i.id.date.year == 2021 and i.id.date.month == 1] + self.spx_option = list(self.option_chain(self.spx)) + self.spx_option = [i.symbol + for i in self.spx_option + if i.symbol.id.strike_price <= 3200 and i.symbol.id.option_right == OptionRight.PUT and i.symbol.id.date.year == 2021 and i.symbol.id.date.month == 1] self.spx_option = list(sorted(self.spx_option, key=lambda x: x.id.strike_price, reverse=True))[0] self.spx_option = self.add_index_option_contract(self.spx_option, Resolution.MINUTE).symbol self.expected_contract = Symbol.create_option( - self.spx, - Market.USA, - OptionStyle.EUROPEAN, - OptionRight.PUT, - 3200, + self.spx, + Market.USA, + OptionStyle.EUROPEAN, + OptionRight.PUT, + 3200, datetime(2021, 1, 15) ) @@ -56,8 +58,8 @@ def initialize(self): raise Exception(f"Contract {self.expected_contract} was not found in the chain") self.schedule.on( - self.date_rules.tomorrow, - self.time_rules.after_market_open(self.spx, 1), + self.date_rules.tomorrow, + self.time_rules.after_market_open(self.spx, 1), lambda: self.market_order(self.spx_option, 1) ) diff --git a/Algorithm.Python/IndexOptionShortCallOTMExpiryRegressionAlgorithm.py b/Algorithm.Python/IndexOptionShortCallOTMExpiryRegressionAlgorithm.py index 687f7c2d9016..c83efebb48a0 100644 --- a/Algorithm.Python/IndexOptionShortCallOTMExpiryRegressionAlgorithm.py +++ b/Algorithm.Python/IndexOptionShortCallOTMExpiryRegressionAlgorithm.py @@ -34,8 +34,10 @@ def initialize(self): self.spx = self.add_index("SPX", Resolution.MINUTE).symbol # Select a index option expiring ITM, and adds it to the algorithm. - self.spx_option = list(self.option_chain_provider.get_option_contract_list(self.spx, self.time)) - self.spx_option = [i for i in self.spx_option if i.id.strike_price >= 4250 and i.id.option_right == OptionRight.CALL and i.id.date.year == 2021 and i.id.date.month == 1] + self.spx_option = list(self.option_chain(self.spx)) + self.spx_option = [i.symbol + for i in self.spx_option + if i.symbol.id.strike_price >= 4250 and i.symbol.id.option_right == OptionRight.CALL and i.symbol.id.date.year == 2021 and i.symbol.id.date.month == 1] self.spx_option = list(sorted(self.spx_option, key=lambda x: x.id.strike_price))[0] self.spx_option = self.add_index_option_contract(self.spx_option, Resolution.MINUTE).symbol diff --git a/Algorithm.Python/IndexOptionShortPutOTMExpiryRegressionAlgorithm.py b/Algorithm.Python/IndexOptionShortPutOTMExpiryRegressionAlgorithm.py index 628ab1c3f84e..765cbc082e36 100644 --- a/Algorithm.Python/IndexOptionShortPutOTMExpiryRegressionAlgorithm.py +++ b/Algorithm.Python/IndexOptionShortPutOTMExpiryRegressionAlgorithm.py @@ -34,8 +34,10 @@ def initialize(self): self.spx = self.add_index("SPX", Resolution.MINUTE).symbol # Select a index option expiring ITM, and adds it to the algorithm. - self.spx_option = list(self.option_chain_provider.get_option_contract_list(self.spx, self.time)) - self.spx_option = [i for i in self.spx_option if i.id.strike_price <= 3200 and i.id.option_right == OptionRight.PUT and i.id.date.year == 2021 and i.id.date.month == 1] + self.spx_option = list(self.option_chain(self.spx)) + self.spx_option = [i.symbol + for i in self.spx_option + if i.symbol.id.strike_price <= 3200 and i.symbol.id.option_right == OptionRight.PUT and i.symbol.id.date.year == 2021 and i.symbol.id.date.month == 1] self.spx_option = list(sorted(self.spx_option, key=lambda x: x.id.strike_price, reverse=True))[0] self.spx_option = self.add_index_option_contract(self.spx_option, Resolution.MINUTE).symbol From cf60f01371a7f74e87e097cb51cf6cde44ffdee2 Mon Sep 17 00:00:00 2001 From: Jhonathan Abreu Date: Wed, 11 Sep 2024 09:49:22 -0400 Subject: [PATCH 07/10] Minor --- Algorithm/QCAlgorithm.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Algorithm/QCAlgorithm.cs b/Algorithm/QCAlgorithm.cs index 789cff1d6b3a..734532980b9e 100644 --- a/Algorithm/QCAlgorithm.cs +++ b/Algorithm/QCAlgorithm.cs @@ -3353,6 +3353,7 @@ public DataHistory OptionChain(Symbol symbol) var canonicalSymbol = GetCanonicalOptionSymbol(symbol); IEnumerable optionChain; + // TODO: Until future options are supported by OptionUniverse, we need to fall back to the OptionChainProvider for them if (canonicalSymbol.SecurityType != SecurityType.FutureOption) { var marketHoursEntry = MarketHoursDatabase.GetEntry(canonicalSymbol.ID.Market, canonicalSymbol, canonicalSymbol.SecurityType); From 97cb36b8b070e28021a6a844e92d30cce5f43062 Mon Sep 17 00:00:00 2001 From: Jhonathan Abreu Date: Wed, 11 Sep 2024 10:01:47 -0400 Subject: [PATCH 08/10] Cleanup --- Algorithm/QCAlgorithm.cs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Algorithm/QCAlgorithm.cs b/Algorithm/QCAlgorithm.cs index 734532980b9e..5c5f83552f98 100644 --- a/Algorithm/QCAlgorithm.cs +++ b/Algorithm/QCAlgorithm.cs @@ -53,9 +53,7 @@ using QuantConnect.Securities.CryptoFuture; using QuantConnect.Algorithm.Framework.Alphas.Analysis; using QuantConnect.Algorithm.Framework.Portfolio.SignalExports; -using QuantConnect.Data.Auxiliary; using Python.Runtime; -using QuantConnect.Python; namespace QuantConnect.Algorithm { @@ -3344,7 +3342,7 @@ public List Fundamentals(List symbols) /// implied volatility and greeks. /// /// - /// As of 2024/09/10, future options chain will not contain any additional data (e.g. daily price data, implied volatility and greeks), + /// As of 2024/09/11, future options chain will not contain any additional data (e.g. daily price data, implied volatility and greeks), /// it will be populated with the contract symbol only. This is expected to change in the future. /// [DocumentationAttribute(AddingData)] @@ -3374,7 +3372,7 @@ public DataHistory OptionChain(Symbol symbol) }); } - return new DataHistory(optionChain, new Lazy(() => new PandasConverter().GetDataFrame(optionChain))); + return new DataHistory(optionChain, new Lazy(() => PandasConverter.GetDataFrame(optionChain))); } private static Symbol GetCanonicalOptionSymbol(Symbol symbol) From 6d6c55eed653cff6f1761265176db74b1f18df01 Mon Sep 17 00:00:00 2001 From: Jhonathan Abreu Date: Wed, 11 Sep 2024 12:58:52 -0400 Subject: [PATCH 09/10] Minor changes in regression algorithms --- .../AddAndRemoveOptionContractRegressionAlgorithm.cs | 6 +++--- .../AddAndRemoveSecuritySameLoopRegressionAlgorithm.cs | 6 +++--- ...OptionContractFromFutureChainRegressionAlgorithm.cs | 2 +- .../AddOptionContractExpiresRegressionAlgorithm.cs | 8 ++++---- ...AddOptionContractFromUniverseRegressionAlgorithm.cs | 8 ++++---- .../AddOptionContractTwiceRegressionAlgorithm.cs | 7 +++---- ...TwoAndRemoveOneOptionContractRegressionAlgorithm.cs | 7 +++---- Algorithm.CSharp/BasicTemplateOptionsAlgorithm.cs | 5 +---- ...yedSettlementAfterManualSecurityRemovalAlgorithm.cs | 7 +++---- .../DelistedIndexOptionDivestedRegression.cs | 3 +-- ...tureOptionBuySellCallIntradayRegressionAlgorithm.cs | 4 ++-- .../FutureOptionCallITMExpiryRegressionAlgorithm.cs | 7 +++---- ...tureOptionCallITMGreeksExpiryRegressionAlgorithm.cs | 7 +++---- .../FutureOptionCallOTMExpiryRegressionAlgorithm.cs | 7 +++---- .../FutureOptionDailyRegressionAlgorithm.cs | 4 ++-- .../FutureOptionIndicatorsRegressionAlgorithm.cs | 7 +++---- .../FutureOptionPutITMExpiryRegressionAlgorithm.cs | 7 +++---- .../FutureOptionPutOTMExpiryRegressionAlgorithm.cs | 7 +++---- ...utureOptionShortCallITMExpiryRegressionAlgorithm.cs | 7 +++---- ...utureOptionShortCallOTMExpiryRegressionAlgorithm.cs | 7 +++---- ...FutureOptionShortPutITMExpiryRegressionAlgorithm.cs | 7 +++---- ...FutureOptionShortPutOTMExpiryRegressionAlgorithm.cs | 7 +++---- ...ndexOptionBuySellCallIntradayRegressionAlgorithm.cs | 4 ++-- .../IndexOptionCallITMExpiryRegressionAlgorithm.cs | 7 +++---- ...ndexOptionCallITMGreeksExpiryRegressionAlgorithm.cs | 7 +++---- .../IndexOptionCallOTMExpiryRegressionAlgorithm.cs | 7 +++---- .../IndexOptionPutITMExpiryRegressionAlgorithm.cs | 7 +++---- .../IndexOptionPutOTMExpiryRegressionAlgorithm.cs | 7 +++---- ...IndexOptionShortCallITMExpiryRegressionAlgorithm.cs | 7 +++---- ...IndexOptionShortCallOTMExpiryRegressionAlgorithm.cs | 7 +++---- .../IndexOptionShortPutITMExpiryRegressionAlgorithm.cs | 7 +++---- .../IndexOptionShortPutOTMExpiryRegressionAlgorithm.cs | 7 +++---- ...yingPowerForAutomaticExerciseRegressionAlgorithm.cs | 2 +- .../OptionAssignmentRegressionAlgorithm.cs | 2 +- .../OptionChainFullDataRegressionAlgorithm.cs | 2 +- ...ionOTMExpiryOrderHasZeroPriceRegressionAlgorithm.cs | 7 +++---- .../OptionSymbolCanonicalRegressionAlgorithm.cs | 2 +- Algorithm.CSharp/OptionTimeSliceRegressionAlgorithm.cs | 7 +++---- ...niverseSelectionSymbolCacheRemovalRegressionTest.cs | 2 +- .../AddOptionContractExpiresRegressionAlgorithm.py | 10 +++++----- ...AddOptionContractFromUniverseRegressionAlgorithm.py | 2 +- ...tureOptionBuySellCallIntradayRegressionAlgorithm.py | 4 ++-- .../FutureOptionCallITMExpiryRegressionAlgorithm.py | 4 +--- .../FutureOptionCallOTMExpiryRegressionAlgorithm.py | 4 ++-- .../FutureOptionDailyRegressionAlgorithm.py | 4 ++-- .../FutureOptionPutITMExpiryRegressionAlgorithm.py | 4 +--- .../FutureOptionPutOTMExpiryRegressionAlgorithm.py | 2 +- ...utureOptionShortCallITMExpiryRegressionAlgorithm.py | 2 +- ...utureOptionShortCallOTMExpiryRegressionAlgorithm.py | 2 +- ...FutureOptionShortPutITMExpiryRegressionAlgorithm.py | 2 +- ...FutureOptionShortPutOTMExpiryRegressionAlgorithm.py | 2 +- ...ndexOptionBuySellCallIntradayRegressionAlgorithm.py | 4 ++-- .../IndexOptionCallITMExpiryRegressionAlgorithm.py | 4 +--- ...ndexOptionCallITMGreeksExpiryRegressionAlgorithm.py | 4 +--- .../IndexOptionCallOTMExpiryRegressionAlgorithm.py | 2 +- .../IndexOptionPutITMExpiryRegressionAlgorithm.py | 4 +--- .../IndexOptionPutOTMExpiryRegressionAlgorithm.py | 4 +--- ...IndexOptionShortCallITMExpiryRegressionAlgorithm.py | 2 +- ...IndexOptionShortCallOTMExpiryRegressionAlgorithm.py | 4 +--- .../IndexOptionShortPutITMExpiryRegressionAlgorithm.py | 4 +--- .../IndexOptionShortPutOTMExpiryRegressionAlgorithm.py | 4 +--- .../OptionChainFullDataRegressionAlgorithm.py | 5 +++-- Algorithm/QCAlgorithm.cs | 9 +++------ 63 files changed, 136 insertions(+), 184 deletions(-) diff --git a/Algorithm.CSharp/AddAndRemoveOptionContractRegressionAlgorithm.cs b/Algorithm.CSharp/AddAndRemoveOptionContractRegressionAlgorithm.cs index 85073972a3c2..4ae44e2c4905 100644 --- a/Algorithm.CSharp/AddAndRemoveOptionContractRegressionAlgorithm.cs +++ b/Algorithm.CSharp/AddAndRemoveOptionContractRegressionAlgorithm.cs @@ -41,9 +41,9 @@ public override void Initialize() var aapl = QuantConnect.Symbol.Create("AAPL", SecurityType.Equity, Market.USA); _contract = OptionChain(aapl) - .OrderBy(x => x.Symbol.ID.Symbol) - .FirstOrDefault(optionContract => optionContract.Symbol.ID.OptionRight == OptionRight.Call - && optionContract.Symbol.ID.OptionStyle == OptionStyle.American)?.Symbol; + .OrderBy(x => x.ID.Symbol) + .FirstOrDefault(optionContract => optionContract.ID.OptionRight == OptionRight.Call + && optionContract.ID.OptionStyle == OptionStyle.American); AddOptionContract(_contract); } diff --git a/Algorithm.CSharp/AddAndRemoveSecuritySameLoopRegressionAlgorithm.cs b/Algorithm.CSharp/AddAndRemoveSecuritySameLoopRegressionAlgorithm.cs index afa1a1066a5e..af89a0823cbe 100644 --- a/Algorithm.CSharp/AddAndRemoveSecuritySameLoopRegressionAlgorithm.cs +++ b/Algorithm.CSharp/AddAndRemoveSecuritySameLoopRegressionAlgorithm.cs @@ -40,9 +40,9 @@ public override void Initialize() var aapl = AddEquity("AAPL").Symbol; _contract = OptionChain(aapl) - .OrderBy(x => x.Symbol.ID.Symbol) - .FirstOrDefault(optionContract => optionContract.Symbol.ID.OptionRight == OptionRight.Call - && optionContract.Symbol.ID.OptionStyle == OptionStyle.American); + .OrderBy(x => x.ID.Symbol) + .FirstOrDefault(optionContract => optionContract.ID.OptionRight == OptionRight.Call + && optionContract.ID.OptionStyle == OptionStyle.American); } public override void OnData(Slice slice) diff --git a/Algorithm.CSharp/AddFutureOptionContractFromFutureChainRegressionAlgorithm.cs b/Algorithm.CSharp/AddFutureOptionContractFromFutureChainRegressionAlgorithm.cs index 57b3b03f728a..cc31259c1d97 100644 --- a/Algorithm.CSharp/AddFutureOptionContractFromFutureChainRegressionAlgorithm.cs +++ b/Algorithm.CSharp/AddFutureOptionContractFromFutureChainRegressionAlgorithm.cs @@ -49,7 +49,7 @@ public override void OnData(Slice slice) { foreach (var contract in futuresContracts) { - var option_contract_symbols = OptionChain(contract.Symbol).Select(x => x.Symbol).ToList(); + var option_contract_symbols = OptionChain(contract.Symbol).ToList(); if(option_contract_symbols.Count == 0) { continue; diff --git a/Algorithm.CSharp/AddOptionContractExpiresRegressionAlgorithm.cs b/Algorithm.CSharp/AddOptionContractExpiresRegressionAlgorithm.cs index ab7f6d6d53bc..c6d8bbd98b0e 100644 --- a/Algorithm.CSharp/AddOptionContractExpiresRegressionAlgorithm.cs +++ b/Algorithm.CSharp/AddOptionContractExpiresRegressionAlgorithm.cs @@ -47,10 +47,10 @@ public override void OnData(Slice slice) if (_option == null) { var option = OptionChain(_twx) - .OrderBy(x => x.Symbol.ID.Symbol) - .FirstOrDefault(optionContract => optionContract.Symbol.ID.Date == _expiration - && optionContract.Symbol.ID.OptionRight == OptionRight.Call - && optionContract.Symbol.ID.OptionStyle == OptionStyle.American)?.Symbol; + .OrderBy(x => x.ID.Symbol) + .FirstOrDefault(optionContract => optionContract.ID.Date == _expiration + && optionContract.ID.OptionRight == OptionRight.Call + && optionContract.ID.OptionStyle == OptionStyle.American); if (option != null) { _option = AddOptionContract(option).Symbol; diff --git a/Algorithm.CSharp/AddOptionContractFromUniverseRegressionAlgorithm.cs b/Algorithm.CSharp/AddOptionContractFromUniverseRegressionAlgorithm.cs index ca342c973db6..88c72be803f0 100644 --- a/Algorithm.CSharp/AddOptionContractFromUniverseRegressionAlgorithm.cs +++ b/Algorithm.CSharp/AddOptionContractFromUniverseRegressionAlgorithm.cs @@ -111,10 +111,10 @@ public override void OnSecuritiesChanged(SecurityChanges changes) foreach (var addedSecurity in changes.AddedSecurities) { var option = OptionChain(addedSecurity.Symbol) - .OrderBy(contractData => contractData.Symbol.ID.Symbol) - .First(optionContract => optionContract.Symbol.ID.Date == _expiration - && optionContract.Symbol.ID.OptionRight == OptionRight.Call - && optionContract.Symbol.ID.OptionStyle == OptionStyle.American); + .OrderBy(contractData => contractData.ID.Symbol) + .First(optionContract => optionContract.ID.Date == _expiration + && optionContract.ID.OptionRight == OptionRight.Call + && optionContract.ID.OptionStyle == OptionStyle.American); AddOptionContract(option); foreach (var symbol in new[] { option.Symbol, option.Underlying.Symbol }) diff --git a/Algorithm.CSharp/AddOptionContractTwiceRegressionAlgorithm.cs b/Algorithm.CSharp/AddOptionContractTwiceRegressionAlgorithm.cs index ecf3c31f32ca..c36a31ead83b 100644 --- a/Algorithm.CSharp/AddOptionContractTwiceRegressionAlgorithm.cs +++ b/Algorithm.CSharp/AddOptionContractTwiceRegressionAlgorithm.cs @@ -44,10 +44,9 @@ public override void Initialize() var aapl = QuantConnect.Symbol.Create("AAPL", SecurityType.Equity, Market.USA); _contract = OptionChain(aapl) - .OrderBy(x => x.Symbol.ID.StrikePrice) - .FirstOrDefault(optionContract => optionContract.Symbol.ID.OptionRight == OptionRight.Call - && optionContract.Symbol.ID.OptionStyle == OptionStyle.American) - .Symbol; + .OrderBy(x => x.ID.StrikePrice) + .FirstOrDefault(optionContract => optionContract.ID.OptionRight == OptionRight.Call + && optionContract.ID.OptionStyle == OptionStyle.American); AddOptionContract(_contract); } diff --git a/Algorithm.CSharp/AddTwoAndRemoveOneOptionContractRegressionAlgorithm.cs b/Algorithm.CSharp/AddTwoAndRemoveOneOptionContractRegressionAlgorithm.cs index 237b90c15bd0..64e01265ed97 100644 --- a/Algorithm.CSharp/AddTwoAndRemoveOneOptionContractRegressionAlgorithm.cs +++ b/Algorithm.CSharp/AddTwoAndRemoveOneOptionContractRegressionAlgorithm.cs @@ -42,11 +42,10 @@ public override void Initialize() var aapl = QuantConnect.Symbol.Create("AAPL", SecurityType.Equity, Market.USA); var contracts = OptionChain(aapl) - .OrderBy(x => x.Symbol.ID.StrikePrice) - .Where(optionContract => optionContract.Symbol.ID.OptionRight == OptionRight.Call - && optionContract.Symbol.ID.OptionStyle == OptionStyle.American) + .OrderBy(x => x.ID.StrikePrice) + .Where(optionContract => optionContract.ID.OptionRight == OptionRight.Call + && optionContract.ID.OptionStyle == OptionStyle.American) .Take(2) - .Select(x => x.Symbol) .ToList(); _contract1 = contracts[0]; diff --git a/Algorithm.CSharp/BasicTemplateOptionsAlgorithm.cs b/Algorithm.CSharp/BasicTemplateOptionsAlgorithm.cs index ac9f1a403b77..69964fc7ff6c 100644 --- a/Algorithm.CSharp/BasicTemplateOptionsAlgorithm.cs +++ b/Algorithm.CSharp/BasicTemplateOptionsAlgorithm.cs @@ -48,10 +48,7 @@ public override void Initialize() _optionSymbol = option.Symbol; // set our strike/expiry filter for this option chain - option.SetFilter(u => u.Strikes(-2, +2) - // Expiration method accepts TimeSpan objects or integer for days. - // The following statements yield the same filtering criteria - .Expiration(0, 180)); + option.SetFilter(u => u); // .Expiration(TimeSpan.Zero, TimeSpan.FromDays(180))); // use the underlying equity as the benchmark diff --git a/Algorithm.CSharp/DelayedSettlementAfterManualSecurityRemovalAlgorithm.cs b/Algorithm.CSharp/DelayedSettlementAfterManualSecurityRemovalAlgorithm.cs index fd27d8c14e13..d2fc422e32a4 100644 --- a/Algorithm.CSharp/DelayedSettlementAfterManualSecurityRemovalAlgorithm.cs +++ b/Algorithm.CSharp/DelayedSettlementAfterManualSecurityRemovalAlgorithm.cs @@ -38,10 +38,9 @@ public override void Initialize() var equity = AddEquity("GOOG"); _optionSymbol = OptionChain(equity.Symbol) - .OrderBy(x => x.Symbol.ID.StrikePrice) - .ThenByDescending(x => x.Symbol.ID.Date) - .First(optionContract => optionContract.Symbol.ID.OptionRight == OptionRight.Call) - .Symbol; + .OrderBy(x => x.ID.StrikePrice) + .ThenByDescending(x => x.ID.Date) + .First(optionContract => optionContract.ID.OptionRight == OptionRight.Call); var option = AddOptionContract(_optionSymbol); option.SetSettlementModel(new DelayedSettlementModel(Option.DefaultSettlementDays, Option.DefaultSettlementTime)); diff --git a/Algorithm.CSharp/DelistedIndexOptionDivestedRegression.cs b/Algorithm.CSharp/DelistedIndexOptionDivestedRegression.cs index 61556acfe751..c5520a8548a9 100644 --- a/Algorithm.CSharp/DelistedIndexOptionDivestedRegression.cs +++ b/Algorithm.CSharp/DelistedIndexOptionDivestedRegression.cs @@ -51,8 +51,7 @@ public override void OnData(Slice slice) if (_addOption) { - var contracts = OptionChain(_spx) - .Where(x => x.Symbol.ID.OptionRight == OptionRight.Put && x.Symbol.ID.Date.Date == new DateTime(2021, 1, 15)); + var contracts = OptionChain(_spx).Where(x => x.ID.OptionRight == OptionRight.Put && x.ID.Date.Date == new DateTime(2021, 1, 15)); var option = AddIndexOptionContract(contracts.First(), Resolution.Minute); _optionExpiry = option.Expiry; diff --git a/Algorithm.CSharp/FutureOptionBuySellCallIntradayRegressionAlgorithm.cs b/Algorithm.CSharp/FutureOptionBuySellCallIntradayRegressionAlgorithm.cs index 42a5a13e8806..0b2df56cc07b 100644 --- a/Algorithm.CSharp/FutureOptionBuySellCallIntradayRegressionAlgorithm.cs +++ b/Algorithm.CSharp/FutureOptionBuySellCallIntradayRegressionAlgorithm.cs @@ -60,8 +60,8 @@ public override void Initialize() // Select a future option expiring ITM, and adds it to the algorithm. var esOptions = OptionChain(es20m20) .Concat(OptionChain(es20h20)) - .Where(contractData => contractData.Symbol.ID.StrikePrice == 3200m && contractData.Symbol.ID.OptionRight == OptionRight.Call) - .Select(contractData => AddFutureOptionContract(contractData.Symbol, Resolution.Minute).Symbol) + .Where(contractData => contractData.ID.StrikePrice == 3200m && contractData.ID.OptionRight == OptionRight.Call) + .Select(contractData => AddFutureOptionContract(contractData, Resolution.Minute).Symbol) .ToList(); var expectedContracts = new[] diff --git a/Algorithm.CSharp/FutureOptionCallITMExpiryRegressionAlgorithm.cs b/Algorithm.CSharp/FutureOptionCallITMExpiryRegressionAlgorithm.cs index 7ba533fb4ba1..25f88b83b090 100644 --- a/Algorithm.CSharp/FutureOptionCallITMExpiryRegressionAlgorithm.cs +++ b/Algorithm.CSharp/FutureOptionCallITMExpiryRegressionAlgorithm.cs @@ -54,11 +54,10 @@ public override void Initialize() // Select a future option expiring ITM, and adds it to the algorithm. _esOption = AddFutureOptionContract(OptionChain(_es19m20) - .Where(x => x.Symbol.ID.StrikePrice <= 3200m && x.Symbol.ID.OptionRight == OptionRight.Call) - .OrderByDescending(x => x.Symbol.ID.StrikePrice) + .Where(x => x.ID.StrikePrice <= 3200m && x.ID.OptionRight == OptionRight.Call) + .OrderByDescending(x => x.ID.StrikePrice) .Take(1) - .Single() - .Symbol, Resolution.Minute).Symbol; + .Single(), Resolution.Minute).Symbol; _expectedOptionContract = QuantConnect.Symbol.CreateOption(_es19m20, Market.CME, OptionStyle.American, OptionRight.Call, 3200m, new DateTime(2020, 6, 19)); if (_esOption != _expectedOptionContract) diff --git a/Algorithm.CSharp/FutureOptionCallITMGreeksExpiryRegressionAlgorithm.cs b/Algorithm.CSharp/FutureOptionCallITMGreeksExpiryRegressionAlgorithm.cs index de2ba17ed5b5..68bce654dfe6 100644 --- a/Algorithm.CSharp/FutureOptionCallITMGreeksExpiryRegressionAlgorithm.cs +++ b/Algorithm.CSharp/FutureOptionCallITMGreeksExpiryRegressionAlgorithm.cs @@ -56,11 +56,10 @@ public override void Initialize() // Select a future option expiring ITM, and adds it to the algorithm. _esOption = AddFutureOptionContract(OptionChain(_es19m20.Symbol) - .Where(x => x.Symbol.ID.StrikePrice <= 3200m && x.Symbol.ID.OptionRight == OptionRight.Call) - .OrderByDescending(x => x.Symbol.ID.StrikePrice) + .Where(x => x.ID.StrikePrice <= 3200m && x.ID.OptionRight == OptionRight.Call) + .OrderByDescending(x => x.ID.StrikePrice) .Take(1) - .Single() - .Symbol, Resolution.Minute); + .Single(), Resolution.Minute); _esOption.PriceModel = OptionPriceModels.BjerksundStensland(); diff --git a/Algorithm.CSharp/FutureOptionCallOTMExpiryRegressionAlgorithm.cs b/Algorithm.CSharp/FutureOptionCallOTMExpiryRegressionAlgorithm.cs index d212d808c4df..19c4bfb64db7 100644 --- a/Algorithm.CSharp/FutureOptionCallOTMExpiryRegressionAlgorithm.cs +++ b/Algorithm.CSharp/FutureOptionCallOTMExpiryRegressionAlgorithm.cs @@ -60,11 +60,10 @@ public override void Initialize() // Select a future option call expiring OTM, and adds it to the algorithm. _esOption = AddFutureOptionContract(OptionChain(_es19m20) - .Where(contractData => contractData.Symbol.ID.StrikePrice >= 3300m && contractData.Symbol.ID.OptionRight == OptionRight.Call) - .OrderBy(contractData => contractData.Symbol.ID.StrikePrice) + .Where(contractData => contractData.ID.StrikePrice >= 3300m && contractData.ID.OptionRight == OptionRight.Call) + .OrderBy(contractData => contractData.ID.StrikePrice) .Take(1) - .Single() - .Symbol, Resolution.Minute).Symbol; + .Single(), Resolution.Minute).Symbol; _expectedContract = QuantConnect.Symbol.CreateOption(_es19m20, Market.CME, OptionStyle.American, OptionRight.Call, 3300m, new DateTime(2020, 6, 19)); if (_esOption != _expectedContract) diff --git a/Algorithm.CSharp/FutureOptionDailyRegressionAlgorithm.cs b/Algorithm.CSharp/FutureOptionDailyRegressionAlgorithm.cs index c3fe3eca3f77..0a94b6140b9f 100644 --- a/Algorithm.CSharp/FutureOptionDailyRegressionAlgorithm.cs +++ b/Algorithm.CSharp/FutureOptionDailyRegressionAlgorithm.cs @@ -48,8 +48,8 @@ public override void Initialize() // Attempt to fetch a specific future option contract DcOption = OptionChain(dc) - .Where(x => x.Symbol.ID.StrikePrice == 17m && x.Symbol.ID.OptionRight == OptionRight.Call) - .Select(x => AddFutureOptionContract(x.Symbol, Resolution).Symbol) + .Where(x => x.ID.StrikePrice == 17m && x.ID.OptionRight == OptionRight.Call) + .Select(x => AddFutureOptionContract(x, Resolution).Symbol) .FirstOrDefault(); // Validate it is the expected contract diff --git a/Algorithm.CSharp/FutureOptionIndicatorsRegressionAlgorithm.cs b/Algorithm.CSharp/FutureOptionIndicatorsRegressionAlgorithm.cs index 1a24bb695310..96a22e915009 100644 --- a/Algorithm.CSharp/FutureOptionIndicatorsRegressionAlgorithm.cs +++ b/Algorithm.CSharp/FutureOptionIndicatorsRegressionAlgorithm.cs @@ -33,11 +33,10 @@ public override void Initialize() Resolution.Minute).Symbol; var option = AddFutureOptionContract(OptionChain(underlying) - .Where(x => x.Symbol.ID.StrikePrice <= 3200m && x.Symbol.ID.OptionRight == OptionRight.Call) - .OrderByDescending(x => x.Symbol.ID.StrikePrice) + .Where(x => x.ID.StrikePrice <= 3200m && x.ID.OptionRight == OptionRight.Call) + .OrderByDescending(x => x.ID.StrikePrice) .Take(1) - .Single() - .Symbol, Resolution.Minute).Symbol; + .Single(), Resolution.Minute).Symbol; InitializeIndicators(option); } diff --git a/Algorithm.CSharp/FutureOptionPutITMExpiryRegressionAlgorithm.cs b/Algorithm.CSharp/FutureOptionPutITMExpiryRegressionAlgorithm.cs index f733f53f1e93..c0a01fa99020 100644 --- a/Algorithm.CSharp/FutureOptionPutITMExpiryRegressionAlgorithm.cs +++ b/Algorithm.CSharp/FutureOptionPutITMExpiryRegressionAlgorithm.cs @@ -55,11 +55,10 @@ public override void Initialize() // Select a future option expiring ITM, and adds it to the algorithm. _esOption = AddFutureOptionContract(OptionChain(_es19m20) - .Where(x => x.Symbol.ID.StrikePrice >= 3300m && x.Symbol.ID.OptionRight == OptionRight.Put) - .OrderBy(x => x.Symbol.ID.StrikePrice) + .Where(x => x.ID.StrikePrice >= 3300m && x.ID.OptionRight == OptionRight.Put) + .OrderBy(x => x.ID.StrikePrice) .Take(1) - .Single() - .Symbol, Resolution.Minute).Symbol; + .Single(), Resolution.Minute).Symbol; _expectedContract = QuantConnect.Symbol.CreateOption(_es19m20, Market.CME, OptionStyle.American, OptionRight.Put, 3300m, new DateTime(2020, 6, 19)); if (_esOption != _expectedContract) diff --git a/Algorithm.CSharp/FutureOptionPutOTMExpiryRegressionAlgorithm.cs b/Algorithm.CSharp/FutureOptionPutOTMExpiryRegressionAlgorithm.cs index 1303e44e449d..50d24d7ca9df 100644 --- a/Algorithm.CSharp/FutureOptionPutOTMExpiryRegressionAlgorithm.cs +++ b/Algorithm.CSharp/FutureOptionPutOTMExpiryRegressionAlgorithm.cs @@ -59,11 +59,10 @@ public override void Initialize() // Select a future option expiring ITM, and adds it to the algorithm. _esOption = AddFutureOptionContract(OptionChain(_es19m20) - .Where(x => x.Symbol.ID.StrikePrice <= 3150m && x.Symbol.ID.OptionRight == OptionRight.Put) - .OrderByDescending(x => x.Symbol.ID.StrikePrice) + .Where(x => x.ID.StrikePrice <= 3150m && x.ID.OptionRight == OptionRight.Put) + .OrderByDescending(x => x.ID.StrikePrice) .Take(1) - .Single() - .Symbol, Resolution.Minute).Symbol; + .Single(), Resolution.Minute).Symbol; _expectedContract = QuantConnect.Symbol.CreateOption(_es19m20, Market.CME, OptionStyle.American, OptionRight.Put, 3150m, new DateTime(2020, 6, 19)); if (_esOption != _expectedContract) diff --git a/Algorithm.CSharp/FutureOptionShortCallITMExpiryRegressionAlgorithm.cs b/Algorithm.CSharp/FutureOptionShortCallITMExpiryRegressionAlgorithm.cs index 8588c91f1a03..d97531471a15 100644 --- a/Algorithm.CSharp/FutureOptionShortCallITMExpiryRegressionAlgorithm.cs +++ b/Algorithm.CSharp/FutureOptionShortCallITMExpiryRegressionAlgorithm.cs @@ -55,11 +55,10 @@ public override void Initialize() // Select a future option expiring ITM, and adds it to the algorithm. _esOption = AddFutureOptionContract(OptionChain(_es19m20) - .Where(x => x.Symbol.ID.StrikePrice <= 3100m && x.Symbol.ID.OptionRight == OptionRight.Call) - .OrderByDescending(x => x.Symbol.ID.StrikePrice) + .Where(x => x.ID.StrikePrice <= 3100m && x.ID.OptionRight == OptionRight.Call) + .OrderByDescending(x => x.ID.StrikePrice) .Take(1) - .Single() - .Symbol, Resolution.Minute).Symbol; + .Single(), Resolution.Minute).Symbol; _expectedContract = QuantConnect.Symbol.CreateOption(_es19m20, Market.CME, OptionStyle.American, OptionRight.Call, 3100m, new DateTime(2020, 6, 19)); if (_esOption != _expectedContract) diff --git a/Algorithm.CSharp/FutureOptionShortCallOTMExpiryRegressionAlgorithm.cs b/Algorithm.CSharp/FutureOptionShortCallOTMExpiryRegressionAlgorithm.cs index 442c14b18a3f..3387daef3906 100644 --- a/Algorithm.CSharp/FutureOptionShortCallOTMExpiryRegressionAlgorithm.cs +++ b/Algorithm.CSharp/FutureOptionShortCallOTMExpiryRegressionAlgorithm.cs @@ -56,11 +56,10 @@ public override void Initialize() // Select a future option expiring ITM, and adds it to the algorithm. _esOption = AddFutureOptionContract(OptionChain(_es19m20) - .Where(x => x.Symbol.ID.StrikePrice >= 3400m && x.Symbol.ID.OptionRight == OptionRight.Call) - .OrderBy(x => x.Symbol.ID.StrikePrice) + .Where(x => x.ID.StrikePrice >= 3400m && x.ID.OptionRight == OptionRight.Call) + .OrderBy(x => x.ID.StrikePrice) .Take(1) - .Single() - .Symbol, Resolution.Minute).Symbol; + .Single(), Resolution.Minute).Symbol; _expectedContract = QuantConnect.Symbol.CreateOption(_es19m20, Market.CME, OptionStyle.American, OptionRight.Call, 3400m, new DateTime(2020, 6, 19)); if (_esOption != _expectedContract) diff --git a/Algorithm.CSharp/FutureOptionShortPutITMExpiryRegressionAlgorithm.cs b/Algorithm.CSharp/FutureOptionShortPutITMExpiryRegressionAlgorithm.cs index 94107fc3f96f..deedc5a91894 100644 --- a/Algorithm.CSharp/FutureOptionShortPutITMExpiryRegressionAlgorithm.cs +++ b/Algorithm.CSharp/FutureOptionShortPutITMExpiryRegressionAlgorithm.cs @@ -55,11 +55,10 @@ public override void Initialize() // Select a future option expiring ITM, and adds it to the algorithm. _esOption = AddFutureOptionContract(OptionChain(_es19m20) - .Where(x => x.Symbol.ID.StrikePrice <= 3400m && x.Symbol.ID.OptionRight == OptionRight.Put) - .OrderByDescending(x => x.Symbol.ID.StrikePrice) + .Where(x => x.ID.StrikePrice <= 3400m && x.ID.OptionRight == OptionRight.Put) + .OrderByDescending(x => x.ID.StrikePrice) .Take(1) - .Single() - .Symbol, Resolution.Minute).Symbol; + .Single(), Resolution.Minute).Symbol; _expectedContract = QuantConnect.Symbol.CreateOption(_es19m20, Market.CME, OptionStyle.American, OptionRight.Put, 3400m, new DateTime(2020, 6, 19)); if (_esOption != _expectedContract) diff --git a/Algorithm.CSharp/FutureOptionShortPutOTMExpiryRegressionAlgorithm.cs b/Algorithm.CSharp/FutureOptionShortPutOTMExpiryRegressionAlgorithm.cs index 6b5fda32c9b2..cb9e6c11a9b9 100644 --- a/Algorithm.CSharp/FutureOptionShortPutOTMExpiryRegressionAlgorithm.cs +++ b/Algorithm.CSharp/FutureOptionShortPutOTMExpiryRegressionAlgorithm.cs @@ -56,11 +56,10 @@ public override void Initialize() // Select a future option expiring ITM, and adds it to the algorithm. _esOption = AddFutureOptionContract(OptionChain(_es19m20) - .Where(x => x.Symbol.ID.StrikePrice <= 3000m && x.Symbol.ID.OptionRight == OptionRight.Put) - .OrderByDescending(x => x.Symbol.ID.StrikePrice) + .Where(x => x.ID.StrikePrice <= 3000m && x.ID.OptionRight == OptionRight.Put) + .OrderByDescending(x => x.ID.StrikePrice) .Take(1) - .Single() - .Symbol, Resolution.Minute).Symbol; + .Single(), Resolution.Minute).Symbol; _expectedContract = QuantConnect.Symbol.CreateOption(_es19m20, Market.CME, OptionStyle.American, OptionRight.Put, 3000m, new DateTime(2020, 6, 19)); if (_esOption != _expectedContract) diff --git a/Algorithm.CSharp/IndexOptionBuySellCallIntradayRegressionAlgorithm.cs b/Algorithm.CSharp/IndexOptionBuySellCallIntradayRegressionAlgorithm.cs index 35601b325a63..1de0f5a77fbb 100644 --- a/Algorithm.CSharp/IndexOptionBuySellCallIntradayRegressionAlgorithm.cs +++ b/Algorithm.CSharp/IndexOptionBuySellCallIntradayRegressionAlgorithm.cs @@ -47,8 +47,8 @@ public override void Initialize() // Select a index option expiring ITM, and adds it to the algorithm. var spxOptions = OptionChain(spx) - .Where(x => (x.Symbol.ID.StrikePrice == 3700m || x.Symbol.ID.StrikePrice == 3800m) && x.Symbol.ID.OptionRight == OptionRight.Call && x.Symbol.ID.Date.Year == 2021 && x.Symbol.ID.Date.Month == 1) - .Select(x => AddIndexOptionContract(x.Symbol, Resolution.Minute).Symbol) + .Where(x => (x.ID.StrikePrice == 3700m || x.ID.StrikePrice == 3800m) && x.ID.OptionRight == OptionRight.Call && x.ID.Date.Year == 2021 && x.ID.Date.Month == 1) + .Select(x => AddIndexOptionContract(x, Resolution.Minute).Symbol) .OrderBy(x => x.ID.StrikePrice) .ToList(); diff --git a/Algorithm.CSharp/IndexOptionCallITMExpiryRegressionAlgorithm.cs b/Algorithm.CSharp/IndexOptionCallITMExpiryRegressionAlgorithm.cs index 7d1da74da4b8..f868b73299b3 100644 --- a/Algorithm.CSharp/IndexOptionCallITMExpiryRegressionAlgorithm.cs +++ b/Algorithm.CSharp/IndexOptionCallITMExpiryRegressionAlgorithm.cs @@ -52,11 +52,10 @@ public override void Initialize() // Select an index option expiring ITM, and adds it to the algorithm. _spxOption = AddIndexOptionContract(OptionChain(_spx) - .Where(x => x.Symbol.ID.StrikePrice <= 3200m && x.Symbol.ID.OptionRight == OptionRight.Call && x.Symbol.ID.Date.Year == 2021 && x.Symbol.ID.Date.Month == 1) - .OrderByDescending(x => x.Symbol.ID.StrikePrice) + .Where(x => x.ID.StrikePrice <= 3200m && x.ID.OptionRight == OptionRight.Call && x.ID.Date.Year == 2021 && x.ID.Date.Month == 1) + .OrderByDescending(x => x.ID.StrikePrice) .Take(1) - .Single() - .Symbol, Resolution).Symbol; + .Single(), Resolution).Symbol; _expectedOptionContract = QuantConnect.Symbol.CreateOption(_spx, Market.USA, OptionStyle.European, OptionRight.Call, 3200m, new DateTime(2021, 1, 15)); if (_spxOption != _expectedOptionContract) diff --git a/Algorithm.CSharp/IndexOptionCallITMGreeksExpiryRegressionAlgorithm.cs b/Algorithm.CSharp/IndexOptionCallITMGreeksExpiryRegressionAlgorithm.cs index 39b3a637f886..7b845267bf3e 100644 --- a/Algorithm.CSharp/IndexOptionCallITMGreeksExpiryRegressionAlgorithm.cs +++ b/Algorithm.CSharp/IndexOptionCallITMGreeksExpiryRegressionAlgorithm.cs @@ -46,11 +46,10 @@ public override void Initialize() // Select an index option expiring ITM, and adds it to the algorithm. _spxOption = AddIndexOptionContract(OptionChain(_spx) - .Where(x => x.Symbol.ID.StrikePrice <= 3200m && x.Symbol.ID.OptionRight == OptionRight.Call && x.Symbol.ID.Date.Year == 2021 && x.Symbol.ID.Date.Month == 1) - .OrderByDescending(x => x.Symbol.ID.StrikePrice) + .Where(x => x.ID.StrikePrice <= 3200m && x.ID.OptionRight == OptionRight.Call && x.ID.Date.Year == 2021 && x.ID.Date.Month == 1) + .OrderByDescending(x => x.ID.StrikePrice) .Take(1) - .Single() - .Symbol, Resolution.Minute); + .Single(), Resolution.Minute); _spxOption.PriceModel = OptionPriceModels.BlackScholes(); diff --git a/Algorithm.CSharp/IndexOptionCallOTMExpiryRegressionAlgorithm.cs b/Algorithm.CSharp/IndexOptionCallOTMExpiryRegressionAlgorithm.cs index e40c19fcf4e8..9e351045a653 100644 --- a/Algorithm.CSharp/IndexOptionCallOTMExpiryRegressionAlgorithm.cs +++ b/Algorithm.CSharp/IndexOptionCallOTMExpiryRegressionAlgorithm.cs @@ -57,11 +57,10 @@ public override void Initialize() // Select a index option call expiring OTM, and adds it to the algorithm. _spxOption = AddIndexOptionContract(OptionChain(_spx) - .Where(x => x.Symbol.ID.StrikePrice >= 4250m && x.Symbol.ID.OptionRight == OptionRight.Call && x.Symbol.ID.Date.Year == 2021 && x.Symbol.ID.Date.Month == 1) - .OrderBy(x => x.Symbol.ID.StrikePrice) + .Where(x => x.ID.StrikePrice >= 4250m && x.ID.OptionRight == OptionRight.Call && x.ID.Date.Year == 2021 && x.ID.Date.Month == 1) + .OrderBy(x => x.ID.StrikePrice) .Take(1) - .Single() - .Symbol, Resolution).Symbol; + .Single(), Resolution).Symbol; _expectedContract = QuantConnect.Symbol.CreateOption(_spx, Market.USA, OptionStyle.European, OptionRight.Call, 4250m, new DateTime(2021, 1, 15)); if (_spxOption != _expectedContract) diff --git a/Algorithm.CSharp/IndexOptionPutITMExpiryRegressionAlgorithm.cs b/Algorithm.CSharp/IndexOptionPutITMExpiryRegressionAlgorithm.cs index 69415a3f6867..02b54cc168b1 100644 --- a/Algorithm.CSharp/IndexOptionPutITMExpiryRegressionAlgorithm.cs +++ b/Algorithm.CSharp/IndexOptionPutITMExpiryRegressionAlgorithm.cs @@ -49,11 +49,10 @@ public override void Initialize() // Select a index option expiring ITM, and adds it to the algorithm. _spxOption = AddIndexOptionContract(OptionChain(_spx) - .Where(x => x.Symbol.ID.StrikePrice >= 4200m && x.Symbol.ID.OptionRight == OptionRight.Put && x.Symbol.ID.Date.Year == 2021 && x.Symbol.ID.Date.Month == 1) - .OrderBy(x => x.Symbol.ID.StrikePrice) + .Where(x => x.ID.StrikePrice >= 4200m && x.ID.OptionRight == OptionRight.Put && x.ID.Date.Year == 2021 && x.ID.Date.Month == 1) + .OrderBy(x => x.ID.StrikePrice) .Take(1) - .Single() - .Symbol, Resolution.Minute).Symbol; + .Single(), Resolution.Minute).Symbol; _expectedContract = QuantConnect.Symbol.CreateOption(_spx, Market.USA, OptionStyle.European, OptionRight.Put, 4200m, new DateTime(2021, 1, 15)); if (_spxOption != _expectedContract) diff --git a/Algorithm.CSharp/IndexOptionPutOTMExpiryRegressionAlgorithm.cs b/Algorithm.CSharp/IndexOptionPutOTMExpiryRegressionAlgorithm.cs index 6e86b14e5188..0e8ba5681485 100644 --- a/Algorithm.CSharp/IndexOptionPutOTMExpiryRegressionAlgorithm.cs +++ b/Algorithm.CSharp/IndexOptionPutOTMExpiryRegressionAlgorithm.cs @@ -54,11 +54,10 @@ public override void Initialize() // Select a index option expiring ITM, and adds it to the algorithm. _spxOption = AddIndexOptionContract(OptionChain(_spx) - .Where(x => x.Symbol.ID.StrikePrice <= 3200m && x.Symbol.ID.OptionRight == OptionRight.Put && x.Symbol.ID.Date.Year == 2021 && x.Symbol.ID.Date.Month == 1) - .OrderByDescending(x => x.Symbol.ID.StrikePrice) + .Where(x => x.ID.StrikePrice <= 3200m && x.ID.OptionRight == OptionRight.Put && x.ID.Date.Year == 2021 && x.ID.Date.Month == 1) + .OrderByDescending(x => x.ID.StrikePrice) .Take(1) - .Single() - .Symbol, Resolution.Minute).Symbol; + .Single(), Resolution.Minute).Symbol; _expectedContract = QuantConnect.Symbol.CreateOption(_spx, Market.USA, OptionStyle.European, OptionRight.Put, 3200m, new DateTime(2021, 1, 15)); if (_spxOption != _expectedContract) diff --git a/Algorithm.CSharp/IndexOptionShortCallITMExpiryRegressionAlgorithm.cs b/Algorithm.CSharp/IndexOptionShortCallITMExpiryRegressionAlgorithm.cs index 1e7f7d8fb8c9..60548ffc3de3 100644 --- a/Algorithm.CSharp/IndexOptionShortCallITMExpiryRegressionAlgorithm.cs +++ b/Algorithm.CSharp/IndexOptionShortCallITMExpiryRegressionAlgorithm.cs @@ -61,11 +61,10 @@ public override void Initialize() // Select a index option expiring ITM, and adds it to the algorithm. _esOption = AddIndexOptionContract(OptionChain(_spx) - .Where(contractData => contractData.Symbol.ID.StrikePrice <= 3200m && contractData.Symbol.ID.OptionRight == OptionRight.Call && contractData.Symbol.ID.Date.Year == 2021 && contractData.Symbol.ID.Date.Month == 1) - .OrderByDescending(contractData => contractData.Symbol.ID.StrikePrice) + .Where(contractData => contractData.ID.StrikePrice <= 3200m && contractData.ID.OptionRight == OptionRight.Call && contractData.ID.Date.Year == 2021 && contractData.ID.Date.Month == 1) + .OrderByDescending(contractData => contractData.ID.StrikePrice) .Take(1) - .Single() - .Symbol, Resolution.Minute).Symbol; + .Single(), Resolution.Minute).Symbol; _expectedContract = QuantConnect.Symbol.CreateOption(_spx, Market.USA, OptionStyle.European, OptionRight.Call, 3200m, new DateTime(2021, 1, 15)); if (_esOption != _expectedContract) diff --git a/Algorithm.CSharp/IndexOptionShortCallOTMExpiryRegressionAlgorithm.cs b/Algorithm.CSharp/IndexOptionShortCallOTMExpiryRegressionAlgorithm.cs index 0cba7712bc24..3eae41d4adff 100644 --- a/Algorithm.CSharp/IndexOptionShortCallOTMExpiryRegressionAlgorithm.cs +++ b/Algorithm.CSharp/IndexOptionShortCallOTMExpiryRegressionAlgorithm.cs @@ -51,11 +51,10 @@ public override void Initialize() // Select a index option expiring ITM, and adds it to the algorithm. _spxOption = AddIndexOptionContract(OptionChain(_spx) - .Where(x => x.Symbol.ID.StrikePrice >= 4250m && x.Symbol.ID.OptionRight == OptionRight.Call && x.Symbol.ID.Date.Year == 2021 && x.Symbol.ID.Date.Month == 1) - .OrderBy(x => x.Symbol.ID.StrikePrice) + .Where(x => x.ID.StrikePrice >= 4250m && x.ID.OptionRight == OptionRight.Call && x.ID.Date.Year == 2021 && x.ID.Date.Month == 1) + .OrderBy(x => x.ID.StrikePrice) .Take(1) - .Single() - .Symbol, Resolution.Minute).Symbol; + .Single(), Resolution.Minute).Symbol; _expectedContract = QuantConnect.Symbol.CreateOption(_spx, Market.USA, OptionStyle.European, OptionRight.Call, 4250m, new DateTime(2021, 1, 15)); if (_spxOption != _expectedContract) diff --git a/Algorithm.CSharp/IndexOptionShortPutITMExpiryRegressionAlgorithm.cs b/Algorithm.CSharp/IndexOptionShortPutITMExpiryRegressionAlgorithm.cs index 08576db310e8..e72fd7824d5a 100644 --- a/Algorithm.CSharp/IndexOptionShortPutITMExpiryRegressionAlgorithm.cs +++ b/Algorithm.CSharp/IndexOptionShortPutITMExpiryRegressionAlgorithm.cs @@ -60,11 +60,10 @@ public override void Initialize() // Select a index option expiring ITM, and adds it to the algorithm. _spxOption = AddIndexOptionContract(OptionChain(_spx) - .Where(contractData => contractData.Symbol.ID.StrikePrice <= 4200m && contractData.Symbol.ID.OptionRight == OptionRight.Put && contractData.Symbol.ID.Date.Year == 2021 && contractData.Symbol.ID.Date.Month == 1) - .OrderByDescending(contractData => contractData.Symbol.ID.StrikePrice) + .Where(contractData => contractData.ID.StrikePrice <= 4200m && contractData.ID.OptionRight == OptionRight.Put && contractData.ID.Date.Year == 2021 && contractData.ID.Date.Month == 1) + .OrderByDescending(contractData => contractData.ID.StrikePrice) .Take(1) - .Single() - .Symbol, Resolution.Minute).Symbol; + .Single(), Resolution.Minute).Symbol; _expectedContract = QuantConnect.Symbol.CreateOption(_spx, Market.USA, OptionStyle.European, OptionRight.Put, 4200m, new DateTime(2021, 1, 15)); if (_spxOption != _expectedContract) diff --git a/Algorithm.CSharp/IndexOptionShortPutOTMExpiryRegressionAlgorithm.cs b/Algorithm.CSharp/IndexOptionShortPutOTMExpiryRegressionAlgorithm.cs index c143b0a39b28..1861abe7d9d9 100644 --- a/Algorithm.CSharp/IndexOptionShortPutOTMExpiryRegressionAlgorithm.cs +++ b/Algorithm.CSharp/IndexOptionShortPutOTMExpiryRegressionAlgorithm.cs @@ -51,11 +51,10 @@ public override void Initialize() // Select a index option expiring ITM, and adds it to the algorithm. _spxOption = AddIndexOptionContract(OptionChain(_spx) - .Where(x => x.Symbol.ID.StrikePrice <= 3200m && x.Symbol.ID.OptionRight == OptionRight.Put && x.Symbol.ID.Date.Year == 2021 && x.Symbol.ID.Date.Month == 1) - .OrderByDescending(x => x.Symbol.ID.StrikePrice) + .Where(x => x.ID.StrikePrice <= 3200m && x.ID.OptionRight == OptionRight.Put && x.ID.Date.Year == 2021 && x.ID.Date.Month == 1) + .OrderByDescending(x => x.ID.StrikePrice) .Take(1) - .Single() - .Symbol, Resolution.Minute).Symbol; + .Single(), Resolution.Minute).Symbol; _expectedContract = QuantConnect.Symbol.CreateOption(_spx, Market.USA, OptionStyle.European, OptionRight.Put, 3200m, new DateTime(2021, 1, 15)); if (_spxOption != _expectedContract) diff --git a/Algorithm.CSharp/InsufficientBuyingPowerForAutomaticExerciseRegressionAlgorithm.cs b/Algorithm.CSharp/InsufficientBuyingPowerForAutomaticExerciseRegressionAlgorithm.cs index c718b5646d6e..9557e626bcdd 100644 --- a/Algorithm.CSharp/InsufficientBuyingPowerForAutomaticExerciseRegressionAlgorithm.cs +++ b/Algorithm.CSharp/InsufficientBuyingPowerForAutomaticExerciseRegressionAlgorithm.cs @@ -44,7 +44,7 @@ public override void Initialize() _stock = AddEquity("GOOG").Symbol; - var contracts = OptionChain(_stock).Select(x => x.Symbol).ToList(); + var contracts = OptionChain(_stock).ToList(); _option = contracts .Where(c => c.ID.OptionRight == OptionRight.Put) .OrderBy(c => c.ID.Date) diff --git a/Algorithm.CSharp/OptionAssignmentRegressionAlgorithm.cs b/Algorithm.CSharp/OptionAssignmentRegressionAlgorithm.cs index 42d5a541f565..b000d57959b4 100644 --- a/Algorithm.CSharp/OptionAssignmentRegressionAlgorithm.cs +++ b/Algorithm.CSharp/OptionAssignmentRegressionAlgorithm.cs @@ -46,7 +46,7 @@ public override void Initialize() SetCash(100000); Stock = AddEquity("GOOG", Resolution.Minute); - var contracts = OptionChain(Stock.Symbol).Select(x => x.Symbol).ToList(); + var contracts = OptionChain(Stock.Symbol).ToList(); PutOptionSymbol = contracts .Where(c => c.ID.OptionRight == OptionRight.Put) diff --git a/Algorithm.CSharp/OptionChainFullDataRegressionAlgorithm.cs b/Algorithm.CSharp/OptionChainFullDataRegressionAlgorithm.cs index 4ec5a0cd7c0e..ce1fe0705ab6 100644 --- a/Algorithm.CSharp/OptionChainFullDataRegressionAlgorithm.cs +++ b/Algorithm.CSharp/OptionChainFullDataRegressionAlgorithm.cs @@ -42,7 +42,7 @@ public override void Initialize() _optionContract = OptionChain(goog) // Get contracts expiring within 10 days, with an implied volatility greater than 0.5 and a delta less than 0.5 - .Where(contractData => contractData.Symbol.ID.Date - Time <= TimeSpan.FromDays(10) && + .Where(contractData => contractData.ID.Date - Time <= TimeSpan.FromDays(10) && contractData.ImpliedVolatility > 0.5m && contractData.Greeks.Delta < 0.5m) // Get the contract with the latest expiration date diff --git a/Algorithm.CSharp/OptionOTMExpiryOrderHasZeroPriceRegressionAlgorithm.cs b/Algorithm.CSharp/OptionOTMExpiryOrderHasZeroPriceRegressionAlgorithm.cs index 12a9068704f9..4e496f3e5ddf 100644 --- a/Algorithm.CSharp/OptionOTMExpiryOrderHasZeroPriceRegressionAlgorithm.cs +++ b/Algorithm.CSharp/OptionOTMExpiryOrderHasZeroPriceRegressionAlgorithm.cs @@ -59,11 +59,10 @@ public override void Initialize() // Select a future option call expiring OTM, and adds it to the algorithm. _esOption = AddFutureOptionContract(OptionChain(_es19m20) - .Where(x => x.Symbol.ID.StrikePrice >= 3300m && x.Symbol.ID.OptionRight == OptionRight.Call) - .OrderBy(x => x.Symbol.ID.StrikePrice) + .Where(x => x.ID.StrikePrice >= 3300m && x.ID.OptionRight == OptionRight.Call) + .OrderBy(x => x.ID.StrikePrice) .Take(1) - .Single() - .Symbol, Resolution.Minute).Symbol; + .Single(), Resolution.Minute).Symbol; _expectedContract = QuantConnect.Symbol.CreateOption(_es19m20, Market.CME, OptionStyle.American, OptionRight.Call, 3300m, new DateTime(2020, 6, 19)); if (_esOption != _expectedContract) diff --git a/Algorithm.CSharp/OptionSymbolCanonicalRegressionAlgorithm.cs b/Algorithm.CSharp/OptionSymbolCanonicalRegressionAlgorithm.cs index f91cd5ddb9ed..8a29c1b295e6 100644 --- a/Algorithm.CSharp/OptionSymbolCanonicalRegressionAlgorithm.cs +++ b/Algorithm.CSharp/OptionSymbolCanonicalRegressionAlgorithm.cs @@ -36,7 +36,7 @@ public override void Initialize() SetEndDate(2014, 06, 09); var equitySymbol = AddEquity("TWX").Symbol; - var contracts = OptionChain(equitySymbol).Select(x => x.Symbol).ToList(); + var contracts = OptionChain(equitySymbol).ToList(); var callOptionSymbol = contracts .Where(c => c.ID.OptionRight == OptionRight.Call) diff --git a/Algorithm.CSharp/OptionTimeSliceRegressionAlgorithm.cs b/Algorithm.CSharp/OptionTimeSliceRegressionAlgorithm.cs index 4725a74a6eda..8ac65831cc4d 100644 --- a/Algorithm.CSharp/OptionTimeSliceRegressionAlgorithm.cs +++ b/Algorithm.CSharp/OptionTimeSliceRegressionAlgorithm.cs @@ -55,10 +55,9 @@ public override void OnData(Slice slice) var underlyingPrice = Securities[_symbol].Price; var contractSymbol = OptionChain(_symbol) - .Where(x => x.Symbol.ID.StrikePrice - underlyingPrice > 0) - .OrderBy(x => x.Symbol.ID.Date) - .FirstOrDefault() - ?.Symbol; + .Where(x => x.ID.StrikePrice - underlyingPrice > 0) + .OrderBy(x => x.ID.Date) + .FirstOrDefault(); if (contractSymbol != null) { diff --git a/Algorithm.CSharp/UniverseSelectionSymbolCacheRemovalRegressionTest.cs b/Algorithm.CSharp/UniverseSelectionSymbolCacheRemovalRegressionTest.cs index 66707df6ffd4..8661b9405f1c 100644 --- a/Algorithm.CSharp/UniverseSelectionSymbolCacheRemovalRegressionTest.cs +++ b/Algorithm.CSharp/UniverseSelectionSymbolCacheRemovalRegressionTest.cs @@ -43,7 +43,7 @@ public override void Initialize() AddEquity("AAPL", Resolution.Daily); _equitySymbol = AddEquity("TWX", Resolution.Minute).Symbol; - var contracts = OptionChain(_equitySymbol).Select(x => x.Symbol).ToList(); + var contracts = OptionChain(_equitySymbol).ToList(); var callOptionSymbol = contracts .Where(c => c.ID.OptionRight == OptionRight.Call) diff --git a/Algorithm.Python/AddOptionContractExpiresRegressionAlgorithm.py b/Algorithm.Python/AddOptionContractExpiresRegressionAlgorithm.py index 9b4318be719d..928ecc75b510 100644 --- a/Algorithm.Python/AddOptionContractExpiresRegressionAlgorithm.py +++ b/Algorithm.Python/AddOptionContractExpiresRegressionAlgorithm.py @@ -44,13 +44,13 @@ def on_data(self, data): ''' if self._option == None: options = self.option_chain(self._twx) - options = sorted(options, key=lambda x: x.symbol.id.symbol) + options = sorted(options, key=lambda x: x.id.symbol) - option = next((option.symbol + option = next((option for option in options - if option.symbol.id.date == self._expiration and - option.symbol.id.option_right == OptionRight.CALL and - option.symbol.id.option_style == OptionStyle.AMERICAN), None) + if option.id.date == self._expiration and + option.id.option_right == OptionRight.CALL and + option.id.option_style == OptionStyle.AMERICAN), None) if option != None: self._option = self.add_option_contract(option).symbol diff --git a/Algorithm.Python/AddOptionContractFromUniverseRegressionAlgorithm.py b/Algorithm.Python/AddOptionContractFromUniverseRegressionAlgorithm.py index d8b69a69b27c..794724789892 100644 --- a/Algorithm.Python/AddOptionContractFromUniverseRegressionAlgorithm.py +++ b/Algorithm.Python/AddOptionContractFromUniverseRegressionAlgorithm.py @@ -68,7 +68,7 @@ def on_securities_changed(self, changes): for addedSecurity in changes.added_securities: options = self.option_chain(addedSecurity.symbol) - options = sorted(options, key=lambda x: x.symbol.id.symbol) + options = sorted(options, key=lambda x: x.id.symbol) option = next((option for option in options diff --git a/Algorithm.Python/FutureOptionBuySellCallIntradayRegressionAlgorithm.py b/Algorithm.Python/FutureOptionBuySellCallIntradayRegressionAlgorithm.py index 4256568cd1d9..4d80427cd61a 100644 --- a/Algorithm.Python/FutureOptionBuySellCallIntradayRegressionAlgorithm.py +++ b/Algorithm.Python/FutureOptionBuySellCallIntradayRegressionAlgorithm.py @@ -51,9 +51,9 @@ def initialize(self): # Select a future option expiring ITM, and adds it to the algorithm. self.es_options = [ - self.add_future_option_contract(i.symbol, Resolution.MINUTE).symbol + self.add_future_option_contract(i, Resolution.MINUTE).symbol for i in (list(self.option_chain(self.es19m20)) + list(self.option_chain(self.es20h20))) - if i.symbol.id.strike_price == 3200.0 and i.symbol.id.option_right == OptionRight.CALL + if i.id.strike_price == 3200.0 and i.id.option_right == OptionRight.CALL ] self.expected_contracts = [ diff --git a/Algorithm.Python/FutureOptionCallITMExpiryRegressionAlgorithm.py b/Algorithm.Python/FutureOptionCallITMExpiryRegressionAlgorithm.py index b4d1b31d181a..055558c83e2e 100644 --- a/Algorithm.Python/FutureOptionCallITMExpiryRegressionAlgorithm.py +++ b/Algorithm.Python/FutureOptionCallITMExpiryRegressionAlgorithm.py @@ -41,9 +41,7 @@ def initialize(self): # Select a future option expiring ITM, and adds it to the algorithm. self.es_option = self.add_future_option_contract( list( - sorted([x.symbol - for x in self.option_chain(self.es19m20) - if x.symbol.id.strike_price <= 3200.0 and x.symbol.id.option_right == OptionRight.CALL], + sorted([x for x in self.option_chain(self.es19m20) if x.id.strike_price <= 3200.0 and x.id.option_right == OptionRight.CALL], key=lambda x: x.id.strike_price, reverse=True) )[0], Resolution.MINUTE).symbol diff --git a/Algorithm.Python/FutureOptionCallOTMExpiryRegressionAlgorithm.py b/Algorithm.Python/FutureOptionCallOTMExpiryRegressionAlgorithm.py index 87626a681ea0..bad1b3a048c5 100644 --- a/Algorithm.Python/FutureOptionCallOTMExpiryRegressionAlgorithm.py +++ b/Algorithm.Python/FutureOptionCallOTMExpiryRegressionAlgorithm.py @@ -45,8 +45,8 @@ def initialize(self): self.es_option = self.add_future_option_contract( list( sorted( - [x.symbol for x in self.option_chain(self.es19m20) - if x.symbol.id.strike_price >= 3300.0 and x.symbol.id.option_right == OptionRight.CALL], + [x for x in self.option_chain(self.es19m20) + if x.id.strike_price >= 3300.0 and x.id.option_right == OptionRight.CALL], key=lambda x: x.id.strike_price ) )[0], Resolution.MINUTE).symbol diff --git a/Algorithm.Python/FutureOptionDailyRegressionAlgorithm.py b/Algorithm.Python/FutureOptionDailyRegressionAlgorithm.py index 9e9916d965fd..cad3b9f9f8d2 100644 --- a/Algorithm.Python/FutureOptionDailyRegressionAlgorithm.py +++ b/Algorithm.Python/FutureOptionDailyRegressionAlgorithm.py @@ -34,9 +34,9 @@ def initialize(self): # Attempt to fetch a specific ITM future option contract dc_options = [ - self.add_future_option_contract(x.symbol, resolution).symbol + self.add_future_option_contract(x, resolution).symbol for x in self.option_chain(self.dc) - if x.symbol.id.strike_price == 17 and x.symbol.id.option_right == OptionRight.CALL + if x.id.strike_price == 17 and x.id.option_right == OptionRight.CALL ] self.dc_option = dc_options[0] diff --git a/Algorithm.Python/FutureOptionPutITMExpiryRegressionAlgorithm.py b/Algorithm.Python/FutureOptionPutITMExpiryRegressionAlgorithm.py index 0e8cfb577122..a69896238abb 100644 --- a/Algorithm.Python/FutureOptionPutITMExpiryRegressionAlgorithm.py +++ b/Algorithm.Python/FutureOptionPutITMExpiryRegressionAlgorithm.py @@ -40,9 +40,7 @@ def initialize(self): # Select a future option expiring ITM, and adds it to the algorithm. self.es_option = self.add_future_option_contract( list( - sorted([x.symbol - for x in self.option_chain(self.es19m20) - if x.symbol.id.strike_price >= 3300.0 and x.symbol.id.option_right == OptionRight.PUT], + sorted([x for x in self.option_chain(self.es19m20) if x.id.strike_price >= 3300.0 and x.id.option_right == OptionRight.PUT], key=lambda x: x.id.strike_price) )[0], Resolution.MINUTE).symbol diff --git a/Algorithm.Python/FutureOptionPutOTMExpiryRegressionAlgorithm.py b/Algorithm.Python/FutureOptionPutOTMExpiryRegressionAlgorithm.py index ad2073998d9c..b467eecf0cda 100644 --- a/Algorithm.Python/FutureOptionPutOTMExpiryRegressionAlgorithm.py +++ b/Algorithm.Python/FutureOptionPutOTMExpiryRegressionAlgorithm.py @@ -45,7 +45,7 @@ def initialize(self): self.es_option = self.add_future_option_contract( list( sorted( - [x.symbol for x in self.option_chain(self.es19m20) if x.symbol.id.strike_price <= 3150.0 and x.symbol.id.option_right == OptionRight.PUT], + [x for x in self.option_chain(self.es19m20) if x.id.strike_price <= 3150.0 and x.id.option_right == OptionRight.PUT], key=lambda x: x.id.strike_price, reverse=True ) diff --git a/Algorithm.Python/FutureOptionShortCallITMExpiryRegressionAlgorithm.py b/Algorithm.Python/FutureOptionShortCallITMExpiryRegressionAlgorithm.py index bc624db42e13..18ca83d8767c 100644 --- a/Algorithm.Python/FutureOptionShortCallITMExpiryRegressionAlgorithm.py +++ b/Algorithm.Python/FutureOptionShortCallITMExpiryRegressionAlgorithm.py @@ -41,7 +41,7 @@ def initialize(self): self.es_option = self.add_future_option_contract( list( sorted( - [x.symbol for x in self.option_chain(self.es19m20) if x.symbol.id.strike_price <= 3100.0 and x.symbol.id.option_right == OptionRight.CALL], + [x for x in self.option_chain(self.es19m20) if x.id.strike_price <= 3100.0 and x.id.option_right == OptionRight.CALL], key=lambda x: x.id.strike_price, reverse=True ) diff --git a/Algorithm.Python/FutureOptionShortCallOTMExpiryRegressionAlgorithm.py b/Algorithm.Python/FutureOptionShortCallOTMExpiryRegressionAlgorithm.py index 4a63f7aad03d..0857ada5aed3 100644 --- a/Algorithm.Python/FutureOptionShortCallOTMExpiryRegressionAlgorithm.py +++ b/Algorithm.Python/FutureOptionShortCallOTMExpiryRegressionAlgorithm.py @@ -42,7 +42,7 @@ def initialize(self): self.es_option = self.add_future_option_contract( list( sorted( - [x.symbol for x in self.option_chain(self.es19m20) if x.symbol.id.strike_price >= 3400.0 and x.symbol.id.option_right == OptionRight.CALL], + [x for x in self.option_chain(self.es19m20) if x.id.strike_price >= 3400.0 and x.id.option_right == OptionRight.CALL], key=lambda x: x.id.strike_price ) )[0], Resolution.MINUTE).symbol diff --git a/Algorithm.Python/FutureOptionShortPutITMExpiryRegressionAlgorithm.py b/Algorithm.Python/FutureOptionShortPutITMExpiryRegressionAlgorithm.py index 5022a24fdd12..db1ce2f80a57 100644 --- a/Algorithm.Python/FutureOptionShortPutITMExpiryRegressionAlgorithm.py +++ b/Algorithm.Python/FutureOptionShortPutITMExpiryRegressionAlgorithm.py @@ -41,7 +41,7 @@ def initialize(self): self.es_option = self.add_future_option_contract( list( sorted( - [x.symbol for x in self.option_chain(self.es19m20) if x.symbol.id.strike_price <= 3400.0 and x.symbol.id.option_right == OptionRight.PUT], + [x for x in self.option_chain(self.es19m20) if x.id.strike_price <= 3400.0 and x.id.option_right == OptionRight.PUT], key=lambda x: x.id.strike_price, reverse=True ) diff --git a/Algorithm.Python/FutureOptionShortPutOTMExpiryRegressionAlgorithm.py b/Algorithm.Python/FutureOptionShortPutOTMExpiryRegressionAlgorithm.py index 3bf486ecaa6b..a1dfc198c203 100644 --- a/Algorithm.Python/FutureOptionShortPutOTMExpiryRegressionAlgorithm.py +++ b/Algorithm.Python/FutureOptionShortPutOTMExpiryRegressionAlgorithm.py @@ -42,7 +42,7 @@ def initialize(self): self.es_option = self.add_future_option_contract( list( sorted( - [x.symbol for x in self.option_chain(self.es19m20) if x.symbol.id.strike_price <= 3000.0 and x.symbol.id.option_right == OptionRight.PUT], + [x for x in self.option_chain(self.es19m20) if x.id.strike_price <= 3000.0 and x.id.option_right == OptionRight.PUT], key=lambda x: x.id.strike_price, reverse=True ) diff --git a/Algorithm.Python/IndexOptionBuySellCallIntradayRegressionAlgorithm.py b/Algorithm.Python/IndexOptionBuySellCallIntradayRegressionAlgorithm.py index 1132a86ffc7c..51ec095dc7d0 100644 --- a/Algorithm.Python/IndexOptionBuySellCallIntradayRegressionAlgorithm.py +++ b/Algorithm.Python/IndexOptionBuySellCallIntradayRegressionAlgorithm.py @@ -37,9 +37,9 @@ def initialize(self): # Select a index option expiring ITM, and adds it to the algorithm. spx_options = list(sorted([ - self.add_index_option_contract(i.symbol, Resolution.MINUTE).symbol \ + self.add_index_option_contract(i, Resolution.MINUTE).symbol \ for i in self.option_chain(spx)\ - if (i.symbol.id.strike_price == 3700 or i.symbol.id.strike_price == 3800) and i.symbol.id.option_right == OptionRight.CALL and i.symbol.id.date.year == 2021 and i.symbol.id.date.month == 1], + if (i.id.strike_price == 3700 or i.id.strike_price == 3800) and i.id.option_right == OptionRight.CALL and i.id.date.year == 2021 and i.id.date.month == 1], key=lambda x: x.id.strike_price )) diff --git a/Algorithm.Python/IndexOptionCallITMExpiryRegressionAlgorithm.py b/Algorithm.Python/IndexOptionCallITMExpiryRegressionAlgorithm.py index f2fa710a44eb..28bc6e457717 100644 --- a/Algorithm.Python/IndexOptionCallITMExpiryRegressionAlgorithm.py +++ b/Algorithm.Python/IndexOptionCallITMExpiryRegressionAlgorithm.py @@ -34,9 +34,7 @@ def initialize(self): # Select an index option expiring ITM, and adds it to the algorithm. self.spx_option = list(self.option_chain(self.spx)) - self.spx_option = [i.symbol - for i in self.spx_option - if i.symbol.id.strike_price <= 3200 and i.symbol.id.option_right == OptionRight.CALL and i.symbol.id.date.year == 2021 and i.symbol.id.date.month == 1] + self.spx_option = [i for i in self.spx_option if i.id.strike_price <= 3200 and i.id.option_right == OptionRight.CALL and i.id.date.year == 2021 and i.id.date.month == 1] self.spx_option = list(sorted(self.spx_option, key=lambda x: x.id.strike_price, reverse=True))[0] self.spx_option = self.add_index_option_contract(self.spx_option, Resolution.MINUTE).symbol diff --git a/Algorithm.Python/IndexOptionCallITMGreeksExpiryRegressionAlgorithm.py b/Algorithm.Python/IndexOptionCallITMGreeksExpiryRegressionAlgorithm.py index ba691dab1560..d14a4e8c7ff9 100644 --- a/Algorithm.Python/IndexOptionCallITMGreeksExpiryRegressionAlgorithm.py +++ b/Algorithm.Python/IndexOptionCallITMGreeksExpiryRegressionAlgorithm.py @@ -31,9 +31,7 @@ def initialize(self): # Select a index option call expiring ITM, and adds it to the algorithm. self.spx_option = list(self.option_chain(self.spx)) - self.spx_option = [i.symbol - for i in self.spx_option - if i.symbol.id.strike_price <= 3200 and i.symbol.id.option_right == OptionRight.CALL and i.symbol.id.date.year == 2021 and i.symbol.id.date.month == 1] + self.spx_option = [i for i in self.spx_option if i.id.strike_price <= 3200 and i.id.option_right == OptionRight.CALL and i.id.date.year == 2021 and i.id.date.month == 1] self.spx_option = list(sorted(self.spx_option, key=lambda x: x.id.strike_price, reverse=True))[0] self.spx_option = self.add_index_option_contract(self.spx_option, Resolution.MINUTE) diff --git a/Algorithm.Python/IndexOptionCallOTMExpiryRegressionAlgorithm.py b/Algorithm.Python/IndexOptionCallOTMExpiryRegressionAlgorithm.py index f4cc1d24bd52..de9dc846fd21 100644 --- a/Algorithm.Python/IndexOptionCallOTMExpiryRegressionAlgorithm.py +++ b/Algorithm.Python/IndexOptionCallOTMExpiryRegressionAlgorithm.py @@ -39,7 +39,7 @@ def initialize(self): # Select a index option call expiring OTM, and adds it to the algorithm. self.spx_option = list(self.option_chain(self.spx)) - self.spx_option = [i.symbol for i in self.spx_option if i.symbol.id.strike_price >= 4250 and i.symbol.id.option_right == OptionRight.CALL and i.symbol.id.date.year == 2021 and i.symbol.id.date.month == 1] + self.spx_option = [i for i in self.spx_option if i.id.strike_price >= 4250 and i.id.option_right == OptionRight.CALL and i.id.date.year == 2021 and i.id.date.month == 1] self.spx_option = list(sorted(self.spx_option, key=lambda x: x.id.strike_price))[0] self.spx_option = self.add_index_option_contract(self.spx_option, Resolution.MINUTE).symbol diff --git a/Algorithm.Python/IndexOptionPutITMExpiryRegressionAlgorithm.py b/Algorithm.Python/IndexOptionPutITMExpiryRegressionAlgorithm.py index 92ebe8a0703d..47fddb9de64a 100644 --- a/Algorithm.Python/IndexOptionPutITMExpiryRegressionAlgorithm.py +++ b/Algorithm.Python/IndexOptionPutITMExpiryRegressionAlgorithm.py @@ -33,9 +33,7 @@ def initialize(self): # Select a index option expiring ITM, and adds it to the algorithm. self.spx_option = list(self.option_chain(self.spx)) - self.spx_option = [i.symbol - for i in self.spx_option - if i.symbol.id.strike_price >= 4200 and i.symbol.id.option_right == OptionRight.PUT and i.symbol.id.date.year == 2021 and i.symbol.id.date.month == 1] + self.spx_option = [i for i in self.spx_option if i.id.strike_price >= 4200 and i.id.option_right == OptionRight.PUT and i.id.date.year == 2021 and i.id.date.month == 1] self.spx_option = list(sorted(self.spx_option, key=lambda x: x.id.strike_price))[0] self.spx_option = self.add_index_option_contract(self.spx_option, Resolution.MINUTE).symbol diff --git a/Algorithm.Python/IndexOptionPutOTMExpiryRegressionAlgorithm.py b/Algorithm.Python/IndexOptionPutOTMExpiryRegressionAlgorithm.py index fbf59017ddde..20362670f508 100644 --- a/Algorithm.Python/IndexOptionPutOTMExpiryRegressionAlgorithm.py +++ b/Algorithm.Python/IndexOptionPutOTMExpiryRegressionAlgorithm.py @@ -39,9 +39,7 @@ def initialize(self): # Select a index option call expiring OTM, and adds it to the algorithm. self.spx_option = list(self.option_chain(self.spx)) - self.spx_option = [i.symbol - for i in self.spx_option - if i.symbol.id.strike_price <= 3200 and i.symbol.id.option_right == OptionRight.PUT and i.symbol.id.date.year == 2021 and i.symbol.id.date.month == 1] + self.spx_option = [i for i in self.spx_option if i.id.strike_price <= 3200 and i.id.option_right == OptionRight.PUT and i.id.date.year == 2021 and i.id.date.month == 1] self.spx_option = list(sorted(self.spx_option, key=lambda x: x.id.strike_price, reverse=True))[0] self.spx_option = self.add_index_option_contract(self.spx_option, Resolution.MINUTE).symbol diff --git a/Algorithm.Python/IndexOptionShortCallITMExpiryRegressionAlgorithm.py b/Algorithm.Python/IndexOptionShortCallITMExpiryRegressionAlgorithm.py index c842382fbce7..89ab74131c71 100644 --- a/Algorithm.Python/IndexOptionShortCallITMExpiryRegressionAlgorithm.py +++ b/Algorithm.Python/IndexOptionShortCallITMExpiryRegressionAlgorithm.py @@ -39,7 +39,7 @@ def initialize(self): # Select a index option expiring ITM, and adds it to the algorithm. self.spx_option = list(self.option_chain(self.spx)) - self.spx_option = [i.symbol for i in self.spx_option if i.symbol.id.strike_price <= 3200 and i.symbol.id.option_right == OptionRight.CALL and i.symbol.id.date.year == 2021 and i.symbol.id.date.month == 1] + self.spx_option = [i for i in self.spx_option if i.id.strike_price <= 3200 and i.id.option_right == OptionRight.CALL and i.id.date.year == 2021 and i.id.date.month == 1] self.spx_option = list(sorted(self.spx_option, key=lambda x: x.id.strike_price, reverse=True))[0] self.spx_option = self.add_index_option_contract(self.spx_option, Resolution.MINUTE).symbol diff --git a/Algorithm.Python/IndexOptionShortCallOTMExpiryRegressionAlgorithm.py b/Algorithm.Python/IndexOptionShortCallOTMExpiryRegressionAlgorithm.py index c83efebb48a0..9c4d1afb7b6a 100644 --- a/Algorithm.Python/IndexOptionShortCallOTMExpiryRegressionAlgorithm.py +++ b/Algorithm.Python/IndexOptionShortCallOTMExpiryRegressionAlgorithm.py @@ -35,9 +35,7 @@ def initialize(self): # Select a index option expiring ITM, and adds it to the algorithm. self.spx_option = list(self.option_chain(self.spx)) - self.spx_option = [i.symbol - for i in self.spx_option - if i.symbol.id.strike_price >= 4250 and i.symbol.id.option_right == OptionRight.CALL and i.symbol.id.date.year == 2021 and i.symbol.id.date.month == 1] + self.spx_option = [i for i in self.spx_option if i.id.strike_price >= 4250 and i.id.option_right == OptionRight.CALL and i.id.date.year == 2021 and i.id.date.month == 1] self.spx_option = list(sorted(self.spx_option, key=lambda x: x.id.strike_price))[0] self.spx_option = self.add_index_option_contract(self.spx_option, Resolution.MINUTE).symbol diff --git a/Algorithm.Python/IndexOptionShortPutITMExpiryRegressionAlgorithm.py b/Algorithm.Python/IndexOptionShortPutITMExpiryRegressionAlgorithm.py index 33e0a86a7ded..c7fcb5b86064 100644 --- a/Algorithm.Python/IndexOptionShortPutITMExpiryRegressionAlgorithm.py +++ b/Algorithm.Python/IndexOptionShortPutITMExpiryRegressionAlgorithm.py @@ -39,9 +39,7 @@ def initialize(self): # Select a index option expiring ITM, and adds it to the algorithm. self.spx_option = list(self.option_chain(self.spx)) - self.spx_option = [i.symbol - for i in self.spx_option - if i.symbol.id.strike_price <= 4200 and i.symbol.id.option_right == OptionRight.PUT and i.symbol.id.date.year == 2021 and i.symbol.id.date.month == 1] + self.spx_option = [i for i in self.spx_option if i.id.strike_price <= 4200 and i.id.option_right == OptionRight.PUT and i.id.date.year == 2021 and i.id.date.month == 1] self.spx_option = list(sorted(self.spx_option, key=lambda x: x.id.strike_price, reverse=True))[0] self.spx_option = self.add_index_option_contract(self.spx_option, Resolution.MINUTE).symbol diff --git a/Algorithm.Python/IndexOptionShortPutOTMExpiryRegressionAlgorithm.py b/Algorithm.Python/IndexOptionShortPutOTMExpiryRegressionAlgorithm.py index 765cbc082e36..b1d1829a42dd 100644 --- a/Algorithm.Python/IndexOptionShortPutOTMExpiryRegressionAlgorithm.py +++ b/Algorithm.Python/IndexOptionShortPutOTMExpiryRegressionAlgorithm.py @@ -35,9 +35,7 @@ def initialize(self): # Select a index option expiring ITM, and adds it to the algorithm. self.spx_option = list(self.option_chain(self.spx)) - self.spx_option = [i.symbol - for i in self.spx_option - if i.symbol.id.strike_price <= 3200 and i.symbol.id.option_right == OptionRight.PUT and i.symbol.id.date.year == 2021 and i.symbol.id.date.month == 1] + self.spx_option = [i for i in self.spx_option if i.id.strike_price <= 3200 and i.id.option_right == OptionRight.PUT and i.id.date.year == 2021 and i.id.date.month == 1] self.spx_option = list(sorted(self.spx_option, key=lambda x: x.id.strike_price, reverse=True))[0] self.spx_option = self.add_index_option_contract(self.spx_option, Resolution.MINUTE).symbol diff --git a/Algorithm.Python/OptionChainFullDataRegressionAlgorithm.py b/Algorithm.Python/OptionChainFullDataRegressionAlgorithm.py index 5abde4ee1137..976d69469d01 100644 --- a/Algorithm.Python/OptionChainFullDataRegressionAlgorithm.py +++ b/Algorithm.Python/OptionChainFullDataRegressionAlgorithm.py @@ -29,9 +29,10 @@ def initialize(self): # Get contracts expiring within 10 days, with an implied volatility greater than 0.5 and a delta less than 0.5 contracts = [ - contract_data.symbol + contract_data for contract_data in self.option_chain(goog) - if contract_data.symbol.id.date - self.time <= timedelta(days=10) and contract_data.implied_volatility > 0.5 and contract_data.greeks.delta < 0.5] + if contract_data.id.date - self.time <= timedelta(days=10) and contract_data.implied_volatility > 0.5 and contract_data.greeks.delta < 0.5 + ] # Get the contract with the latest expiration date self._option_contract = sorted(contracts, key=lambda x: x.id.date, reverse=True)[0] diff --git a/Algorithm/QCAlgorithm.cs b/Algorithm/QCAlgorithm.cs index 5c5f83552f98..1f7b8008ea1a 100644 --- a/Algorithm/QCAlgorithm.cs +++ b/Algorithm/QCAlgorithm.cs @@ -3354,12 +3354,9 @@ public DataHistory OptionChain(Symbol symbol) // TODO: Until future options are supported by OptionUniverse, we need to fall back to the OptionChainProvider for them if (canonicalSymbol.SecurityType != SecurityType.FutureOption) { - var marketHoursEntry = MarketHoursDatabase.GetEntry(canonicalSymbol.ID.Market, canonicalSymbol, canonicalSymbol.SecurityType); - var previousTradingDate = QuantConnect.Time.GetStartTimeForTradeBars(marketHoursEntry.ExchangeHours, Time, QuantConnect.Time.OneDay, 1, - extendedMarketHours: false, marketHoursEntry.DataTimeZone); - previousTradingDate = previousTradingDate.ConvertTo(marketHoursEntry.DataTimeZone, TimeZone); - - var history = History(canonicalSymbol, previousTradingDate, Time, Resolution.Daily); + // TODO: History(canonicalSymbol, 1) should be enough, + // the universe resolution should always be daily. Change this when this is fixed in #8317 + var history = History(canonicalSymbol, 1, Resolution.Daily); optionChain = history?.SingleOrDefault()?.Data?.Cast() ?? Enumerable.Empty(); } else From 7dca80a52cd091b1506c2d9336abbc509a7f8e53 Mon Sep 17 00:00:00 2001 From: Jhonathan Abreu Date: Wed, 11 Sep 2024 14:22:21 -0400 Subject: [PATCH 10/10] Minor adjustments --- Algorithm.CSharp/BasicTemplateOptionsAlgorithm.cs | 5 ++++- .../AddOptionContractFromUniverseRegressionAlgorithm.py | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/Algorithm.CSharp/BasicTemplateOptionsAlgorithm.cs b/Algorithm.CSharp/BasicTemplateOptionsAlgorithm.cs index 69964fc7ff6c..ac9f1a403b77 100644 --- a/Algorithm.CSharp/BasicTemplateOptionsAlgorithm.cs +++ b/Algorithm.CSharp/BasicTemplateOptionsAlgorithm.cs @@ -48,7 +48,10 @@ public override void Initialize() _optionSymbol = option.Symbol; // set our strike/expiry filter for this option chain - option.SetFilter(u => u); + option.SetFilter(u => u.Strikes(-2, +2) + // Expiration method accepts TimeSpan objects or integer for days. + // The following statements yield the same filtering criteria + .Expiration(0, 180)); // .Expiration(TimeSpan.Zero, TimeSpan.FromDays(180))); // use the underlying equity as the benchmark diff --git a/Algorithm.Python/AddOptionContractFromUniverseRegressionAlgorithm.py b/Algorithm.Python/AddOptionContractFromUniverseRegressionAlgorithm.py index 794724789892..c6a25867253d 100644 --- a/Algorithm.Python/AddOptionContractFromUniverseRegressionAlgorithm.py +++ b/Algorithm.Python/AddOptionContractFromUniverseRegressionAlgorithm.py @@ -76,7 +76,7 @@ def on_securities_changed(self, changes): option.id.option_right == OptionRight.CALL and option.id.option_style == OptionStyle.AMERICAN), None) - self.add_option_contract(option.symbol) + self.add_option_contract(option) # just keep the first we got if self._option == None: