public void AddUniverseOptions(Symbol underlyingSymbol, Func <OptionFilterUniverse, OptionFilterUniverse> optionFilter) { // We need to load the universe associated with the provided Symbol and provide that universe to the option filter universe. // The option filter universe will subscribe to any changes in the universe of the underlying Symbol, // ensuring that we load the option chain for every asset found in the underlying's Universe. Universe universe; if (!UniverseManager.TryGetValue(underlyingSymbol, out universe)) { // The universe might be already added, but not registered with the UniverseManager. universe = _pendingUniverseAdditions.SingleOrDefault(u => u.Configuration.Symbol == underlyingSymbol); if (universe == null) { underlyingSymbol = AddSecurity(underlyingSymbol).Symbol; } // Recheck again, we should have a universe addition pending for the provided Symbol universe = _pendingUniverseAdditions.SingleOrDefault(u => u.Configuration.Symbol == underlyingSymbol); if (universe == null) { // Should never happen, but it could be that the subscription // created with AddSecurity is not aligned with the Symbol we're using. throw new InvalidOperationException($"Universe not found for underlying Symbol: {underlyingSymbol}."); } } // Allow all option contracts through without filtering if we're provided a null filter. AddUniverseOptions(universe, optionFilter ?? (_ => _)); }
/// <summary> /// Adds the security to the user defined universe for the specified /// </summary> private void AddToUserDefinedUniverse(Security security) { // if we are adding a non-internal security which already has an internal feed, we remove it first Security existingSecurity; if (Securities.TryGetValue(security.Symbol, out existingSecurity)) { if (!security.IsInternalFeed() && existingSecurity.IsInternalFeed()) { var securityUniverse = UniverseManager.Select(x => x.Value).OfType <UserDefinedUniverse>().FirstOrDefault(x => x.Members.ContainsKey(security.Symbol)); securityUniverse?.Remove(security.Symbol); Securities.Remove(security.Symbol); } } Securities.Add(security); // add this security to the user defined universe Universe universe; var subscription = security.Subscriptions.First(); var universeSymbol = UserDefinedUniverse.CreateSymbol(subscription.SecurityType, subscription.Market); if (!UniverseManager.TryGetValue(universeSymbol, out universe)) { // create a new universe, these subscription settings don't currently get used // since universe selection proper is never invoked on this type of universe var uconfig = new SubscriptionDataConfig(subscription, symbol: universeSymbol, isInternalFeed: true, fillForward: false); if (security.Type == SecurityType.Base) { // set entry in market hours database for the universe subscription to match the custom data var symbolString = MarketHoursDatabase.GetDatabaseSymbolKey(uconfig.Symbol); MarketHoursDatabase.SetEntry(uconfig.Market, symbolString, uconfig.SecurityType, security.Exchange.Hours, uconfig.DataTimeZone); } universe = new UserDefinedUniverse(uconfig, new UniverseSettings(security.Resolution, security.Leverage, security.IsFillDataForward, security.IsExtendedMarketHours, TimeSpan.Zero), SecurityInitializer, QuantConnect.Time.MaxTimeSpan, new List <Symbol> { security.Symbol } ); UniverseManager.Add(universeSymbol, universe); } var userDefinedUniverse = universe as UserDefinedUniverse; if (userDefinedUniverse != null) { userDefinedUniverse.Add(security.Symbol); } else { // should never happen, someone would need to add a non-user defined universe with this symbol throw new Exception("Expected universe with symbol '" + universeSymbol.Value + "' to be of type UserDefinedUniverse."); } }
public Option AddChineseOption(string underlying, Resolution resolution = Resolution.Minute, string market = null, bool fillDataForward = true, decimal leverage = 0m) { if (market == null) { if (!BrokerageModel.DefaultMarkets.TryGetValue(SecurityType.Option, out market)) { throw new Exception("No default market set for security type: " + SecurityType.Option); } } Symbol canonicalSymbol; var alias = "?" + underlying; if (!SymbolCache.TryGetSymbol(alias, out canonicalSymbol)) { //canonicalSymbol = QuantConnect.Symbol.Create(underlying, SecurityType.Option, market, alias); canonicalSymbol = QuantConnect.Symbol.CreateOption(underlying, market, OptionStyle.European, default(OptionRight), 0, SecurityIdentifier.DefaultDate, alias); } var marketHoursEntry = MarketHoursDatabase.FromDataFolder().GetEntry(market, underlying, SecurityType.Option); var symbolProperties = SymbolPropertiesDatabase.FromDataFolder().GetSymbolProperties(market, underlying, SecurityType.Option, CashBook.AccountCurrency); var canonicalSecurity = (Option)SecurityManager.CreateSecurity(new List <Type>() { typeof(ZipEntryName) }, Portfolio, SubscriptionManager, marketHoursEntry.ExchangeHours, marketHoursEntry.DataTimeZone, symbolProperties, SecurityInitializer, canonicalSymbol, resolution, fillDataForward, leverage, false, false, false, LiveMode, true, false); canonicalSecurity.IsTradable = false; Securities.Add(canonicalSecurity); // add this security to the user defined universe Universe universe; if (!UniverseManager.TryGetValue(canonicalSymbol, out universe)) { var settings = new UniverseSettings(resolution, leverage, true, false, TimeSpan.Zero); universe = new OptionChainUniverse(canonicalSecurity, settings, SubscriptionManager, SecurityInitializer); UniverseManager.Add(canonicalSymbol, universe); } return(canonicalSecurity); }
/// <summary> /// Adds the security to the user defined universe for the specified /// </summary> private void AddToUserDefinedUniverse(Security security) { Securities.Add(security); // add this security to the user defined universe Universe universe; var securityConfig = security.SubscriptionDataConfig; var universeSymbol = UserDefinedUniverse.CreateSymbol(securityConfig.SecurityType, securityConfig.Market); if (!UniverseManager.TryGetValue(universeSymbol, out universe)) { // create a new universe, these subscription settings don't currently get used // since universe selection proper is never invoked on this type of universe var uconfig = new SubscriptionDataConfig(securityConfig, symbol: universeSymbol, isInternalFeed: true, fillForward: false); universe = new UserDefinedUniverse(uconfig, new UniverseSettings(security.Resolution, security.Leverage, security.IsFillDataForward, security.IsExtendedMarketHours, TimeSpan.Zero), SecurityInitializer, QuantConnect.Time.OneDay, new List <Symbol> { security.Symbol } ); UniverseManager.Add(universeSymbol, universe); } var userDefinedUniverse = universe as UserDefinedUniverse; if (userDefinedUniverse != null) { userDefinedUniverse.Add(security.Symbol); } else { // should never happen, someone would need to add a non-user defined universe with this symbol throw new Exception("Expected universe with symbol '" + universeSymbol.Value + "' to be of type UserDefinedUniverse."); } }
/// <summary> /// Adds the security to the user defined universe /// </summary> /// <param name="security">The security to add</param> /// <param name="configurations">The <see cref="SubscriptionDataConfig"/> instances we want to add</param> private Security AddToUserDefinedUniverse( Security security, List <SubscriptionDataConfig> configurations) { var subscription = configurations.First(); // if we are adding a non-internal security which already has an internal feed, we remove it first if (Securities.TryGetValue(security.Symbol, out var existingSecurity)) { if (!subscription.IsInternalFeed && existingSecurity.IsInternalFeed()) { var securityUniverse = UniverseManager.Select(x => x.Value).OfType <UserDefinedUniverse>().FirstOrDefault(x => x.Members.ContainsKey(security.Symbol)); securityUniverse?.Remove(security.Symbol); Securities.Remove(security.Symbol); Securities.Add(security); } else { var isTradable = security.IsTradable; // We will reuse existing so we return it to the user. // We will use the IsTradable flag of the new security, since existing could of been set to false when removed security = existingSecurity; security.IsTradable = isTradable; } } else { Securities.Add(security); } // add this security to the user defined universe Universe universe; var universeSymbol = UserDefinedUniverse.CreateSymbol(security.Type, security.Symbol.ID.Market); lock (_pendingUniverseAdditionsLock) { if (!UniverseManager.TryGetValue(universeSymbol, out universe)) { universe = _pendingUniverseAdditions.FirstOrDefault(x => x.Configuration.Symbol == universeSymbol); if (universe == null) { // create a new universe, these subscription settings don't currently get used // since universe selection proper is never invoked on this type of universe var uconfig = new SubscriptionDataConfig(subscription, symbol: universeSymbol, isInternalFeed: true, fillForward: false, exchangeTimeZone: DateTimeZone.Utc, dataTimeZone: DateTimeZone.Utc); // this is the universe symbol, has no real entry in the mhdb, will default to market and security type // set entry in market hours database for the universe subscription to match the config var symbolString = MarketHoursDatabase.GetDatabaseSymbolKey(uconfig.Symbol); MarketHoursDatabase.SetEntry(uconfig.Market, symbolString, uconfig.SecurityType, SecurityExchangeHours.AlwaysOpen(uconfig.ExchangeTimeZone), uconfig.DataTimeZone); universe = new UserDefinedUniverse(uconfig, new UniverseSettings( subscription.Resolution, security.Leverage, subscription.FillDataForward, subscription.ExtendedMarketHours, TimeSpan.Zero), QuantConnect.Time.MaxTimeSpan, new List <Symbol>()); AddUniverse(universe); } } } var userDefinedUniverse = universe as UserDefinedUniverse; if (userDefinedUniverse != null) { lock (_pendingUniverseAdditionsLock) { _pendingUserDefinedUniverseSecurityAdditions.Add( new UserDefinedUniverseAddition(userDefinedUniverse, configurations, security)); } } else { // should never happen, someone would need to add a non-user defined universe with this symbol throw new Exception($"Expected universe with symbol '{universeSymbol.Value}' to be of type {nameof(UserDefinedUniverse)} but was {universe.GetType().Name}."); } return(security); }