Lean  $LEAN_TAG$
StandardDeviationOfReturnsVolatilityModel.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 
19 using MathNet.Numerics.Statistics;
20 
21 using QuantConnect.Data;
24 
26 {
27  /// <summary>
28  /// Provides an implementation of <see cref="IVolatilityModel"/> that computes the
29  /// annualized sample standard deviation of daily returns as the volatility of the security
30  /// </summary>
32  {
33  private bool _needsUpdate;
34  private decimal _volatility;
35  private DateTime _lastUpdate = DateTime.MinValue;
36  private decimal _lastPrice;
37  private Resolution? _resolution;
38  private TimeSpan _periodSpan;
39  private readonly object _sync = new object();
40  private RollingWindow<double> _window;
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  lock (_sync)
50  {
51  if (_window.Count < 2)
52  {
53  return 0m;
54  }
55 
56  if (_needsUpdate)
57  {
58  _needsUpdate = false;
59  var std = _window.StandardDeviation().SafeDecimalCast();
60  _volatility = std * (decimal)Math.Sqrt(252.0);
61  }
62  }
63 
64  return _volatility;
65  }
66  }
67 
68  /// <summary>
69  /// Initializes a new instance of the <see cref="StandardDeviationOfReturnsVolatilityModel"/> class
70  /// </summary>
71  /// <param name="periods">The max number of samples in the rolling window to be considered for calculating the standard deviation of returns</param>
72  /// <param name="resolution">
73  /// Resolution of the price data inserted into the rolling window series to calculate standard deviation.
74  /// Will be used as the default value for update frequency if a value is not provided for <paramref name="updateFrequency"/>.
75  /// This only has a material effect in live mode. For backtesting, this value does not cause any behavioral changes.
76  /// </param>
77  /// <param name="updateFrequency">Frequency at which we insert new values into the rolling window for the standard deviation calculation</param>
78  /// <remarks>
79  /// The volatility model will be updated with the most granular/highest resolution data that was added to your algorithm.
80  /// That means that if I added <see cref="Resolution.Tick"/> data for my Futures strategy, that this model will be
81  /// updated using <see cref="Resolution.Tick"/> data as the algorithm progresses in time.
82  ///
83  /// Keep this in mind when setting the period and update frequency. The Resolution parameter is only used for live mode, or for
84  /// the default value of the <paramref name="updateFrequency"/> if no value is provided.
85  /// </remarks>
87  int periods,
88  Resolution? resolution = null,
89  TimeSpan? updateFrequency = null
90  )
91  {
92  if (periods < 2)
93  {
94  throw new ArgumentOutOfRangeException(nameof(periods), "'periods' must be greater than or equal to 2.");
95  }
96 
97  _window = new RollingWindow<double>(periods);
98  _resolution = resolution;
99  _periodSpan = updateFrequency ?? resolution?.ToTimeSpan() ?? TimeSpan.FromDays(1);
100  }
101 
102  /// <summary>
103  /// Initializes a new instance of the <see cref="StandardDeviationOfReturnsVolatilityModel"/> class
104  /// </summary>
105  /// <param name="resolution">
106  /// Resolution of the price data inserted into the rolling window series to calculate standard deviation.
107  /// Will be used as the default value for update frequency if a value is not provided for <paramref name="updateFrequency"/>.
108  /// This only has a material effect in live mode. For backtesting, this value does not cause any behavioral changes.
109  /// </param>
110  /// <param name="updateFrequency">Frequency at which we insert new values into the rolling window for the standard deviation calculation</param>
111  /// <remarks>
112  /// The volatility model will be updated with the most granular/highest resolution data that was added to your algorithm.
113  /// That means that if I added <see cref="Resolution.Tick"/> data for my Futures strategy, that this model will be
114  /// updated using <see cref="Resolution.Tick"/> data as the algorithm progresses in time.
115  ///
116  /// Keep this in mind when setting the period and update frequency. The Resolution parameter is only used for live mode, or for
117  /// the default value of the <paramref name="updateFrequency"/> if no value is provided.
118  /// </remarks>
120  Resolution resolution,
121  TimeSpan? updateFrequency = null
122  ) : this(PeriodsInResolution(resolution), resolution, updateFrequency)
123  {
124  }
125 
126  /// <summary>
127  /// Updates this model using the new price information in
128  /// the specified security instance
129  /// </summary>
130  /// <param name="security">The security to calculate volatility for</param>
131  /// <param name="data">Data to update the volatility model with</param>
132  public override void Update(Security security, BaseData data)
133  {
134  var timeSinceLastUpdate = data.EndTime - _lastUpdate;
135  if (timeSinceLastUpdate >= _periodSpan && data.Price > 0)
136  {
137  lock (_sync)
138  {
139  if (_lastPrice > 0.0m)
140  {
141  _needsUpdate = true;
142  _window.Add((double)(data.Price / _lastPrice) - 1.0);
143  }
144  }
145 
146  _lastUpdate = data.EndTime;
147  _lastPrice = data.Price;
148  }
149  }
150 
151  /// <summary>
152  /// Returns history requirements for the volatility model expressed in the form of history request
153  /// </summary>
154  /// <param name="security">The security of the request</param>
155  /// <param name="utcTime">The date of the request</param>
156  /// <returns>History request object list, or empty if no requirements</returns>
157  public override IEnumerable<HistoryRequest> GetHistoryRequirements(Security security, DateTime utcTime)
158  {
159  // Let's reset the model since it will get warmed up again using these history requirements
160  Reset();
161 
162  return GetHistoryRequirements(
163  security,
164  utcTime,
165  _resolution,
166  _window.Size + 1);
167  }
168 
169  /// <summary>
170  /// Resets the model to its initial state
171  /// </summary>
172  private void Reset()
173  {
174  _needsUpdate = false;
175  _volatility = 0m;
176  _lastUpdate = DateTime.MinValue;
177  _lastPrice = 0m;
178  _window.Reset();
179  }
180 
181  private static int PeriodsInResolution(Resolution resolution)
182  {
183  int periods;
184  switch (resolution)
185  {
186  case Resolution.Tick:
187  case Resolution.Second:
188  periods = 600;
189  break;
190  case Resolution.Minute:
191  periods = 60 * 24;
192  break;
193  case Resolution.Hour:
194  periods = 24 * 30;
195  break;
196  default:
197  periods = 30;
198  break;
199  }
200 
201  return periods;
202  }
203  }
204 }