Lean  $LEAN_TAG$
TradeBarConsolidator.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 
17 using System;
19 using Python.Runtime;
20 
22 {
23  /// <summary>
24  /// A data consolidator that can make bigger bars from smaller ones over a given
25  /// time span or a count of pieces of data.
26  ///
27  /// Use this consolidator to turn data of a lower resolution into data of a higher resolution,
28  /// for example, if you subscribe to minute data but want to have a 15 minute bar.
29  /// </summary>
31  {
32  /// <summary>
33  /// Create a new TradeBarConsolidator for the desired resolution
34  /// </summary>
35  /// <param name="resolution">The resolution desired</param>
36  /// <returns>A consolidator that produces data on the resolution interval</returns>
38  {
39  return new TradeBarConsolidator(resolution.ToTimeSpan());
40  }
41 
42  /// <summary>
43  /// Creates a consolidator to produce a new 'TradeBar' representing the period
44  /// </summary>
45  /// <param name="period">The minimum span of time before emitting a consolidated bar</param>
46  public TradeBarConsolidator(TimeSpan period)
47  : base(period)
48  {
49  }
50 
51  /// <summary>
52  /// Creates a consolidator to produce a new 'TradeBar' representing the last count pieces of data
53  /// </summary>
54  /// <param name="maxCount">The number of pieces to accept before emitting a consolidated bar</param>
55  public TradeBarConsolidator(int maxCount)
56  : base(maxCount)
57  {
58  }
59 
60  /// <summary>
61  /// Creates a consolidator to produce a new 'TradeBar' representing the last count pieces of data or the period, whichever comes first
62  /// </summary>
63  /// <param name="maxCount">The number of pieces to accept before emitting a consolidated bar</param>
64  /// <param name="period">The minimum span of time before emitting a consolidated bar</param>
65  public TradeBarConsolidator(int maxCount, TimeSpan period)
66  : base(maxCount, period)
67  {
68  }
69 
70  /// <summary>
71  /// Creates a consolidator to produce a new 'TradeBar' representing the last count pieces of data or the period, whichever comes first
72  /// </summary>
73  /// <param name="func">Func that defines the start time of a consolidated data</param>
74  public TradeBarConsolidator(Func<DateTime, CalendarInfo> func)
75  : base(func)
76  {
77  }
78 
79  /// <summary>
80  /// Creates a consolidator to produce a new 'TradeBar' representing the last count pieces of data or the period, whichever comes first
81  /// </summary>
82  /// <param name="pyfuncobj">Python function object that defines the start time of a consolidated data</param>
83  public TradeBarConsolidator(PyObject pyfuncobj)
84  : base(pyfuncobj)
85  {
86  }
87 
88  /// <summary>
89  /// Aggregates the new 'data' into the 'workingBar'. The 'workingBar' will be
90  /// null following the event firing
91  /// </summary>
92  /// <param name="workingBar">The bar we're building, null if the event was just fired and we're starting a new trade bar</param>
93  /// <param name="data">The new data</param>
94  protected override void AggregateBar(ref TradeBar workingBar, TradeBar data)
95  {
96  if (workingBar == null)
97  {
98  workingBar = new TradeBar
99  {
100  Time = GetRoundedBarTime(data),
101  Symbol = data.Symbol,
102  Open = data.Open,
103  High = data.High,
104  Low = data.Low,
105  Close = data.Close,
106  Volume = data.Volume,
107  DataType = MarketDataType.TradeBar,
108  Period = IsTimeBased && Period.HasValue ? (TimeSpan)Period : data.Period
109  };
110  }
111  else
112  {
113  //Aggregate the working bar
114  workingBar.Close = data.Close;
115  workingBar.Volume += data.Volume;
116  if (!IsTimeBased) workingBar.Period += data.Period;
117  if (data.Low < workingBar.Low) workingBar.Low = data.Low;
118  if (data.High > workingBar.High) workingBar.High = data.High;
119  }
120  }
121  }
122 }