# 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>### Example algorithm showing that Slice, Securities and Portfolio behave as a Python Dictionary### </summary>class PythonDictionaryFeatureRegressionAlgorithm(QCAlgorithm):'''Example algorithm showing that Slice, Securities and Portfolio behave as a Python Dictionary'''def initialize(self):self.set_start_date(2013,10, 7) #Set Start Dateself.set_end_date(2013,10,11) #Set End Dateself.set_cash(100000) #Set Strategy Cashself.spy_symbol = self.add_equity("SPY").symbolself.ibm_symbol = self.add_equity("IBM").symbolself.aig_symbol = self.add_equity("AIG").symbolself.aapl_symbol = Symbol.create("AAPL", SecurityType.EQUITY, Market.USA)date_rules = self.date_rules.on(2013, 10, 7)self.schedule.on(date_rules, self.time_rules.at(13, 0), self.test_securities_dictionary)self.schedule.on(date_rules, self.time_rules.at(14, 0), self.test_portfolio_dictionary)self.schedule.on(date_rules, self.time_rules.at(15, 0), self.test_slice_dictionary)def test_slice_dictionary(self):slice = self.current_slicesymbols = ', '.join([f'{x}' for x in slice.keys()])slice_data = ', '.join([f'{x}' for x in slice.values()])slice_bars = ', '.join([f'{x}' for x in slice.bars.values()])if "SPY" not in slice:raise AssertionError('SPY (string) is not in Slice')if self.spy_symbol not in slice:raise AssertionError('SPY (Symbol) is not in Slice')spy = slice.get(self.spy_symbol)if spy is None:raise AssertionError('SPY is not in Slice')for symbol, bar in slice.bars.items():self.plot(symbol, 'Price', bar.close)def test_securities_dictionary(self):symbols = ', '.join([f'{x}' for x in self.securities.keys()])leverages = ', '.join([str(x.get_last_data()) for x in self.securities.values()])if "IBM" not in self.securities:raise AssertionError('IBM (string) is not in Securities')if self.ibm_symbol not in self.securities:raise AssertionError('IBM (Symbol) is not in Securities')ibm = self.securities.get(self.ibm_symbol)if ibm is None:raise AssertionError('ibm is None')aapl = self.securities.get(self.aapl_symbol)if aapl is not None:raise AssertionError('aapl is not None')for symbol, security in self.securities.items():self.plot(symbol, 'Price', security.price)def test_portfolio_dictionary(self):symbols = ', '.join([f'{x}' for x in self.portfolio.keys()])leverages = ', '.join([f'{x.symbol}: {x.leverage}' for x in self.portfolio.values()])if "AIG" not in self.securities:raise AssertionError('AIG (string) is not in Portfolio')if self.aig_symbol not in self.securities:raise AssertionError('AIG (Symbol) is not in Portfolio')aig = self.portfolio.get(self.aig_symbol)if aig is None:raise AssertionError('aig is None')aapl = self.portfolio.get(self.aapl_symbol)if aapl is not None:raise AssertionError('aapl is not None')for symbol, holdings in self.portfolio.items():msg = f'{symbol}: {holdings.leverage}'def on_end_of_algorithm(self):portfolio_copy = self.portfolio.copy()try:self.portfolio.clear() # Throws exceptionexcept Exception as e:self.debug(e)bar = self.securities.pop("SPY")length = len(self.securities)if length != 2:raise AssertionError(f'After popping SPY, Securities should have 2 elements, {length} found')securities_copy = self.securities.copy()self.securities.clear() # Does not throwdef on_data(self, data):'''on_data event is the primary entry point for your algorithm. Each new data point will be pumped in here.Arguments:data: Slice object keyed by symbol containing the stock data'''if not self.portfolio.invested:self.set_holdings("SPY", 1/3)self.set_holdings("IBM", 1/3)self.set_holdings("AIG", 1/3)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。