Lean  $LEAN_TAG$
DynamicDataConsolidator.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 
20 {
21  /// <summary>
22  /// A data csolidator that can make trade bars from DynamicData derived types. This is useful for
23  /// aggregating Quandl and other highly flexible dynamic custom data types.
24  /// </summary>
26  {
27  /// <summary>
28  /// Creates a consolidator to produce a new 'TradeBar' representing the period.
29  /// </summary>
30  /// <param name="period">The minimum span of time before emitting a consolidated bar</param>
31  public DynamicDataConsolidator(TimeSpan period)
32  : base(period)
33  {
34  }
35 
36  /// <summary>
37  /// Creates a consolidator to produce a new 'TradeBar' representing the last count pieces of data.
38  /// </summary>
39  /// <param name="maxCount">The number of pieces to accept before emiting a consolidated bar</param>
40  public DynamicDataConsolidator(int maxCount)
41  : base(maxCount)
42  {
43  }
44 
45  /// <summary>
46  /// Creates a consolidator to produce a new 'TradeBar' representing the last count pieces of data or the period, whichever comes first.
47  /// </summary>
48  /// <param name="maxCount">The number of pieces to accept before emiting a consolidated bar</param>
49  /// <param name="period">The minimum span of time before emitting a consolidated bar</param>
50  public DynamicDataConsolidator(int maxCount, TimeSpan period)
51  : base(maxCount, period)
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="func">Func that defines the start time of a consolidated data</param>
59  public DynamicDataConsolidator(Func<DateTime, CalendarInfo> func)
60  : base(func)
61  {
62  }
63 
64  /// <summary>
65  /// Aggregates the new 'data' into the 'workingBar'. The 'workingBar' will be
66  /// null following the event firing
67  /// </summary>
68  /// <param name="workingBar">The bar we're building, null if the event was just fired and we're starting a new trade bar</param>
69  /// <param name="data">The new data</param>
70  protected override void AggregateBar(ref TradeBar workingBar, DynamicData data)
71  {
72  // grab the properties, if they don't exist just use the .Value property
73  var open = GetNamedPropertyOrValueProperty(data, "Open");
74  var high = GetNamedPropertyOrValueProperty(data, "High");
75  var low = GetNamedPropertyOrValueProperty(data, "Low");
76  var close = GetNamedPropertyOrValueProperty(data, "Close");
77 
78  // if we have volume, use it, otherwise just use zero
79  var volume = data.HasProperty("Volume")
80  ? data.GetProperty("Volume").ConvertInvariant<long>()
81  : 0L;
82 
83  if (workingBar == null)
84  {
85  workingBar = new TradeBar
86  {
87  Symbol = data.Symbol,
88  Time = GetRoundedBarTime(data),
89  Open = open,
90  High = high,
91  Low = low,
92  Close = close,
93  Volume = volume
94  };
95  }
96  else
97  {
98  //Aggregate the working bar
99  workingBar.Close = close;
100  workingBar.Volume += volume;
101  if (low < workingBar.Low) workingBar.Low = low;
102  if (high > workingBar.High) workingBar.High = high;
103  }
104  }
105 
106  private static decimal GetNamedPropertyOrValueProperty(DynamicData data, string propertyName)
107  {
108  if (!data.HasProperty(propertyName))
109  {
110  return data.Value;
111  }
112  return data.GetProperty(propertyName).ConvertInvariant<decimal>();
113  }
114  }
115 }