/// <summary>
 /// Initializes a new instance of the <see cref="UserDefinedUniverse"/> class
 /// </summary>
 /// <param name="configuration">The configuration used to resolve the data for universe selection</param>
 /// <param name="universeSettings">The settings used for new subscriptions generated by this universe</param>
 /// <param name="securityInitializer">Initializes securities when they're added to the universe</param>
 /// <param name="interval">The interval at which selection should be performed</param>
 /// <param name="symbols">The initial set of symbols in this universe</param>
 public UserDefinedUniverse(SubscriptionDataConfig configuration, UniverseSettings universeSettings, ISecurityInitializer securityInitializer, TimeSpan interval, IEnumerable<Symbol> symbols)
     : base(configuration, securityInitializer)
 {
     _interval = interval;
     _symbols = symbols.ToHashSet();
     _universeSettings = universeSettings;
     _selector = time => _symbols;
 }
        /// <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)
        {
            Func <DateTime, Symbol[]> func;

            selector.TryConvertToDelegate(out func);
            _dateRule    = dateRule;
            _timeRule    = timeRule;
            _selector    = func;
            _settings    = settings;
            _initializer = initializer;
        }
Example #3
0
 public OptionChainUniverse(Option option,
                            UniverseSettings universeSettings,
                            ISecurityInitializer securityInitializer,
                            bool liveMode)
     : base(option.SubscriptionDataConfig, securityInitializer)
 {
     Option            = option;
     _underlyingSymbol = new[] { Option.Symbol.Underlying };
     _universeSettings = universeSettings;
     _liveMode         = liveMode;
 }
Example #4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="FineFundamentalUniverseSelectionModel"/> class
 /// </summary>
 /// <param name="coarseSelector">Selects symbols from the provided coarse data set</param>
 /// <param name="fineSelector">Selects symbols from the provided fine data set (this set has already been filtered according to the coarse selection)</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>
 /// <param name="securityInitializer">Performs extra initialization (such as setting models) after we create a new security object</param>
 public FineFundamentalUniverseSelectionModel(
     Func <IEnumerable <CoarseFundamental>, IEnumerable <Symbol> > coarseSelector,
     Func <IEnumerable <FineFundamental>, IEnumerable <Symbol> > fineSelector,
     UniverseSettings universeSettings        = null,
     ISecurityInitializer securityInitializer = null
     )
     : base(true, universeSettings, securityInitializer)
 {
     _coarseSelector = coarseSelector;
     _fineSelector   = fineSelector;
 }
