/// <summary>
        /// Creates the canonical <see cref="Future"/> chain security for a given symbol
        /// </summary>
        /// <param name="algorithm">The algorithm instance to create universes for</param>
        /// <param name="symbol">Symbol of the future</param>
        /// <param name="settings">Universe settings define attributes of created subscriptions, such as their resolution and the minimum time in universe before they can be removed</param>
        /// <param name="initializer">Performs extra initialization (such as setting models) after we create a new security object</param>
        /// <returns><see cref="Future"/> for the given symbol</returns>
        protected virtual Future CreateFutureChainSecurity(QCAlgorithmFramework algorithm, Symbol symbol, UniverseSettings settings, ISecurityInitializer initializer)
        {
            var market = symbol.ID.Market;

            var marketHoursEntry = MarketHoursDatabase.FromDataFolder()
                                   .GetEntry(market, symbol, SecurityType.Future);

            var symbolProperties = SymbolPropertiesDatabase.FromDataFolder()
                                   .GetSymbolProperties(market, symbol, SecurityType.Future, CashBook.AccountCurrency);

            return((Future)SecurityManager.CreateSecurity(typeof(ZipEntryName), algorithm.Portfolio,
                                                          algorithm.SubscriptionManager, marketHoursEntry.ExchangeHours, marketHoursEntry.DataTimeZone, symbolProperties,
                                                          initializer, symbol, settings.Resolution, settings.FillForward, settings.Leverage, settings.ExtendedMarketHours,
                                                          false, false, algorithm.LiveMode, false, false));
        }
 /// <summary>
 /// Creates a new universe and adds it to the algorithm. This will use the default universe settings
 /// specified via the <see cref="UniverseSettings"/> property. This universe will use the defaults
 /// of SecurityType.Equity, and Market.USA
 /// </summary>
 /// <typeparam name="T">The data type</typeparam>
 /// <param name="name">A unique name for this universe</param>
 /// <param name="resolution">The epected resolution of the universe data</param>
 /// <param name="universeSettings">The settings used for securities added by this universe</param>
 /// <param name="selector">Function delegate that performs selection on the universe data</param>
 public void AddUniverse <T>(string name, Resolution resolution, UniverseSettings universeSettings, Func <IEnumerable <T>, IEnumerable <Symbol> > selector)
 {
     AddUniverse(SecurityType.Equity, name, resolution, Market.USA, universeSettings, selector);
 }
        /// <summary>
        /// Creates a new user defined universe that will fire on the requested resolution during market hours.
        /// </summary>
        /// <param name="securityType">The security type of the universe</param>
        /// <param name="name">A unique name for this universe</param>
        /// <param name="resolution">The resolution this universe should be triggered on</param>
        /// <param name="market">The market of the universe</param>
        /// <param name="universeSettings">The subscription settings used for securities added from this universe</param>
        /// <param name="selector">Function delegate that accepts a DateTime and returns a collection of string symbols</param>
        public void AddUniverse(SecurityType securityType, string name, Resolution resolution, string market, UniverseSettings universeSettings, Func <DateTime, IEnumerable <string> > selector)
        {
            var marketHoursDbEntry = MarketHoursDatabase.GetEntry(market, name, securityType);
            var dataTimeZone       = marketHoursDbEntry.DataTimeZone;
            var exchangeTimeZone   = marketHoursDbEntry.ExchangeHours.TimeZone;
            var symbol             = QuantConnect.Symbol.Create(name, securityType, market);
            var config             = new SubscriptionDataConfig(typeof(CoarseFundamental), symbol, resolution, dataTimeZone, exchangeTimeZone, false, false, true, isFilteredSubscription: false);

            AddUniverse(new UserDefinedUniverse(config, universeSettings, resolution.ToTimeSpan(), selector));
        }
