Lean  $LEAN_TAG$
TickAggregator.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using QuantConnect.Data;
7 using QuantConnect.Util;
8 
9 namespace QuantConnect.ToolBox
10 {
11  /// <summary>
12  /// Class that uses consolidators to aggregate tick data data
13  /// </summary>
14  public abstract class TickAggregator
15  {
16  protected TickAggregator(Resolution resolution, TickType tickType)
17  {
18  TickType = tickType;
19  Resolution = resolution;
20  }
21 
22  /// <summary>
23  /// Gets the tick type of the consolidator
24  /// </summary>
25  public TickType TickType { get; protected set; }
26 
27  /// <summary>
28  /// The consolidator used to aggregate data from
29  /// higher resolutions to data in lower resolutions
30  /// </summary>
31  public IDataConsolidator Consolidator { get; protected set; }
32 
33  /// <summary>
34  /// The consolidated data
35  /// </summary>
36  public List<BaseData> Consolidated { get; protected set; }
37 
38  /// <summary>
39  /// The resolution that the data is being aggregated into
40  /// </summary>
41  public Resolution Resolution { get; }
42 
43  /// <summary>
44  /// Updates the consolidator with the specified bar.
45  /// </summary>
46  /// <param name="data">The latest data observation.</param>
47  public virtual void Update(BaseData data)
48  {
49  Consolidator.Update(data);
50  }
51 
52  /// <summary>
53  /// Return all the consolidated data as well as the
54  /// bar the consolidator is currently working on
55  /// </summary>
56  public List<BaseData> Flush()
57  {
58  var data = new List<BaseData>(Consolidated);
59  if (Consolidator.WorkingData != null)
60  {
61  data.Add(Consolidator.WorkingData as BaseData);
62  }
63 
64  return data;
65  }
66 
67  /// <summary>
68  /// Creates the correct <see cref="TickAggregator"/> instances for the specified tick types and resolution.
69  /// <see cref="QuantConnect.TickType.OpenInterest"/> will ignore <paramref name="resolution"/> and use <see cref="QuantConnect.Resolution.Daily"/>
70  /// </summary>
71  public static IEnumerable<TickAggregator> ForTickTypes(SecurityType securityType, Resolution resolution, params TickType[] tickTypes)
72  {
73  if (resolution == Resolution.Tick)
74  {
75  foreach (var tickType in tickTypes.Where(t => LeanData.IsValidConfiguration(securityType, resolution, t)))
76  {
77  // OI is special
78  if (tickType == TickType.OpenInterest)
79  {
80  yield return new OpenInterestTickAggregator(resolution);
81  continue;
82  }
83 
84  yield return new IdentityTickAggregator(tickType);
85  }
86 
87  yield break;
88  }
89 
90  foreach (var tickType in tickTypes.Where(t => LeanData.IsValidConfiguration(securityType, resolution, t)))
91  {
92  switch (tickType)
93  {
94  case TickType.Trade:
95  yield return new TradeTickAggregator(resolution);
96  break;
97 
98  case TickType.Quote:
99  yield return new QuoteTickAggregator(resolution);
100  break;
101 
102  case TickType.OpenInterest:
103  yield return new OpenInterestTickAggregator(resolution);
104  break;
105 
106  default:
107  throw new ArgumentOutOfRangeException(nameof(tickType), tickType, null);
108  }
109  }
110  }
111  }
112 
113  /// <summary>
114  /// Use <see cref="TickQuoteBarConsolidator"/> to consolidate quote ticks into a specified resolution
115  /// </summary>
117  {
118  public QuoteTickAggregator(Resolution resolution)
119  : base(resolution, TickType.Quote)
120  {
121  Consolidated = new List<BaseData>();
122  Consolidator = new TickQuoteBarConsolidator(resolution.ToTimeSpan());
123  Consolidator.DataConsolidated += (sender, consolidated) =>
124  {
125  Consolidated.Add(consolidated as QuoteBar);
126  };
127  }
128  }
129 
130  /// <summary>
131  /// Use <see cref="TickQuoteBarConsolidator"/> to consolidate trade ticks into a specified resolution
132  /// </summary>
134  {
135  public TradeTickAggregator(Resolution resolution)
136  : base(resolution, TickType.Trade)
137  {
138  Consolidated = new List<BaseData>();
139  Consolidator = new TickConsolidator(resolution.ToTimeSpan());
140  Consolidator.DataConsolidated += (sender, consolidated) =>
141  {
142  Consolidated.Add(consolidated as TradeBar);
143  };
144  }
145  }
146 
147  /// <summary>
148  /// Use <see cref="OpenInterestConsolidator"/> to consolidate open interest ticks into a specified resolution
149  /// </summary>
151  {
152  public OpenInterestTickAggregator(Resolution resolution)
153  : base(resolution, TickType.OpenInterest)
154  {
155  Consolidated = new List<BaseData>();
156  Consolidator = new OpenInterestConsolidator(resolution.ToTimeSpan());
157  Consolidator.DataConsolidated += (sender, consolidated) =>
158  {
159  Consolidated.Add(consolidated as OpenInterest);
160  };
161  }
162  }
163 
164  /// <summary>
165  /// Use <see cref="IdentityDataConsolidator{T}"/> to yield ticks unmodified into the consolidated data collection
166  /// </summary>
168  {
169  public IdentityTickAggregator(TickType tickType)
170  : base(Resolution.Tick, tickType)
171  {
172  Consolidated = new List<BaseData>();
174  Consolidator.DataConsolidated += (sender, consolidated) =>
175  {
176  Consolidated.Add(consolidated as Tick);
177  };
178  }
179  }
180 }