Esempio n. 1
0
        public void DoesNotUpdateFillForwardResolutionWhenAddingBiggerResolution()
        {
            var subscriptionColletion = new SubscriptionCollection();
            var subscription          = CreateSubscription(Resolution.Second);
            var subscription2         = CreateSubscription(Resolution.Minute);

            subscriptionColletion.TryAdd(subscription);
            Assert.AreEqual(subscriptionColletion.UpdateAndGetFillForwardResolution().Value, new TimeSpan(0, 0, 1));
            subscriptionColletion.TryAdd(subscription2);
            Assert.AreEqual(subscriptionColletion.UpdateAndGetFillForwardResolution().Value, new TimeSpan(0, 0, 1));
        }
Esempio n. 2
0
        public void UpdatesFillForwardResolutionWhenRemoving()
        {
            var subscriptionColletion = new SubscriptionCollection();
            var subscription          = CreateSubscription(Resolution.Second);
            var subscription2         = CreateSubscription(Resolution.Daily);

            subscriptionColletion.TryAdd(subscription);
            subscriptionColletion.TryAdd(subscription2);
            Assert.AreEqual(subscriptionColletion.UpdateAndGetFillForwardResolution().Value, new TimeSpan(0, 0, 1));
            subscriptionColletion.TryRemove(subscription.Configuration, out subscription);
            Assert.AreEqual(subscriptionColletion.UpdateAndGetFillForwardResolution().Value, new TimeSpan(1, 0, 0, 0));
            subscriptionColletion.TryRemove(subscription2.Configuration, out subscription2);
            Assert.AreEqual(subscriptionColletion.UpdateAndGetFillForwardResolution().Value, new TimeSpan(0, 1, 0));
        }
        public void DoesNotUpdateFillForwardResolutionWhenRemovingDuplicateResolution()
        {
            var subscriptionColletion = new SubscriptionCollection();
            var subscription          = CreateSubscription(Resolution.Second);
            var subscription2         = CreateSubscription(Resolution.Second, "SPY");

            subscriptionColletion.TryAdd(subscription);
            Assert.AreEqual(subscriptionColletion.UpdateAndGetFillForwardResolution().Value, new TimeSpan(0, 0, 1));
            subscriptionColletion.TryAdd(subscription2);
            Assert.AreEqual(subscriptionColletion.UpdateAndGetFillForwardResolution().Value, new TimeSpan(0, 0, 1));
            subscriptionColletion.TryRemove(subscription.Configuration, out subscription);
            Assert.AreEqual(subscriptionColletion.UpdateAndGetFillForwardResolution().Value, new TimeSpan(0, 0, 1));
            subscriptionColletion.TryRemove(subscription2.Configuration, out subscription2);
            Assert.AreEqual(subscriptionColletion.UpdateAndGetFillForwardResolution().Value, new TimeSpan(0, 1, 0));
            subscription.Dispose();
            subscription2.Dispose();
        }
        public void UpdatesFillForwardResolutionSuccessfullyWhenAdding()
        {
            var subscriptionColletion = new SubscriptionCollection();
            var subscription          = CreateSubscription(Resolution.Second);

            subscriptionColletion.TryAdd(subscription);
            Assert.AreEqual(subscriptionColletion.UpdateAndGetFillForwardResolution().Value, new TimeSpan(0, 0, 1));
        }
Esempio n. 5
0
        public void UpdatesFillForwardResolutionSuccessfullyOverridesDefaultWhenAdding()
        {
            var subscriptionColletion = new SubscriptionCollection();
            var subscription          = CreateSubscription(Resolution.Daily);

            subscriptionColletion.TryAdd(subscription);
            Assert.AreEqual(subscriptionColletion.UpdateAndGetFillForwardResolution().Value, new TimeSpan(1, 0, 0, 0));
        }
