Lean  $LEAN_TAG$
BaseVolatilityModel.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 using System.Linq;
19 
20 using QuantConnect.Data;
22 using QuantConnect.Util;
23 
25 {
26  /// <summary>
27  /// Represents a base model that computes the volatility of a security
28  /// </summary>
30  {
31  /// <summary>
32  /// Provides access to registered <see cref="SubscriptionDataConfig"/>
33  /// </summary>
35 
36  /// <summary>
37  /// Gets the volatility of the security as a percentage
38  /// </summary>
39  public virtual decimal Volatility { get; }
40 
41  /// <summary>
42  /// Sets the <see cref="ISubscriptionDataConfigProvider"/> instance to use.
43  /// </summary>
44  /// <param name="subscriptionDataConfigProvider">Provides access to registered <see cref="SubscriptionDataConfig"/></param>
45  public virtual void SetSubscriptionDataConfigProvider(
46  ISubscriptionDataConfigProvider subscriptionDataConfigProvider)
47  {
48  SubscriptionDataConfigProvider = subscriptionDataConfigProvider;
49  }
50 
51  /// <summary>
52  /// Updates this model using the new price information in
53  /// the specified security instance
54  /// </summary>
55  /// <param name="security">The security to calculate volatility for</param>
56  /// <param name="data">The new data used to update the model</param>
57  public virtual void Update(Security security, BaseData data)
58  {
59  }
60 
61  /// <summary>
62  /// Returns history requirements for the volatility model expressed in the form of history request
63  /// </summary>
64  /// <param name="security">The security of the request</param>
65  /// <param name="utcTime">The date/time of the request</param>
66  /// <returns>History request object list, or empty if no requirements</returns>
67  public virtual IEnumerable<HistoryRequest> GetHistoryRequirements(Security security, DateTime utcTime)
68  {
69  return Enumerable.Empty<HistoryRequest>();
70  }
71 
72  /// <summary>
73  /// Gets history requests required for warming up the greeks with the provided resolution
74  /// </summary>
75  /// <param name="security">Security to get history for</param>
76  /// <param name="utcTime">UTC time of the request (end time)</param>
77  /// <param name="resolution">Resolution of the security</param>
78  /// <param name="barCount">Number of bars to lookback for the start date</param>
79  /// <returns>Enumerable of history requests</returns>
80  /// <exception cref="InvalidOperationException">The <see cref="SubscriptionDataConfigProvider"/> has not been set</exception>
81  public IEnumerable<HistoryRequest> GetHistoryRequirements(
82  Security security,
83  DateTime utcTime,
84  Resolution? resolution,
85  int barCount)
86  {
88  {
89  throw new InvalidOperationException(
90  "BaseVolatilityModel.GetHistoryRequirements(): " +
91  "SubscriptionDataConfigProvider was not set."
92  );
93  }
94 
95  var configurations = SubscriptionDataConfigProvider
97  .OrderBy(c => c.TickType)
98  .ToList();
99  var configuration = configurations.First();
100 
101  var bar = configuration.Type.GetBaseDataInstance();
102  bar.Symbol = security.Symbol;
103 
104  var historyResolution = resolution ?? bar.SupportedResolutions().Max();
105 
106  var periodSpan = historyResolution.ToTimeSpan();
107 
108  // hour resolution does no have extended market hours data
109  var extendedMarketHours = periodSpan != Time.OneHour && configurations.IsExtendedMarketHours();
110  var localStartTime = Time.GetStartTimeForTradeBars(
111  security.Exchange.Hours,
112  utcTime.ConvertFromUtc(security.Exchange.TimeZone),
113  periodSpan,
114  barCount,
115  extendedMarketHours,
116  configuration.DataTimeZone, dailyPreciseEndTime: false);
117  var utcStartTime = localStartTime.ConvertToUtc(security.Exchange.TimeZone);
118 
119  return new[]
120  {
121  new HistoryRequest(utcStartTime,
122  utcTime,
123  configuration.Type,
124  configuration.Symbol,
125  historyResolution,
126  security.Exchange.Hours,
127  configuration.DataTimeZone,
128  historyResolution,
129  extendedMarketHours,
130  configurations.IsCustomData(),
131  configuration.DataNormalizationMode,
132  LeanData.GetCommonTickTypeForCommonDataTypes(configuration.Type, security.Type))
133  };
134  }
135  }
136 }