Lean  $LEAN_TAG$
EmaCrossAlphaModel.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.Collections.Generic;
17 using QuantConnect.Data;
22 
24 {
25  /// <summary>
26  /// Alpha model that uses an EMA cross to create insights
27  /// </summary>
29  {
30  private readonly int _fastPeriod;
31  private readonly int _slowPeriod;
32  private readonly Resolution _resolution;
33  private readonly int _predictionInterval;
34 
35  /// <summary>
36  /// This is made protected for testing purposes
37  /// </summary>
38  protected Dictionary<Symbol, SymbolData> SymbolDataBySymbol { get; }
39 
40  /// <summary>
41  /// Initializes a new instance of the <see cref="EmaCrossAlphaModel"/> class
42  /// </summary>
43  /// <param name="fastPeriod">The fast EMA period</param>
44  /// <param name="slowPeriod">The slow EMA period</param>
45  /// <param name="resolution">The resolution of data sent into the EMA indicators</param>
47  int fastPeriod = 12,
48  int slowPeriod = 26,
49  Resolution resolution = Resolution.Daily
50  )
51  {
52  _fastPeriod = fastPeriod;
53  _slowPeriod = slowPeriod;
54  _resolution = resolution;
55  _predictionInterval = fastPeriod;
56  SymbolDataBySymbol = new Dictionary<Symbol, SymbolData>();
57  Name = $"{nameof(EmaCrossAlphaModel)}({fastPeriod},{slowPeriod},{resolution})";
58  }
59 
60  /// <summary>
61  /// Updates this alpha model with the latest data from the algorithm.
62  /// This is called each time the algorithm receives data for subscribed securities
63  /// </summary>
64  /// <param name="algorithm">The algorithm instance</param>
65  /// <param name="data">The new data available</param>
66  /// <returns>The new insights generated</returns>
67  public override IEnumerable<Insight> Update(QCAlgorithm algorithm, Slice data)
68  {
69  var insights = new List<Insight>();
70  foreach (var symbolData in SymbolDataBySymbol.Values)
71  {
72  if (symbolData.Fast.IsReady && symbolData.Slow.IsReady)
73  {
74  var insightPeriod = _resolution.ToTimeSpan().Multiply(_predictionInterval);
75  if (symbolData.FastIsOverSlow)
76  {
77  if (symbolData.Slow > symbolData.Fast)
78  {
79  insights.Add(Insight.Price(symbolData.Symbol, insightPeriod, InsightDirection.Down));
80  }
81  }
82  else if (symbolData.SlowIsOverFast)
83  {
84  if (symbolData.Fast > symbolData.Slow)
85  {
86  insights.Add(Insight.Price(symbolData.Symbol, insightPeriod, InsightDirection.Up));
87  }
88  }
89  }
90 
91  symbolData.FastIsOverSlow = symbolData.Fast > symbolData.Slow;
92  }
93 
94  return insights;
95  }
96 
97  /// <summary>
98  /// Event fired each time the we add/remove securities from the data feed
99  /// </summary>
100  /// <param name="algorithm">The algorithm instance that experienced the change in securities</param>
101  /// <param name="changes">The security additions and removals from the algorithm</param>
102  public override void OnSecuritiesChanged(QCAlgorithm algorithm, SecurityChanges changes)
103  {
104  foreach (var added in changes.AddedSecurities)
105  {
106  SymbolData symbolData;
107  if (!SymbolDataBySymbol.TryGetValue(added.Symbol, out symbolData))
108  {
109  SymbolDataBySymbol[added.Symbol] = new SymbolData(added, _fastPeriod, _slowPeriod, algorithm, _resolution);
110  }
111  else
112  {
113  // a security that was already initialized was re-added, reset the indicators
114  symbolData.Fast.Reset();
115  symbolData.Slow.Reset();
116  }
117  }
118 
119  foreach (var removed in changes.RemovedSecurities)
120  {
121  SymbolData symbolData;
122  if (SymbolDataBySymbol.TryGetValue(removed.Symbol, out symbolData))
123  {
124  // clean up our consolidators
125  symbolData.RemoveConsolidators();
126  SymbolDataBySymbol.Remove(removed.Symbol);
127  }
128  }
129  }
130 
131  /// <summary>
132  /// Contains data specific to a symbol required by this model
133  /// </summary>
134  public class SymbolData
135  {
136  private readonly QCAlgorithm _algorithm;
137  private readonly IDataConsolidator _fastConsolidator;
138  private readonly IDataConsolidator _slowConsolidator;
139  private readonly ExponentialMovingAverage _fast;
140  private readonly ExponentialMovingAverage _slow;
141  private readonly Security _security;
142 
143  /// <summary>
144  /// Symbol associated with the data
145  /// </summary>
146  public Symbol Symbol => _security.Symbol;
147 
148  /// <summary>
149  /// Fast Exponential Moving Average (EMA)
150  /// </summary>
152 
153  /// <summary>
154  /// Slow Exponential Moving Average (EMA)
155  /// </summary>
157 
158  /// <summary>
159  /// True if the fast is above the slow, otherwise false.
160  /// This is used to prevent emitting the same signal repeatedly
161  /// </summary>
162  public bool FastIsOverSlow { get; set; }
163 
164  /// <summary>
165  /// Flag indicating if the Slow EMA is over the Fast one
166  /// </summary>
168 
169  /// <summary>
170  /// Initializes an instance of the class SymbolData with the given arguments
171  /// </summary>
172  public SymbolData(
173  Security security,
174  int fastPeriod,
175  int slowPeriod,
176  QCAlgorithm algorithm,
177  Resolution resolution)
178  {
179  _algorithm = algorithm;
180  _security = security;
181 
182  _fastConsolidator = algorithm.ResolveConsolidator(security.Symbol, resolution);
183  _slowConsolidator = algorithm.ResolveConsolidator(security.Symbol, resolution);
184 
185  algorithm.SubscriptionManager.AddConsolidator(security.Symbol, _fastConsolidator);
186  algorithm.SubscriptionManager.AddConsolidator(security.Symbol, _slowConsolidator);
187 
188  // create fast/slow EMAs
189  _fast = new ExponentialMovingAverage(security.Symbol, fastPeriod, ExponentialMovingAverage.SmoothingFactorDefault(fastPeriod));
190  _slow = new ExponentialMovingAverage(security.Symbol, slowPeriod, ExponentialMovingAverage.SmoothingFactorDefault(slowPeriod));
191 
192  algorithm.RegisterIndicator(security.Symbol, _fast, _fastConsolidator);
193  algorithm.RegisterIndicator(security.Symbol, _slow, _slowConsolidator);
194 
195  algorithm.WarmUpIndicator(security.Symbol, _fast, resolution);
196  algorithm.WarmUpIndicator(security.Symbol, _slow, resolution);
197  }
198 
199  /// <summary>
200  /// Remove Fast and Slow consolidators
201  /// </summary>
202  public void RemoveConsolidators()
203  {
204  _algorithm.SubscriptionManager.RemoveConsolidator(Symbol, _fastConsolidator);
205  _algorithm.SubscriptionManager.RemoveConsolidator(Symbol, _slowConsolidator);
206  }
207  }
208  }
209 }