/* * 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 open interest /// public class OpenInterestConsolidator : PeriodCountConsolidatorBase { private bool _hourOrDailyConsolidation; // Keep track of the last input to detect hour or date change private Tick _lastInput; /// /// Create a new OpenInterestConsolidator for the desired resolution /// /// The resolution desired /// A consolidator that produces data on the resolution interval public static OpenInterestConsolidator FromResolution(Resolution resolution) { return new OpenInterestConsolidator(resolution.ToTimeSpan()); } /// /// Creates a consolidator to produce a new 'OpenInterest' representing the period /// /// The minimum span of time before emitting a consolidated bar public OpenInterestConsolidator(TimeSpan period) : base(period) { _hourOrDailyConsolidation = period >= Time.OneHour; } /// /// Creates a consolidator to produce a new 'OpenInterest' representing the last count pieces of data /// /// The number of pieces to accept before emitting a consolidated bar public OpenInterestConsolidator(int maxCount) : base(maxCount) { } /// /// Creates a consolidator to produce a new 'OpenInterest' 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 OpenInterestConsolidator(int maxCount, TimeSpan period) : base(maxCount, period) { } /// /// Creates a consolidator to produce a new 'OpenInterest' /// /// Func that defines the start time of a consolidated data public OpenInterestConsolidator(Func func) : base(func) { } /// /// Creates a consolidator to produce a new 'OpenInterest' /// /// Python function object that defines the start time of a consolidated data public OpenInterestConsolidator(PyObject pyfuncobj) : base(pyfuncobj) { } /// /// Determines whether or not the specified data should be processed /// /// The data to check /// True if the consolidator should process this data, false otherwise protected override bool ShouldProcess(Tick data) { return data.TickType == TickType.OpenInterest; } /// /// 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 OI bar /// The new data protected override void AggregateBar(ref OpenInterest workingBar, Tick data) { if (workingBar == null) { workingBar = new OpenInterest { Symbol = data.Symbol, Time = _hourOrDailyConsolidation ? data.EndTime : GetRoundedBarTime(data), Value = data.Value }; } else { //Update the working bar workingBar.Value = data.Value; // If we are consolidating hourly or daily, we need to update the time of the working bar // for the end time to match the last data point time if (_hourOrDailyConsolidation) { workingBar.Time = data.EndTime; } } } /// /// Updates this consolidator with the specified data. This method is /// responsible for raising the DataConsolidated event. /// It will check for date or hour change and force consolidation if needed. /// /// The new data for the consolidator public override void Update(Tick data) { if (_lastInput != null && _hourOrDailyConsolidation && // Detect hour or date change ((Period == Time.OneHour && data.EndTime.Hour != _lastInput.EndTime.Hour) || (Period == Time.OneDay && data.EndTime.Date != _lastInput.EndTime.Date))) { // Date or hour change, force consolidation, no need to wait for the whole period to pass. // Force consolidation by scanning at a time after the end of the period Scan(_lastInput.EndTime.Add(Period.Value + Time.OneSecond)); } base.Update(data); _lastInput = data; } } }