Files
quantconnect--lean/Common/Parameters/ParameterAttribute.cs
T
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

164 lines
7.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.Linq;
using System.Reflection;
using QuantConnect.Logging;
using QuantConnect.Packets;
namespace QuantConnect.Parameters
{
/// <summary>
/// Specifies a field or property is a parameter that can be set
/// from an <see cref="AlgorithmNodePacket.Parameters"/> dictionary
/// </summary>
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class ParameterAttribute : Attribute
{
/// <summary>
/// Specifies the binding flags used by this implementation to resolve parameter attributes
/// </summary>
public const BindingFlags BindingFlags = System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Instance;
private static readonly string ParameterAttributeNameProperty = "Name";
/// <summary>
/// Gets the name of this parameter
/// </summary>
public string Name { get; private set; }
/// <summary>
/// Initializes a new instance of the <see cref="ParameterAttribute"/> class
/// </summary>
/// <param name="name">The name of the parameter. If null is specified
/// then the field or property name will be used</param>
public ParameterAttribute(string name = null)
{
Name = name;
}
/// <summary>
/// Uses reflections to inspect the instance for any parameter attributes.
/// If a value is found in the parameters dictionary, it is set.
/// </summary>
/// <param name="parameters">The parameters dictionary</param>
/// <param name="instance">The instance to set parameters on</param>
public static void ApplyAttributes(Dictionary<string, string> parameters, object instance)
{
if (instance == null) throw new ArgumentNullException(nameof(instance));
var type = instance.GetType();
// get all fields/properties on the instance
var members = type.GetFields(BindingFlags).Concat<MemberInfo>(type.GetProperties(BindingFlags));
foreach (var memberInfo in members)
{
var fieldInfo = memberInfo as FieldInfo;
var propertyInfo = memberInfo as PropertyInfo;
// this line make static analysis a little happier, but should never actually throw
if (fieldInfo == null && propertyInfo == null)
{
throw new InvalidOperationException("Resolved member that is neither FieldInfo or PropertyInfo");
}
// check the member for our custom attribute
var attribute = memberInfo.GetCustomAttribute<ParameterAttribute>();
if (attribute == null) continue;
// if no name is specified in the attribute then use the member name
var parameterName = attribute.Name ?? memberInfo.Name;
// get the parameter string value to apply to the member
string parameterValue;
if (!parameters.TryGetValue(parameterName, out parameterValue)) continue;
// if it's a read-only property with a parameter value we can't really do anything, bail
if (propertyInfo != null && !propertyInfo.CanWrite)
{
var message = $"The specified property is read only: {propertyInfo.DeclaringType}.{propertyInfo.Name}";
throw new InvalidOperationException(message);
}
// resolve the member type
var memberType = fieldInfo != null ? fieldInfo.FieldType : propertyInfo.PropertyType;
// convert the parameter string value to the member type
var value = parameterValue.ConvertTo(memberType);
// set the value to the field/property
if (fieldInfo != null)
{
fieldInfo.SetValue(instance, value);
}
else
{
propertyInfo.SetValue(instance, value);
}
}
}
/// <summary>
/// Resolves all parameter attributes from the specified compiled assembly path
/// </summary>
/// <param name="assembly">The assembly to inspect</param>
/// <returns>Parameters dictionary keyed by parameter name with a value of the member type</returns>
public static Dictionary<string, string> GetParametersFromAssembly(Assembly assembly)
{
var parameters = new Dictionary<string, string>();
foreach (var type in assembly.GetTypes())
{
foreach (var kvp in GetParametersFromType(type))
{
parameters[kvp.Key] = kvp.Value;
}
}
return parameters;
}
/// <summary>
/// Resolves all parameter attributes from the specified type
/// </summary>
/// <param name="type">The type to inspect</param>
/// <returns>Parameters dictionary keyed by parameter name with a value of the member type</returns>
public static IEnumerable<KeyValuePair<string, string>> GetParametersFromType(Type type)
{
foreach (var field in type.GetFields(BindingFlags))
{
var attribute = field.GetCustomAttribute<ParameterAttribute>();
if (attribute != null)
{
var parameterName = attribute.Name ?? field.Name;
yield return new KeyValuePair<string, string>(parameterName, field.FieldType.GetBetterTypeName());
}
}
foreach (var property in type.GetProperties(BindingFlags))
{
// ignore non-writeable properties
if (!property.CanWrite) continue;
var attribute = property.GetCustomAttribute<ParameterAttribute>();
if (attribute != null)
{
var parameterName = attribute.Name ?? property.Name;
yield return new KeyValuePair<string, string>(parameterName, property.PropertyType.GetBetterTypeName());
}
}
}
}
}