Esempio n. 6
0
        public void SubscriptionsAreSortedWhenAdding2()
        {
            var subscriptionColletion = new SubscriptionCollection();
            var subscription          = CreateSubscription(Resolution.Second, "GC", false, SecurityType.Future);
            var subscription2         = CreateSubscription(Resolution.Second, "SPY");
            var subscription3         = CreateSubscription(Resolution.Second, "AAPL", false, SecurityType.Option);
            var subscription4         = CreateSubscription(Resolution.Second, "EURGBP");

            subscriptionColletion.TryAdd(subscription);
            Assert.AreEqual(subscriptionColletion.ToList(), new[] { subscription });
            subscriptionColletion.TryAdd(subscription2);
            Assert.AreEqual(subscriptionColletion.ToList(), new[] { subscription2, subscription });
            subscriptionColletion.TryAdd(subscription3);
            Assert.AreEqual(subscriptionColletion.ToList(), new[] { subscription2, subscription3, subscription });
            subscriptionColletion.TryAdd(subscription4);

            Assert.AreEqual(subscriptionColletion.ToList(), new[] { subscription4, subscription2, subscription3, subscription });
            Assert.AreEqual(subscriptionColletion.Select(x => x.Security.Type).ToList(), new[] { SecurityType.Equity, SecurityType.Equity, SecurityType.Option, SecurityType.Future });
        }
