Lean  $LEAN_TAG$
BaseFundamentalDataProvider.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;
17 using System.Globalization;
20 using System.Runtime.CompilerServices;
21 
23 {
24  /// <summary>
25  /// Base fundamental data provider
26  /// </summary>
28  {
29  /// <summary>
30  /// True if live trading
31  /// </summary>
32  public bool LiveMode { get; set; }
33 
34  /// <summary>
35  /// THe data provider instance to use
36  /// </summary>
37  protected IDataProvider DataProvider { get; set; }
38 
39  /// <summary>
40  /// Initializes the service
41  /// </summary>
42  /// <param name="dataProvider">The data provider instance to use</param>
43  /// <param name="liveMode">True if running in live mode</param>
44  public virtual void Initialize(IDataProvider dataProvider, bool liveMode)
45  {
46  LiveMode = liveMode;
47  DataProvider = dataProvider;
48  }
49 
50  /// <summary>
51  /// Will fetch the requested fundamental information for the requested time and symbol
52  /// </summary>
53  /// <typeparam name="T">The expected data type</typeparam>
54  /// <param name="time">The time to request this data for</param>
55  /// <param name="securityIdentifier">The security identifier</param>
56  /// <param name="name">The name of the fundamental property</param>
57  /// <returns>The fundamental information</returns>
58  public virtual T Get<T>(DateTime time, SecurityIdentifier securityIdentifier, FundamentalProperty name)
59  {
60  throw new NotImplementedException();
61  }
62 
63  /// <summary>
64  /// Get's the default value for the given T type
65  /// </summary>
66  /// <typeparam name="T">The expected T type</typeparam>
67  /// <returns>The default value</returns>
68  [MethodImpl(MethodImplOptions.AggressiveInlining)]
69  public static T GetDefault<T>()
70  {
71  if (typeof(T) == typeof(double))
72  {
73  return (T)Convert.ChangeType(double.NaN, typeof(T), CultureInfo.InvariantCulture);
74  }
75  else if (typeof(T) == typeof(decimal))
76  {
77  return (T)Convert.ChangeType(decimal.Zero, typeof(T), CultureInfo.InvariantCulture);
78  }
79  return default;
80  }
81 
82  /// <summary>
83  /// True if the given value is none
84  /// </summary>
85  [MethodImpl(MethodImplOptions.AggressiveInlining)]
86  public static bool IsNone(object value) => IsNone(value?.GetType(), value);
87 
88  /// <summary>
89  /// True if the given value is none
90  /// </summary>
91  [MethodImpl(MethodImplOptions.AggressiveInlining)]
92  public static bool IsNone(Type type, object value)
93  {
94  if (type == null || value == null)
95  {
96  return true;
97  }
98  else if(type == typeof(double))
99  {
100  return ((double)value).IsNaNOrInfinity();
101  }
102  else if (type == typeof(decimal))
103  {
104  return default(decimal) == (decimal)value;
105  }
106  else if (type == typeof(DateTime))
107  {
108  return default(DateTime) == (DateTime)value;
109  }
110  return false;
111  }
112  }
113 }