Lean  $LEAN_TAG$
InsightManager.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 Python.Runtime;
19 using System.Collections.Generic;
20 
22 {
23  /// <summary>
24  /// Encapsulates the storage of insights.
25  /// </summary>
27  {
28  private readonly IAlgorithm _algorithm;
29  private IInsightScoreFunction _insightScoreFunction;
30 
31  /// <summary>
32  /// Creates a new instance
33  /// </summary>
34  /// <param name="algorithm">The associated algorithm instance</param>
35  public InsightManager(IAlgorithm algorithm)
36  {
37  _algorithm = algorithm;
38  }
39 
40  /// <summary>
41  /// Process a new time step handling insights scoring
42  /// </summary>
43  /// <param name="utcNow">The current utc time</param>
44  public void Step(DateTime utcNow)
45  {
46  _insightScoreFunction?.Score(this, utcNow);
47  }
48 
49  /// <summary>
50  /// Sets the insight score function to use
51  /// </summary>
52  /// <param name="insightScoreFunction">Model that scores insights</param>
53  public void SetInsightScoreFunction(IInsightScoreFunction insightScoreFunction)
54  {
55  _insightScoreFunction = insightScoreFunction;
56  }
57 
58  /// <summary>
59  /// Sets the insight score function to use
60  /// </summary>
61  /// <param name="insightScoreFunction">Model that scores insights</param>
62  public void SetInsightScoreFunction(PyObject insightScoreFunction)
63  {
65  if (insightScoreFunction.TryConvert(out model))
66  {
68  }
69  else
70  {
71  _insightScoreFunction = new InsightScoreFunctionPythonWrapper(insightScoreFunction);
72  }
73  }
74 
75  /// <summary>
76  /// Expire the insights of the given symbols
77  /// </summary>
78  /// <param name="symbols">Symbol we want to expire insights for</param>
79  public void Expire(IEnumerable<Symbol> symbols)
80  {
81  if (symbols == null)
82  {
83  return;
84  }
85 
86  foreach (var symbol in symbols)
87  {
88  if (TryGetValue(symbol, out var insights))
89  {
90  Expire(insights);
91  }
92  }
93  }
94 
95  /// <summary>
96  /// Cancel the insights of the given symbols
97  /// </summary>
98  /// <param name="symbols">Symbol we want to cancel insights for</param>
99  public void Cancel(IEnumerable<Symbol> symbols)
100  {
101  Expire(symbols);
102  }
103 
104  /// <summary>
105  /// Expire the given insights
106  /// </summary>
107  /// <param name="insights">Insights to expire</param>
108  public void Expire(IEnumerable<Insight> insights)
109  {
110  if (insights == null)
111  {
112  return;
113  }
114 
115  var currentUtcTime = _algorithm.UtcTime;
116  foreach (var insight in insights)
117  {
118  insight.Expire(currentUtcTime);
119  }
120  }
121 
122  /// <summary>
123  /// Cancel the given insights
124  /// </summary>
125  /// <param name="insights">Insights to cancel</param>
126  public void Cancel(IEnumerable<Insight> insights)
127  {
128  Expire(insights);
129  }
130  }
131 }