Lean  $LEAN_TAG$
InsightScore.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 
17 using System;
18 using Newtonsoft.Json;
19 
21 {
22  /// <summary>
23  /// Defines the scores given to a particular insight
24  /// </summary>
25  public class InsightScore
26  {
27  /// <summary>
28  /// Gets the time these scores were last updated
29  /// </summary>
30  [JsonProperty]
31  public DateTime UpdatedTimeUtc { get; private set; }
32 
33  /// <summary>
34  /// Gets the direction score
35  /// </summary>
36  [JsonProperty]
37  public double Direction { get; private set; }
38 
39  /// <summary>
40  /// Gets the magnitude score
41  /// </summary>
42  [JsonProperty]
43  public double Magnitude { get; private set; }
44 
45  /// <summary>
46  /// Gets whether or not this is the insight's final score
47  /// </summary>
48  [JsonProperty]
49  public bool IsFinalScore { get; private set; }
50 
51  /// <summary>
52  /// Initializes a new, default instance of the <see cref="InsightScore"/> class
53  /// </summary>
54  public InsightScore()
55  {
56  }
57 
58  /// <summary>
59  /// Initializes a new instance of the <see cref="InsightScore"/> class
60  /// </summary>
61  /// <param name="direction">The insight direction score</param>
62  /// <param name="magnitude">The insight percent change score</param>
63  /// <param name="updatedTimeUtc">The algorithm utc time these scores were computed</param>
64  public InsightScore(double direction, double magnitude, DateTime updatedTimeUtc)
65  {
66  Direction = direction;
67  Magnitude = magnitude;
68  UpdatedTimeUtc = updatedTimeUtc;
69  }
70 
71  /// <summary>
72  /// Sets the specified score type with the value
73  /// </summary>
74  /// <param name="type">The score type to be set, Direction/Magnitude</param>
75  /// <param name="value">The new value for the score</param>
76  /// <param name="algorithmUtcTime">The algorithm's utc time at which time the new score was computed</param>
77  public void SetScore(InsightScoreType type, double value, DateTime algorithmUtcTime)
78  {
79  if (IsFinalScore) return;
80 
81  UpdatedTimeUtc = algorithmUtcTime;
82 
83  switch (type)
84  {
85  case InsightScoreType.Direction:
86  Direction = Math.Max(0, Math.Min(1, value));
87  break;
88 
89  case InsightScoreType.Magnitude:
90  Magnitude = Math.Max(0, Math.Min(1, value));
91  break;
92 
93  default:
94  throw new ArgumentOutOfRangeException(nameof(type), type, null);
95  }
96  }
97 
98  /// <summary>
99  /// Marks the score as finalized, preventing any further updates.
100  /// </summary>
101  /// <param name="algorithmUtcTime">The algorithm's utc time at which time these scores were finalized</param>
102  public void Finalize(DateTime algorithmUtcTime)
103  {
104  IsFinalScore = true;
105  UpdatedTimeUtc = algorithmUtcTime;
106  }
107 
108  /// <summary>
109  /// Gets the specified score
110  /// </summary>
111  /// <param name="type">The type of score to get, Direction/Magnitude</param>
112  /// <returns>The requested score</returns>
113  public double GetScore(InsightScoreType type)
114  {
115  switch (type)
116  {
117  case InsightScoreType.Direction:
118  return Direction;
119 
120  case InsightScoreType.Magnitude:
121  return Magnitude;
122 
123  default:
124  throw new ArgumentOutOfRangeException(nameof(type), type, null);
125  }
126  }
127 
128  /// <summary>Returns a string that represents the current object.</summary>
129  /// <returns>A string that represents the current object.</returns>
130  /// <filterpriority>2</filterpriority>
131  public override string ToString()
132  {
133  return Messages.InsightScore.ToString(this);
134  }
135  }
136 }