Lean  $LEAN_TAG$
TickQuoteBarConsolidator.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  /// Consolidates ticks into quote bars. This consolidator ignores trade ticks
25  /// </summary>
27  {
28  /// <summary>
29  /// Initializes a new instance of the <see cref="TickQuoteBarConsolidator"/> class
30  /// </summary>
31  /// <param name="period">The minimum span of time before emitting a consolidated bar</param>
32  public TickQuoteBarConsolidator(TimeSpan period)
33  : base(period)
34  {
35  }
36 
37  /// <summary>
38  /// Initializes a new instance of the <see cref="TickQuoteBarConsolidator"/> class
39  /// </summary>
40  /// <param name="maxCount">The number of pieces to accept before emitting a consolidated bar</param>
41  public TickQuoteBarConsolidator(int maxCount)
42  : base(maxCount)
43  {
44  }
45 
46  /// <summary>
47  /// Initializes a new instance of the <see cref="TickQuoteBarConsolidator"/> class
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 TickQuoteBarConsolidator(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 TickQuoteBarConsolidator(Func<DateTime, CalendarInfo> func)
61  : base(func)
62  {
63  }
64 
65  /// <summary>
66  /// Initializes a new instance of the <see cref="TickQuoteBarConsolidator"/> class
67  /// </summary>
68  /// <param name="pyfuncobj">Python function object that defines the start time of a consolidated data</param>
69  public TickQuoteBarConsolidator(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.Quote;
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 consolidated bar</param>
89  /// <param name="data">The new data</param>
90  protected override void AggregateBar(ref QuoteBar workingBar, Tick data)
91  {
92  if (workingBar == null)
93  {
94  workingBar = new QuoteBar(GetRoundedBarTime(data), data.Symbol, null, decimal.Zero, null, decimal.Zero, Period);
95 
96  // open ask and bid should match previous close ask and bid
97  if (Consolidated != null)
98  {
99  // note that we will only fill forward previous close ask and bid when a new data point comes in and we generate a new working bar which is not a fill forward bar
100  var previous = Consolidated as QuoteBar;
101  workingBar.Update(decimal.Zero, previous.Bid?.Close ?? decimal.Zero, previous.Ask?.Close ?? decimal.Zero, decimal.Zero, previous.LastBidSize, previous.LastAskSize);
102  }
103  }
104 
105  // update the bid and ask
106  workingBar.Update(decimal.Zero, data.BidPrice, data.AskPrice, decimal.Zero, data.BidSize, data.AskSize);
107  if (!Period.HasValue) workingBar.EndTime = GetRoundedBarTime(data.EndTime);
108  }
109  }
110 }