/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using QuantConnect.Data.Market;
using Python.Runtime;
namespace QuantConnect.Data.Consolidators
{
///
/// This consolidator can transform a stream of instances into a stream of .
/// The difference between this consolidator and , is that this last one creates intermediate/
/// phantom RangeBar's (RangeBar's with zero volume) if the price rises up or falls down by above/below two times the range
/// size. Therefore, leaves no space between two adyacent RangeBar's since it always start
/// a new RangeBar one range above the last RangeBar's High value or one range below the last RangeBar's Low value, where
/// one range equals to one minimum price change.
///
public class ClassicRangeConsolidator : RangeConsolidator
{
///
/// Initializes a new instance of the class.
///
/// The Range interval sets the range in which the price moves, which in turn initiates the formation of a new bar.
/// One range equals to one minimum price change, where this last value is defined depending of the RangeBar's symbol
/// Extracts the value from a data instance to be formed into a . The default
/// value is (x => x.Value) the property on
/// Extracts the volume from a data instance. The default value is null which does
/// not aggregate volume per bar, except if the input is a TradeBar.
public ClassicRangeConsolidator(
int range,
Func selector = null,
Func volumeSelector = null)
: base(range, selector, volumeSelector)
{
}
///
/// Initializes a new instance of the class.
///
/// The Range interval sets the range in which the price moves, which in turn initiates the formation of a new bar.
/// One range equals to one minimum price change, where this last value is defined depending of the RangeBar's symbol
/// Extracts the value from a data instance to be formed into a . The default
/// value is (x => x.Value) the property on
/// Extracts the volume from a data instance. The default value is null which does
/// not aggregate volume per bar.
public ClassicRangeConsolidator(int range,
PyObject selector,
PyObject volumeSelector = null)
: base(range, selector, volumeSelector)
{
}
///
/// Updates the current RangeBar being created with the given data.
/// Additionally, if it's the case, it consolidates the current RangeBar
///
/// Time of the given data
/// Value of the given data
/// Volume of the given data
protected override void UpdateBar(DateTime time, decimal currentValue, decimal volume)
{
CurrentBar.Update(time, currentValue, volume);
if (CurrentBar.IsClosed)
{
OnDataConsolidated(CurrentBar);
CurrentBar = null;
}
}
}
}