Files
quantconnect--lean/Common/Data/Custom/Estimize/EstimizeConsensus.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

256 lines
8.7 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 Newtonsoft.Json;
using System;
using System.IO;
using NodaTime;
using ProtoBuf;
using static QuantConnect.StringExtensions;
namespace QuantConnect.Data.Custom.Estimize
{
/// <summary>
/// Consensus of the specified release
/// </summary>
[ProtoContract(SkipConstructor = true)]
public class EstimizeConsensus : BaseData
{
/// <summary>
/// The unique identifier for the estimate
/// </summary>
[ProtoMember(10)]
[JsonProperty(PropertyName = "id")]
public string Id { get; set; }
/// <summary>
/// Consensus source (Wall Street or Estimize)
/// </summary>
[ProtoMember(11)]
[JsonProperty(PropertyName = "source")]
public Source? Source { get; set; }
/// <summary>
/// Type of Consensus (EPS or Revenue)
/// </summary>
[ProtoMember(12)]
[JsonProperty(PropertyName = "type")]
public Type? Type { get; set; }
/// <summary>
/// The mean of the distribution of estimates (the "consensus")
/// </summary>
[ProtoMember(13)]
[JsonProperty(PropertyName = "mean")]
public decimal? Mean { get; set; }
/// <summary>
/// The mean of the distribution of estimates (the "consensus")
/// </summary>
public override decimal Value => Mean ?? 0m;
/// <summary>
/// The highest estimate in the distribution
/// </summary>
[ProtoMember(14)]
[JsonProperty(PropertyName = "high")]
public decimal? High { get; set; }
/// <summary>
/// The lowest estimate in the distribution
/// </summary>
[ProtoMember(15)]
[JsonProperty(PropertyName = "low")]
public decimal? Low { get; set; }
/// <summary>
/// The standard deviation of the distribution
/// </summary>
[ProtoMember(16)]
[JsonProperty(PropertyName = "standard_deviation")]
public decimal? StandardDeviation { get; set; }
/// <summary>
/// The number of estimates in the distribution
/// </summary>
[ProtoMember(17)]
[JsonProperty(PropertyName = "count")]
public int? Count { get; set; }
/// <summary>
/// The timestamp of this consensus (UTC)
/// </summary>
[ProtoMember(18)]
[JsonProperty(PropertyName = "updated_at")]
public DateTime UpdatedAt
{
get { return Time; }
set { Time = value; }
}
/// <summary>
/// The fiscal year for the release
/// </summary>
[ProtoMember(19)]
[JsonProperty(PropertyName = "fiscal_year")]
public int? FiscalYear { get; set; }
/// <summary>
/// The fiscal quarter for the release
/// </summary>
[ProtoMember(20)]
[JsonProperty(PropertyName = "fiscal_quarter")]
public int? FiscalQuarter { get; set; }
/// <summary>
/// The timestamp of this consensus (UTC)
/// </summary>
public override DateTime EndTime => UpdatedAt;
/// <summary>
/// Empty constructor required for successful Json.NET deserialization
/// </summary>
public EstimizeConsensus()
{
}
/// <summary>
/// Creates an instance from CSV lines
/// </summary>
/// <param name="csvLine">CSV file</param>
public EstimizeConsensus(string csvLine)
{
// UpdatedAt[0], Id[1], Source[2], Type[3], Mean[4], High[5], Low[6], StandardDeviation[7], FiscalYear[8], FiscalQuarter[9], Count[10]
var csv = csvLine.Split(',');
UpdatedAt = Parse.DateTimeExact(csv[0], "yyyyMMdd HH:mm:ss");
Id = csv[1];
Source = (Source)Enum.Parse(typeof(Source), csv[2]);
Type = csv[3].IfNotNullOrEmpty(s => (Type)Enum.Parse(typeof(Type), s));
Mean = csv[4].IfNotNullOrEmpty<decimal?>(s => Parse.Decimal(s));
High = csv[5].IfNotNullOrEmpty<decimal?>(s => Parse.Decimal(s));
Low = csv[6].IfNotNullOrEmpty<decimal?>(s => Parse.Decimal(s));
StandardDeviation = csv[7].IfNotNullOrEmpty<decimal?>(s => Parse.Decimal(s));
FiscalYear = csv[8].IfNotNullOrEmpty<int?>(s => Parse.Int(s));
FiscalQuarter = csv[9].IfNotNullOrEmpty<int?>(s => Parse.Int(s));
Count = csv[10].IfNotNullOrEmpty<int?>(s => Parse.Int(s));
}
/// <summary>
/// Return the Subscription Data Source gained from the URL
/// </summary>
/// <param name="config">Configuration object</param>
/// <param name="date">Date of this source file</param>
/// <param name="isLiveMode">true if we're in live mode, false for backtesting mode</param>
/// <returns>Subscription Data Source.</returns>
public override SubscriptionDataSource GetSource(SubscriptionDataConfig config, DateTime date, bool isLiveMode)
{
var source = Path.Combine(
Globals.DataFolder,
"alternative",
"estimize",
"consensus",
$"{config.Symbol.Value.ToLowerInvariant()}.csv"
);
return new SubscriptionDataSource(source, SubscriptionTransportMedium.LocalFile, FileFormat.Csv);
}
/// <summary>
/// Reader converts each line of the data source into BaseData objects.
/// </summary>
/// <param name="config">Subscription data config setup object</param>
/// <param name="line">Content of the source document</param>
/// <param name="date">Date of the requested data</param>
/// <param name="isLiveMode">true if we're in live mode, false for backtesting mode</param>
/// <returns>
/// Estimize consensus object
/// </returns>
public override BaseData Reader(SubscriptionDataConfig config, string line, DateTime date, bool isLiveMode)
{
return new EstimizeConsensus(line)
{
Symbol = config.Symbol
};
}
/// <summary>
/// Formats a string with the Estimize Estimate information.
/// </summary>
public override string ToString()
{
return Invariant($"{Symbol}(Q{FiscalQuarter} {FiscalYear}) :: {Type} - ") +
Invariant($"Mean: {Mean} ") +
Invariant($"High: {High} ") +
Invariant($"Low: {Low} ") +
Invariant($"STD: {StandardDeviation} ") +
Invariant($"Count: {Count} on ") +
Invariant($"{EndTime:yyyyMMdd} ") +
Invariant($"by {Source}");
}
/// <summary>
/// Indicates if there is support for mapping
/// </summary>
/// <returns>True indicates mapping should be used</returns>
public override bool RequiresMapping()
{
return true;
}
/// <summary>
/// Specifies the data time zone for this data type. This is useful for custom data types
/// </summary>
/// <returns>The <see cref="DateTimeZone"/> of this data type</returns>
public override DateTimeZone DataTimeZone()
{
return TimeZones.Utc;
}
}
/// <summary>
/// Source of the Consensus
/// </summary>
public enum Source
{
/// <summary>
/// Consensus from Wall Street
/// </summary>
[JsonProperty(PropertyName = "wallstreet")]
WallStreet,
/// <summary>
/// Consensus from Estimize
/// </summary>
[JsonProperty(PropertyName = "estimize")]
Estimize
}
/// <summary>
/// Type of the consensus
/// </summary>
public enum Type
{
/// <summary>
/// Consensus on earnings per share value
/// </summary>
[JsonProperty(PropertyName = "eps")] Eps,
/// <summary>
/// Consensus on revenue value
/// </summary>
[JsonProperty(PropertyName = "revenue")]
Revenue
}
}