Lean  $LEAN_TAG$
SubscriptionDataConfigExtensions.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14 */
15 
16 using System.Collections.Generic;
17 using System.Linq;
18 using QuantConnect.Util;
19 
20 namespace QuantConnect.Data
21 {
22  /// <summary>
23  /// Helper methods used to determine different configurations properties
24  /// for a given set of <see cref="SubscriptionDataConfig"/>
25  /// </summary>
27  {
28  /// <summary>
29  /// Extension method used to obtain the highest <see cref="Resolution"/>
30  /// for a given set of <see cref="SubscriptionDataConfig"/>
31  /// </summary>
32  /// <param name="subscriptionDataConfigs"></param>
33  /// <returns>The highest resolution, <see cref="Resolution.Daily"/> if there
34  /// are no subscriptions</returns>
36  this IEnumerable<SubscriptionDataConfig> subscriptionDataConfigs)
37  {
38  return subscriptionDataConfigs
39  .Select(x => x.Resolution)
40  .DefaultIfEmpty(Resolution.Daily)
41  .Min();
42  }
43 
44  /// <summary>
45  /// Extension method used to determine if FillForward is enabled
46  /// for a given set of <see cref="SubscriptionDataConfig"/>
47  /// </summary>
48  /// <param name="subscriptionDataConfigs"></param>
49  /// <returns>True, at least one subscription has it enabled</returns>
50  public static bool IsFillForward(
51  this IEnumerable<SubscriptionDataConfig> subscriptionDataConfigs)
52  {
53  return subscriptionDataConfigs.Any(x => x.FillDataForward);
54  }
55 
56  /// <summary>
57  /// Extension method used to determine if ExtendedMarketHours is enabled
58  /// for a given set of <see cref="SubscriptionDataConfig"/>
59  /// </summary>
60  /// <param name="subscriptionDataConfigs"></param>
61  /// <returns>True, at least one subscription has it enabled</returns>
62  public static bool IsExtendedMarketHours(
63  this IEnumerable<SubscriptionDataConfig> subscriptionDataConfigs)
64  {
65  return subscriptionDataConfigs.Any(x => x.ExtendedMarketHours);
66  }
67 
68  /// <summary>
69  /// Extension method used to determine if it is custom data
70  /// for a given set of <see cref="SubscriptionDataConfig"/>
71  /// </summary>
72  /// <param name="subscriptionDataConfigs"></param>
73  /// <returns>True, at least one subscription is custom data</returns>
74  public static bool IsCustomData(
75  this IEnumerable<SubscriptionDataConfig> subscriptionDataConfigs)
76  {
77  return subscriptionDataConfigs.Any(x => x.IsCustomData);
78  }
79 
80  /// <summary>
81  /// Extension method used to determine what <see cref="QuantConnect.DataNormalizationMode"/>
82  /// to use for a given set of <see cref="SubscriptionDataConfig"/>
83  /// </summary>
84  /// <param name="subscriptionDataConfigs"></param>
85  /// <returns>The first DataNormalizationMode,
86  /// <see cref="DataNormalizationMode.Adjusted"/> if there are no subscriptions</returns>
88  this IEnumerable<SubscriptionDataConfig> subscriptionDataConfigs)
89  {
90  return subscriptionDataConfigs.
91  Select(x => x.DataNormalizationMode)
92  .DefaultIfEmpty(QuantConnect.DataNormalizationMode.Adjusted)
93  .First();
94  }
95 
96  /// <summary>
97  /// Sets the data normalization mode to be used by
98  /// this set of <see cref="SubscriptionDataConfig"/>
99  /// </summary>
100  public static void SetDataNormalizationMode(
101  this IEnumerable<SubscriptionDataConfig> subscriptionDataConfigs,
103  {
104  foreach (var subscription in subscriptionDataConfigs)
105  {
106  subscription.DataNormalizationMode = mode;
107  }
108  }
109 
110  /// <summary>
111  /// Will determine if mapping should be used for this subscription configuration
112  /// </summary>
113  /// <param name="config">The subscription data configuration we are processing</param>
114  /// <remarks>One of the objectives of this method is to normalize the 'use mapping'
115  /// check and void code duplication and related issues</remarks>
116  /// <returns>True if ticker should be mapped</returns>
117  public static bool TickerShouldBeMapped(this SubscriptionDataConfig config)
118  {
119  // we create an instance of the data type, if it is a custom type
120  // it can override RequiresMapping else it will use security type\
121  return config.GetBaseDataInstance().RequiresMapping();
122  }
123 
124  /// <summary>
125  /// Will determine if price scaling should be used for this subscription configuration
126  /// </summary>
127  /// <param name="config">The subscription data configuration we are processing</param>
128  /// <remarks>One of the objectives of this method is to normalize the 'use price scale'
129  /// check and void code duplication and related issues</remarks>
130  /// <param name="liveMode">True, is this is a live mode data stream</param>
131  /// <returns>True if ticker prices should be scaled</returns>
132  public static bool PricesShouldBeScaled(this SubscriptionDataConfig config, bool liveMode = false)
133  {
134  if (config.IsCustomData || config.Symbol.Value.Contains("UNIVERSE"))
135  {
136  return false;
137  }
138 
139  if(config.SecurityType == SecurityType.Equity && !liveMode)
140  {
141  return true;
142  }
143  if (config.SecurityType == SecurityType.Future && config.Symbol.IsCanonical())
144  {
145  return LeanData.IsCommonLeanDataType(config.Type);
146  }
147 
148  return false;
149  }
150 
151  /// <summary>
152  /// Will determine if splits and dividends should be used for this subscription configuration
153  /// </summary>
154  /// <param name="config">The subscription data configuration we are processing</param>
155  /// <remarks>Different than <see cref="PricesShouldBeScaled"/> because prices could be scale and no split and dividends
156  /// really exist, like in the continuous futures case</remarks>
157  /// <returns>True if this configuration requires split and divided handling</returns>
158  public static bool EmitSplitsAndDividends(this SubscriptionDataConfig config)
159  {
160  return !config.IsCustomData && !config.Symbol.Value.Contains("UNIVERSE") && config.SecurityType == SecurityType.Equity;
161  }
162 
163  /// <summary>
164  /// Initializes a new instance of the <see cref="BaseData"/> type defined in <paramref name="config"/> with the symbol properly set
165  /// </summary>
167  {
168  var instance = config.Type.GetBaseDataInstance();
169  instance.Symbol = config.Symbol;
170  return instance;
171  }
172  }
173 }