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