Пример #4
0
        public void RefreshesOptionChainUniverseOnDateChange()
        {
            var startTime    = new DateTime(2018, 10, 19, 10, 0, 0);
            var timeProvider = new ManualTimeProvider(startTime);

            var canonicalSymbol = Symbol.Create("SPY", SecurityType.Option, Market.USA, "?SPY");

            var quoteCurrency = new Cash(CashBook.AccountCurrency, 0, 1);
            var exchangeHours = MarketHoursDatabase.FromDataFolder().GetExchangeHours(Market.USA, canonicalSymbol, SecurityType.Option);
            var config        = new SubscriptionDataConfig(
                typeof(ZipEntryName),
                canonicalSymbol,
                Resolution.Minute,
                TimeZones.Utc,
                TimeZones.NewYork,
                true,
                false,
                false,
                false,
                TickType.Quote,
                false,
                DataNormalizationMode.Raw
                );

            var option = new Option(
                canonicalSymbol,
                exchangeHours,
                quoteCurrency,
                new OptionSymbolProperties(SymbolProperties.GetDefault(CashBook.AccountCurrency)),
                ErrorCurrencyConverter.Instance
                );

            var fillForwardResolution = Ref.CreateReadOnly(() => Resolution.Minute.ToTimeSpan());
            var symbolUniverse        = new TestDataQueueUniverseProvider(timeProvider);
            TradeBarBuilderEnumerator underlyingEnumerator = null;
            Func <SubscriptionRequest, IEnumerator <BaseData>, IEnumerator <BaseData> > underlyingEnumeratorFunc =
                (req, input) =>
            {
                underlyingEnumerator = (TradeBarBuilderEnumerator)input;
                return(new LiveFillForwardEnumerator(
                           timeProvider,
                           input,
                           option.Exchange,
                           fillForwardResolution,
                           false,
                           Time.EndOfTime,
                           Resolution.Minute.ToTimeSpan(),
                           TimeZones.Utc));
            };
            var factory = new OptionChainUniverseSubscriptionEnumeratorFactory(underlyingEnumeratorFunc, symbolUniverse, timeProvider);

            var universeSettings = new UniverseSettings(Resolution.Minute, 0, true, false, TimeSpan.Zero);
            var universe         = new OptionChainUniverse(option, universeSettings, true);
            var request          = new SubscriptionRequest(true, universe, option, config, startTime, Time.EndOfTime);
            var enumerator       = (DataQueueOptionChainUniverseDataCollectionEnumerator)factory.CreateEnumerator(request, new DefaultDataProvider());

            // 2018-10-19 10:00 AM UTC
            underlyingEnumerator.ProcessData(new Tick {
                Symbol = Symbols.SPY, Value = 280m
            });

            Assert.IsTrue(enumerator.MoveNext());
            // no underlying data available yet
            Assert.IsNull(enumerator.Current);
            Assert.AreEqual(0, symbolUniverse.TotalLookupCalls);

            // 2018-10-19 10:01 AM UTC
            timeProvider.Advance(Time.OneMinute);

            underlyingEnumerator.ProcessData(new Tick {
                Symbol = Symbols.SPY, Value = 280m
            });

            Assert.IsTrue(enumerator.MoveNext());
            Assert.IsNotNull(enumerator.Current);
            Assert.AreEqual(1, symbolUniverse.TotalLookupCalls);
            var data = enumerator.Current;

            Assert.IsNotNull(data);
            Assert.AreEqual(1, data.Data.Count);
            Assert.IsNotNull(data.Underlying);

            // 2018-10-19 10:02 AM UTC
            timeProvider.Advance(Time.OneMinute);

            underlyingEnumerator.ProcessData(new Tick {
                Symbol = Symbols.SPY, Value = 280m
            });

            Assert.IsTrue(enumerator.MoveNext());
            Assert.IsNotNull(enumerator.Current);
            Assert.AreEqual(1, symbolUniverse.TotalLookupCalls);
            data = enumerator.Current;
            Assert.IsNotNull(data);
            Assert.AreEqual(1, data.Data.Count);
            Assert.IsNotNull(data.Underlying);

            // 2018-10-19 10:03 AM UTC
            timeProvider.Advance(Time.OneMinute);

            underlyingEnumerator.ProcessData(new Tick {
                Symbol = Symbols.SPY, Value = 280m
            });

            Assert.IsTrue(enumerator.MoveNext());
            Assert.IsNotNull(enumerator.Current);
            Assert.AreEqual(1, symbolUniverse.TotalLookupCalls);
            data = enumerator.Current;
            Assert.IsNotNull(data);
            Assert.AreEqual(1, data.Data.Count);
            Assert.IsNotNull(data.Underlying);

            // 2018-10-20 10:03 AM UTC
            timeProvider.Advance(Time.OneDay);

            underlyingEnumerator.ProcessData(new Tick {
                Symbol = Symbols.SPY, Value = 280m
            });

            Assert.IsTrue(enumerator.MoveNext());
            Assert.IsNotNull(enumerator.Current);
            Assert.AreEqual(2, symbolUniverse.TotalLookupCalls);
            data = enumerator.Current;
            Assert.IsNotNull(data);
            Assert.AreEqual(2, data.Data.Count);
            Assert.IsNotNull(data.Underlying);

            // 2018-10-20 10:04 AM UTC
            timeProvider.Advance(Time.OneMinute);

            underlyingEnumerator.ProcessData(new Tick {
                Symbol = Symbols.SPY, Value = 280m
            });

            Assert.IsTrue(enumerator.MoveNext());
            Assert.IsNotNull(enumerator.Current);
            Assert.AreEqual(2, symbolUniverse.TotalLookupCalls);
            data = enumerator.Current;
            Assert.IsNotNull(data);
            Assert.AreEqual(2, data.Data.Count);
            Assert.IsNotNull(data.Underlying);
        }
