forked from QuantConnect/Lean
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTickAggregator.cs
180 lines (161 loc) · 6.23 KB
/
TickAggregator.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
using System;
using System.Collections.Generic;
using System.Linq;
using QuantConnect.Data;
using QuantConnect.Data.Consolidators;
using QuantConnect.Data.Market;
using QuantConnect.Util;
namespace QuantConnect.ToolBox
{
/// <summary>
/// Class that uses consolidators to aggregate tick data data
/// </summary>
public abstract class TickAggregator
{
protected TickAggregator(Resolution resolution, TickType tickType)
{
TickType = tickType;
Resolution = resolution;
}
/// <summary>
/// Gets the tick type of the consolidator
/// </summary>
public TickType TickType { get; protected set; }
/// <summary>
/// The consolidator used to aggregate data from
/// higher resolutions to data in lower resolutions
/// </summary>
public IDataConsolidator Consolidator { get; protected set; }
/// <summary>
/// The consolidated data
/// </summary>
public List<BaseData> Consolidated { get; protected set; }
/// <summary>
/// The resolution that the data is being aggregated into
/// </summary>
public Resolution Resolution { get; }
/// <summary>
/// Updates the consolidator with the specified bar.
/// </summary>
/// <param name="data">The latest data observation.</param>
public virtual void Update(BaseData data)
{
Consolidator.Update(data);
}
/// <summary>
/// Return all the consolidated data as well as the
/// bar the consolidator is currently working on
/// </summary>
public List<BaseData> Flush()
{
var data = new List<BaseData>(Consolidated);
if (Consolidator.WorkingData != null)
{
data.Add(Consolidator.WorkingData as BaseData);
}
return data;
}
/// <summary>
/// Creates the correct <see cref="TickAggregator"/> instances for the specified tick types and resolution.
/// <see cref="QuantConnect.TickType.OpenInterest"/> will ignore <paramref name="resolution"/> and use <see cref="QuantConnect.Resolution.Daily"/>
/// </summary>
public static IEnumerable<TickAggregator> ForTickTypes(SecurityType securityType, Resolution resolution, params TickType[] tickTypes)
{
if (resolution == Resolution.Tick)
{
foreach (var tickType in tickTypes.Where(t => LeanData.IsValidConfiguration(securityType, resolution, t)))
{
// OI is special
if (tickType == TickType.OpenInterest)
{
yield return new OpenInterestTickAggregator(resolution);
continue;
}
yield return new IdentityTickAggregator(tickType);
}
yield break;
}
foreach (var tickType in tickTypes.Where(t => LeanData.IsValidConfiguration(securityType, resolution, t)))
{
switch (tickType)
{
case TickType.Trade:
yield return new TradeTickAggregator(resolution);
break;
case TickType.Quote:
yield return new QuoteTickAggregator(resolution);
break;
case TickType.OpenInterest:
yield return new OpenInterestTickAggregator(resolution);
break;
default:
throw new ArgumentOutOfRangeException(nameof(tickType), tickType, null);
}
}
}
}
/// <summary>
/// Use <see cref="TickQuoteBarConsolidator"/> to consolidate quote ticks into a specified resolution
/// </summary>
public class QuoteTickAggregator : TickAggregator
{
public QuoteTickAggregator(Resolution resolution)
: base(resolution, TickType.Quote)
{
Consolidated = new List<BaseData>();
Consolidator = new TickQuoteBarConsolidator(resolution.ToTimeSpan());
Consolidator.DataConsolidated += (sender, consolidated) =>
{
Consolidated.Add(consolidated as QuoteBar);
};
}
}
/// <summary>
/// Use <see cref="TickQuoteBarConsolidator"/> to consolidate trade ticks into a specified resolution
/// </summary>
public class TradeTickAggregator : TickAggregator
{
public TradeTickAggregator(Resolution resolution)
: base(resolution, TickType.Trade)
{
Consolidated = new List<BaseData>();
Consolidator = new TickConsolidator(resolution.ToTimeSpan());
Consolidator.DataConsolidated += (sender, consolidated) =>
{
Consolidated.Add(consolidated as TradeBar);
};
}
}
/// <summary>
/// Use <see cref="OpenInterestConsolidator"/> to consolidate open interest ticks into a specified resolution
/// </summary>
public class OpenInterestTickAggregator : TickAggregator
{
public OpenInterestTickAggregator(Resolution resolution)
: base(resolution, TickType.OpenInterest)
{
Consolidated = new List<BaseData>();
Consolidator = new OpenInterestConsolidator(resolution.ToTimeSpan());
Consolidator.DataConsolidated += (sender, consolidated) =>
{
Consolidated.Add(consolidated as OpenInterest);
};
}
}
/// <summary>
/// Use <see cref="IdentityDataConsolidator{T}"/> to yield ticks unmodified into the consolidated data collection
/// </summary>
public class IdentityTickAggregator : TickAggregator
{
public IdentityTickAggregator(TickType tickType)
: base(Resolution.Tick, tickType)
{
Consolidated = new List<BaseData>();
Consolidator = FilteredIdentityDataConsolidator.ForTickType(tickType);
Consolidator.DataConsolidated += (sender, consolidated) =>
{
Consolidated.Add(consolidated as Tick);
};
}
}
}