/* * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ using System; using System.Collections.Generic; using QuantConnect.Data; using QuantConnect.Data.UniverseSelection; using System.Collections; using System.Linq; using QuantConnect.Data.Auxiliary; using QuantConnect.Interfaces; namespace QuantConnect.Lean.Engine.DataFeeds.Enumerators { /// /// Enumerates live futures symbol universe data into instances /// public class DataQueueFuturesChainUniverseDataCollectionEnumerator : IEnumerator { private readonly SubscriptionRequest _subscriptionRequest; private readonly IDataQueueUniverseProvider _universeProvider; private readonly ITimeProvider _timeProvider; private bool _needNewCurrent; private DateTime _lastEmitTime; /// /// Initializes a new instance of the class. /// /// The subscription request to be used /// Symbol universe provider of the data queue /// The time provider to be used public DataQueueFuturesChainUniverseDataCollectionEnumerator( SubscriptionRequest subscriptionRequest, IDataQueueUniverseProvider universeProvider, ITimeProvider timeProvider) { _subscriptionRequest = subscriptionRequest; _universeProvider = universeProvider; _timeProvider = timeProvider; _needNewCurrent = true; } /// /// Returns current futures chain enumerator position /// public FuturesChainUniverseDataCollection Current { get; private set; } /// /// Returns current futures chain enumerator position /// object IEnumerator.Current => Current; /// /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// public void Dispose() { } /// /// Advances the enumerator to the next element of the collection. /// /// /// true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection. /// public bool MoveNext() { if (!_needNewCurrent) { // refresh on date change (in exchange time zone) _needNewCurrent = _timeProvider.GetUtcNow().ConvertFromUtc(_subscriptionRequest.Configuration.ExchangeTimeZone).Date != _lastEmitTime.Date; } if (_needNewCurrent) { var localTime = _timeProvider.GetUtcNow() .RoundDown(_subscriptionRequest.Configuration.Increment) .ConvertFromUtc(_subscriptionRequest.Configuration.ExchangeTimeZone); // loading the list of futures contracts and converting them into zip entries var symbols = _universeProvider.LookupSymbols(_subscriptionRequest.Security.Symbol.ID.Symbol, _subscriptionRequest.Security.Type); var zipEntries = symbols.Select(x => new ZipEntryName { Time = localTime, Symbol = x } as BaseData).ToList(); var current = new FuturesChainUniverseDataCollection { Symbol = _subscriptionRequest.Security.Symbol, Data = zipEntries, Time = localTime, EndTime = localTime }; _lastEmitTime = localTime; Current = current; _needNewCurrent = false; } else { Current = null; } return true; } /// /// Sets the enumerator to its initial position, which is before the first element in the collection. /// public void Reset() { _needNewCurrent = true; } } }