Esempio n. 7
0
        public void FillForwardResolutionIgnoresInternalFeed()
        {
            var subscriptionColletion = new SubscriptionCollection();
            var subscription          = CreateSubscription(Resolution.Second, "AAPL", true);

            subscriptionColletion.TryAdd(subscription);
            Assert.AreEqual(subscriptionColletion.UpdateAndGetFillForwardResolution().Value, new TimeSpan(0, 1, 0));
            subscriptionColletion.TryRemove(subscription.Configuration, out subscription);
            Assert.AreEqual(subscriptionColletion.UpdateAndGetFillForwardResolution().Value, new TimeSpan(0, 1, 0));
        }
        public void FillForwardResolutionIgnoresTick()
        {
            var subscriptionColletion = new SubscriptionCollection();
            var subscription          = CreateSubscription(Resolution.Tick);

            subscriptionColletion.TryAdd(subscription);
            Assert.AreEqual(subscriptionColletion.UpdateAndGetFillForwardResolution().Value, new TimeSpan(0, 1, 0));
            subscriptionColletion.TryRemove(subscription.Configuration, out subscription);
            Assert.AreEqual(subscriptionColletion.UpdateAndGetFillForwardResolution().Value, new TimeSpan(0, 1, 0));
            subscription.Dispose();
        }
        public void SubscriptionsAreSortedWhenAdding()
        {
            var subscriptionColletion = new SubscriptionCollection();
            var subscription          = CreateSubscription(Resolution.Second, Futures.Metals.Gold, false, SecurityType.Future);
            var subscription2         = CreateSubscription(Resolution.Second, "SPY");
            var subscription3         = CreateSubscription(Resolution.Second, "AAPL", false, SecurityType.Option);
            var subscription4         = CreateSubscription(Resolution.Second, "EURGBP");
            var subscription5         = CreateSubscription(Resolution.Second, "AAPL", false, SecurityType.Option, TickType.OpenInterest);
            var subscription6         = CreateSubscription(Resolution.Second, "AAPL", false, SecurityType.Option, TickType.Quote);

            subscriptionColletion.TryAdd(subscription);
            Assert.AreEqual(subscriptionColletion.ToList(), new[] { subscription });
            subscriptionColletion.TryAdd(subscription2);
            Assert.AreEqual(subscriptionColletion.ToList(), new[] { subscription2, subscription });
            subscriptionColletion.TryAdd(subscription3);
            Assert.AreEqual(subscriptionColletion.ToList(), new[] { subscription2, subscription3, subscription });
            subscriptionColletion.TryAdd(subscription4);
            Assert.AreEqual(subscriptionColletion.ToList(), new[] { subscription4, subscription2, subscription3, subscription });
            subscriptionColletion.TryAdd(subscription5);
            Assert.AreEqual(subscriptionColletion.ToList(), new[] { subscription4, subscription2, subscription3, subscription5, subscription });
            subscriptionColletion.TryAdd(subscription6);
            Assert.AreEqual(subscriptionColletion.ToList(), new[] { subscription4, subscription2, subscription3, subscription6, subscription5, subscription });


            Assert.AreEqual(subscriptionColletion.Select(x => x.Configuration.SecurityType).ToList(), new[] { SecurityType.Equity, SecurityType.Equity, SecurityType.Option,
                                                                                                              SecurityType.Option, SecurityType.Option, SecurityType.Future });

            subscription.Dispose();
            subscription2.Dispose();
            subscription3.Dispose();
            subscription4.Dispose();
            subscription5.Dispose();
            subscription6.Dispose();
        }
        public void SubscriptionsAreSortedWhenRemoving()
        {
            var subscriptionColletion = new SubscriptionCollection();
            var subscription          = CreateSubscription(Resolution.Second, "BTCEUR", false, SecurityType.Future);
            var subscription2         = CreateSubscription(Resolution.Second, "SPY");
            var subscription3         = CreateSubscription(Resolution.Second, "AAPL", false, SecurityType.Option);
            var subscription4         = CreateSubscription(Resolution.Second, "EURGBP");
            var subscription5         = CreateSubscription(Resolution.Second, "AAPL", false, SecurityType.Option, TickType.OpenInterest);
            var subscription6         = CreateSubscription(Resolution.Second, "AAPL", false, SecurityType.Option, TickType.Quote);

            subscriptionColletion.TryAdd(subscription);
            subscriptionColletion.TryAdd(subscription2);
            subscriptionColletion.TryAdd(subscription3);
            subscriptionColletion.TryAdd(subscription4);
            subscriptionColletion.TryAdd(subscription5);
            subscriptionColletion.TryAdd(subscription6);
            Assert.AreEqual(subscriptionColletion.ToList(), new[] { subscription4, subscription2, subscription3, subscription6, subscription5, subscription });

            subscriptionColletion.TryRemove(subscription2.Configuration, out subscription2);
            Assert.AreEqual(subscriptionColletion.Select(x => x.Configuration.SecurityType).ToList(), new[] { SecurityType.Equity, SecurityType.Option,
                                                                                                              SecurityType.Option, SecurityType.Option, SecurityType.Future });

            subscriptionColletion.TryRemove(subscription3.Configuration, out subscription3);
            Assert.AreEqual(subscriptionColletion.Select(x => x.Configuration.SecurityType).ToList(), new[] { SecurityType.Equity, SecurityType.Option, SecurityType.Option, SecurityType.Future });

            subscriptionColletion.TryRemove(subscription.Configuration, out subscription);
            Assert.AreEqual(subscriptionColletion.Select(x => x.Configuration.SecurityType).ToList(), new[] { SecurityType.Equity, SecurityType.Option, SecurityType.Option });
            Assert.AreEqual(subscriptionColletion.ToList(), new[] { subscription4, subscription6, subscription5 });

            subscriptionColletion.TryRemove(subscription6.Configuration, out subscription6);
            Assert.AreEqual(subscriptionColletion.Select(x => x.Configuration.SecurityType).ToList(), new[] { SecurityType.Equity, SecurityType.Option });
            Assert.AreEqual(subscriptionColletion.ToList(), new[] { subscription4, subscription5 });

            subscriptionColletion.TryRemove(subscription5.Configuration, out subscription5);
            Assert.AreEqual(subscriptionColletion.Select(x => x.Configuration.SecurityType).ToList(), new[] { SecurityType.Equity });

            subscriptionColletion.TryRemove(subscription4.Configuration, out subscription4);
            Assert.IsTrue(subscriptionColletion.Select(x => x.Configuration.SecurityType).ToList().IsNullOrEmpty());

            subscription.Dispose();
            subscription2.Dispose();
            subscription3.Dispose();
            subscription4.Dispose();
            subscription5.Dispose();
            subscription6.Dispose();
        }
        public void EnumerationWhileUpdatingDoesNotThrow()
        {
            var cts           = new CancellationTokenSource();
            var subscriptions = new SubscriptionCollection();
            var start         = DateTime.UtcNow;
            var end           = start.AddSeconds(10);
            var config        = new SubscriptionDataConfig(typeof(TradeBar), Symbols.SPY, Resolution.Minute, DateTimeZone.Utc, DateTimeZone.Utc, true, false, false);
            var security      = new Equity(
                Symbols.SPY,
                SecurityExchangeHours.AlwaysOpen(DateTimeZone.Utc),
                new Cash(Currencies.USD, 0, 1),
                SymbolProperties.GetDefault(Currencies.USD),
                ErrorCurrencyConverter.Instance,
                RegisteredSecurityDataTypesProvider.Null,
                new SecurityCache()
                );
            var timeZoneOffsetProvider     = new TimeZoneOffsetProvider(DateTimeZone.Utc, start, end);
            var enumerator                 = new EnqueueableEnumerator <BaseData>();
            var subscriptionDataEnumerator = new SubscriptionDataEnumerator(config, security.Exchange.Hours, timeZoneOffsetProvider, enumerator, false);
            var subscriptionRequest        = new SubscriptionRequest(false, null, security, config, start, end);
            var subscription               = new Subscription(subscriptionRequest, subscriptionDataEnumerator, timeZoneOffsetProvider);

            var addTask = Task.Factory.StartNew(() =>
            {
                Log.Trace("Add task started");

                while (DateTime.UtcNow < end)
                {
                    if (!subscriptions.Contains(config))
                    {
                        subscriptions.TryAdd(subscription);
                    }

                    Thread.Sleep(1);
                }

                Log.Trace("Add task ended");
            }, cts.Token);

            var removeTask = Task.Factory.StartNew(() =>
            {
                Log.Trace("Remove task started");

                while (DateTime.UtcNow < end)
                {
                    Subscription removed;
                    subscriptions.TryRemove(config, out removed);

                    Thread.Sleep(1);
                }

                Log.Trace("Remove task ended");
            }, cts.Token);

            var readTask = Task.Factory.StartNew(() =>
            {
                Log.Trace("Read task started");

                while (DateTime.UtcNow < end)
                {
                    foreach (var sub in subscriptions)
                    {
                    }

                    Thread.Sleep(1);
                }

                Log.Trace("Read task ended");
            }, cts.Token);

            Task.WaitAll(addTask, removeTask, readTask);
            subscription.Dispose();
        }
