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 readonly Dictionary<Symbol, SymbolData> SymbolDataBySymbol;
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  public Symbol Symbol => _security.Symbol;
144  public ExponentialMovingAverage Fast => _fast;
145  public ExponentialMovingAverage Slow => _slow;
146 
147  /// <summary>
148  /// True if the fast is above the slow, otherwise false.
149  /// This is used to prevent emitting the same signal repeatedly
150  /// </summary>
151  public bool FastIsOverSlow { get; set; }
152  public bool SlowIsOverFast => !FastIsOverSlow;
153 
154  public SymbolData(
155  Security security,
156  int fastPeriod,
157  int slowPeriod,
158  QCAlgorithm algorithm,
159  Resolution resolution)
160  {
161  _algorithm = algorithm;
162  _security = security;
163 
164  _fastConsolidator = algorithm.ResolveConsolidator(security.Symbol, resolution);
165  _slowConsolidator = algorithm.ResolveConsolidator(security.Symbol, resolution);
166 
167  algorithm.SubscriptionManager.AddConsolidator(security.Symbol, _fastConsolidator);
168  algorithm.SubscriptionManager.AddConsolidator(security.Symbol, _slowConsolidator);
169 
170  // create fast/slow EMAs
171  _fast = new ExponentialMovingAverage(security.Symbol, fastPeriod, ExponentialMovingAverage.SmoothingFactorDefault(fastPeriod));
172  _slow = new ExponentialMovingAverage(security.Symbol, slowPeriod, ExponentialMovingAverage.SmoothingFactorDefault(slowPeriod));
173 
174  algorithm.RegisterIndicator(security.Symbol, _fast, _fastConsolidator);
175  algorithm.RegisterIndicator(security.Symbol, _slow, _slowConsolidator);
176 
177  algorithm.WarmUpIndicator(security.Symbol, _fast, resolution);
178  algorithm.WarmUpIndicator(security.Symbol, _slow, resolution);
179  }
180 
181  /// <summary>
182  /// Remove Fast and Slow consolidators
183  /// </summary>
184  public void RemoveConsolidators()
185  {
186  _algorithm.SubscriptionManager.RemoveConsolidator(Symbol, _fastConsolidator);
187  _algorithm.SubscriptionManager.RemoveConsolidator(Symbol, _slowConsolidator);
188  }
189  }
190  }
191 }