Lean  $LEAN_TAG$
DataConsolidatorPythonWrapper.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;
18 using QuantConnect.Data;
20 
21 namespace QuantConnect.Python
22 {
23  /// <summary>
24  /// Provides an Data Consolidator that wraps a <see cref="PyObject"/> object that represents a custom Python consolidator
25  /// </summary>
26  /// TODO: Inherit from BasePythonWrapper<IDataConsolidator> instead of BasePythonWrapper. But first fix ValidateImplementationOf to exclude properties getters and setters (IsSpecialName)
28  {
29  /// <summary>
30  /// Gets the most recently consolidated piece of data. This will be null if this consolidator
31  /// has not produced any data yet.
32  /// </summary>
33  public IBaseData Consolidated
34  {
35  get { return GetProperty<IBaseData>(nameof(Consolidated)); }
36  }
37 
38  /// <summary>
39  /// Gets a clone of the data being currently consolidated
40  /// </summary>
41  public IBaseData WorkingData
42  {
43  get { return GetProperty<IBaseData>(nameof(WorkingData)); }
44  }
45 
46  /// <summary>
47  /// Gets the type consumed by this consolidator
48  /// </summary>
49  public Type InputType
50  {
51  get { return GetProperty<Type>(nameof(InputType)); }
52  }
53 
54  /// <summary>
55  /// Gets the type produced by this consolidator
56  /// </summary>
57  public Type OutputType
58  {
59  get { return GetProperty<Type>(nameof(OutputType)); }
60  }
61 
62  /// <summary>
63  /// Event handler that fires when a new piece of data is produced
64  /// </summary>
66  {
67  add
68  {
69  var eventHandler = GetEvent(nameof(DataConsolidated));
70  eventHandler += value;
71  }
72  remove
73  {
74  var eventHandler = GetEvent(nameof(DataConsolidated));
75  eventHandler -= value;
76  }
77  }
78 
79  /// <summary>
80  /// Constructor for initialising the <see cref="DataConsolidatorPythonWrapper"/> class with wrapped <see cref="PyObject"/> object
81  /// </summary>
82  /// <param name="consolidator">Represents a custom python consolidator</param>
83  public DataConsolidatorPythonWrapper(PyObject consolidator)
84  : base(consolidator, false)
85  {
86  foreach (var attributeName in new[] { "InputType", "OutputType", "WorkingData", "Consolidated" })
87  {
88  if (!HasAttr(attributeName))
89  {
90  throw new NotImplementedException(
91  Messages.PythonCommon.AttributeNotImplemented($"IDataConsolidator.{attributeName}", consolidator.GetPythonType()));
92  }
93  }
94  }
95 
96  /// <summary>
97  /// Scans this consolidator to see if it should emit a bar due to time passing
98  /// </summary>
99  /// <param name="currentLocalTime">The current time in the local time zone (same as <see cref="BaseData.Time"/>)</param>
100  public void Scan(DateTime currentLocalTime)
101  {
102  InvokeMethod(nameof(Scan), currentLocalTime);
103  }
104 
105  /// <summary>
106  /// Updates this consolidator with the specified data
107  /// </summary>
108  /// <param name="data">The new data for the consolidator</param>
109  public void Update(IBaseData data)
110  {
111  InvokeMethod(nameof(Update), data);
112  }
113 
114  /// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
115  /// <filterpriority>2</filterpriority>
116  public void Dispose()
117  {
118  }
119  }
120 }