Example #5
0
        public void Setup()
        {
            var timeKeeper = new TimeKeeper(new DateTime(2015, 12, 07));

            _securityManager            = new SecurityManager(timeKeeper);
            _securityTransactionManager = new SecurityTransactionManager(_securityManager);
            _securityPortfolioManager   = new SecurityPortfolioManager(_securityManager, _securityTransactionManager);
            _subscriptionManager        = new SubscriptionManager(new AlgorithmSettings(), timeKeeper);
            _marketHoursDatabase        = MarketHoursDatabase.FromDataFolder();
            _symbolPropertiesDatabase   = SymbolPropertiesDatabase.FromDataFolder();
            _securityInitializer        = SecurityInitializer.Null;
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="UserDefinedUniverse"/> class
 /// </summary>
 /// <param name="configuration">The configuration used to resolve the data for universe selection</param>
 /// <param name="universeSettings">The settings used for new subscriptions generated by this universe</param>
 /// <param name="securityInitializer">Initializes securities when they're added to the universe</param>
 /// <param name="interval">The interval at which selection should be performed</param>
 /// <param name="selector">Universe selection function invoked for each time returned via GetTriggerTimes.
 /// The function parameter is a DateTime in the time zone of configuration.ExchangeTimeZone</param>
 public UserDefinedUniverse(SubscriptionDataConfig configuration, UniverseSettings universeSettings, ISecurityInitializer securityInitializer, TimeSpan interval, Func<DateTime,IEnumerable<string>> selector)
     : base(configuration, securityInitializer)
 {
     _interval = interval;
     _universeSettings = universeSettings;
     _selector = time =>
     {
         var selectSymbolsResult = selector(time.ConvertFromUtc(Configuration.ExchangeTimeZone));
         // if we received an unchaged then short circuit the symbol creation and return it directly
         if (ReferenceEquals(selectSymbolsResult, Unchanged)) return Unchanged;
         return selectSymbolsResult.Select(sym => Symbol.Create(sym, Configuration.SecurityType, Configuration.Market));
     };
 }
        public OptionUniverseSelectionModel(TimeSpan refreshInterval,
                                            Func <DateTime, IEnumerable <Symbol> > optionChainSymbolSelector,
                                            UniverseSettings universeSettings,
                                            ISecurityInitializer securityInitializer
                                            )
        {
            _nextRefreshTimeUtc = DateTime.MinValue;

            _refreshInterval           = refreshInterval;
            _universeSettings          = universeSettings;
            _securityInitializer       = securityInitializer;
            _optionChainSymbolSelector = optionChainSymbolSelector;
        }
Example #8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="EmaCrossUniverseSelectionModel"/> class
 /// </summary>
 /// <param name="fastPeriod">Fast EMA period</param>
 /// <param name="slowPeriod">Slow EMA period</param>
 /// <param name="universeCount">Maximum number of members of this universe selection</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>
 public EmaCrossUniverseSelectionModel(
     int fastPeriod    = 100,
     int slowPeriod    = 300,
     int universeCount = 500,
     UniverseSettings universeSettings        = null,
     ISecurityInitializer securityInitializer = null)
     : base(false, universeSettings, securityInitializer)
 {
     _fastPeriod    = fastPeriod;
     _slowPeriod    = slowPeriod;
     _universeCount = universeCount;
     _averages      = new ConcurrentDictionary <Symbol, SelectionData>();
 }
Example #9
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CoarseFundamentalUniverseSelectionModel"/> class
        /// </summary>
        /// <param name="coarseSelector">Selects symbols from the provided coarse data set</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>
        /// <param name="securityInitializer">Performs extra initialization (such as setting models) after we create a new security object</param>
        public CoarseFundamentalUniverseSelectionModel(
            PyObject coarseSelector,
            UniverseSettings universeSettings        = null,
            ISecurityInitializer securityInitializer = null
            )
            : base(false, universeSettings, securityInitializer)
        {
            Func <IEnumerable <CoarseFundamental>, Symbol[]> func;

            if (coarseSelector.TryConvertToDelegate(out func))
            {
                _coarseSelector = func;
            }
        }
Example #10
0
        public void Setup()
        {
            SymbolCache.Clear();

            var timeKeeper = new TimeKeeper(new DateTime(2015, 12, 07));

            _securityManager            = new SecurityManager(timeKeeper);
            _securityTransactionManager = new SecurityTransactionManager(null, _securityManager);
            _securityPortfolioManager   = new SecurityPortfolioManager(_securityManager, _securityTransactionManager);
            _subscriptionManager        = new SubscriptionManager(timeKeeper, new DataManagerStub());
            _marketHoursDatabase        = MarketHoursDatabase.FromDataFolder();
            _symbolPropertiesDatabase   = SymbolPropertiesDatabase.FromDataFolder();
            _securityInitializer        = SecurityInitializer.Null;
        }
Example #11
0
 public OptionChainUniverse(Option option,
                            UniverseSettings universeSettings,
                            ISecurityInitializer securityInitializer,
                            bool liveMode)
     : base(option.SubscriptionDataConfig, securityInitializer)
 {
     Option            = option;
     _underlyingSymbol = new[] { Option.Symbol.Underlying };
     _universeSettings = new UniverseSettings(universeSettings)
     {
         DataNormalizationMode = DataNormalizationMode.Raw
     };
     _liveMode             = liveMode;
     _optionFilterUniverse = new OptionFilterUniverse();
 }
Example #12
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ManualUniverseSelectionModel"/> class
        /// </summary>
        /// <param name="symbols">The symbols to subscribe to</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>
        public ManualUniverseSelectionModel(IEnumerable <Symbol> symbols, UniverseSettings universeSettings, ISecurityInitializer securityInitializer)
        {
            if (symbols == null)
            {
                throw new ArgumentNullException(nameof(symbols));
            }

            _symbols             = symbols.Where(s => !s.IsCanonical()).ToList();
            _universeSettings    = universeSettings;
            _securityInitializer = securityInitializer;

            foreach (var symbol in _symbols)
            {
                SymbolCache.Set(symbol.Value, symbol);
            }
        }
Example #13
0
 /// <summary>
 /// Initializes a new instance of the <see cref="FineFundamentalUniverseSelectionModel"/> class
 /// </summary>
 /// <param name="coarseSelector">Selects symbols from the provided coarse data set</param>
 /// <param name="fineSelector">Selects symbols from the provided fine data set (this set has already been filtered according to the coarse selection)</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>
 /// <param name="securityInitializer">Performs extra initialization (such as setting models) after we create a new security object</param>
 public FineFundamentalUniverseSelectionModel(
     PyObject coarseSelector,
     PyObject fineSelector,
     UniverseSettings universeSettings = null,
     ISecurityInitializer securityInitializer = null
     )
     : base(true, universeSettings, securityInitializer)
 {
     Func<IEnumerable<FineFundamental>, Symbol[]> fineFunc;
     Func<IEnumerable<CoarseFundamental>, Symbol[]> coarseFunc;
     if (fineSelector.TryConvertToDelegate(out fineFunc) &&
         coarseSelector.TryConvertToDelegate(out coarseFunc))
     {
         _fineSelector = fineFunc;
         _coarseSelector = coarseFunc;
     }
 }
Example #14
0
        /// <summary>
        /// Creates a security and matching configuration. This applies the default leverage if
        /// leverage is less than or equal to zero.
        /// This method also add the new symbol mapping to the <see cref="SymbolCache"/>
        /// </summary>
        public static Security CreateSecurity(SecurityPortfolioManager securityPortfolioManager,
                                              SubscriptionManager subscriptionManager,
                                              MarketHoursDatabase marketHoursDatabase,
                                              ISecurityInitializer securityInitializer,
                                              Symbol symbol,
                                              Resolution resolution,
                                              bool fillDataForward,
                                              decimal leverage,
                                              bool extendedMarketHours,
                                              bool isInternalFeed,
                                              bool isCustomData,
                                              bool addToSymbolCache = true)
        {
            var marketHoursDbEntry = marketHoursDatabase.GetEntry(symbol.ID.Market, symbol.Value, symbol.ID.SecurityType);
            var exchangeHours      = marketHoursDbEntry.ExchangeHours;
            var tradeBarType       = typeof(TradeBar);
            var type = resolution == Resolution.Tick ? typeof(Tick) : tradeBarType;

            return(CreateSecurity(type, securityPortfolioManager, subscriptionManager, exchangeHours, marketHoursDbEntry.DataTimeZone, securityInitializer, symbol, resolution,
                                  fillDataForward, leverage, extendedMarketHours, isInternalFeed, isCustomData, addToSymbolCache));
        }
Example #15
0
 /// <summary>
 /// Creates a security and matching configuration. This applies the default leverage if
 /// leverage is less than or equal to zero.
 /// This method also add the new symbol mapping to the <see cref="SymbolCache"/>
 /// </summary>
 public static Security CreateSecurity(Type dataType,
                                       SecurityPortfolioManager securityPortfolioManager,
                                       SubscriptionManager subscriptionManager,
                                       SecurityExchangeHours exchangeHours,
                                       DateTimeZone dataTimeZone,
                                       SymbolProperties symbolProperties,
                                       ISecurityInitializer securityInitializer,
                                       Symbol symbol,
                                       Resolution resolution,
                                       bool fillDataForward,
                                       decimal leverage,
                                       bool extendedMarketHours,
                                       bool isInternalFeed,
                                       bool isCustomData,
                                       bool isLiveMode,
                                       bool addToSymbolCache       = true,
                                       bool isFilteredSubscription = true)
 {
     return(CreateSecurity(
                new List <Tuple <Type, TickType> >
     {
         new Tuple <Type, TickType>(dataType, LeanData.GetCommonTickTypeForCommonDataTypes(dataType, symbol.SecurityType))
     },
                securityPortfolioManager,
                subscriptionManager,
                exchangeHours,
                dataTimeZone,
                symbolProperties,
                securityInitializer,
                symbol,
                resolution,
                fillDataForward,
                leverage,
                extendedMarketHours,
                isInternalFeed,
                isCustomData,
                isLiveMode,
                addToSymbolCache,
                isFilteredSubscription));
 }
Example #16
0
        /// <summary>
        /// Creates a security and matching configuration. This applies the default leverage if
        /// leverage is less than or equal to zero.
        /// This method also add the new symbol mapping to the <see cref="SymbolCache"/>
        /// </summary>
        public static Security CreateSecurity(SecurityPortfolioManager securityPortfolioManager,
                                              SubscriptionManager subscriptionManager,
                                              MarketHoursDatabase marketHoursDatabase,
                                              SymbolPropertiesDatabase symbolPropertiesDatabase,
                                              ISecurityInitializer securityInitializer,
                                              Symbol symbol,
                                              Resolution resolution,
                                              bool fillDataForward,
                                              decimal leverage,
                                              bool extendedMarketHours,
                                              bool isInternalFeed,
                                              bool isCustomData,
                                              bool isLiveMode,
                                              bool addToSymbolCache = true
                                              )
        {
            var marketHoursDbEntry = marketHoursDatabase.GetEntry(symbol.ID.Market, symbol.Value, symbol.ID.SecurityType);
            var exchangeHours      = marketHoursDbEntry.ExchangeHours;

            var defaultQuoteCurrency = CashBook.AccountCurrency;

            if (symbol.ID.SecurityType == SecurityType.Forex)
            {
                defaultQuoteCurrency = symbol.Value.Substring(3);
            }
            var symbolProperties = symbolPropertiesDatabase.GetSymbolProperties(symbol.ID.Market, symbol.Value, symbol.ID.SecurityType, defaultQuoteCurrency);

            var type = resolution == Resolution.Tick ? typeof(Tick) : typeof(TradeBar);

            if (symbol.ID.SecurityType == SecurityType.Option && resolution != Resolution.Tick)
            {
                type = typeof(QuoteBar);
            }
            return(CreateSecurity(type, securityPortfolioManager, subscriptionManager, exchangeHours, marketHoursDbEntry.DataTimeZone, symbolProperties, securityInitializer, symbol, resolution,
                                  fillDataForward, leverage, extendedMarketHours, isInternalFeed, isCustomData, isLiveMode, addToSymbolCache));
        }
Example #17
0
 protected Universe(SubscriptionDataConfig config, ISecurityInitializer securityInitializer)
     : this(config)
 {
     SecurityInitializer = securityInitializer;
 }
Example #18
0
        /// <summary>
        /// Creates a security and matching configuration. This applies the default leverage if
        /// leverage is less than or equal to zero.
        /// This method also add the new symbol mapping to the <see cref="SymbolCache"/>
        /// </summary>
        public static Security CreateSecurity(List <Tuple <Type, TickType> > subscriptionDataTypes,
                                              SecurityPortfolioManager securityPortfolioManager,
                                              SubscriptionManager subscriptionManager,
                                              SecurityExchangeHours exchangeHours,
                                              DateTimeZone dataTimeZone,
                                              SymbolProperties symbolProperties,
                                              ISecurityInitializer securityInitializer,
                                              Symbol symbol,
                                              Resolution resolution,
                                              bool fillDataForward,
                                              decimal leverage,
                                              bool extendedMarketHours,
                                              bool isInternalFeed,
                                              bool isCustomData,
                                              bool isLiveMode,
                                              bool addToSymbolCache       = true,
                                              bool isFilteredSubscription = true)
        {
            if (!subscriptionDataTypes.Any())
            {
                throw new ArgumentNullException(nameof(subscriptionDataTypes), "At least one type needed to create security");
            }

            // add the symbol to our cache
            if (addToSymbolCache)
            {
                SymbolCache.Set(symbol.Value, symbol);
            }

            // Add the symbol to Data Manager -- generate unified data streams for algorithm events
            var configList = new SubscriptionDataConfigList(symbol);

            configList.AddRange(from subscriptionDataType
                                in subscriptionDataTypes
                                let dataType = subscriptionDataType.Item1
                                               let tickType = subscriptionDataType.Item2
                                                              select subscriptionManager.Add(dataType, tickType,
                                                                                             symbol, resolution, dataTimeZone,
                                                                                             exchangeHours.TimeZone, isCustomData,
                                                                                             fillDataForward, extendedMarketHours,
                                                                                             isInternalFeed, isFilteredSubscription));

            // verify the cash book is in a ready state
            var quoteCurrency = symbolProperties.QuoteCurrency;

            if (!securityPortfolioManager.CashBook.ContainsKey(quoteCurrency))
            {
                // since we have none it's safe to say the conversion is zero
                securityPortfolioManager.CashBook.Add(quoteCurrency, 0, 0);
            }
            if (symbol.ID.SecurityType == SecurityType.Forex || symbol.ID.SecurityType == SecurityType.Crypto)
            {
                // decompose the symbol into each currency pair
                string baseCurrency;
                Forex.Forex.DecomposeCurrencyPair(symbol.Value, out baseCurrency, out quoteCurrency);

                if (!securityPortfolioManager.CashBook.ContainsKey(baseCurrency))
                {
                    // since we have none it's safe to say the conversion is zero
                    securityPortfolioManager.CashBook.Add(baseCurrency, 0, 0);
                }
                if (!securityPortfolioManager.CashBook.ContainsKey(quoteCurrency))
                {
                    // since we have none it's safe to say the conversion is zero
                    securityPortfolioManager.CashBook.Add(quoteCurrency, 0, 0);
                }
            }

            var quoteCash = securityPortfolioManager.CashBook[symbolProperties.QuoteCurrency];

            Security security;

            switch (configList.Symbol.ID.SecurityType)
            {
            case SecurityType.Equity:
                security = new Equity.Equity(symbol, exchangeHours, quoteCash, symbolProperties);
                break;

            case SecurityType.Option:
                if (addToSymbolCache)
                {
                    SymbolCache.Set(symbol.Underlying.Value, symbol.Underlying);
                }
                configList.SetDataNormalizationMode(DataNormalizationMode.Raw);
                security = new Option.Option(symbol, exchangeHours, securityPortfolioManager.CashBook[CashBook.AccountCurrency], new Option.OptionSymbolProperties(symbolProperties));
                break;

            case SecurityType.Future:
                configList.SetDataNormalizationMode(DataNormalizationMode.Raw);
                security = new Future.Future(symbol, exchangeHours, securityPortfolioManager.CashBook[CashBook.AccountCurrency], symbolProperties);
                break;

            case SecurityType.Forex:
                security = new Forex.Forex(symbol, exchangeHours, quoteCash, symbolProperties);
                break;

            case SecurityType.Cfd:
                security = new Cfd.Cfd(symbol, exchangeHours, quoteCash, symbolProperties);
                break;

            case SecurityType.Crypto:
                security = new Crypto.Crypto(symbol, exchangeHours, quoteCash, symbolProperties);
                break;

            default:
            case SecurityType.Base:
                security = new Security(symbol, exchangeHours, quoteCash, symbolProperties);
                break;
            }

            // if we're just creating this security and it only has an internal
            // feed, mark it as non-tradable since the user didn't request this data
            if (!configList.IsInternalFeed)
            {
                security.IsTradable = true;
            }

            security.AddData(configList);

            // invoke the security initializer
            securityInitializer.Initialize(security, true);

            // if leverage was specified then apply to security after the initializer has run, parameters of this
            // method take precedence over the intializer
            if (leverage > 0)
            {
                security.SetLeverage(leverage);
            }

            // In live mode, equity assumes specific price variation model
            if (isLiveMode && security.Type == SecurityType.Equity)
            {
                security.PriceVariationModel = new EquityPriceVariationModel();
            }

            return(security);
        }
Example #19
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ScheduledUniverse"/> 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="securityInitializer">Security initializer for new securities created via this universe, null will default to algorithm's security initializer</param>
 public ScheduledUniverse(DateTimeZone timeZone, IDateRule dateRule, ITimeRule timeRule, Func <DateTime, IEnumerable <Symbol> > selector, UniverseSettings settings = null, ISecurityInitializer securityInitializer = null)
     : base(CreateConfiguration(timeZone, dateRule, timeRule), securityInitializer)
 {
     _dateRule        = dateRule;
     _timeRule        = timeRule;
     _selector        = selector;
     UniverseSettings = settings;
 }
Example #20
0
        public UserDefinedUniverse(SubscriptionDataConfig configuration, UniverseSettings universeSettings, ISecurityInitializer securityInitializer, TimeSpan interval, IEnumerable<Symbol> symbols)
            : base(configuration, securityInitializer)
        {
            _interval = interval;
#if NETCORE
            _symbols = LinqExtensions.ToHashSet(symbols);
#else
            _symbols = symbols.ToHashSet();
#endif
            _universeSettings = universeSettings;
            _selector = time => _subscriptionDataConfigs.Select(x => x.Symbol).Union(_symbols);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="FineFundamentalUniverse"/> class
 /// </summary>
 /// <param name="symbol">Defines the symbol to use for this universe</param>
 /// <param name="universeSettings">The settings used for new subscriptions generated by this universe</param>
 /// <param name="securityInitializer">Initializes securities when they're added to the universe</param>
 /// <param name="selector">Returns the symbols that should be included in the universe</param>
 public FineFundamentalUniverse(Symbol symbol, UniverseSettings universeSettings, ISecurityInitializer securityInitializer, Func<IEnumerable<FineFundamental>, IEnumerable<Symbol>> selector)
     : base(CreateConfiguration(symbol), securityInitializer)
 {
     _universeSettings = universeSettings;
     _selector = selector;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="UserDefinedUniverse"/> class
 /// </summary>
 /// <param name="configuration">The configuration used to resolve the data for universe selection</param>
 /// <param name="universeSettings">The settings used for new subscriptions generated by this universe</param>
 /// <param name="securityInitializer">Initializes securities when they're added to the universe</param>
 /// <param name="interval">The interval at which selection should be performed</param>
 /// <param name="selector">Universe selection function invoked for each time returned via GetTriggerTimes.
 /// The function parameter is a DateTime in the time zone of configuration.ExchangeTimeZone</param>
 public UserDefinedUniverse(SubscriptionDataConfig configuration, UniverseSettings universeSettings, ISecurityInitializer securityInitializer, TimeSpan interval, Func <DateTime, IEnumerable <string> > selector)
     : base(configuration, securityInitializer)
 {
     _interval         = interval;
     _universeSettings = universeSettings;
     _selector         = time =>
     {
         var selectSymbolsResult = selector(time.ConvertFromUtc(Configuration.ExchangeTimeZone));
         // if we received an unchaged then short circuit the symbol creation and return it directly
         if (ReferenceEquals(selectSymbolsResult, Unchanged))
         {
             return(Unchanged);
         }
         return(selectSymbolsResult.Select(sym => Symbol.Create(sym, Configuration.SecurityType, Configuration.Market)));
     };
 }
Example #23
0
 /// <summary>
 /// Initializes a new instance of the <see cref="OptionChainUniverse"/> class
 /// </summary>
 /// <param name="option">The canonical option chain security</param>
 /// <param name="universeSettings">The universe settings to be used for new subscriptions</param>
 /// <param name="securityInitializer">The security initializer to use on newly created securities</param>
 public OptionChainUniverse(Option option, UniverseSettings universeSettings, ISecurityInitializer securityInitializer = null)
     : base(option.SubscriptionDataConfig, securityInitializer)
 {
     _option = option;
     _universeSettings = universeSettings;
 }
Example #24
0
 public virtual void SetSecurityInitializer(ISecurityInitializer securityInitializer)
 {
     SecurityInitializer = securityInitializer;
 }
Example #25
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;
 }
Example #26
0
        /// <summary>
        /// Creates a security and matching configuration. This applies the default leverage if
        /// leverage is less than or equal to zero.
        /// This method also add the new symbol mapping to the <see cref="SymbolCache"/>
        /// </summary>
        public static Security CreateSecurity(Type factoryType,
                                              SecurityPortfolioManager securityPortfolioManager,
                                              SubscriptionManager subscriptionManager,
                                              SecurityExchangeHours exchangeHours,
                                              DateTimeZone dataTimeZone,
                                              ISecurityInitializer securityInitializer,
                                              Symbol symbol,
                                              Resolution resolution,
                                              bool fillDataForward,
                                              decimal leverage,
                                              bool extendedMarketHours,
                                              bool isInternalFeed,
                                              bool isCustomData,
                                              bool addToSymbolCache = true)
        {
            var sid = symbol.ID;

            // add the symbol to our cache
            if (addToSymbolCache)
            {
                SymbolCache.Set(symbol.Value, symbol);
            }

            //Add the symbol to Data Manager -- generate unified data streams for algorithm events
            var config = subscriptionManager.Add(factoryType, symbol, resolution, dataTimeZone, exchangeHours.TimeZone, isCustomData, fillDataForward,
                                                 extendedMarketHours, isInternalFeed);

            Security security;

            switch (config.SecurityType)
            {
            case SecurityType.Equity:
                security = new Equity.Equity(exchangeHours, config);
                break;

            case SecurityType.Forex:
                // decompose the symbol into each currency pair
                string baseCurrency, quoteCurrency;
                Forex.Forex.DecomposeCurrencyPair(symbol.Value, out baseCurrency, out quoteCurrency);

                if (!securityPortfolioManager.CashBook.ContainsKey(baseCurrency))
                {
                    // since we have none it's safe to say the conversion is zero
                    securityPortfolioManager.CashBook.Add(baseCurrency, 0, 0);
                }
                if (!securityPortfolioManager.CashBook.ContainsKey(quoteCurrency))
                {
                    // since we have none it's safe to say the conversion is zero
                    securityPortfolioManager.CashBook.Add(quoteCurrency, 0, 0);
                }
                security = new Forex.Forex(exchangeHours, securityPortfolioManager.CashBook[quoteCurrency], config);
                break;

            default:
            case SecurityType.Base:
                security = new Security(exchangeHours, config);
                break;
            }

            // invoke the security initializer
            securityInitializer.Initialize(security);

            // if leverage was specified then apply to security after the initializer has run, parameters of this
            // method take precedence over the intializer
            if (leverage > 0)
            {
                security.SetLeverage(leverage);
            }

            return(security);
        }
Example #27
0
 public OptionChainUniverse(Option option, UniverseSettings universeSettings, ISecurityInitializer securityInitializer = null)
     : base(option.SubscriptionDataConfig, securityInitializer)
 {
     _option           = option;
     _universeSettings = universeSettings;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="FineFundamentalUniverse"/> class
 /// </summary>
 /// <param name="universeSettings">The settings used for new subscriptions generated by this universe</param>
 /// <param name="securityInitializer">Initializes securities when they're added to the universe</param>
 /// <param name="selector">Returns the symbols that should be included in the universe</param>
 public FineFundamentalUniverse(UniverseSettings universeSettings, ISecurityInitializer securityInitializer, Func<IEnumerable<FineFundamental>, IEnumerable<Symbol>> selector)
     : base(CreateConfiguration(FineFundamental.CreateUniverseSymbol(QuantConnect.Market.USA)), securityInitializer)
 {
     _universeSettings = universeSettings;
     _selector = selector;
 }
 protected virtual Future CreateFutureChainSecurity(QCAlgorithm algorithm, Symbol symbol, UniverseSettings settings, ISecurityInitializer initializer)
 {
     return(CreateFutureChainSecurity(
                algorithm.SubscriptionManager.SubscriptionDataConfigService,
                symbol,
                settings,
                algorithm.Securities));
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="ManualPortfolioSelectionModel"/> class
 /// </summary>
 /// <param name="symbols">The symbols to subscribe to</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>
 public ManualPortfolioSelectionModel(IEnumerable <Symbol> symbols, UniverseSettings universeSettings, ISecurityInitializer securityInitializer)
 {
     _symbols             = symbols.ToList();
     _universeSettings    = universeSettings;
     _securityInitializer = securityInitializer;
 }
Example #31
0
 public FuncUniverse(SubscriptionDataConfig configuration, UniverseSettings universeSettings, ISecurityInitializer securityInitializer, Func <IEnumerable <BaseData>, IEnumerable <Symbol> > universeSelector)
     : base(configuration, securityInitializer)
 {
     _universeSelector = universeSelector;
     _universeSettings = universeSettings;
 }
Example #32
0
 /// <summary>
 /// Initializes a new instance of the <see cref="FuncUniverse"/> class
 /// </summary>
 /// <param name="configuration">The configuration used to resolve the data for universe selection</param>
 /// <param name="universeSettings">The settings used for new subscriptions generated by this universe</param>
 /// <param name="securityInitializer">Initializes securities when they're added to the universe</param>
 /// <param name="universeSelector">Returns the symbols that should be included in the universe</param>
 public FuncUniverse(SubscriptionDataConfig configuration, UniverseSettings universeSettings, ISecurityInitializer securityInitializer, Func<IEnumerable<BaseData>, IEnumerable<Symbol>> universeSelector)
     : base(configuration, securityInitializer)
 {
     _universeSelector = universeSelector;
     _universeSettings = universeSettings;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="UserDefinedUniverse"/> class
 /// </summary>
 /// <param name="configuration">The configuration used to resolve the data for universe selection</param>
 /// <param name="universeSettings">The settings used for new subscriptions generated by this universe</param>
 /// <param name="securityInitializer">Initializes securities when they're added to the universe</param>
 /// <param name="interval">The interval at which selection should be performed</param>
 /// <param name="symbols">The initial set of symbols in this universe</param>
 public UserDefinedUniverse(SubscriptionDataConfig configuration, UniverseSettings universeSettings, ISecurityInitializer securityInitializer, TimeSpan interval, IEnumerable <Symbol> symbols)
     : base(configuration, securityInitializer)
 {
     _interval         = interval;
     _symbols          = symbols.ToHashSet();
     _universeSettings = universeSettings;
     _selector         = time => _symbols;
 }
        protected virtual Option CreateOptionChainSecurity(QCAlgorithmFramework algorithm, Symbol symbol, UniverseSettings settings, ISecurityInitializer initializer)
        {
            algorithm.Log($"OptionUniverseSelectionModel.CreateOptionChainSecurity({algorithm.UtcTime}, {symbol}): Creating Option Chain Security");

            var market     = symbol.ID.Market;
            var underlying = symbol.Underlying;

            var marketHoursEntry = MarketHoursDatabase.FromDataFolder()
                                   .GetEntry(market, underlying, SecurityType.Option);

            var symbolProperties = SymbolPropertiesDatabase.FromDataFolder()
                                   .GetSymbolProperties(market, underlying, SecurityType.Option, CashBook.AccountCurrency);

            var optionChain = (Option)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);

            return(optionChain);
        }
Example #35
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;
 }
 /// <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)
 {
 }