Lean  $LEAN_TAG$
CompositeAlphaModel.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.Collections.Generic;
18 using Python.Runtime;
19 using QuantConnect.Data;
21 using QuantConnect.Util;
22 
24 {
25  /// <summary>
26  /// Provides an implementation of <see cref="IAlphaModel"/> that combines multiple alpha
27  /// models into a single alpha model and properly sets each insights 'SourceModel' property.
28  /// </summary>
30  {
31  private readonly List<IAlphaModel> _alphaModels = new List<IAlphaModel>();
32 
33  /// <summary>
34  /// Initializes a new instance of the <see cref="CompositeAlphaModel"/> class
35  /// </summary>
36  /// <param name="alphaModels">The individual alpha models defining this composite model</param>
37  public CompositeAlphaModel(params IAlphaModel[] alphaModels)
38  {
39  if (alphaModels.IsNullOrEmpty())
40  {
41  throw new ArgumentException("Must specify at least 1 alpha model for the CompositeAlphaModel");
42  }
43 
44  _alphaModels.AddRange(alphaModels);
45  }
46 
47  /// <summary>
48  /// Initializes a new instance of the <see cref="CompositeAlphaModel"/> class
49  /// </summary>
50  /// <param name="alphaModels">The individual alpha models defining this composite model</param>
51  public CompositeAlphaModel(params PyObject[] alphaModels)
52  {
53  if (alphaModels.IsNullOrEmpty())
54  {
55  throw new ArgumentException("Must specify at least 1 alpha model for the CompositeAlphaModel");
56  }
57 
58  foreach (var pyAlphaModel in alphaModels)
59  {
60  AddAlpha(pyAlphaModel);
61  }
62  }
63 
64  /// <summary>
65  /// Initializes a new instance of the <see cref="CompositeAlphaModel"/> class
66  /// </summary>
67  /// <param name="alphaModel">The individual alpha model defining this composite model</param>
68  public CompositeAlphaModel(PyObject alphaModel)
69  : this(new[] { alphaModel} )
70  {
71 
72  }
73 
74  /// <summary>
75  /// Updates this alpha model with the latest data from the algorithm.
76  /// This is called each time the algorithm receives data for subscribed securities.
77  /// This method patches this call through the each of the wrapped models.
78  /// </summary>
79  /// <param name="algorithm">The algorithm instance</param>
80  /// <param name="data">The new data available</param>
81  /// <returns>The new insights generated</returns>
82  public override IEnumerable<Insight> Update(QCAlgorithm algorithm, Slice data)
83  {
84  foreach (var model in _alphaModels)
85  {
86  var name = model.GetModelName();
87  foreach (var insight in model.Update(algorithm, data))
88  {
89  if (string.IsNullOrEmpty(insight.SourceModel))
90  {
91  // set the source model name if not already set
92  insight.SourceModel = name;
93  }
94 
95  yield return insight;
96  }
97  }
98  }
99 
100  /// <summary>
101  /// Event fired each time the we add/remove securities from the data feed.
102  /// This method patches this call through the each of the wrapped models.
103  /// </summary>
104  /// <param name="algorithm">The algorithm instance that experienced the change in securities</param>
105  /// <param name="changes">The security additions and removals from the algorithm</param>
106  public override void OnSecuritiesChanged(QCAlgorithm algorithm, SecurityChanges changes)
107  {
108  foreach (var model in _alphaModels)
109  {
110  model.OnSecuritiesChanged(algorithm, changes);
111  }
112  }
113 
114  /// <summary>
115  /// Adds a new <see cref="AlphaModel"/>
116  /// </summary>
117  /// <param name="alphaModel">The alpha model to add</param>
118  public void AddAlpha(IAlphaModel alphaModel)
119  {
120  _alphaModels.Add(alphaModel);
121  }
122 
123  /// <summary>
124  /// Adds a new <see cref="AlphaModel"/>
125  /// </summary>
126  /// <param name="pyAlphaModel">The alpha model to add</param>
127  public void AddAlpha(PyObject pyAlphaModel)
128  {
129  IAlphaModel alphaModel;
130  if (!pyAlphaModel.TryConvert(out alphaModel))
131  {
132  alphaModel = new AlphaModelPythonWrapper(pyAlphaModel);
133  }
134  _alphaModels.Add(alphaModel);
135  }
136  }
137 }