/*
* 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 QuantConnect.Data;
using QuantConnect.Util;
using QuantConnect.Interfaces;
using System.Collections.Generic;
using QuantConnect.Data.UniverseSelection;
namespace QuantConnect.Lean.Engine.DataFeeds
{
///
/// Data source reader that will aggregate data points into a base data collection
///
public class BaseDataCollectionAggregatorReader : TextSubscriptionDataSourceReader
{
private readonly Type _collectionType;
private BaseDataCollection _collection;
///
/// Initializes a new instance of the class
///
/// This provider caches files if needed
/// The subscription's configuration
/// The date this factory was produced to read data for
/// True if we're in live mode, false for backtesting
/// The object storage for data persistence
public BaseDataCollectionAggregatorReader(IDataCacheProvider dataCacheProvider, SubscriptionDataConfig config, DateTime date,
bool isLiveMode, IObjectStore objectStore)
: base(dataCacheProvider, config, date, isLiveMode, objectStore)
{
// if the type is not a BaseDataCollection, we'll default to BaseDataCollection.
// e.g. custom Python dynamic folding collections need to be aggregated into a BaseDataCollection,
// but they implement PythonData, so casting an instance of PythonData to BaseDataCollection will fail.
_collectionType = config.Type.IsAssignableTo(typeof(BaseDataCollection)) ? config.Type : typeof(BaseDataCollection);
}
///
/// Reads the specified
///
/// The source to be read
/// An that contains the data in the source
public override IEnumerable Read(SubscriptionDataSource source)
{
foreach (var point in base.Read(source))
{
if (point is BaseDataCollection collection && !collection.Data.IsNullOrEmpty())
{
// if underlying already is returning an aggregated collection let it through as is
yield return point;
}
else
{
if (_collection != null && _collection.EndTime != point.EndTime)
{
// when we get a new time we flush current collection instance, if any
yield return _collection;
_collection = null;
}
if (_collection == null)
{
_collection = (BaseDataCollection)Activator.CreateInstance(_collectionType);
_collection.Time = point.Time;
_collection.Symbol = Config.Symbol;
_collection.EndTime = point.EndTime;
}
// aggregate the data points
_collection.Add(point);
}
}
// underlying reader ended, flush current collection instance if any
if (_collection != null)
{
yield return _collection;
_collection = null;
}
}
}
}