Lean  $LEAN_TAG$
TickConsolidator.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 using System;
18 using Python.Runtime;
19 
21 {
22  /// <summary>
23  /// A data consolidator that can make bigger bars from ticks over a given
24  /// time span or a count of pieces of data.
25  /// </summary>
27  {
28  /// <summary>
29  /// Creates a consolidator to produce a new 'TradeBar' representing the period
30  /// </summary>
31  /// <param name="period">The minimum span of time before emitting a consolidated bar</param>
32  public TickConsolidator(TimeSpan period)
33  : base(period)
34  {
35  }
36 
37  /// <summary>
38  /// Creates a consolidator to produce a new 'TradeBar' representing the last count pieces of data
39  /// </summary>
40  /// <param name="maxCount">The number of pieces to accept before emitting a consolidated bar</param>
41  public TickConsolidator(int maxCount)
42  : base(maxCount)
43  {
44  }
45 
46  /// <summary>
47  /// Creates a consolidator to produce a new 'TradeBar' representing the last count pieces of data or the period, whichever comes first
48  /// </summary>
49  /// <param name="maxCount">The number of pieces to accept before emitting a consolidated bar</param>
50  /// <param name="period">The minimum span of time before emitting a consolidated bar</param>
51  public TickConsolidator(int maxCount, TimeSpan period)
52  : base(maxCount, period)
53  {
54  }
55 
56  /// <summary>
57  /// Initializes a new instance of the <see cref="TickQuoteBarConsolidator"/> class
58  /// </summary>
59  /// <param name="func">Func that defines the start time of a consolidated data</param>
60  public TickConsolidator(Func<DateTime, CalendarInfo> func)
61  : base(func)
62  {
63  }
64 
65  /// <summary>
66  /// Creates a consolidator to produce a new 'TradeBar' representing the last count pieces of data or the period, whichever comes first
67  /// </summary>
68  /// <param name="pyfuncobj">Python function object that defines the start time of a consolidated data</param>
69  public TickConsolidator(PyObject pyfuncobj)
70  : base(pyfuncobj)
71  {
72  }
73 
74  /// <summary>
75  /// Determines whether or not the specified data should be processed
76  /// </summary>
77  /// <param name="data">The data to check</param>
78  /// <returns>True if the consolidator should process this data, false otherwise</returns>
79  protected override bool ShouldProcess(Tick data)
80  {
81  return data.TickType == TickType.Trade;
82  }
83 
84  /// <summary>
85  /// Aggregates the new 'data' into the 'workingBar'. The 'workingBar' will be
86  /// null following the event firing
87  /// </summary>
88  /// <param name="workingBar">The bar we're building</param>
89  /// <param name="data">The new data</param>
90  protected override void AggregateBar(ref TradeBar workingBar, Tick data)
91  {
92  if (workingBar == null)
93  {
94  workingBar = new TradeBar(GetRoundedBarTime(data),
95  data.Symbol,
96  data.Value,
97  data.Value,
98  data.Value,
99  data.Value,
100  data.Quantity,
101  Period);
102  }
103  else
104  {
105  //Aggregate the working bar
106  workingBar.Close = data.Value;
107  workingBar.Volume += data.Quantity;
108  if (data.Value < workingBar.Low) workingBar.Low = data.Value;
109  if (data.Value > workingBar.High) workingBar.High = data.Value;
110  }
111  }
112  }
113 }