Пример #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CustomUniverseSelectionModel"/> class
 /// </summary>
 /// <param name="securityType">The security type of the universe</param>
 /// <param name="name">A unique name for this universe</param>
 /// <param name="market">The market of the universe</param>
 /// <param name="selector">Function delegate that accepts a DateTime and returns a collection of string symbols</param>
 /// <param name="universeSettings">The settings used when adding symbols to the algorithm, specify null to use algorithm.UniverseSettings</param>
 public CustomUniverseSelectionModel(SecurityType securityType, string name, string market, PyObject selector, UniverseSettings universeSettings, TimeSpan interval)
     : this(
         securityType,
         name,
         market,
         selector.ConvertToDelegate <Func <DateTime, object> >().ConvertToUniverseSelectionStringDelegate(),
         universeSettings,
         interval
         )
 {
 }
Пример #6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ScheduledUniverseSelectionModel"/> class
 /// </summary>
 /// <param name="timeZone">The time zone the date/time rules are in</param>
 /// <param name="dateRule">Date rule defines what days the universe selection function will be invoked</param>
 /// <param name="timeRule">Time rule defines what times on each day selected by date rule the universe selection function will be invoked</param>
 /// <param name="selector">Selector function accepting the date time firing time and returning the universe selected symbols</param>
 /// <param name="settings">Universe settings for subscriptions added via this universe, null will default to algorithm's universe settings</param>
 /// <param name="initializer">Security initializer for new securities created via this universe, null will default to algorithm's security initializer</param>
 public ScheduledUniverseSelectionModel(DateTimeZone timeZone, IDateRule dateRule, ITimeRule timeRule, Func <DateTime, IEnumerable <Symbol> > selector, UniverseSettings settings = null, ISecurityInitializer initializer = null)
 {
     _timeZone    = timeZone;
     _dateRule    = dateRule;
     _timeRule    = timeRule;
     _selector    = selector;
     _settings    = settings;
     _initializer = initializer;
 }
