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  public static string InvalidBarCount = "Insight barCount must be greater than zero.";
34 
35  public static string InvalidPeriod = "Insight period must be greater than or equal to 1 second.";
36 
37  public static string InvalidCloseTimeUtc = "Insight closeTimeUtc must be greater than generatedTimeUtc.";
38 
39  public static string InvalidCloseTimeLocal = "Insight closeTimeLocal must not be in the past.";
40 
41  [MethodImpl(MethodImplOptions.AggressiveInlining)]
42  public static string GeneratedTimeUtcNotSet(Algorithm.Framework.Alphas.Insight insight)
43  {
44  return Invariant($@"The insight's '{nameof(insight.GeneratedTimeUtc)}' property must be set before calling {
45  nameof(insight.SetPeriodAndCloseTime)}.");
46  }
47 
48  [MethodImpl(MethodImplOptions.AggressiveInlining)]
49  public static string InsightAlreadyAssignedToAGroup(Algorithm.Framework.Alphas.Insight insight)
50  {
51  return Invariant($"Unable to set group id on insight {insight} because it has already been assigned to a group.");
52  }
53 
54  [MethodImpl(MethodImplOptions.AggressiveInlining)]
55  public static string ToString(Algorithm.Framework.Alphas.Insight insight)
56  {
57  var str = Invariant($"{insight.Id:N}: {insight.Symbol} {insight.Type} {insight.Direction} within {insight.Period}");
58 
59  if (insight.Magnitude.HasValue)
60  {
61  str += Invariant($" by {insight.Magnitude.Value}%");
62  }
63  if (insight.Confidence.HasValue)
64  {
65  str += Invariant($" with {Math.Round(100 * insight.Confidence.Value, 1)}% confidence");
66  }
67  if (insight.Weight.HasValue)
68  {
69  str += Invariant($" and {Math.Round(100 * insight.Weight.Value, 1)}% weight");
70  }
71 
72  if (!string.IsNullOrEmpty(insight.Tag))
73  {
74  str += Invariant($": {insight.Tag}");
75  }
76 
77  return str;
78  }
79 
80  [MethodImpl(MethodImplOptions.AggressiveInlining)]
81  public static string ShortToString(Algorithm.Framework.Alphas.Insight insight)
82  {
83  var str = Invariant($"{insight.Symbol.Value} {insight.Type} {insight.Direction} {insight.Period}");
84 
85  if (insight.Magnitude.HasValue)
86  {
87  str += Invariant($" M:{insight.Magnitude.Value}%");
88  }
89  if (insight.Confidence.HasValue)
90  {
91  str += Invariant($" C:{Math.Round(100 * insight.Confidence.Value, 1)}%");
92  }
93  if (insight.Weight.HasValue)
94  {
95  str += Invariant($" W:{Math.Round(100 * insight.Weight.Value, 1)}%");
96  }
97  if (!string.IsNullOrEmpty(insight.Tag))
98  {
99  str += Invariant($". {insight.Tag}");
100  }
101 
102  return str;
103  }
104  }
105 
106  /// <summary>
107  /// Provides user-facing messages for the <see cref="Algorithm.Framework.Alphas.InsightScore"/> class and its consumers or related classes
108  /// </summary>
109  public static class InsightScore
110  {
111  [MethodImpl(MethodImplOptions.AggressiveInlining)]
112  public static string ToString(Algorithm.Framework.Alphas.InsightScore insightScore)
113  {
114  return Invariant($@"Direction: {Math.Round(100 * insightScore.Direction, 2)} Magnitude: {
115  Math.Round(100 * insightScore.Magnitude, 2)}");
116  }
117  }
118  }
119 }