Lean  $LEAN_TAG$
IndicatorVolatilityModel.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 QuantConnect.Data;
20 
22 {
23  /// <summary>
24  /// Provides an implementation of <see cref="IVolatilityModel"/> that uses an indicator
25  /// to compute its value
26  /// </summary>
27  /// <typeparam name="T">The indicator's input type</typeparam>
29  {
30  private readonly IIndicator _indicator;
31  private readonly Action<Security, BaseData, IIndicator> _indicatorUpdate;
32 
33  /// <summary>
34  /// Gets the volatility of the security as a percentage
35  /// </summary>
36  public override decimal Volatility
37  {
38  get { return _indicator.Current.Value; }
39  }
40 
41  /// <summary>
42  /// Initializes a new instance of the <see cref="IVolatilityModel"/> using
43  /// the specified <paramref name="indicator"/>. The <paramref name="indicator"/>
44  /// is assumed to but updated externally from this model, such as being registered
45  /// into the consolidator system.
46  /// </summary>
47  /// <param name="indicator">The auto-updating indicator</param>
49  {
50  _indicator = indicator;
51  }
52 
53  /// <summary>
54  /// Initializes a new instance of the <see cref="IVolatilityModel"/> using
55  /// the specified <paramref name="indicator"/>. The <paramref name="indicator"/>
56  /// is assumed to but updated externally from this model, such as being registered
57  /// into the consolidator system.
58  /// </summary>
59  /// <param name="indicator">The auto-updating indicator</param>
60  /// <param name="indicatorUpdate">Function delegate used to update the indicator on each call to <see cref="Update"/></param>
61  public IndicatorVolatilityModel(IIndicator indicator, Action<Security, BaseData, IIndicator> indicatorUpdate)
62  {
63  _indicator = indicator;
64  _indicatorUpdate = indicatorUpdate;
65  }
66 
67  /// <summary>
68  /// Updates this model using the new price information in
69  /// the specified security instance
70  /// </summary>
71  /// <param name="security">The security to calculate volatility for</param>
72  /// <param name="data">The new piece of data for the security</param>
73  public override void Update(Security security, BaseData data)
74  {
75  if (_indicatorUpdate != null)
76  {
77  _indicatorUpdate(security, data, _indicator);
78  }
79  }
80  }
81 }