Пример #7
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ScheduledUniverseSelectionModel"/> class
        /// </summary>
        /// <param name="timeZone">The time zone the date/time rules are in</param>
        /// <param name="dateRule">Date rule defines what days the universe selection function will be invoked</param>
        /// <param name="timeRule">Time rule defines what times on each day selected by date rule the universe selection function will be invoked</param>
        /// <param name="selector">Selector function accepting the date time firing time and returning the universe selected symbols</param>
        /// <param name="settings">Universe settings for subscriptions added via this universe, null will default to algorithm's universe settings</param>
        /// <param name="initializer">Security initializer for new securities created via this universe, null will default to algorithm's security initializer</param>
        public ScheduledUniverseSelectionModel(DateTimeZone timeZone, IDateRule dateRule, ITimeRule timeRule, PyObject selector, UniverseSettings settings = null, ISecurityInitializer initializer = null)
        {
            Func <DateTime, object> func;

            selector.TryConvertToDelegate(out func);
            _timeZone    = timeZone;
            _dateRule    = dateRule;
            _timeRule    = timeRule;
            _selector    = func.ConvertToUniverseSelectionSymbolDelegate();
            _settings    = settings;
            _initializer = initializer;
        }
        /// <summary>
        /// Creates a new user defined universe that will fire on the requested resolution during market hours.
        /// </summary>
        /// <param name="securityType">The security type of the universe</param>
        /// <param name="name">A unique name for this universe</param>
        /// <param name="resolution">The resolution this universe should be triggered on</param>
        /// <param name="market">The market of the universe</param>
        /// <param name="universeSettings">The subscription settings used for securities added from this universe</param>
        /// <param name="pySelector">Function delegate that accepts a DateTime and returns a collection of string symbols</param>
        public void AddUniverse(SecurityType securityType, string name, Resolution resolution, string market, UniverseSettings universeSettings, PyObject pySelector)
        {
            var selector = PythonUtil.ToFunc <DateTime, object[]>(pySelector);

            AddUniverse(securityType, name, resolution, market, universeSettings, d => selector(d).Select(x => (string)x));
        }
 /// <summary>
 /// Creates a new universe and adds it to the algorithm. This will use the default universe settings
 /// specified via the <see cref="UniverseSettings"/> property. This universe will use the defaults
 /// of SecurityType.Equity, Resolution.Daily, and Market.USA
 /// </summary>
 /// <param name="T">The data type</param>
 /// <param name="name">A unique name for this universe</param>
 /// <param name="universeSettings">The settings used for securities added by this universe</param>
 /// <param name="selector">Function delegate that performs selection on the universe data</param>
 public void AddUniverse(PyObject T, string name, UniverseSettings universeSettings, PyObject selector)
 {
     AddUniverse(CreateType(T), SecurityType.Equity, name, Resolution.Daily, Market.USA, universeSettings, selector);
 }
Пример #10
0
 public TestUniverse(SubscriptionDataConfig config, UniverseSettings universeSettings)
     : base(config)
 {
     UniverseSettings = universeSettings;
 }
Пример #11
0
        /// <summary>
        /// Creates a new user defined universe that will fire on the requested resolution during market hours.
        /// </summary>
        /// <param name="securityType">The security type of the universe</param>
        /// <param name="name">A unique name for this universe</param>
        /// <param name="resolution">The resolution this universe should be triggered on</param>
        /// <param name="market">The market of the universe</param>
        /// <param name="universeSettings">The subscription settings used for securities added from this universe</param>
        /// <param name="pySelector">Function delegate that accepts a DateTime and returns a collection of string symbols</param>
        public void AddUniverse(SecurityType securityType, string name, Resolution resolution, string market, UniverseSettings universeSettings, PyObject pySelector)
        {
            var selector = pySelector.ConvertToDelegate <Func <DateTime, object[]> >();

            AddUniverse(securityType, name, resolution, market, universeSettings, d => selector(d).Select(x => (string)x));
        }
Пример #12
0
 public Universe Top(int count, UniverseSettings universeSettings = null)
 {
     return(_algorithm.Universe.Top(count, universeSettings));
 }
Пример #13
0
 /// <summary>
 /// Initializes a new instance of the <see cref="FundamentalUniverseSelectionModel"/> class
 /// </summary>
 /// <param name="filterFineData">True to also filter using fine fundamental data, false to only filter on coarse data</param>
 /// <param name="universeSettings">The settings used when adding symbols to the algorithm, specify null to use algorthm.UniverseSettings</param>
 /// <param name="securityInitializer">Optional security initializer invoked when creating new securities, specify null to use algorithm.SecurityInitializer</param>
 protected FundamentalUniverseSelectionModel(bool filterFineData, UniverseSettings universeSettings, ISecurityInitializer securityInitializer)
 {
     _filterFineData      = filterFineData;
     _universeSettings    = universeSettings;
     _securityInitializer = securityInitializer;
 }
 public CustomDataCoarseFundamentalUniverse(UniverseSettings universeSettings, ISecurityInitializer securityInitializer, Func <IEnumerable <CoarseFundamental>, IEnumerable <Symbol> > selector)
     : base(universeSettings, securityInitializer, selector)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="FundamentalUniverseSelectionModel"/> class
 /// </summary>
 /// <param name="filterFineData">True to also filter using fine fundamental data, false to only filter on coarse data</param>
 /// <param name="universeSettings">The settings used when adding symbols to the algorithm, specify null to use algorithm.UniverseSettings</param>
 protected FundamentalUniverseSelectionModel(bool filterFineData, UniverseSettings universeSettings)
 {
     _filterFineData   = filterFineData;
     _universeSettings = universeSettings;
 }
 /// <summary>
 /// Creates a new universe and adds it to the algorithm
 /// </summary>
 /// <param name="T">The data type</param>
 /// <param name="securityType">The security type the universe produces</param>
 /// <param name="name">A unique name for this universe</param>
 /// <param name="resolution">The epected resolution of the universe data</param>
 /// <param name="market">The market for selected symbols</param>
 /// <param name="universeSettings">The subscription settings to use for newly created subscriptions</param>
 /// <param name="selector">Function delegate that performs selection on the universe data</param>
 public void AddUniverse(PyObject T, SecurityType securityType, string name, Resolution resolution, string market, UniverseSettings universeSettings, PyObject selector)
 {
     AddUniverse(CreateType(T), securityType, name, resolution, market, universeSettings, selector);
 }
