/* * 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 { /// /// Type capable of consolidating trade bars from any base data instance /// public class BaseDataConsolidator : TradeBarConsolidatorBase { /// /// Create a new TickConsolidator for the desired resolution /// /// The resolution desired /// A consolidator that produces data on the resolution interval public static BaseDataConsolidator FromResolution(Resolution resolution) { return new BaseDataConsolidator(resolution.ToTimeSpan()); } /// /// Creates a consolidator to produce a new 'TradeBar' representing the period /// /// The minimum span of time before emitting a consolidated bar public BaseDataConsolidator(TimeSpan period) : base(period) { } /// /// Creates a consolidator to produce a new 'TradeBar' representing the last count pieces of data /// /// The number of pieces to accept before emitting a consolidated bar public BaseDataConsolidator(int maxCount) : base(maxCount) { } /// /// Creates a consolidator to produce a new 'TradeBar' representing the last count pieces of data or the period, whichever comes first /// /// The number of pieces to accept before emitting a consolidated bar /// The minimum span of time before emitting a consolidated bar public BaseDataConsolidator(int maxCount, TimeSpan period) : base(maxCount, period) { } /// /// Initializes a new instance of the class /// /// Func that defines the start time of a consolidated data public BaseDataConsolidator(Func func) : base(func) { } /// /// Initializes a new instance of the class /// /// Func that defines the start time of a consolidated data public BaseDataConsolidator(PyObject pyfuncobj) : base(pyfuncobj) { } /// /// Aggregates the new 'data' into the 'workingBar'. The 'workingBar' will be /// null following the event firing /// /// The bar we're building, null if the event was just fired and we're starting a new trade bar /// The new data protected override void AggregateBar(ref TradeBar workingBar, BaseData data) { if (workingBar == null) { workingBar = new TradeBar { Symbol = data.Symbol, Time = GetRoundedBarTime(data.Time), Close = data.Value, High = data.Value, Low = data.Value, Open = data.Value, DataType = data.DataType, Value = data.Value }; } else { //Aggregate the working bar workingBar.Close = data.Value; if (data.Value < workingBar.Low) workingBar.Low = data.Value; if (data.Value > workingBar.High) workingBar.High = data.Value; } } } }