Lean  $LEAN_TAG$
BaseDataConsolidator.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  /// Type capable of consolidating trade bars from any base data instance
24  /// </summary>
26  {
27  /// <summary>
28  /// Create a new TickConsolidator for the desired resolution
29  /// </summary>
30  /// <param name="resolution">The resolution desired</param>
31  /// <returns>A consolidator that produces data on the resolution interval</returns>
33  {
34  return new BaseDataConsolidator(resolution.ToTimeSpan());
35  }
36 
37  /// <summary>
38  /// Creates a consolidator to produce a new 'TradeBar' representing the period
39  /// </summary>
40  /// <param name="period">The minimum span of time before emitting a consolidated bar</param>
41  public BaseDataConsolidator(TimeSpan period)
42  : base(period)
43  {
44  }
45 
46  /// <summary>
47  /// Creates a consolidator to produce a new 'TradeBar' representing the last count pieces of data
48  /// </summary>
49  /// <param name="maxCount">The number of pieces to accept before emitting a consolidated bar</param>
50  public BaseDataConsolidator(int maxCount)
51  : base(maxCount)
52  {
53  }
54 
55  /// <summary>
56  /// Creates a consolidator to produce a new 'TradeBar' representing the last count pieces of data or the period, whichever comes first
57  /// </summary>
58  /// <param name="maxCount">The number of pieces to accept before emitting a consolidated bar</param>
59  /// <param name="period">The minimum span of time before emitting a consolidated bar</param>
60  public BaseDataConsolidator(int maxCount, TimeSpan period)
61  : base(maxCount, period)
62  {
63  }
64 
65  /// <summary>
66  /// Initializes a new instance of the <see cref="BaseDataConsolidator"/> class
67  /// </summary>
68  /// <param name="func">Func that defines the start time of a consolidated data</param>
69  public BaseDataConsolidator(Func<DateTime, CalendarInfo> func)
70  : base(func)
71  {
72  }
73 
74 
75  /// <summary>
76  /// Initializes a new instance of the <see cref="BaseDataConsolidator"/> class
77  /// </summary>
78  /// <param name="pyfuncobj">Func that defines the start time of a consolidated data</param>
79  public BaseDataConsolidator(PyObject pyfuncobj)
80  : base(pyfuncobj)
81  {
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, null if the event was just fired and we're starting a new trade bar</param>
89  /// <param name="data">The new data</param>
90  protected override void AggregateBar(ref TradeBar workingBar, BaseData data)
91  {
92  if (workingBar == null)
93  {
94  workingBar = new TradeBar
95  {
96  Symbol = data.Symbol,
97  Time = GetRoundedBarTime(data.Time),
98  Close = data.Value,
99  High = data.Value,
100  Low = data.Value,
101  Open = data.Value,
102  DataType = data.DataType,
103  Value = data.Value
104  };
105  }
106  else
107  {
108  //Aggregate the working bar
109  workingBar.Close = data.Value;
110  if (data.Value < workingBar.Low) workingBar.Low = data.Value;
111  if (data.Value > workingBar.High) workingBar.High = data.Value;
112  }
113  }
114  }
115 }