Пример #17
0
 /// <summary>
 /// Initializes a new instance of the <see cref="QC500PortfolioSelectionModel"/>
 /// </summary>
 /// <param name="universeSettings">Universe settings defines what subscription properties will be applied to selected securities</param>
 /// <param name="securityInitializer">Security initializer initializes newly selected securities</param>
 public QC500PortfolioSelectionModel(UniverseSettings universeSettings, ISecurityInitializer securityInitializer)
     : base(true, universeSettings, securityInitializer)
 {
 }
        /// <summary>
        /// Creates a new universe and adds it to the algorithm
        /// </summary>
        /// <param name="dataType">The data type</param>
        /// <param name="securityType">The security type the universe produces</param>
        /// <param name="name">A unique name for this universe</param>
        /// <param name="resolution">The epected resolution of the universe data</param>
        /// <param name="market">The market for selected symbols</param>
        /// <param name="universeSettings">The subscription settings to use for newly created subscriptions</param>
        /// <param name="pySelector">Function delegate that performs selection on the universe data</param>
        public void AddUniverse(Type dataType, SecurityType securityType, string name, Resolution resolution, string market, UniverseSettings universeSettings, PyObject pySelector)
        {
            var marketHoursDbEntry = _marketHoursDatabase.GetEntry(market, name, securityType);
            var dataTimeZone       = marketHoursDbEntry.DataTimeZone;
            var exchangeTimeZone   = marketHoursDbEntry.ExchangeHours.TimeZone;
            var symbol             = QuantConnect.Symbol.Create(name, securityType, market);
            var config             = new SubscriptionDataConfig(dataType, symbol, resolution, dataTimeZone, exchangeTimeZone, false, false, true, true, isFilteredSubscription: false);

            var selector = PythonUtil.ToFunc <IEnumerable <IBaseData>, object[]>(pySelector);

            AddUniverse(new FuncUniverse(config, universeSettings, SecurityInitializer, d => selector(d)
                                         .Select(x => x is Symbol ? (Symbol)x : QuantConnect.Symbol.Create((string)x, securityType, market))));
        }
Пример #19
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ScheduledUniverseSelectionModel"/> class using the algorithm's time zone
 /// </summary>
 /// <param name="dateRule">Date rule defines what days the universe selection function will be invoked</param>
 /// <param name="timeRule">Time rule defines what times on each day selected by date rule the universe selection function will be invoked</param>
 /// <param name="selector">Selector function accepting the date time firing time and returning the universe selected symbols</param>
 /// <param name="settings">Universe settings for subscriptions added via this universe, null will default to algorithm's universe settings</param>
 /// <param name="initializer">Security initializer for new securities created via this universe, null will default to algorithm's security initializer</param>
 public ScheduledUniverseSelectionModel(IDateRule dateRule, ITimeRule timeRule, PyObject selector, UniverseSettings settings = null, ISecurityInitializer initializer = null)
     : this(null, dateRule, timeRule, selector, settings, initializer)
 {
 }