Esempio n. 12
0
        public void EnumerationWhileUpdatingDoesNotThrow()
        {
            var cts                        = new CancellationTokenSource();
            var subscriptions              = new SubscriptionCollection();
            var start                      = DateTime.UtcNow;
            var end                        = start.AddSeconds(10);
            var config                     = new SubscriptionDataConfig(typeof(TradeBar), Symbols.SPY, Resolution.Minute, DateTimeZone.Utc, DateTimeZone.Utc, true, false, false);
            var security                   = new Equity(Symbols.SPY, SecurityExchangeHours.AlwaysOpen(DateTimeZone.Utc), new Cash("USD", 0, 1), SymbolProperties.GetDefault("USD"));
            var timeZoneOffsetProvider     = new TimeZoneOffsetProvider(DateTimeZone.Utc, start, end);
            var enumerator                 = new EnqueueableEnumerator <BaseData>();
            var subscriptionDataEnumerator = SubscriptionData.Enumerator(config, security, timeZoneOffsetProvider, enumerator);
            var subscription               = new Subscription(null, security, config, subscriptionDataEnumerator, timeZoneOffsetProvider, start, end, false);

            var addTask = new TaskFactory().StartNew(() =>
            {
                Console.WriteLine("Add task started");

                while (DateTime.UtcNow < end)
                {
                    if (!subscriptions.Contains(config))
                    {
                        subscriptions.TryAdd(subscription);
                    }

                    Thread.Sleep(1);
                }

                Console.WriteLine("Add task ended");
            }, cts.Token);

            var removeTask = new TaskFactory().StartNew(() =>
            {
                Console.WriteLine("Remove task started");

                while (DateTime.UtcNow < end)
                {
                    Subscription removed;
                    subscriptions.TryRemove(config, out removed);

                    Thread.Sleep(1);
                }

                Console.WriteLine("Remove task ended");
            }, cts.Token);

            var readTask = new TaskFactory().StartNew(() =>
            {
                Console.WriteLine("Read task started");

                while (DateTime.UtcNow < end)
                {
                    foreach (var sub in subscriptions)
                    {
                    }

                    Thread.Sleep(1);
                }

                Console.WriteLine("Read task ended");
            }, cts.Token);

            Task.WaitAll(addTask, removeTask, readTask);
        }
