Files
quantconnect--lean/Algorithm/Selection/OptionChainedUniverseSelectionModel.cs
T
Gerardo Salazar 4c085ff853 Adds Indexes and Index Options asset types (Backtesting/Live, IB only) (#5379)
* Add support for Index SecurityType  🚀 (#5364)

* Add Index SecurityType  🚀

* Extend SecurityIdentifier & Lean Data classes with Index support

* Add Index SecurityType  🚀

* Extend SecurityIdentifier & Lean Data classes with Index support

* Fixes

* Added index cross basic template demonstration

* WIP: Prototype index security type for LEAN as non tradable asset

* Re-adds Index entries to MHDB after rebase

* First steps to getting Index Options running

  * Looks at any instance where we pattern match for an option type
    and replaces it with a generic call to `.IsOption()` for easier
    extensibility in the future for additional option security types

  * Adds IndexOption security and misc. classes

  * Misc. changes, mainly related to any sort of special casing of
    equity options and made index options take the same path

* Enables index options data for backtesting

  * Adds new index options market hours to MHDB
  * Misc. bug fixes for index options
  * WIP: add live support for index options and indexes
  * Use OptionMarginModel for Index Options because they both use the
    same calculation for margin requirements

* Fixes contract not found errors on SPX index options and SPX index in IB

  * Turns out index options' last trading day is the day before expiry,
    which IB was expecting the last trading day.

* Add index option test cases (temp)

* LiveOptionChainProvider fix, use Symbol vs. ticker

  * Description updates to regression algorithms

* Fixes bug in live trading for indexes and index options

  * Adds overridable minimum price variation symbol property
  * Adds variable sized minimum price variation for index options
  * Adjusts symbol properties for index options
  * Misc. bug fixes

* Fixes option assignment simulation for European options

  * Updates index options regression algorithms (WIP)

* Fixes bug where index option exercise would trade index underlying

  * Fixes bugs where SecurityType.Index was getting flagged as tradable

* Regression algorithms updates and addresses review

  * Misc. style fixes and refactoring + a few bug fixes
  * Updates regression algorithms to run without runtime errors
  * Adds data for regression algos

* Sets DefaultOptionStyle on Canonical and support index options

* Update regression algos statistics

* Removes bad line in regression algorithm causing build to fail

* Minor tweaks

* Address review add comment about quoteBar parse scale

Co-authored-by: Balamurali Pandranki <balamurali@live.com>
Co-authored-by: Jared Broad <jaredbroad@gmail.com>
Co-authored-by: Martin-Molinero <martin@quantconnect.com>
2021-03-12 20:46:23 -03:00

94 lines
4.3 KiB
C#

/*
* 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 QuantConnect.Interfaces;
using QuantConnect.Securities;
using System.Collections.Generic;
using QuantConnect.Data.UniverseSelection;
using QuantConnect.Algorithm.Framework.Selection;
using QuantConnect.Securities.Future;
namespace QuantConnect.Algorithm.Selection
{
/// <summary>
/// This universe selection model will chain to the security changes of a given <see cref="Universe"/> selection
/// output and create a new <see cref="OptionChainUniverse"/> for each of them
/// </summary>
public class OptionChainedUniverseSelectionModel : UniverseSelectionModel
{
private DateTime _nextRefreshTimeUtc;
private IEnumerable<Symbol> _currentSymbols;
private readonly UniverseSettings _universeSettings;
private readonly Func<OptionFilterUniverse, OptionFilterUniverse> _optionFilter;
/// <summary>
/// Gets the next time the framework should invoke the `CreateUniverses` method to refresh the set of universes.
/// </summary>
public override DateTime GetNextRefreshTimeUtc() => _nextRefreshTimeUtc;
/// <summary>
/// Creates a new instance of <see cref="OptionChainedUniverseSelectionModel"/>
/// </summary>
/// <param name="universe">The universe we want to chain to</param>
/// <param name="optionFilter">The option filter universe to use</param>
/// <param name="universeSettings">Universe settings define attributes of created subscriptions, such as their resolution and the minimum time in universe before they can be removed</param>
public OptionChainedUniverseSelectionModel(Universe universe,
Func<OptionFilterUniverse, OptionFilterUniverse> optionFilter,
UniverseSettings universeSettings = null)
{
_optionFilter = optionFilter;
_universeSettings = universeSettings;
_nextRefreshTimeUtc = DateTime.MaxValue;
_currentSymbols = Enumerable.Empty<Symbol>();
universe.SelectionChanged += (sender, args) =>
{
// the universe we were watching changed, this will trigger a call to CreateUniverses
_nextRefreshTimeUtc = DateTime.MinValue;
// We must create the new option Symbol using the CreateOption(Symbol, ...) overload.
// Otherwise, we'll end up loading equity data for the selected Symbol, which won't
// work whenever we're loading options data for any non-equity underlying asset class.
_currentSymbols = ((Universe.SelectionEventArgs)args).CurrentSelection
.Select(symbol => Symbol.CreateOption(
symbol,
symbol.ID.Market,
symbol.SecurityType.DefaultOptionStyle(),
default(OptionRight),
0m,
SecurityIdentifier.DefaultDate))
.ToList();
};
}
/// <summary>
/// Creates the universes for this algorithm. Called once after <see cref="IAlgorithm.Initialize"/>
/// </summary>
/// <param name="algorithm">The algorithm instance to create universes for</param>
/// <returns>The universes to be used by the algorithm</returns>
public override IEnumerable<Universe> CreateUniverses(QCAlgorithm algorithm)
{
_nextRefreshTimeUtc = DateTime.MaxValue;
foreach (var optionSymbol in _currentSymbols)
{
yield return algorithm.CreateOptionChain(optionSymbol, _optionFilter, _universeSettings);
}
}
}
}