# 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>### Universe Selection regression algorithm simulates an edge case. In one week, Google listed two new symbols, delisted one of them and changed tickers.### </summary>### <meta name="tag" content="regression test" />class UniverseSelectionRegressionAlgorithm(QCAlgorithm):def initialize(self):self.set_start_date(2014,3,22) #Set Start Dateself.set_end_date(2014,4,7) #Set End Dateself.set_cash(100000) #Set Strategy Cash# Find more symbols here: http://quantconnect.com/data# security that exists with no mappingsself.add_equity("SPY", Resolution.DAILY)# security that doesn't exist until half way in backtest (comes in as GOOCV)self.add_equity("GOOG", Resolution.DAILY)self.universe_settings.resolution = Resolution.DAILYself.add_universe(self.coarse_selection_function)self.delisted_symbols = []self.changes = Nonedef coarse_selection_function(self, coarse):return [ c.symbol for c in coarse if c.symbol.value == "GOOG" or c.symbol.value == "GOOCV" or c.symbol.value == "GOOAV" or c.symbol.value == "GOOGL" ]def on_data(self, data):if self.transactions.orders_count == 0:self.market_order("SPY", 100)for kvp in data.delistings:self.delisted_symbols.append(kvp.key)if self.changes is None:returnif not all(data.bars.contains_key(x.symbol) for x in self.changes.added_securities):returnfor security in self.changes.added_securities:self.log("{0}: Added Security: {1}".format(self.time, security.symbol))self.market_on_open_order(security.symbol, 100)for security in self.changes.removed_securities:self.log("{0}: Removed Security: {1}".format(self.time, security.symbol))if security.symbol not in self.delisted_symbols:self.log("Not in delisted: {0}:".format(security.symbol))self.market_on_open_order(security.symbol, -100)self.changes = Nonedef on_securities_changed(self, changes):self.changes = changesdef on_order_event(self, orderEvent):if orderEvent.status == OrderStatus.SUBMITTED:self.log("{0}: Submitted: {1}".format(self.time, self.transactions.get_order_by_id(orderEvent.order_id)))if orderEvent.status == OrderStatus.FILLED:self.log("{0}: Filled: {1}".format(self.time, self.transactions.get_order_by_id(orderEvent.order_id)))
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。