Lean  $LEAN_TAG$
Messages.Algorithm.Framework.Alphas.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.Runtime.CompilerServices;
18 
19 using static QuantConnect.StringExtensions;
20 
21 namespace QuantConnect
22 {
23  /// <summary>
24  /// Provides user-facing message construction methods and static messages for the <see cref="Algorithm.Framework.Alphas"/> namespace
25  /// </summary>
26  public static partial class Messages
27  {
28  /// <summary>
29  /// Provides user-facing messages for the <see cref="Algorithm.Framework.Alphas.Insight"/> class and its consumers or related classes
30  /// </summary>
31  public static class Insight
32  {
33  /// <summary>
34  /// Returns a string message saying: Insight barCount must be grater than zero
35  /// </summary>
36  public static string InvalidBarCount = "Insight barCount must be greater than zero.";
37 
38  /// <summary>
39  /// Returns a string message saying: Insight period must be greater than or equal to 1 second
40  /// </summary>
41  public static string InvalidPeriod = "Insight period must be greater than or equal to 1 second.";
42 
43  /// <summary>
44  /// Returns a string message saying: Insight closeTimeUtc must be greater than generatedTimeUtc
45  /// </summary>
46  public static string InvalidCloseTimeUtc = "Insight closeTimeUtc must be greater than generatedTimeUtc.";
47 
48  /// <summary>
49  /// Returns a string message saying: Insight closeTimeLocal must not be in the past
50  /// </summary>
51  public static string InvalidCloseTimeLocal = "Insight closeTimeLocal must not be in the past.";
52 
53  /// <summary>
54  /// Returns a string message saying the Insight's GeneratedTimeUtc property must be set before calling SetPeriodAndCloseTime
55  /// </summary>
56  [MethodImpl(MethodImplOptions.AggressiveInlining)]
57  public static string GeneratedTimeUtcNotSet(Algorithm.Framework.Alphas.Insight insight)
58  {
59  return Invariant($@"The insight's '{nameof(insight.GeneratedTimeUtc)}' property must be set before calling {
60  nameof(insight.SetPeriodAndCloseTime)}.");
61  }
62 
63  /// <summary>
64  /// Returns a string message saying it was impossible to set group id on the given insight because it has already
65  /// been assigned to a group
66  /// </summary>
67  [MethodImpl(MethodImplOptions.AggressiveInlining)]
68  public static string InsightAlreadyAssignedToAGroup(Algorithm.Framework.Alphas.Insight insight)
69  {
70  return Invariant($"Unable to set group id on insight {insight} because it has already been assigned to a group.");
71  }
72 
73  /// <summary>
74  /// Parses the given insight into a string containing basic information about it
75  /// </summary>
76  [MethodImpl(MethodImplOptions.AggressiveInlining)]
77  public static string ToString(Algorithm.Framework.Alphas.Insight insight)
78  {
79  var str = Invariant($"{insight.Id:N}: {insight.Symbol} {insight.Type} {insight.Direction} within {insight.Period}");
80 
81  if (insight.Magnitude.HasValue)
82  {
83  str += Invariant($" by {insight.Magnitude.Value}%");
84  }
85  if (insight.Confidence.HasValue)
86  {
87  str += Invariant($" with {Math.Round(100 * insight.Confidence.Value, 1)}% confidence");
88  }
89  if (insight.Weight.HasValue)
90  {
91  str += Invariant($" and {Math.Round(100 * insight.Weight.Value, 1)}% weight");
92  }
93 
94  if (!string.IsNullOrEmpty(insight.Tag))
95  {
96  str += Invariant($": {insight.Tag}");
97  }
98 
99  return str;
100  }
101 
102  /// <summary>
103  /// Parses a short insight into a string containing basic information about it
104  /// </summary>
105  [MethodImpl(MethodImplOptions.AggressiveInlining)]
106  public static string ShortToString(Algorithm.Framework.Alphas.Insight insight)
107  {
108  var str = Invariant($"{insight.Symbol.Value} {insight.Type} {insight.Direction} {insight.Period}");
109 
110  if (insight.Magnitude.HasValue)
111  {
112  str += Invariant($" M:{insight.Magnitude.Value}%");
113  }
114  if (insight.Confidence.HasValue)
115  {
116  str += Invariant($" C:{Math.Round(100 * insight.Confidence.Value, 1)}%");
117  }
118  if (insight.Weight.HasValue)
119  {
120  str += Invariant($" W:{Math.Round(100 * insight.Weight.Value, 1)}%");
121  }
122  if (!string.IsNullOrEmpty(insight.Tag))
123  {
124  str += Invariant($". {insight.Tag}");
125  }
126 
127  return str;
128  }
129  }
130 
131  /// <summary>
132  /// Provides user-facing messages for the <see cref="Algorithm.Framework.Alphas.InsightScore"/> class and its consumers or related classes
133  /// </summary>
134  public static class InsightScore
135  {
136  /// <summary>
137  /// Parses an InsightScore object into a string message containing basic information about it
138  /// </summary>
139  [MethodImpl(MethodImplOptions.AggressiveInlining)]
140  public static string ToString(Algorithm.Framework.Alphas.InsightScore insightScore)
141  {
142  return Invariant($@"Direction: {Math.Round(100 * insightScore.Direction, 2)} Magnitude: {
143  Math.Round(100 * insightScore.Magnitude, 2)}");
144  }
145  }
146  }
147 }