Lean  $LEAN_TAG$
SymbolCapacity.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 
17 using System;
18 using System.Linq;
21 using QuantConnect.Logging;
22 using QuantConnect.Orders;
24 
25 namespace QuantConnect
26 {
27  /// <summary>
28  /// Per-symbol capacity estimations, tightly coupled with the <see cref="CapacityEstimate"/> class.
29  /// </summary>
30  internal class SymbolCapacity
31  {
32  /// <summary>
33  /// The period for which a symbol trade influentiates capacity estimate
34  /// </summary>
35  public static TimeSpan CapacityEffectPeriod = TimeSpan.FromDays(30);
36 
37  /// <summary>
38  /// An estimate of how much volume the FX market trades per minute
39  /// </summary>
40  /// <remarks>
41  /// Any mentions of "dollar volume" are in account currency. They are not always in dollars.
42  /// </remarks>
43  private const decimal _forexMinuteVolume = 25000000m;
44 
45  /// <summary>
46  /// An estimate of how much volume the CFD market trades per minute
47  /// </summary>
48  /// <remarks>
49  /// This is pure estimation since we don't have CFD volume data. Based on 300k per day.
50  /// </remarks>
51  private const decimal _cfdMinuteVolume = 200m;
52  private const decimal _fastTradingVolumeScalingFactor = 2m;
53 
54  private readonly IAlgorithm _algorithm;
55  private readonly Symbol _symbol;
56 
57  private decimal _previousVolume;
58  private DateTime? _previousTime;
59 
60  private bool _isInternal;
61  private decimal _averageDollarVolume;
62  private decimal _resolutionScaleFactor;
63  private decimal _marketCapacityDollarVolume;
64  private bool _resetMarketCapacityDollarVolume;
65  private decimal _fastTradingVolumeDiscountFactor;
66  private OrderEvent _previousOrderEvent;
67 
68  /// <summary>
69  /// Total trades made in between snapshots
70  /// </summary>
71  public int Trades { get; private set; }
72 
73  /// <summary>
74  /// The Symbol's Security
75  /// </summary>
76  public Security Security { get; }
77 
78  /// <summary>
79  /// The absolute dollar volume (in account currency) we've traded
80  /// </summary>
81  public decimal SaleVolume { get; private set; }
82 
83  /// <summary>
84  /// Market capacity dollar volume, i.e. the capacity the market is able to provide for this Symbol
85  /// </summary>
86  /// <remarks>
87  /// Dollar volume is in account currency, but name is used for consistency with financial literature.
88  /// </remarks>
89  public decimal MarketCapacityDollarVolume => _marketCapacityDollarVolume * _resolutionScaleFactor;
90 
91  /// <summary>
92  /// Creates a new SymbolCapacity object, capable of determining market capacity for a Symbol
93  /// </summary>
94  /// <param name="algorithm"></param>
95  /// <param name="symbol"></param>
96  public SymbolCapacity(IAlgorithm algorithm, Symbol symbol)
97  {
98  _algorithm = algorithm;
99  Security = _algorithm.Securities[symbol];
100  _symbol = symbol;
101 
102  _isInternal = _algorithm
103  .SubscriptionManager
104  .SubscriptionDataConfigService
105  .GetSubscriptionDataConfigs(symbol, includeInternalConfigs: true)
106  .All(config => config.IsInternalFeed);
107  }
108 
109  /// <summary>
110  /// New order event handler. Handles the aggregation of SaleVolume and
111  /// sometimes resetting the <seealso cref="MarketCapacityDollarVolume"/>
112  /// </summary>
113  /// <param name="orderEvent">Parent class filters out other events so only fill events reach this method.</param>
114  public void OnOrderEvent(OrderEvent orderEvent)
115  {
117 
118  // To reduce the capacity of high frequency strategies, we scale down the
119  // volume captured on each bar proportional to the trades per day.
120  // Default to -1 day for the first order to not reduce the volume of the first order.
121  _fastTradingVolumeDiscountFactor = _fastTradingVolumeScalingFactor * ((decimal)((orderEvent.UtcTime - (_previousOrderEvent?.UtcTime ?? orderEvent.UtcTime.AddDays(-1))).TotalMinutes) / 390m);
122  _fastTradingVolumeDiscountFactor = _fastTradingVolumeDiscountFactor > 1 ? 1 : Math.Max(0.20m, _fastTradingVolumeDiscountFactor);
123 
124  if (_resetMarketCapacityDollarVolume)
125  {
126  _marketCapacityDollarVolume = 0;
127  Trades = 0;
128  _resetMarketCapacityDollarVolume = false;
129  }
130 
131  Trades++;
132  _previousOrderEvent = orderEvent;
133  }
134 
135  /// <summary>
136  /// Determines whether we should add the Market Volume to the <see cref="MarketCapacityDollarVolume"/>
137  /// </summary>
138  /// <returns></returns>
139  private bool IncludeMarketVolume(Resolution resolution)
140  {
141  if (_previousOrderEvent == null)
142  {
143  return false;
144  }
145 
146  var dollarVolumeScaleFactor = 6000000;
147  DateTime timeout;
148  decimal k;
149 
150  switch (resolution)
151  {
152  case Resolution.Tick:
153  case Resolution.Second:
154  dollarVolumeScaleFactor = dollarVolumeScaleFactor / 60;
155  k = _averageDollarVolume != 0
156  ? dollarVolumeScaleFactor / _averageDollarVolume
157  : 10;
158 
159  var timeoutPeriod = k > 120 ? 120 : (int)Math.Max(5, (double)k);
160  timeout = _previousOrderEvent.UtcTime.AddMinutes(timeoutPeriod);
161  break;
162 
163  case Resolution.Minute:
164  k = _averageDollarVolume != 0
165  ? dollarVolumeScaleFactor / _averageDollarVolume
166  : 10;
167 
168  var timeoutMinutes = k > 120 ? 120 : (int)Math.Max(1, (double)k);
169  timeout = _previousOrderEvent.UtcTime.AddMinutes(timeoutMinutes);
170  break;
171 
172  case Resolution.Hour:
173  return _algorithm.UtcTime == _previousOrderEvent.UtcTime.RoundUp(resolution.ToTimeSpan());
174 
175  case Resolution.Daily:
176  // At the end of a daily bar, the EndTime is the next day.
177  // Increment the order by one day to match it
178  return _algorithm.UtcTime == _previousOrderEvent.UtcTime ||
179  _algorithm.UtcTime.Date == _previousOrderEvent.UtcTime.RoundUp(resolution.ToTimeSpan());
180 
181  default:
182  timeout = _previousOrderEvent.UtcTime.AddHours(1);
183  break;
184  }
185 
186  return _algorithm.UtcTime <= timeout;
187  }
188 
189  /// <summary>
190  /// Updates the market capacity of the Symbol. Called on each time step of the algorithm
191  /// </summary>
192  /// <returns>False if we're currently within the timeout period, True if the Symbol has went past the timeout</returns>
193  public bool UpdateMarketCapacity()
194  {
195  var bar = GetBar();
196  if (bar == null || bar.Volume == 0)
197  {
198  return false;
199  }
200 
201  var utcTime = _algorithm.UtcTime;
202  var resolution = bar.Period.ToHigherResolutionEquivalent(false);
203  var conversionRate = Security.QuoteCurrency.ConversionRate;
204  var timeBetweenBars = (decimal)(utcTime - (_previousTime ?? utcTime)).TotalMinutes;
205 
206  if (_previousTime == null || timeBetweenBars == 0)
207  {
208  _averageDollarVolume = conversionRate * bar.Close * bar.Volume;
209  }
210  else
211  {
212  _averageDollarVolume = ((bar.Close * conversionRate) * (bar.Volume + _previousVolume)) / timeBetweenBars;
213  }
214 
215  _previousTime = utcTime;
216  _previousVolume = bar.Volume;
217 
218  var includeMarketVolume = IncludeMarketVolume(resolution);
219  if (includeMarketVolume)
220  {
221  _resolutionScaleFactor = ResolutionScaleFactor(resolution);
222  _marketCapacityDollarVolume += bar.Close * _fastTradingVolumeDiscountFactor * bar.Volume * conversionRate * Security.SymbolProperties.ContractMultiplier;
223  }
224 
225  // When we've finished including market volume, signal completed
226  return !includeMarketVolume;
227  }
228 
229  /// <summary>
230  /// Gets the TradeBar for the given time step. For Quotes, we convert
231  /// it into a TradeBar using market depth as a proxy for volume.
232  /// </summary>
233  /// <returns>TradeBar</returns>
234  private TradeBar GetBar()
235  {
236  TradeBar bar;
237  if (_algorithm.CurrentSlice.Bars.TryGetValue(_symbol, out bar))
238  {
239  return bar;
240  }
241 
242  QuoteBar quote;
243  if (_algorithm.CurrentSlice.QuoteBars.TryGetValue(_symbol, out quote))
244  {
245  // Fake a tradebar for quote data using market depth as a proxy for volume
246  var volume = (quote.LastBidSize + quote.LastAskSize) / 2;
247 
248  // Handle volume estimation for security types that don't have volume values
249  switch (_symbol.SecurityType)
250  {
251  case SecurityType.Forex:
252  volume = _forexMinuteVolume;
253  break;
254  case SecurityType.Cfd:
255  volume = _cfdMinuteVolume;
256  break;
257  }
258 
259 
260  return new TradeBar(
261  quote.Time,
262  quote.Symbol,
263  quote.Open,
264  quote.High,
265  quote.Low,
266  quote.Close,
267  volume,
268  quote.Period);
269  }
270 
271  if (!_isInternal)
272  {
273  return null;
274  }
275  // internal subscriptions, like mapped continuous future contract won't be sent through the slice
276  // but will be available in the security cache, if not present will return null
277  var result = Security.Cache.GetData<TradeBar>();
278  if (result != null
279  && _algorithm.UtcTime == result.EndTime.ConvertToUtc(Security.Exchange.Hours.TimeZone))
280  {
281  return result;
282  }
283 
284  return null;
285  }
286 
287  private static decimal ResolutionScaleFactor(Resolution resolution)
288  {
289  switch (resolution)
290  {
291  case Resolution.Daily:
292  return 0.02m;
293 
294  case Resolution.Hour:
295  return 0.05m;
296 
297  case Resolution.Minute:
298  return 0.20m;
299 
300  case Resolution.Tick:
301  case Resolution.Second:
302  return 0.50m;
303 
304  default:
305  return 1m;
306  }
307  }
308 
309  /// <summary>
310  /// Signals a reset for the <see cref="MarketCapacityDollarVolume"/> and <see cref="SaleVolume"/>
311  /// </summary>
312  public void Reset()
313  {
314  _resetMarketCapacityDollarVolume = true;
315  SaleVolume = 0;
316  }
317 
318  /// <summary>
319  /// Determines if we should remove a symbol from capacity estimation
320  /// </summary>
321  public bool ShouldRemove()
322  {
323  if (Security.Invested || _algorithm.UtcTime < _previousOrderEvent.UtcTime + CapacityEffectPeriod)
324  {
325  return false;
326  }
327 
328  return true;
329  }
330  }
331 }