Пример #20
0
        private Security GetOrCreateSecurity(Dictionary <Symbol, Security> pendingAdditions, Symbol symbol, UniverseSettings universeSettings, Security underlying = null)
        {
            // create the new security, the algorithm thread will add this at the appropriate time
            Security security;

            if (!pendingAdditions.TryGetValue(symbol, out security) && !_algorithm.Securities.TryGetValue(symbol, out security))
            {
                // For now this is required for retro compatibility with usages of security.Subscriptions
                var configs = _algorithm.SubscriptionManager.SubscriptionDataConfigService.Add(symbol,
                                                                                               universeSettings.Resolution,
                                                                                               universeSettings.FillForward,
                                                                                               universeSettings.ExtendedMarketHours,
                                                                                               dataNormalizationMode: universeSettings.DataNormalizationMode);

                security = _securityService.CreateSecurity(symbol, configs, universeSettings.Leverage, symbol.ID.SecurityType.IsOption(), underlying);

                pendingAdditions.Add(symbol, security);
            }

            return(security);
        }
        public void RefreshesFutureChainUniverseOnDateChange()
        {
            var startTime    = new DateTime(2018, 10, 17, 10, 0, 0);
            var timeProvider = new ManualTimeProvider(startTime);

            var symbolUniverse = new TestDataQueueUniverseProvider(timeProvider);
            var factory        = new FuturesChainUniverseSubscriptionEnumeratorFactory(symbolUniverse, timeProvider);

            var canonicalSymbol = Symbol.Create("VX", SecurityType.Future, Market.USA, "/VX");

            var quoteCurrency = new Cash(Currencies.USD, 0, 1);
            var exchangeHours = MarketHoursDatabase.FromDataFolder().GetExchangeHours(Market.USA, canonicalSymbol, SecurityType.Future);
            var config        = new SubscriptionDataConfig(
                typeof(ZipEntryName),
                canonicalSymbol,
                Resolution.Minute,
                TimeZones.Utc,
                TimeZones.Chicago,
                true,
                false,
                false,
                false,
                TickType.Quote,
                false,
                DataNormalizationMode.Raw
                );

            var future = new Future(
                canonicalSymbol,
                exchangeHours,
                quoteCurrency,
                SymbolProperties.GetDefault(Currencies.USD),
                ErrorCurrencyConverter.Instance,
                RegisteredSecurityDataTypesProvider.Null,
                new SecurityCache()
                );

            var universeSettings = new UniverseSettings(Resolution.Minute, 0, true, false, TimeSpan.Zero);
            var universe         = new FuturesChainUniverse(future, universeSettings);
            var request          = new SubscriptionRequest(true, universe, future, config, startTime, Time.EndOfTime);
            var enumerator       = factory.CreateEnumerator(request, new DefaultDataProvider());

            Assert.IsTrue(enumerator.MoveNext());
            Assert.IsNotNull(enumerator.Current);
            Assert.AreEqual(1, symbolUniverse.TotalLookupCalls);
            var data = enumerator.Current as FuturesChainUniverseDataCollection;

            Assert.IsNotNull(data);
            Assert.AreEqual(1, data.Data.Count);

            timeProvider.Advance(Time.OneSecond);

            Assert.IsTrue(enumerator.MoveNext());
            Assert.IsNull(enumerator.Current);
            Assert.AreEqual(1, symbolUniverse.TotalLookupCalls);

            timeProvider.Advance(Time.OneMinute);

            Assert.IsTrue(enumerator.MoveNext());
            Assert.IsNull(enumerator.Current);
            Assert.AreEqual(1, symbolUniverse.TotalLookupCalls);

            timeProvider.Advance(Time.OneDay);

            Assert.IsTrue(enumerator.MoveNext());
            Assert.IsNotNull(enumerator.Current);
            Assert.AreEqual(2, symbolUniverse.TotalLookupCalls);
            data = enumerator.Current as FuturesChainUniverseDataCollection;
            Assert.IsNotNull(data);
            Assert.AreEqual(2, data.Data.Count);

            timeProvider.Advance(Time.OneMinute);

            Assert.IsTrue(enumerator.MoveNext());
            Assert.IsNull(enumerator.Current);
            Assert.AreEqual(2, symbolUniverse.TotalLookupCalls);

            enumerator.Dispose();
        }
Пример #22
0
        private Security GetOrCreateSecurity(Dictionary <Symbol, Security> pendingAdditions, Symbol symbol, UniverseSettings universeSettings, Security underlying = null)
        {
            // create the new security, the algorithm thread will add this at the appropriate time
            Security security;

            if (!pendingAdditions.TryGetValue(symbol, out security) && !_algorithm.Securities.TryGetValue(symbol, out security))
            {
                security = _securityService.CreateSecurity(symbol, new List <SubscriptionDataConfig>(), universeSettings.Leverage, symbol.ID.SecurityType.IsOption(), underlying);

                pendingAdditions.Add(symbol, security);
            }

            return(security);
        }
