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>
202 lines
8.0 KiB
C#
202 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.Generic;
|
|
using System.Threading;
|
|
using QuantConnect.Configuration;
|
|
using QuantConnect.Data.UniverseSelection;
|
|
using QuantConnect.Interfaces;
|
|
using QuantConnect.Logging;
|
|
using QuantConnect.Util;
|
|
|
|
namespace QuantConnect.Lean.Engine.DataFeeds
|
|
{
|
|
/// <summary>
|
|
/// Implementation of the <see cref="ISynchronizer"/> interface which provides the mechanism to stream live data to the algorithm
|
|
/// </summary>
|
|
public class LiveSynchronizer : Synchronizer
|
|
{
|
|
private ITimeProvider _timeProvider;
|
|
private RealTimeScheduleEventService _realTimeScheduleEventService;
|
|
private readonly int _batchingDelay = Config.GetInt("consumer-batching-timeout-ms");
|
|
private readonly ManualResetEventSlim _newLiveDataEmitted = new ManualResetEventSlim(false);
|
|
|
|
/// <summary>
|
|
/// Continuous UTC time provider
|
|
/// </summary>
|
|
public override ITimeProvider TimeProvider => _timeProvider;
|
|
|
|
/// <summary>
|
|
/// Initializes the instance of the Synchronizer class
|
|
/// </summary>
|
|
public override void Initialize(
|
|
IAlgorithm algorithm,
|
|
IDataFeedSubscriptionManager dataFeedSubscriptionManager)
|
|
{
|
|
base.Initialize(algorithm, dataFeedSubscriptionManager);
|
|
|
|
_timeProvider = GetTimeProvider();
|
|
SubscriptionSynchronizer.SetTimeProvider(TimeProvider);
|
|
|
|
// attach event handlers to subscriptions
|
|
dataFeedSubscriptionManager.SubscriptionAdded += (sender, subscription) =>
|
|
{
|
|
subscription.NewDataAvailable += OnSubscriptionNewDataAvailable;
|
|
};
|
|
|
|
dataFeedSubscriptionManager.SubscriptionRemoved += (sender, subscription) =>
|
|
{
|
|
subscription.NewDataAvailable -= OnSubscriptionNewDataAvailable;
|
|
};
|
|
|
|
_realTimeScheduleEventService = new RealTimeScheduleEventService(new RealTimeProvider());
|
|
// this schedule event will be our time pulse
|
|
_realTimeScheduleEventService.NewEvent += (sender, args) => _newLiveDataEmitted.Set();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns an enumerable which provides the data to stream to the algorithm
|
|
/// </summary>
|
|
public override IEnumerable<TimeSlice> StreamData(CancellationToken cancellationToken)
|
|
{
|
|
PostInitialize();
|
|
|
|
var shouldSendExtraEmptyPacket = false;
|
|
var nextEmit = DateTime.MinValue;
|
|
var lastLoopStart = DateTime.UtcNow;
|
|
|
|
var enumerator = SubscriptionSynchronizer
|
|
.Sync(SubscriptionManager.DataFeedSubscriptions, cancellationToken)
|
|
.GetEnumerator();
|
|
|
|
var previousWasTimePulse = false;
|
|
while (!cancellationToken.IsCancellationRequested)
|
|
{
|
|
var now = DateTime.UtcNow;
|
|
if (!previousWasTimePulse)
|
|
{
|
|
if (!_newLiveDataEmitted.IsSet)
|
|
{
|
|
// if we just crossed into the next second let's loop again, we will flush any consolidator bar
|
|
// else we will wait to be notified by the subscriptions or our scheduled event service every second
|
|
if (lastLoopStart.Second == now.Second)
|
|
{
|
|
_realTimeScheduleEventService.ScheduleEvent(TimeSpan.FromMilliseconds(GetPulseDueTime(now)), now);
|
|
_newLiveDataEmitted.Wait();
|
|
}
|
|
}
|
|
_newLiveDataEmitted.Reset();
|
|
}
|
|
|
|
lastLoopStart = now;
|
|
|
|
TimeSlice timeSlice;
|
|
try
|
|
{
|
|
if (!enumerator.MoveNext())
|
|
{
|
|
// the enumerator ended
|
|
break;
|
|
}
|
|
timeSlice = enumerator.Current;
|
|
}
|
|
catch (Exception err)
|
|
{
|
|
Log.Error(err);
|
|
// notify the algorithm about the error, so it can be reported to the user
|
|
Algorithm.RunTimeError = err;
|
|
Algorithm.Status = AlgorithmStatus.RuntimeError;
|
|
shouldSendExtraEmptyPacket = true;
|
|
break;
|
|
}
|
|
|
|
// check for cancellation
|
|
if (timeSlice == null || cancellationToken.IsCancellationRequested) break;
|
|
|
|
var frontierUtc = FrontierTimeProvider.GetUtcNow();
|
|
// emit on data or if we've elapsed a full second since last emit or there are security changes
|
|
if (timeSlice.SecurityChanges != SecurityChanges.None
|
|
|| timeSlice.IsTimePulse
|
|
|| timeSlice.Data.Count != 0
|
|
|| frontierUtc >= nextEmit)
|
|
{
|
|
previousWasTimePulse = timeSlice.IsTimePulse;
|
|
yield return timeSlice;
|
|
|
|
// force emitting every second since the data feed is
|
|
// the heartbeat of the application
|
|
nextEmit = frontierUtc.RoundDown(Time.OneSecond).Add(Time.OneSecond);
|
|
}
|
|
}
|
|
|
|
if (shouldSendExtraEmptyPacket)
|
|
{
|
|
// send last empty packet list before terminating,
|
|
// so the algorithm manager has a chance to detect the runtime error
|
|
// and exit showing the correct error instead of a timeout
|
|
nextEmit = FrontierTimeProvider.GetUtcNow().RoundDown(Time.OneSecond);
|
|
if (!cancellationToken.IsCancellationRequested)
|
|
{
|
|
var timeSlice = TimeSliceFactory.Create(
|
|
nextEmit,
|
|
new List<DataFeedPacket>(),
|
|
SecurityChanges.None,
|
|
new Dictionary<Universe, BaseDataCollection>());
|
|
yield return timeSlice;
|
|
}
|
|
}
|
|
|
|
enumerator.DisposeSafely();
|
|
Log.Trace("LiveSynchronizer.GetEnumerator(): Exited thread.");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Free resources
|
|
/// </summary>
|
|
public override void Dispose()
|
|
{
|
|
_newLiveDataEmitted.Set();
|
|
_newLiveDataEmitted?.DisposeSafely();
|
|
_realTimeScheduleEventService?.DisposeSafely();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the <see cref="ITimeProvider"/> to use. By default this will load the
|
|
/// <see cref="RealTimeProvider"/> for live mode, else <see cref="SubscriptionFrontierTimeProvider"/>
|
|
/// </summary>
|
|
/// <returns>The <see cref="ITimeProvider"/> to use</returns>
|
|
protected override ITimeProvider GetTimeProvider()
|
|
{
|
|
return new RealTimeProvider();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Will return the amount of milliseconds that are missing for the next time pulse
|
|
/// </summary>
|
|
protected virtual int GetPulseDueTime(DateTime now)
|
|
{
|
|
// let's wait until the next second starts
|
|
return 1000 - now.Millisecond + _batchingDelay;
|
|
}
|
|
|
|
protected virtual void OnSubscriptionNewDataAvailable(object sender, EventArgs args)
|
|
{
|
|
_newLiveDataEmitted.Set();
|
|
}
|
|
}
|
|
}
|