# 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.add_security_initializer(self.initialize_session_tracking)self.initialize_security()# Check initial session valuessession = self.security.sessionif session is None:raise RegressionTestException("Security.Session is none")if (session.open != 0or session.high != 0or session.low != 0or session.close != 0or session.volume != 0or session.open_interest != 0):raise RegressionTestException("Session should start with all zero values.")self.security_was_removed = Falseself.open = self.close = self.high = self.volume = 0self.low = float('inf')self.current_date = self.start_dateself.previous_session_bar = Noneself.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 initialize_session_tracking(self, security):# activate session trackingsecurity.session.size = 3def _are_equal(self, value1, value2):tolerance = 1e-10return abs(value1 - value2) <= tolerancedef validate_session_bars(self):session = self.security.session# At this point the data was consolidated (market close)# Save previous session barself.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 = Noneself.security_was_removed = Falsereturn# Check current session valuesif (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_closedef on_data(self, data):if not self.is_within_market_hours(data.time):# Skip data outside market hoursreturn# Accumulate data within regular market hours# to later compare against the Session valuesself.accumulate_session_data(data)def accumulate_session_data(self, data):symbol = self.security.symbolif self.current_date.date() == data.time.date():# Same trading dayif self.open == 0:self.open = data[symbol].openself.high = max(self.high, data[symbol].high)self.low = min(self.low, data[symbol].low)self.close = data[symbol].closeself.volume += data[symbol].volumeelse:# New trading dayif self.previous_session_bar is not None:session = self.security.sessionif (self.previous_session_bar['open'] != session[1].openor self.previous_session_bar['high'] != session[1].highor self.previous_session_bar['low'] != session[1].lowor self.previous_session_bar['close'] != session[1].closeor 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 sessionself.open = data[symbol].openself.close = data[symbol].closeself.high = data[symbol].highself.low = data[symbol].lowself.volume = data[symbol].volumeself.current_date = data.time
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。