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