Esempio n. 13
0
        /// <summary>
        /// Initializes the data feed for the specified job and algorithm
        /// </summary>
        public void Initialize(IAlgorithm algorithm, AlgorithmNodePacket job, IResultHandler resultHandler, IMapFileProvider mapFileProvider, IFactorFileProvider factorFileProvider)
        {
            if (!(job is LiveNodePacket))
            {
                throw new ArgumentException("The LiveTradingDataFeed requires a LiveNodePacket.");
            }

            _cancellationTokenSource = new CancellationTokenSource();

            _algorithm = algorithm;
            _job = (LiveNodePacket) job;
            _resultHandler = resultHandler;
            _timeProvider = GetTimeProvider();
            _dataQueueHandler = GetDataQueueHandler();

            _frontierTimeProvider = new ManualTimeProvider(_timeProvider.GetUtcNow());
            _customExchange = new BaseDataExchange("CustomDataExchange") {SleepInterval = 10};
            // sleep is controlled on this exchange via the GetNextTicksEnumerator
            _exchange = new BaseDataExchange("DataQueueExchange"){SleepInterval = 0};
            _exchange.AddEnumerator(DataQueueHandlerSymbol, GetNextTicksEnumerator());
            _subscriptions = new SubscriptionCollection();

            _bridge = new BusyBlockingCollection<TimeSlice>();
            _universeSelection = new UniverseSelection(this, algorithm, job.Controls);

            // run the exchanges
            Task.Run(() => _exchange.Start(_cancellationTokenSource.Token));
            Task.Run(() => _customExchange.Start(_cancellationTokenSource.Token));

            // this value will be modified via calls to AddSubscription/RemoveSubscription
            var ffres = Time.OneMinute;
            _fillForwardResolution = Ref.Create(() => ffres, v => ffres = v);

            // wire ourselves up to receive notifications when universes are added/removed
            var start = _timeProvider.GetUtcNow();
            algorithm.UniverseManager.CollectionChanged += (sender, args) =>
            {
                switch (args.Action)
                {
                    case NotifyCollectionChangedAction.Add:
                        foreach (var universe in args.NewItems.OfType<Universe>())
                        {
                            if (!_subscriptions.Contains(universe.Configuration))
                            {
                                _subscriptions.TryAdd(CreateUniverseSubscription(universe, start, Time.EndOfTime));
                            }

                            // Not sure if this is needed but left here because of this:
                            // https://github.com/QuantConnect/Lean/commit/029d70bde6ca83a1eb0c667bb5cc4444bea05678
                            UpdateFillForwardResolution();
                        }
                        break;

                    case NotifyCollectionChangedAction.Remove:
                        foreach (var universe in args.OldItems.OfType<Universe>())
                        {
                            RemoveSubscription(universe.Configuration);
                        }
                        break;

                    default:
                        throw new NotImplementedException("The specified action is not implemented: " + args.Action);
                }
            };
        }