/*
* 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.Indicators;
using QuantConnect.Securities;
using Common.Data.Consolidators;
namespace QuantConnect.Data.Market
{
///
/// Provides a rolling window of with size 2,
/// where [0] contains the current session values in progress (OHLCV + OpenInterest),
/// and [1] contains the fully consolidated data of the previous trading day.
///
public class Session : RollingWindow, IBar
{
private readonly Symbol _symbol;
private readonly TickType _tickType;
private readonly SecurityExchangeHours _exchangeHours;
private SessionConsolidator _consolidator;
///
/// Opening price of the session
///
public decimal Open => _consolidator?.WorkingInstance.Open ?? 0;
///
/// High price of the session
///
public decimal High => _consolidator?.WorkingInstance.High ?? 0;
///
/// Low price of the session
///
public decimal Low => _consolidator?.WorkingInstance.Low ?? 0;
///
/// Closing price of the session
///
public decimal Close => _consolidator?.WorkingInstance.Close ?? 0;
///
/// Volume traded during the session
///
public decimal Volume => _consolidator?.WorkingInstance.Volume ?? 0;
///
/// Open Interest of the session
///
public decimal OpenInterest => _consolidator?.WorkingInstance.OpenInterest ?? 0;
///
/// The symbol of the session
///
public Symbol Symbol => _symbol;
///
/// The end time of the session
///
public DateTime EndTime => _consolidator?.WorkingInstance.EndTime ?? default;
///
/// Gets the size of this window
///
public override int Size
{
set
{
base.Size = value;
TryInitialize();
}
}
///
/// Initializes a new instance of the class
///
/// The tick type to use
/// The exchange hours
/// The symbol
/// The number of items to hold
public Session(TickType tickType, SecurityExchangeHours exchangeHours, Symbol symbol, int size = 0)
: base(size)
{
_symbol = symbol;
_tickType = tickType;
_exchangeHours = exchangeHours;
TryInitialize();
}
///
/// Updates the session with new market data
///
/// The new data to update the session with
public void Update(BaseData data)
{
_consolidator?.Update(data);
}
private void OnConsolidated(object sender, IBaseData consolidated)
{
// Finished current trading day
// Add the new working session bar at [0], this will shift the previous trading day's bar to [1]
Add(_consolidator.WorkingInstance);
}
///
/// Scans the consolidator to see if it should emit a bar due to time passing
///
public void Scan(DateTime currentLocalTime)
{
// Delegates the scan decision to the underlying consolidator.
_consolidator?.ValidateAndScan(currentLocalTime);
}
///
/// Resets the session
///
public override void Reset()
{
if (_consolidator != null)
{
base.Reset();
_consolidator.Reset();
// We need to add the working session bar at [0]
Add(_consolidator.WorkingInstance);
}
}
///
/// Returns a string representation of current session bar with OHLCV and OpenInterest values formatted.
/// Example: "O: 101.00 H: 112.00 L: 95.00 C: 110.00 V: 1005.00 OI: 12"
///
public override string ToString()
{
if (_consolidator != null)
{
return _consolidator.WorkingInstance.ToString();
}
return string.Empty;
}
private void TryInitialize()
{
if (base.Size > 0 && _consolidator == null)
{
_consolidator = new SessionConsolidator(_exchangeHours, _tickType, _symbol);
_consolidator.DataConsolidated += OnConsolidated;
Add(_consolidator.WorkingInstance);
}
}
}
}