Ejemplo n.º 1
0
        public void StoresZipEntryNamesInDataCollection()
        {
            Console.WriteLine(new DateTime().Ticks);
            Console.WriteLine(DateTime.MinValue.Ticks);
            var list = new List <BaseData>
            {
                new Tick(),
                new Tick(),
                new ZipEntryName(),
                new ZipEntryName(),
                new Tick()
            };

            var aggregator = new OptionChainUniverseDataCollectionAggregatorEnumerator(list.GetEnumerator(), Symbols.SPY);

            Assert.IsTrue(aggregator.MoveNext());
            Assert.IsNotNull(aggregator.Current);
            Assert.AreEqual(2, aggregator.Current.Data.Count);
            Assert.IsTrue(aggregator.Current.Data.All(x => x is ZipEntryName));
            Assert.AreEqual(list.Last(), aggregator.Current.Underlying);
        }
        public void StoresZipEntryNamesInDataCollection()
        {
            Console.WriteLine(new DateTime().Ticks);
            Console.WriteLine(DateTime.MinValue.Ticks);
            var list = new List<BaseData>
            {
                new Tick(),
                new Tick(),
                new ZipEntryName(),
                new ZipEntryName(),
                new Tick()
            };

            var aggregator = new OptionChainUniverseDataCollectionAggregatorEnumerator(list.GetEnumerator(), Symbols.SPY);

            Assert.IsTrue(aggregator.MoveNext());
            Assert.IsNotNull(aggregator.Current);
            Assert.AreEqual(2, aggregator.Current.Data.Count);
            Assert.IsTrue(aggregator.Current.Data.All(x => x is ZipEntryName));
            Assert.AreEqual(list.Last(), aggregator.Current.Underlying);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Adds a new subscription for universe selection
        /// </summary>
        /// <param name="universe">The universe to add a subscription for</param>
        /// <param name="startTimeUtc">The start time of the subscription in utc</param>
        /// <param name="endTimeUtc">The end time of the subscription in utc</param>
        public void AddUniverseSubscription(Universe universe, DateTime startTimeUtc, DateTime endTimeUtc)
        {
            // TODO : Consider moving the creating of universe subscriptions to a separate, testable class

            // grab the relevant exchange hours
            var config = universe.Configuration;

            var marketHoursDatabase = MarketHoursDatabase.FromDataFolder();
            var exchangeHours       = marketHoursDatabase.GetExchangeHours(config);

            Security security;

            if (!_algorithm.Securities.TryGetValue(config.Symbol, out security))
            {
                // create a canonical security object if it doesn't exist
                security = new Security(exchangeHours, config, _algorithm.Portfolio.CashBook[CashBook.AccountCurrency], SymbolProperties.GetDefault(CashBook.AccountCurrency));
            }

            var localStartTime = startTimeUtc.ConvertFromUtc(security.Exchange.TimeZone);
            var localEndTime   = endTimeUtc.ConvertFromUtc(security.Exchange.TimeZone);

            // define our data enumerator
            IEnumerator <BaseData> enumerator;

            var tradeableDates = Time.EachTradeableDayInTimeZone(security.Exchange.Hours, localStartTime, localEndTime, config.DataTimeZone, config.ExtendedMarketHours);

            var userDefined = universe as UserDefinedUniverse;

            if (userDefined != null)
            {
                // spoof a tick on the requested interval to trigger the universe selection function
                enumerator = userDefined.GetTriggerTimes(startTimeUtc, endTimeUtc, marketHoursDatabase)
                             .Select(x => new Tick {
                    Time = x, Symbol = config.Symbol
                }).GetEnumerator();

                // route these custom subscriptions through the exchange for buffering
                var enqueueable = new EnqueueableEnumerator <BaseData>(true);

                // add this enumerator to our exchange
                ScheduleEnumerator(enumerator, enqueueable, GetLowerThreshold(config.Resolution), GetUpperThreshold(config.Resolution));

                enumerator = enqueueable;
            }
            else if (config.Type == typeof(CoarseFundamental))
            {
                var cf = new CoarseFundamental();

                // load coarse data day by day
                enumerator = (from date in Time.EachTradeableDayInTimeZone(security.Exchange.Hours, _algorithm.StartDate, _algorithm.EndDate, config.DataTimeZone, config.ExtendedMarketHours)
                              let source = cf.GetSource(config, date, false)
                                           let factory = SubscriptionDataSourceReader.ForSource(source, config, date, false)
                                                         let coarseFundamentalForDate = factory.Read(source)
                                                                                        select new BaseDataCollection(date.AddDays(1), config.Symbol, coarseFundamentalForDate)
                              ).GetEnumerator();

                var enqueueable = new EnqueueableEnumerator <BaseData>(true);
                ScheduleEnumerator(enumerator, enqueueable, 5, 100000, 2);

                enumerator = enqueueable;
            }
            else if (config.SecurityType == SecurityType.Option && security is Option)
            {
                var configs     = universe.GetSubscriptions(security);
                var enumerators = configs.Select(c =>
                                                 CreateSubscriptionEnumerator(security, c, localStartTime, localEndTime, _mapFileProvider.Get(c.Market), tradeableDates, false, true)
                                                 ).ToList();

                var sync = new SynchronizingEnumerator(enumerators);
                enumerator = new OptionChainUniverseDataCollectionAggregatorEnumerator(sync, config.Symbol);

                var enqueueable = new EnqueueableEnumerator <BaseData>(true);

                // add this enumerator to our exchange
                ScheduleEnumerator(enumerator, enqueueable, GetLowerThreshold(config.Resolution), GetUpperThreshold(config.Resolution));

                enumerator = enqueueable;
            }
            else
            {
                // normal reader for all others
                enumerator = CreateSubscriptionEnumerator(security, config, localStartTime, localEndTime, MapFileResolver.Empty, tradeableDates, true, false);

                // route these custom subscriptions through the exchange for buffering
                var enqueueable = new EnqueueableEnumerator <BaseData>(true);

                // add this enumerator to our exchange
                ScheduleEnumerator(enumerator, enqueueable, GetLowerThreshold(config.Resolution), GetUpperThreshold(config.Resolution));

                enumerator = enqueueable;
            }

            // create the subscription
            var timeZoneOffsetProvider = new TimeZoneOffsetProvider(security.Exchange.TimeZone, startTimeUtc, endTimeUtc);
            var subscription           = new Subscription(universe, security, config, enumerator, timeZoneOffsetProvider, startTimeUtc, endTimeUtc, true);

            _subscriptions.TryAdd(subscription);

            UpdateFillForwardResolution();
        }