Files
quantconnect--lean/Algorithm.Python/SecuritySessionRegressionAlgorithm.py
T
JosueNina f7ce35ae82
API Tests / build (push) Has been cancelled
Benchmarks / build (push) Has been cancelled
Build & Test Lean / build (push) Has been cancelled
Report Generator Tests / build (push) Has been cancelled
Regression Tests / build (push) Has been cancelled
Research Regression Tests / build (push) Has been cancelled
Syntax Tests / build (push) Has been cancelled
Python Virtual Environments / build (push) Has been cancelled
Track OHLCV for Current Trading Day (#8922)
* Implement Session class

* Use MarketHourAwareConsolidator inside Session

* Delete SecurityCacheSessionConfig

* Refactor session class

* Solve review comments

* Fix regression algorithms

* Add null check to AvailableDataTypes

* Update session class

* Solve new review comments

* Add new test for futures and update regression test to PEP8

* Add new regression algorithm for Session with futures

* Resolve review comments

* Add local time keeper to SecurityCache

* Scan on consolidator updates and time updates

* Add new regression algorithms and unit tests

* Add unit tests and improve comments

* Fix syntax test

* Solve review comments

* Add regression algorithms

* Resolve review comments

* Make SessionConsolidator implement IDataConsolidator

* Add caching to avoid recreating SessionBar instances on every access

* Solve review comments

* Make SessionConsolidator inherit from MarketHourAwareConsolidator

* Add unit test to verify Session preserves previous day values

* Update regression algorithms

* Solve review comments

* Resolve review comments

* Solve new review comments

* Add unit test for Session class

* Refactor SessionBar properties and Update method

* Use WorkingDataInstance reference instead of _workingData.Clone()

* Use a delegate to get the workingBar instance

* Fix bug in MarketHourAwareConsolidator

* Solve review comments

* Handle OI values

* Add regression algorithms for future contracts and options

* Solve review comments

* Refactor SessionConsolidator to inherit PeriodCountConsolidatorBase

* Prevent overlapping higher resolution data

* Update tests

* Solve review comments

* Minor tweaks
2025-09-19 12:25:24 -05:00

128 lines
5.2 KiB
Python

# 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.
from AlgorithmImports import *
### <summary>
### Regression algorithm to validate SecurityCache.Session functionality.
### Verifies that daily session bars (Open, High, Low, Close, Volume) are correctly
### </summary>
class SecuritySessionRegressionAlgorithm(QCAlgorithm):
def initialize(self):
self.initialize_security()
# Check initial session values
session = self.security.session
if session is None:
raise RegressionTestException("Security.Session is none")
if (session.open != 0
or session.high != 0
or session.low != 0
or session.close != 0
or session.volume != 0
or session.open_interest != 0):
raise RegressionTestException("Session should start with all zero values.")
self.security_was_removed = False
self.open = self.close = self.high = self.volume = 0
self.low = float('inf')
self.current_date = self.start_date
self.previous_session_bar = None
self.schedule.on(
self.date_rules.every_day(),
self.time_rules.after_market_close(self.security.symbol, 1),
self.validate_session_bars
)
def initialize_security(self):
self.set_start_date(2013, 10, 7)
self.set_end_date(2013, 10, 11)
self.security = self.add_equity("SPY", Resolution.HOUR)
def _are_equal(self, value1, value2):
tolerance = 1e-10
return abs(value1 - value2) <= tolerance
def validate_session_bars(self):
session = self.security.session
# At this point the data was consolidated (market close)
# Save previous session bar
self.previous_session_bar = {
'date': self.current_date,
'open': self.open,
'high': self.high,
'low': self.low,
'close': self.close,
'volume': self.volume
}
if self.security_was_removed:
self.previous_session_bar = None
self.security_was_removed = False
return
# Check current session values
if (not self._are_equal(session.open, self.open)
or not self._are_equal(session.high, self.high)
or not self._are_equal(session.low, self.low)
or not self._are_equal(session.close, self.close)
or not self._are_equal(session.volume, self.volume)):
raise RegressionTestException("Mismatch in current session bar (OHLCV)")
def is_within_market_hours(self, current_date_time):
market_open = self.security.exchange.hours.get_next_market_open(current_date_time.date(), False).time()
market_close = self.security.exchange.hours.get_next_market_close(current_date_time.date(), False).time()
current_time = current_date_time.time()
return market_open < current_time <= market_close
def on_data(self, data):
if not self.is_within_market_hours(data.time):
# Skip data outside market hours
return
# Accumulate data within regular market hours
# to later compare against the Session values
self.accumulate_session_data(data)
def accumulate_session_data(self, data):
symbol = self.security.symbol
if self.current_date.date() == data.time.date():
# Same trading day
if self.open == 0:
self.open = data[symbol].open
self.high = max(self.high, data[symbol].high)
self.low = min(self.low, data[symbol].low)
self.close = data[symbol].close
self.volume += data[symbol].volume
else:
# New trading day
if self.previous_session_bar is not None:
session = self.security.session
if (self.previous_session_bar['open'] != session[1].open
or self.previous_session_bar['high'] != session[1].high
or self.previous_session_bar['low'] != session[1].low
or self.previous_session_bar['close'] != session[1].close
or self.previous_session_bar['volume'] != session[1].volume):
raise RegressionTestException("Mismatch in previous session bar (OHLCV)")
# This is the first data point of the new session
self.open = data[symbol].open
self.close = data[symbol].close
self.high = data[symbol].high
self.low = data[symbol].low
self.volume = data[symbol].volume
self.current_date = data.time