9cdb4a91c5
* 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>
166 lines
6.9 KiB
C#
166 lines
6.9 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 QuantConnect.Data;
|
|
using QuantConnect.Data.Consolidators;
|
|
using QuantConnect.Data.Market;
|
|
using QuantConnect.Lean.Engine.DataFeeds.Enumerators;
|
|
using QuantConnect.Logging;
|
|
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace QuantConnect.Lean.Engine.DataFeeds
|
|
{
|
|
/// <summary>
|
|
/// Aggregates ticks and bars based on given subscriptions.
|
|
/// Current implementation is based on <see cref="IDataConsolidator"/> that consolidates ticks and put them into enumerator.
|
|
/// </summary>
|
|
public class AggregationManager : IDataAggregator
|
|
{
|
|
private readonly ConcurrentDictionary<SecurityIdentifier, List<KeyValuePair<SubscriptionDataConfig, ScannableEnumerator<BaseData>>>> _enumerators
|
|
= new ConcurrentDictionary<SecurityIdentifier, List<KeyValuePair<SubscriptionDataConfig, ScannableEnumerator<BaseData>>>>();
|
|
|
|
/// <summary>
|
|
/// Continuous UTC time provider
|
|
/// </summary>
|
|
protected ITimeProvider TimeProvider { get; set; } = new RealTimeProvider();
|
|
|
|
/// <summary>
|
|
/// Add new subscription to current <see cref="IDataAggregator"/> instance
|
|
/// </summary>
|
|
/// <param name="dataConfig">defines the parameters to subscribe to a data feed</param>
|
|
/// <param name="newDataAvailableHandler">handler to be fired on new data available</param>
|
|
/// <returns>The new enumerator for this subscription request</returns>
|
|
public IEnumerator<BaseData> Add(SubscriptionDataConfig dataConfig, EventHandler newDataAvailableHandler)
|
|
{
|
|
IDataConsolidator consolidator;
|
|
var period = dataConfig.Resolution.ToTimeSpan();
|
|
var isPeriodBased = false;
|
|
switch (dataConfig.Type.Name)
|
|
{
|
|
case nameof(QuoteBar):
|
|
isPeriodBased = dataConfig.Resolution != Resolution.Tick;
|
|
consolidator = new TickQuoteBarConsolidator(period);
|
|
break;
|
|
|
|
case nameof(TradeBar):
|
|
isPeriodBased = dataConfig.Resolution != Resolution.Tick;
|
|
consolidator = new TickConsolidator(period);
|
|
break;
|
|
|
|
case nameof(OpenInterest):
|
|
isPeriodBased = dataConfig.Resolution != Resolution.Tick;
|
|
consolidator = new OpenInterestConsolidator(period);
|
|
break;
|
|
|
|
case nameof(Tick):
|
|
consolidator = FilteredIdentityDataConsolidator.ForTickType(dataConfig.TickType);
|
|
break;
|
|
|
|
case nameof(Split):
|
|
consolidator = new IdentityDataConsolidator<Split>();
|
|
break;
|
|
|
|
case nameof(Dividend):
|
|
consolidator = new IdentityDataConsolidator<Dividend>();
|
|
break;
|
|
|
|
default:
|
|
// streaming custom data subscriptions can pass right through
|
|
consolidator = new FilteredIdentityDataConsolidator<BaseData>(data => data.GetType() == dataConfig.Type);
|
|
break;
|
|
}
|
|
|
|
var enumerator = new ScannableEnumerator<BaseData>(consolidator, dataConfig.ExchangeTimeZone, TimeProvider, newDataAvailableHandler, isPeriodBased);
|
|
|
|
_enumerators.AddOrUpdate(
|
|
dataConfig.Symbol.ID,
|
|
new List<KeyValuePair<SubscriptionDataConfig, ScannableEnumerator<BaseData>>> { new KeyValuePair<SubscriptionDataConfig, ScannableEnumerator<BaseData>>(dataConfig, enumerator) },
|
|
(k, v) => { return v.Concat(new[] { new KeyValuePair<SubscriptionDataConfig, ScannableEnumerator<BaseData>>(dataConfig, enumerator) }).ToList(); });
|
|
|
|
return enumerator;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Removes the handler with the specified identifier
|
|
/// </summary>
|
|
/// <param name="dataConfig">Subscription data configuration to be removed</param>
|
|
public bool Remove(SubscriptionDataConfig dataConfig)
|
|
{
|
|
List<KeyValuePair<SubscriptionDataConfig, ScannableEnumerator<BaseData>>> enumerators;
|
|
if (_enumerators.TryGetValue(dataConfig.Symbol.ID, out enumerators))
|
|
{
|
|
if (enumerators.Count == 1)
|
|
{
|
|
List<KeyValuePair<SubscriptionDataConfig, ScannableEnumerator<BaseData>>> output;
|
|
return _enumerators.TryRemove(dataConfig.Symbol.ID, out output);
|
|
}
|
|
else
|
|
{
|
|
_enumerators[dataConfig.Symbol.ID] = enumerators.Where(pair => pair.Key != dataConfig).ToList();
|
|
return true;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Log.Debug($"AggregationManager.Update(): IDataConsolidator for symbol ({dataConfig.Symbol.Value}) was not found.");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Add new data to aggregator
|
|
/// </summary>
|
|
/// <param name="input">The new data</param>
|
|
public void Update(BaseData input)
|
|
{
|
|
try
|
|
{
|
|
List<KeyValuePair<SubscriptionDataConfig, ScannableEnumerator<BaseData>>> enumerators;
|
|
if (_enumerators.TryGetValue(input.Symbol.ID, out enumerators))
|
|
{
|
|
for (var i = 0; i < enumerators.Count; i++)
|
|
{
|
|
var kvp = enumerators[i];
|
|
|
|
// for non tick resolution subscriptions drop suspicious ticks
|
|
if (kvp.Key.Resolution != Resolution.Tick)
|
|
{
|
|
var tick = input as Tick;
|
|
if (tick != null && tick.Suspicious)
|
|
{
|
|
continue;
|
|
}
|
|
}
|
|
|
|
kvp.Value.Update(input);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Log.Error(exception);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Dispose of the aggregation manager.
|
|
/// </summary>
|
|
public void Dispose() { }
|
|
}
|
|
}
|