Lean  $LEAN_TAG$
VolatilityModelPythonWrapper.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 Python.Runtime;
17 using QuantConnect.Data;
19 using System;
20 using System.Collections.Generic;
23 
24 namespace QuantConnect.Python
25 {
26  /// <summary>
27  /// Provides a volatility model that wraps a <see cref="PyObject"/> object that represents a model that computes the volatility of a security
28  /// </summary>
30  {
31  private readonly BasePythonWrapper<IVolatilityModel> _model;
32 
33  /// <summary>
34  /// Constructor for initialising the <see cref="VolatilityModelPythonWrapper"/> class with wrapped <see cref="PyObject"/> object
35  /// </summary>
36  /// <param name="model"> Represents a model that computes the volatility of a security</param>
37  public VolatilityModelPythonWrapper(PyObject model)
38  {
39  _model = new BasePythonWrapper<IVolatilityModel>(model);
40  }
41 
42  /// <summary>
43  /// Gets the volatility of the security as a percentage
44  /// </summary>
45  public override decimal Volatility
46  {
47  get
48  {
49  return _model.GetProperty<decimal>(nameof(Volatility));
50  }
51  }
52 
53  /// <summary>
54  /// Updates this model using the new price information in
55  /// the specified security instance
56  /// </summary>
57  /// <param name="security">The security to calculate volatility for</param>
58  /// <param name="data">The new data used to update the model</param>
59  public override void Update(Security security, BaseData data)
60  {
61  _model.InvokeMethod(nameof(Update), security, data).Dispose();
62  }
63 
64  /// <summary>
65  /// Returns history requirements for the volatility model expressed in the form of history request
66  /// </summary>
67  /// <param name="security">The security of the request</param>
68  /// <param name="utcTime">The date/time of the request</param>
69  /// <returns>History request object list, or empty if no requirements</returns>
70  public override IEnumerable<HistoryRequest> GetHistoryRequirements(Security security, DateTime utcTime)
71  {
72  return _model.InvokeMethod<IEnumerable<HistoryRequest>>(nameof(GetHistoryRequirements), security, utcTime);
73  }
74 
75  /// <summary>
76  /// Sets the <see cref="ISubscriptionDataConfigProvider"/> instance to use.
77  /// </summary>
78  /// <param name="subscriptionDataConfigProvider">Provides access to registered <see cref="SubscriptionDataConfig"/></param>
79  public override void SetSubscriptionDataConfigProvider(
80  ISubscriptionDataConfigProvider subscriptionDataConfigProvider)
81  {
82  if (_model.HasAttr(nameof(SetSubscriptionDataConfigProvider)))
83  {
84  _model.InvokeMethod(nameof(SetSubscriptionDataConfigProvider), subscriptionDataConfigProvider).Dispose();
85  }
86  }
87  }
88 }