Lean  $LEAN_TAG$
IndicatorDataPoint.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 
18 using QuantConnect.Data;
19 
21 {
22  /// <summary>
23  /// Represents a piece of data at a specific time
24  /// </summary>
25  public class IndicatorDataPoint : BaseData, IEquatable<IndicatorDataPoint>, IComparable<IndicatorDataPoint>, IComparable
26  {
27  /// <summary>
28  /// Initializes a new default instance of IndicatorDataPoint with a time of
29  /// DateTime.MinValue and a Value of 0m.
30  /// </summary>
32  {
33  Value = 0m;
34  Time = DateTime.MinValue;
35  }
36 
37  /// <summary>
38  /// Initializes a new instance of the DataPoint type using the specified time/data
39  /// </summary>
40  /// <param name="time">The time this data was produced</param>
41  /// <param name="value">The data</param>
42  public IndicatorDataPoint(DateTime time, decimal value)
43  {
44  Time = time;
45  Value = value;
46  }
47 
48  /// <summary>
49  /// Initializes a new instance of the DataPoint type using the specified time/data
50  /// </summary>
51  /// <param name="symbol">The symbol associated with this data</param>
52  /// <param name="time">The time this data was produced</param>
53  /// <param name="value">The data</param>
54  public IndicatorDataPoint(Symbol symbol, DateTime time, decimal value)
55  {
56  Symbol = symbol;
57  Time = time;
58  Value = value;
59  }
60 
61  /// <summary>
62  /// Indicates whether the current object is equal to another object of the same type.
63  /// </summary>
64  /// <returns>
65  /// true if the current object is equal to the <paramref name="other" /> parameter; otherwise, false.
66  /// </returns>
67  /// <param name="other">An object to compare with this object.</param>
68  public bool Equals(IndicatorDataPoint other)
69  {
70  if (other == null)
71  {
72  return false;
73  }
74  return other.Time == Time && other.Value == Value;
75  }
76 
77  /// <summary>
78  /// Compares the current object with another object of the same type.
79  /// </summary>
80  /// <returns>
81  /// A value that indicates the relative order of the objects being compared. The return value has the following meanings: Value Meaning Less than zero This object is less than the <paramref name="other"/> parameter.Zero This object is equal to <paramref name="other"/>. Greater than zero This object is greater than <paramref name="other"/>.
82  /// </returns>
83  /// <param name="other">An object to compare with this object.</param>
84  public int CompareTo(IndicatorDataPoint other)
85  {
86  if (ReferenceEquals(other, null))
87  {
88  // everything is greater than null via MSDN
89  return 1;
90  }
91  return Value.CompareTo(other.Value);
92  }
93 
94  /// <summary>
95  /// Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.
96  /// </summary>
97  /// <returns>
98  /// A value that indicates the relative order of the objects being compared. The return value has these meanings: Value Meaning Less than zero This instance precedes <paramref name="obj"/> in the sort order. Zero This instance occurs in the same position in the sort order as <paramref name="obj"/>. Greater than zero This instance follows <paramref name="obj"/> in the sort order.
99  /// </returns>
100  /// <param name="obj">An object to compare with this instance. </param><exception cref="T:System.ArgumentException"><paramref name="obj"/> is not the same type as this instance. </exception><filterpriority>2</filterpriority>
101  public int CompareTo(object obj)
102  {
103  var other = obj as IndicatorDataPoint;
104  if (other == null)
105  {
106  throw new ArgumentException(Messages.IndicatorDataPoint.InvalidObjectTypeToCompareTo(GetType()));
107  }
108  return CompareTo(other);
109  }
110 
111  /// <summary>
112  /// Returns a string representation of this DataPoint instance using ISO8601 formatting for the date
113  /// </summary>
114  /// <returns>
115  /// A <see cref="T:System.String" /> containing a fully qualified type name.
116  /// </returns>
117  /// <filterpriority>2</filterpriority>
118  public override string ToString()
119  {
120  return Messages.IndicatorDataPoint.ToString(this);
121  }
122 
123  /// <summary>
124  /// Indicates whether this instance and a specified object are equal.
125  /// </summary>
126  /// <returns>
127  /// true if <paramref name="obj" /> and this instance are the same type and represent the same value; otherwise, false.
128  /// </returns>
129  /// <param name="obj">Another object to compare to. </param>
130  /// <filterpriority>2</filterpriority>
131  public override bool Equals(object obj)
132  {
133  if (ReferenceEquals(null, obj)) return false;
134  return obj is IndicatorDataPoint && Equals((IndicatorDataPoint) obj);
135  }
136 
137  /// <summary>
138  /// Returns the hash code for this instance.
139  /// </summary>
140  /// <returns>
141  /// A 32-bit signed integer that is the hash code for this instance.
142  /// </returns>
143  /// <filterpriority>2</filterpriority>
144  public override int GetHashCode()
145  {
146  unchecked
147  {
148  return (Value.GetHashCode()*397) ^ Time.GetHashCode();
149  }
150  }
151 
152  /// <summary>
153  /// Returns the data held within the instance
154  /// </summary>
155  /// <param name="instance">The DataPoint instance</param>
156  /// <returns>The data held within the instance</returns>
157  public static implicit operator decimal(IndicatorDataPoint instance)
158  {
159  return instance.Value;
160  }
161 
162  /// <summary>
163  /// This function is purposefully not implemented.
164  /// </summary>
165  public override BaseData Reader(SubscriptionDataConfig config, string line, DateTime date, bool isLiveMode)
166  {
167  throw new NotImplementedException(Messages.IndicatorDataPoint.UnsupportedMethod(nameof(Reader)));
168  }
169 
170  /// <summary>
171  /// This function is purposefully not implemented.
172  /// </summary>
173  public override SubscriptionDataSource GetSource(SubscriptionDataConfig config, DateTime date, bool isLiveMode)
174  {
175  throw new NotImplementedException(Messages.IndicatorDataPoint.UnsupportedMethod(nameof(GetSource)));
176  }
177  }
178 }