forked from QuantConnect/Lean
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndicatorVolatilityModelAlgorithm.py
More file actions
85 lines (68 loc) · 3.55 KB
/
Copy pathIndicatorVolatilityModelAlgorithm.py
File metadata and controls
85 lines (68 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
# Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from AlgorithmImports import *
### <summary>
### Algorithm illustrating the usage of the IndicatorVolatilityModel and
### how to handle splits and dividends to avoid price discontinuities
### </summary>
class IndicatorVolatilityModelAlgorithm(QCAlgorithm):
_indicator_periods = 7
_data_normalization_mode = DataNormalizationMode.RAW
def initialize(self):
self.set_start_date(2014, 1, 1)
self.set_end_date(2014, 12, 31)
self.set_cash(100000)
equity = self.add_equity("AAPL", Resolution.DAILY, data_normalization_mode=self._data_normalization_mode)
self._aapl = equity.symbol
std = StandardDeviation(self._indicator_periods)
mean = SimpleMovingAverage(self._indicator_periods)
self._indicator = IndicatorExtensions.over(std, mean)
def update_indicator(security, data, indicator):
if data.price > 0:
std.update(data.time, data.price)
mean.update(data.time, data.price)
self._volatility_model = IndicatorVolatilityModel(self._indicator, update_indicator)
equity.set_volatility_model(self._volatility_model)
self._splits_and_dividends_count = 0
self._volatility_checked = False
def on_data(self, slice):
if slice.splits.contains_key(self._aapl) or slice.dividends.contains_key(self._aapl):
self._splits_and_dividends_count += 1
# On a split or dividend event, we need to reset and warm the indicator up as Lean does to BaseVolatilityModel's
# to avoid big jumps in volatility due to price discontinuities
self._indicator.reset()
equity = self.securities[self._aapl]
VolatilityModelExtensions.warm_up(
self._volatility_model,
self,
equity,
equity.resolution,
self._indicator_periods,
self._data_normalization_mode
)
def on_end_of_day(self, symbol):
if symbol != self._aapl or not self._indicator.is_ready:
return
self._volatility_checked = True
# This is expected only in this case, 0.05 is not a magical number of any kind.
# Just making sure we don't get big jumps on volatility
volatility = self.securities[self._aapl].volatility_model.volatility
if volatility <= 0 or volatility > 0.05:
raise RegressionTestException(
"Expected volatility to stay less than 0.05 (not big jumps due to price discontinuities on splits and dividends), "
f"but got {volatility}")
def on_end_of_algorithm(self):
if self._splits_and_dividends_count == 0:
raise RegressionTestException("Expected to get at least one split or dividend event")
if not self._volatility_checked:
raise RegressionTestException("Expected to check volatility at least once")