Lean  $LEAN_TAG$
StandardDeviationExecutionModel.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;
24 using QuantConnect.Orders;
25 
27 {
28  /// <summary>
29  /// Execution model that submits orders while the current market prices is at least the configured number of standard
30  /// deviations away from the mean in the favorable direction (below/above for buy/sell respectively)
31  /// </summary>
33  {
34  private readonly int _period;
35  private readonly decimal _deviations;
36  private readonly Resolution _resolution;
37  private readonly PortfolioTargetCollection _targetsCollection;
38  private readonly Dictionary<Symbol, SymbolData> _symbolData;
39 
40  /// <summary>
41  /// Gets or sets the maximum order value in units of the account currency.
42  /// This defaults to $20,000. For example, if purchasing a stock with a price
43  /// of $100, then the maximum order size would be 200 shares.
44  /// </summary>
45  public decimal MaximumOrderValue { get; set; } = 20 * 1000;
46 
47  /// <summary>
48  /// Initializes a new instance of the <see cref="StandardDeviationExecutionModel"/> class
49  /// </summary>
50  /// <param name="period">Period of the standard deviation indicator</param>
51  /// <param name="deviations">The number of deviations away from the mean before submitting an order</param>
52  /// <param name="resolution">The resolution of the STD and SMA indicators</param>
54  int period = 60,
55  decimal deviations = 2m,
56  Resolution resolution = Resolution.Minute
57  )
58  {
59  _period = period;
60  _deviations = deviations;
61  _resolution = resolution;
62  _targetsCollection = new PortfolioTargetCollection();
63  _symbolData = new Dictionary<Symbol, SymbolData>();
64  }
65 
66  /// <summary>
67  /// Executes market orders if the standard deviation of price is more than the configured number of deviations
68  /// in the favorable direction.
69  /// </summary>
70  /// <param name="algorithm">The algorithm instance</param>
71  /// <param name="targets">The portfolio targets</param>
72  public override void Execute(QCAlgorithm algorithm, IPortfolioTarget[] targets)
73  {
74  _targetsCollection.AddRange(targets);
75 
76  // for performance we check count value, OrderByMarginImpact and ClearFulfilled are expensive to call
77  if (!_targetsCollection.IsEmpty)
78  {
79  foreach (var target in _targetsCollection.OrderByMarginImpact(algorithm))
80  {
81  var symbol = target.Symbol;
82 
83  // calculate remaining quantity to be ordered
84  var unorderedQuantity = OrderSizing.GetUnorderedQuantity(algorithm, target);
85 
86  // fetch our symbol data containing our STD/SMA indicators
87  SymbolData data;
88  if (!_symbolData.TryGetValue(symbol, out data))
89  {
90  continue;
91  }
92 
93  // check order entry conditions
94  if (data.STD.IsReady && PriceIsFavorable(data, unorderedQuantity))
95  {
96  // Adjust order size to respect the maximum total order value
97  var orderSize = OrderSizing.GetOrderSizeForMaximumValue(data.Security, MaximumOrderValue, unorderedQuantity);
98 
99  if (orderSize != 0)
100  {
101  algorithm.MarketOrder(symbol, orderSize);
102  }
103  }
104  }
105 
106  _targetsCollection.ClearFulfilled(algorithm);
107  }
108  }
109 
110  /// <summary>
111  /// Event fired each time the we add/remove securities from the data feed
112  /// </summary>
113  /// <param name="algorithm">The algorithm instance that experienced the change in securities</param>
114  /// <param name="changes">The security additions and removals from the algorithm</param>
115  public override void OnSecuritiesChanged(QCAlgorithm algorithm, SecurityChanges changes)
116  {
117  foreach (var added in changes.AddedSecurities)
118  {
119  // initialize new securities
120  if (!_symbolData.ContainsKey(added.Symbol))
121  {
122  _symbolData[added.Symbol] = new SymbolData(algorithm, added, _period, _resolution);
123  }
124  }
125 
126  foreach (var removed in changes.RemovedSecurities)
127  {
128  // clean up data from removed securities
129  SymbolData data;
130  if (_symbolData.TryGetValue(removed.Symbol, out data))
131  {
132  if (IsSafeToRemove(algorithm, removed.Symbol))
133  {
134  _symbolData.Remove(removed.Symbol);
135  algorithm.SubscriptionManager.RemoveConsolidator(removed.Symbol, data.Consolidator);
136  }
137  }
138  }
139  }
140 
141  /// <summary>
142  /// Determines if the current price is more than the configured number of standard deviations
143  /// away from the mean in the favorable direction.
144  /// </summary>
145  protected virtual bool PriceIsFavorable(SymbolData data, decimal unorderedQuantity)
146  {
147  var deviations = _deviations * data.STD;
148  return unorderedQuantity > 0
149  ? data.Security.BidPrice < data.SMA - deviations
150  : data.Security.AskPrice > data.SMA + deviations;
151  }
152 
153  /// <summary>
154  /// Determines if it's safe to remove the associated symbol data
155  /// </summary>
156  protected virtual bool IsSafeToRemove(QCAlgorithm algorithm, Symbol symbol)
157  {
158  // confirm the security isn't currently a member of any universe
159  return !algorithm.UniverseManager.Any(kvp => kvp.Value.ContainsMember(symbol));
160  }
161 
162  /// <summary>
163  /// Symbol Data for this Execution Model
164  /// </summary>
165  protected class SymbolData
166  {
167  /// <summary>
168  /// Security
169  /// </summary>
170  public Security Security { get; }
171 
172  /// <summary>
173  /// Standard Deviation
174  /// </summary>
175  public StandardDeviation STD { get; }
176 
177  /// <summary>
178  /// Simple Moving Average
179  /// </summary>
180  public SimpleMovingAverage SMA { get; }
181 
182  /// <summary>
183  /// Data Consolidator
184  /// </summary>
186 
187  /// <summary>
188  /// Initialize an instance of <see cref="SymbolData"/>
189  /// </summary>
190  /// <param name="algorithm">Algorithm for this security</param>
191  /// <param name="security">The security we are using</param>
192  /// <param name="period">Period of the SMA and STD</param>
193  /// <param name="resolution">Resolution for this symbol</param>
194  public SymbolData(QCAlgorithm algorithm, Security security, int period, Resolution resolution)
195  {
196  Security = security;
197  Consolidator = algorithm.ResolveConsolidator(security.Symbol, resolution);
198 
199  var smaName = algorithm.CreateIndicatorName(security.Symbol, "SMA" + period, resolution);
200  SMA = new SimpleMovingAverage(smaName, period);
201  algorithm.RegisterIndicator(security.Symbol, SMA, Consolidator);
202 
203  var stdName = algorithm.CreateIndicatorName(security.Symbol, "STD" + period, resolution);
204  STD = new StandardDeviation(stdName, period);
205  algorithm.RegisterIndicator(security.Symbol, STD, Consolidator);
206 
207  // warmup our indicators by pushing history through the indicators
208  foreach (var bar in algorithm.History(Security.Symbol, period, resolution))
209  {
210  SMA.Update(bar.EndTime, bar.Value);
211  STD.Update(bar.EndTime, bar.Value);
212  }
213  }
214  }
215  }
216 }