Files
quantconnect--lean/Engine/DataFeeds/Enumerators/LiveEquityDataSynchronizingEnumerator.cs
Martin-Molinero 9cdb4a91c5 Refactor live data feed (#4636)
* Live Coarse universe refactor

- Live trading will source Coarse and Fine fundamental data directly
  from disk. Updating unit tests.

* Adds ILiveDataProvider interface

  * Adds wrapper for IDataQueueHandler implementations

  * Replaces IDataQueueHandler with ILiveDataProvider in
    LiveTradingDataFeed

  * Edits IDataQueueHandler documentation

* Maintains aggregation for current IDQH impls and skips for ILDF impls

  * Note: No unit test was created for this method, go back and TODO

* Protobuf Market data

- Adding protobuf support for Ticks, TradeBars and QuoteBars. Adding
  unit tests.

* Adds unit tests for LiveDataAggregator changes

  * Fixes bug where custom data was not handled as it was before
  * Fixes race condition bug because of variable reuse in class

* Add protobuf extension serialization

* Fixes for protobuf serialization

* Refactor

* Fix OptionChainUniverse

* replace BaseDataExchange pumping ticks with consolidators

* AlpacaBrokerage

* BitfinexBrokerage

* GDAXBrokerage

* OandaBrokerage

* InteractiveBrokers

* TradierBrokerage

* FxcmBrokerage

* PaperBrokerage

* etc

* WIP fixes for existing LTDF unit tests

* Fixes more LTDF unit tests

* make IDataAggregator.Update recieving Generic BaseData rather than Tick

* Change IDataQueueHandler.Subscribe method

* Some fixes after adding new commits

* Adds protobuf (de)serialization support for Dividend and Split

* Serialize protobuf with length prefix

* Fix missing LTDF unit tests

* Adds TiingoNews protobuf definitions

* fix comments

* more fixes on IQFeedDataQueueHandler

* disallow putting ticks into enumerator directly

* ScannableEnumerator tests

* fix OandaBrokerage

* AggregationManager unit tests

* fix AlpacaBrokerage tests

* fix InteractiveBrokers

* fix FxcmBrokerage tests

* call AggregationManager.Remove method on unsubscribe

* fix GDAX existing tests

* Fixes, refactor adding more tests for AggregatorManager

* Adds BenzingaNews protobuf definitions and round trip unit test

* Adds missing TiingoNews unit test to Protobuf round trip tests

* Improve sleep sequence of LiveSynchronizer

* need start aggregating first, and then can subscribe

* More test fixes and refactor

- Refactoring AggregationManager and ScannableEnumerator so the last is
  the one that owns the consolidator
- Adding pulse on the main LiveSynchronizer

* Improve performance of LEquityDataSynchronizingEnu

* Add missing Set job packet method

* Minor performance improvements

* Improvements add test timeout

- Improvements adding test timeout to find blocking test in travis

* Improve aggregationManager performance

* Testing improvements for travis

* Remove test timeouts

* More test fixes

- Adding more missing dispose calls and improving determinism

* fix IEXDataQueueHandler and tests

* Final tweaks to LTDF tests

* more AggregationManager tests

* consume and log ticks

* fix test: couldn't subscribe to Forex tickers

* change Resolution for all bar configs

* Improve RealTimeScheduleEventServiceAccuracy

* refactoring: move common code to base class

* fixed bug; unsubscribe SubscriptionDataConfig

* Small performance improvement

* Minor fixes

* Avoid Symbol serialization

* Fixes coarse selection in live mode

* Fix for live coarse

* Adds protobuf (de)serialization support for Robintrack

  * Adds round-trip unit test

* Minor performance improvements

* More minor performance improvements

* pass LiveNodePacket through to OandaBrokerage

* Fixes empty list becoming null value when deserializing with protobuf

* Reverts BZ live trading exception removal and fixes tests

* Refactor WorkQueue making it abstract

* Add try catch for composer

* Adds optional data batching period to LiveFillForwardEnumerator

* Override data-queue-handler with config

* Improve PeriodCountConsolidator.Scan performance

* Move batching delay to main Synchornizer thread

* Reverts addition of Robintrack protobuf definitions

* Give priority to config history provider if set

* Add Estimize protobuffing

- Add Estimize protobuffing support. Adding unit tests

* Always dispose of data queue handler

Co-authored-by: Gerardo Salazar <gsalaz9800@gmail.com>
Co-authored-by: Adalyat Nazirov <aenazirov@gmail.com>
2020-08-18 20:21:10 -03:00

193 lines
8.0 KiB
C#

/*
* 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;
using System.Collections.Generic;
using System.Linq;
using NodaTime;
using QuantConnect.Data;
using QuantConnect.Util;
namespace QuantConnect.Lean.Engine.DataFeeds.Enumerators
{
/// <summary>
/// Represents an enumerator capable of synchronizing live equity data enumerators in time.
/// This assumes that all enumerators have data time stamped in the same time zone.
/// </summary>
public class LiveEquityDataSynchronizingEnumerator : IEnumerator<BaseData>
{
private readonly ITimeProvider _timeProvider;
private readonly DateTimeZone _exchangeTimeZone;
private readonly List<IEnumerator<BaseData>> _auxDataEnumerators;
private readonly IEnumerator<BaseData> _tradeBarAggregator;
/// <summary>
/// Initializes a new instance of the <see cref="LiveEquityDataSynchronizingEnumerator"/> class
/// </summary>
/// <param name="timeProvider">The source of time used to gauge when this enumerator should emit extra bars when null data is returned from the source enumerator</param>
/// <param name="exchangeTimeZone">The time zone the raw data is time stamped in</param>
/// <param name="tradeBarAggregator">The trade bar aggregator enumerator</param>
/// <param name="auxDataEnumerators">The auxiliary data enumerators</param>
public LiveEquityDataSynchronizingEnumerator(ITimeProvider timeProvider, DateTimeZone exchangeTimeZone, IEnumerator<BaseData> tradeBarAggregator, params IEnumerator<BaseData>[] auxDataEnumerators)
{
_timeProvider = timeProvider;
_exchangeTimeZone = exchangeTimeZone;
_auxDataEnumerators = auxDataEnumerators.ToList();
_tradeBarAggregator = tradeBarAggregator;
}
/// <summary>
/// Advances the enumerator to the next element of the collection.
/// </summary>
/// <returns> true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection.</returns>
/// <exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created.</exception>
public bool MoveNext()
{
// use manual time provider from LiveTradingDataFeed
var frontierUtc = _timeProvider.GetUtcNow();
// check if any enumerator is ready to emit
if (DataPointEmitted(frontierUtc))
return true;
// advance enumerators with no current data
for (var i = 0; i < _auxDataEnumerators.Count; i++)
{
if (_auxDataEnumerators[i].Current == null)
{
_auxDataEnumerators[i].MoveNext();
}
}
if (_tradeBarAggregator.Current == null) _tradeBarAggregator.MoveNext();
// check if any enumerator is ready to emit
if (DataPointEmitted(frontierUtc))
return true;
Current = null;
// IEnumerator contract dictates that we return true unless we're actually
// finished with the 'collection' and since this is live, we're never finished
return true;
}
/// <summary>
/// Sets the enumerator to its initial position, which is before the first element in the collection.
/// </summary>
/// <exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created.</exception>
public void Reset()
{
foreach (var auxDataEnumerator in _auxDataEnumerators)
{
auxDataEnumerator.Reset();
}
_tradeBarAggregator.Reset();
}
/// <summary>
/// Gets the element in the collection at the current position of the enumerator.
/// </summary>
/// <returns>The element in the collection at the current position of the enumerator.</returns>
public BaseData Current { get; private set; }
/// <summary>
/// Gets the current element in the collection.
/// </summary>
/// <returns>The current element in the collection.</returns>
object IEnumerator.Current => Current;
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
foreach (var auxDataEnumerator in _auxDataEnumerators)
{
auxDataEnumerator.DisposeSafely();
}
_tradeBarAggregator.DisposeSafely();
}
private bool DataPointEmitted(DateTime frontierUtc)
{
// we get the aux enumerator that has the smallest endTime if any
IEnumerator<BaseData> auxDataEnumerator = null;
for (var i = 0; i < _auxDataEnumerators.Count; i++)
{
var currentEnum = _auxDataEnumerators[i];
if (currentEnum.Current != null)
{
if (auxDataEnumerator == null)
{
auxDataEnumerator = currentEnum;
}
else
{
auxDataEnumerator = auxDataEnumerator.Current.EndTime > currentEnum.Current.EndTime ? currentEnum : auxDataEnumerator;
}
}
}
// check if any enumerator is ready to emit
if (auxDataEnumerator?.Current != null && _tradeBarAggregator.Current != null)
{
var auxDataEndTime = auxDataEnumerator.Current.EndTime.ConvertToUtc(_exchangeTimeZone);
var tradeBarEndTime = _tradeBarAggregator.Current.EndTime.ConvertToUtc(_exchangeTimeZone);
if (auxDataEndTime < tradeBarEndTime)
{
if (auxDataEndTime <= frontierUtc)
{
Current = auxDataEnumerator.Current;
auxDataEnumerator.MoveNext();
return true;
}
}
else
{
if (tradeBarEndTime <= frontierUtc)
{
Current = _tradeBarAggregator.Current;
_tradeBarAggregator.MoveNext();
return true;
}
}
}
else if (auxDataEnumerator?.Current != null)
{
var auxDataEndTime = auxDataEnumerator.Current.EndTime.ConvertToUtc(_exchangeTimeZone);
if (auxDataEndTime <= frontierUtc)
{
Current = auxDataEnumerator.Current;
auxDataEnumerator.MoveNext();
return true;
}
}
else if (_tradeBarAggregator.Current != null)
{
var tradeBarEndTime = _tradeBarAggregator.Current.EndTime.ConvertToUtc(_exchangeTimeZone);
if (tradeBarEndTime <= frontierUtc)
{
Current = _tradeBarAggregator.Current;
_tradeBarAggregator.MoveNext();
return true;
}
}
return false;
}
}
}