Lean  $LEAN_TAG$
ClassicRangeConsolidator.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  /// This consolidator can transform a stream of <see cref="IBaseData"/> instances into a stream of <see cref="RangeBar"/>.
24  /// The difference between this consolidator and <see cref="RangeConsolidator"/>, is that this last one creates intermediate/
25  /// phantom RangeBar's (RangeBar's with zero volume) if the price rises up or falls down by above/below two times the range
26  /// size. Therefore, <see cref="RangeConsolidator"/> leaves no space between two adyacent RangeBar's since it always start
27  /// a new RangeBar one range above the last RangeBar's High value or one range below the last RangeBar's Low value, where
28  /// one range equals to one minimum price change.
29  /// </summary>
31  {
32  /// <summary>
33  /// Initializes a new instance of the <see cref="ClassicRangeConsolidator" /> class.
34  /// </summary>
35  /// <param name="range">The Range interval sets the range in which the price moves, which in turn initiates the formation of a new bar.
36  /// One range equals to one minimum price change, where this last value is defined depending of the RangeBar's symbol</param>
37  /// <param name="selector">Extracts the value from a data instance to be formed into a <see cref="RangeBar"/>. The default
38  /// value is (x => x.Value) the <see cref="IBaseData.Value"/> property on <see cref="IBaseData"/></param>
39  /// <param name="volumeSelector">Extracts the volume from a data instance. The default value is null which does
40  /// not aggregate volume per bar, except if the input is a TradeBar.</param>
42  int range,
43  Func<IBaseData, decimal> selector = null,
44  Func<IBaseData, decimal> volumeSelector = null)
45  : base(range, selector, volumeSelector)
46  {
47  }
48 
49  /// <summary>
50  /// Initializes a new instance of the <see cref="RangeConsolidator" /> class.
51  /// </summary>
52  /// <param name="range">The Range interval sets the range in which the price moves, which in turn initiates the formation of a new bar.
53  /// One range equals to one minimum price change, where this last value is defined depending of the RangeBar's symbol</param>
54  /// <param name="selector">Extracts the value from a data instance to be formed into a <see cref="RangeBar"/>. The default
55  /// value is (x => x.Value) the <see cref="IBaseData.Value"/> property on <see cref="IBaseData"/></param>
56  /// <param name="volumeSelector">Extracts the volume from a data instance. The default value is null which does
57  /// not aggregate volume per bar.</param>
58  public ClassicRangeConsolidator(int range,
59  PyObject selector,
60  PyObject volumeSelector = null)
61  : base(range, selector, volumeSelector)
62  {
63  }
64 
65  /// <summary>
66  /// Updates the current RangeBar being created with the given data.
67  /// Additionally, if it's the case, it consolidates the current RangeBar
68  /// </summary>
69  /// <param name="time">Time of the given data</param>
70  /// <param name="currentValue">Value of the given data</param>
71  /// <param name="volume">Volume of the given data</param>
72  protected override void UpdateBar(DateTime time, decimal currentValue, decimal volume)
73  {
74  CurrentBar.Update(time, currentValue, volume);
75 
76  if (CurrentBar.IsClosed)
77  {
79  CurrentBar = null;
80  }
81  }
82  }
83 }