Lean  $LEAN_TAG$
OpenInterestConsolidator.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 open interest
24  /// </summary>
25  public class OpenInterestConsolidator : PeriodCountConsolidatorBase<Tick, OpenInterest>
26  {
27  /// <summary>
28  /// Create a new OpenInterestConsolidator 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 OpenInterestConsolidator(resolution.ToTimeSpan());
35  }
36 
37  /// <summary>
38  /// Creates a consolidator to produce a new 'OpenInterest' representing the period
39  /// </summary>
40  /// <param name="period">The minimum span of time before emitting a consolidated bar</param>
41  public OpenInterestConsolidator(TimeSpan period)
42  : base(period)
43  {
44  }
45 
46  /// <summary>
47  /// Creates a consolidator to produce a new 'OpenInterest' 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 OpenInterestConsolidator(int maxCount)
51  : base(maxCount)
52  {
53  }
54 
55  /// <summary>
56  /// Creates a consolidator to produce a new 'OpenInterest' 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 OpenInterestConsolidator(int maxCount, TimeSpan period)
61  : base(maxCount, period)
62  {
63  }
64 
65  /// <summary>
66  /// Creates a consolidator to produce a new 'OpenInterest'
67  /// </summary>
68  /// <param name="func">Func that defines the start time of a consolidated data</param>
69  public OpenInterestConsolidator(Func<DateTime, CalendarInfo> func)
70  : base(func)
71  {
72  }
73 
74  /// <summary>
75  /// Creates a consolidator to produce a new 'OpenInterest'
76  /// </summary>
77  /// <param name="pyfuncobj">Python function object that defines the start time of a consolidated data</param>
78  public OpenInterestConsolidator(PyObject pyfuncobj)
79  : base(pyfuncobj)
80  {
81  }
82 
83 
84  /// <summary>
85  /// Determines whether or not the specified data should be processed
86  /// </summary>
87  /// <param name="data">The data to check</param>
88  /// <returns>True if the consolidator should process this data, false otherwise</returns>
89  protected override bool ShouldProcess(Tick data)
90  {
91  return data.TickType == TickType.OpenInterest;
92  }
93 
94  /// <summary>
95  /// Aggregates the new 'data' into the 'workingBar'. The 'workingBar' will be
96  /// null following the event firing
97  /// </summary>
98  /// <param name="workingBar">The bar we're building, null if the event was just fired and we're starting a new OI bar</param>
99  /// <param name="data">The new data</param>
100  protected override void AggregateBar(ref OpenInterest workingBar, Tick data)
101  {
102  if (workingBar == null)
103  {
104  workingBar = new OpenInterest
105  {
106  Symbol = data.Symbol,
107  Time = GetRoundedBarTime(data),
108  Value = data.Value
109  };
110 
111  }
112  else
113  {
114  //Update the working bar
115  workingBar.Value = data.Value;
116  }
117  }
118  }
119 }