forked from QuantConnect/Lean
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TickAggregator.cs
77 lines (70 loc) · 2.44 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
using System.Collections.Generic;
using QuantConnect.Data;
using QuantConnect.Data.Consolidators;
using QuantConnect.Data.Market;
namespace QuantConnect.ToolBox
{
/// <summary>
/// Class that uses consolidators to aggregate tick data data
/// </summary>
public abstract class TickAggregator
{
protected TickAggregator(Resolution resolution)
{
Resolution = resolution;
}
/// <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>
/// Return all the consolidated data as well as the
/// bar the consolidator is currently working on
/// </summary>
public List<BaseData> Flush()
{
return new List<BaseData>(Consolidated) { Consolidator.WorkingData as BaseData };
}
}
/// <summary>
/// Use <see cref="TickQuoteBarConsolidator"/> to consolidate quote ticks into a specified resolution
/// </summary>
public class QuoteTickAggregator : TickAggregator
{
public QuoteTickAggregator(Resolution resolution)
: base(resolution)
{
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)
{
Consolidated = new List<BaseData>();
Consolidator = new TickConsolidator(resolution.ToTimeSpan());
Consolidator.DataConsolidated += (sender, consolidated) =>
{
Consolidated.Add(consolidated as TradeBar);
};
}
}
}