Пример #23
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CustomUniverseSelectionModel"/> class
 /// </summary>
 /// <param name="securityType">The security type of the universe</param>
 /// <param name="name">A unique name for this universe</param>
 /// <param name="market">The market of the universe</param>
 /// <param name="selector">Function delegate that accepts a DateTime and returns a collection of string symbols</param>
 /// <param name="universeSettings">The settings used when adding symbols to the algorithm, specify null to use algorithm.UniverseSettings</param>
 public CustomUniverseSelectionModel(SecurityType securityType, string name, string market, Func <DateTime, IEnumerable <string> > selector, UniverseSettings universeSettings, TimeSpan interval)
 {
     _interval         = interval;
     _selector         = selector;
     _universeSettings = universeSettings;
     _symbol           = Symbol.Create($"{name}-{securityType}-{market}", securityType, market);
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="ScheduledUniverseSelectionModel"/> class using the algorithm's time zone
 /// </summary>
 /// <param name="dateRule">Date rule defines what days the universe selection function will be invoked</param>
 /// <param name="timeRule">Time rule defines what times on each day selected by date rule the universe selection function will be invoked</param>
 /// <param name="selector">Selector function accepting the date time firing time and returning the universe selected symbols</param>
 /// <param name="settings">Universe settings for subscriptions added via this universe, null will default to algorithm's universe settings</param>
 public ScheduledUniverseSelectionModel(IDateRule dateRule, ITimeRule timeRule, Func <DateTime, IEnumerable <Symbol> > selector, UniverseSettings settings = null)
 {
     _dateRule = dateRule;
     _timeRule = timeRule;
     _selector = selector;
     _settings = settings;
 }
Пример #25
0
        private IEnumerable <SubscriptionDataConfig> GetMatchingSubscriptions(Symbol symbol, Type type, Resolution?resolution = null)
        {
            var matchingSubscriptions = SubscriptionManager.SubscriptionDataConfigService
                                        // we add internal subscription so that history requests are covered, this allows us to warm them up too
                                        .GetSubscriptionDataConfigs(symbol, includeInternalConfigs: true)
                                        // find all subscriptions matching the requested type with a higher resolution than requested
                                        .OrderByDescending(s => s.Resolution)
                                        // lets make sure to respect the order of the data types
                                        .ThenByDescending(config => GetTickTypeOrder(config.SecurityType, config.TickType))
                                        .Where(s => SubscriptionDataConfigTypeFilter(type, s.Type));

            var internalConfig = new List <SubscriptionDataConfig>();
            var userConfig     = new List <SubscriptionDataConfig>();

            foreach (var config in matchingSubscriptions)
            {
                if (config.IsInternalFeed)
                {
                    internalConfig.Add(config);
                }
                else
                {
                    userConfig.Add(config);
                }
            }

            // if we have any user defined subscription configuration we use it, else we use internal ones if any
            List <SubscriptionDataConfig> configs = null;

            if (userConfig.Count != 0)
            {
                configs = userConfig;
            }
            else if (internalConfig.Count != 0)
            {
                configs = internalConfig;
            }

            // we use the subscription manager registered configurations here, we can not rely on the Securities collection
            // since this might be called when creating a security and warming it up
            if (configs != null && configs.Count != 0)
            {
                if (resolution.HasValue &&
                    (resolution == Resolution.Daily || resolution == Resolution.Hour) &&
                    symbol.SecurityType == SecurityType.Equity)
                {
                    // for Daily and Hour resolution, for equities, we have to
                    // filter out any existing subscriptions that could be of Quote type
                    // This could happen if they were Resolution.Minute/Second/Tick
                    return(configs.Where(s => s.TickType != TickType.Quote));
                }

                return(configs);
            }
            else
            {
                var entry = MarketHoursDatabase.GetEntry(symbol, new [] { type });
                resolution = GetResolution(symbol, resolution);

                return(SubscriptionManager
                       .LookupSubscriptionConfigDataTypes(symbol.SecurityType, resolution.Value, symbol.IsCanonical())
                       .Where(tuple => SubscriptionDataConfigTypeFilter(type, tuple.Item1))
                       .Select(x => new SubscriptionDataConfig(
                                   x.Item1,
                                   symbol,
                                   resolution.Value,
                                   entry.DataTimeZone,
                                   entry.ExchangeHours.TimeZone,
                                   UniverseSettings.FillForward,
                                   UniverseSettings.ExtendedMarketHours,
                                   true,
                                   false,
                                   x.Item2,
                                   true,
                                   UniverseSettings.GetUniverseNormalizationModeOrDefault(symbol.SecurityType))));
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="ScheduledUniverseSelectionModel"/> class using the algorithm's time zone
 /// </summary>
 /// <param name="dateRule">Date rule defines what days the universe selection function will be invoked</param>
 /// <param name="timeRule">Time rule defines what times on each day selected by date rule the universe selection function will be invoked</param>
 /// <param name="selector">Selector function accepting the date time firing time and returning the universe selected symbols</param>
 /// <param name="settings">Universe settings for subscriptions added via this universe, null will default to algorithm's universe settings</param>
 public ScheduledUniverseSelectionModel(IDateRule dateRule, ITimeRule timeRule, PyObject selector, UniverseSettings settings = null)
     : this(null, dateRule, timeRule, selector, settings)
 {
 }
        /// <summary>
        /// Creates a new universe and adds it to the algorithm
        /// </summary>
        /// <typeparam name="T">The data type</typeparam>
        /// <param name="securityType">The security type the universe produces</param>
        /// <param name="name">A unique name for this universe</param>
        /// <param name="resolution">The epected resolution of the universe data</param>
        /// <param name="market">The market for selected symbols</param>
        /// <param name="universeSettings">The subscription settings to use for newly created subscriptions</param>
        /// <param name="selector">Function delegate that performs selection on the universe data</param>
        public void AddUniverse <T>(SecurityType securityType, string name, Resolution resolution, string market, UniverseSettings universeSettings, Func <IEnumerable <T>, IEnumerable <string> > selector)
        {
            var marketHoursDbEntry = MarketHoursDatabase.GetEntry(market, name, securityType);
            var dataTimeZone       = marketHoursDbEntry.DataTimeZone;
            var exchangeTimeZone   = marketHoursDbEntry.ExchangeHours.TimeZone;
            var symbol             = QuantConnect.Symbol.Create(name, securityType, market, baseDataType: typeof(T));
            var config             = new SubscriptionDataConfig(typeof(T), symbol, resolution, dataTimeZone, exchangeTimeZone, false, false, true, true, isFilteredSubscription: false);

            AddUniverse(new FuncUniverse(config, universeSettings, SecurityInitializer,
                                         d => selector(d.OfType <T>()).Select(x => QuantConnect.Symbol.Create(x, securityType, market, baseDataType: typeof(T))))
                        );
        }
Пример #28
0
 /// <summary>
 /// Creates a new instance of the <see cref="ManualUniverse"/>
 /// </summary>
 public ManualUniverse(SubscriptionDataConfig configuration,
                       UniverseSettings universeSettings,
                       Symbol[] symbols)
     : base(configuration, universeSettings, Time.MaxTimeSpan, symbols)
 {
 }
Пример #29
0
 /// <summary>
 /// Creates a new universe and adds it to the algorithm. This will use the default universe settings
 /// specified via the <see cref="UniverseSettings"/> property. This universe will use the defaults
 /// of SecurityType.Equity, and Market.USA
 /// </summary>
 /// <typeparam name="T">The data type</typeparam>
 /// <param name="name">A unique name for this universe</param>
 /// <param name="resolution">The epected resolution of the universe data</param>
 /// <param name="universeSettings">The settings used for securities added by this universe</param>
 /// <param name="selector">Function delegate that performs selection on the universe data</param>
 public Universe AddUniverse <T>(string name, Resolution resolution, UniverseSettings universeSettings, Func <IEnumerable <T>, IEnumerable <string> > selector)
 {
     return(AddUniverse(SecurityType.Equity, name, resolution, Market.USA, universeSettings, selector));
 }
Пример #30
0
 protected virtual Future CreateFutureChainSecurity(QCAlgorithmFramework algorithm, Symbol symbol, UniverseSettings settings, ISecurityInitializer initializer)
 {
     return(CreateFutureChainSecurity(
                algorithm.SubscriptionManager.SubscriptionDataConfigService,
                symbol,
                settings,
                algorithm.Securities));
 }