# 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 *from collections import deque### <summary>### Demonstrates how to create a custom indicator and register it for automatic updated### </summary>### <meta name="tag" content="indicators" />### <meta name="tag" content="indicator classes" />### <meta name="tag" content="custom indicator" />class CustomIndicatorAlgorithm(QCAlgorithm):def initialize(self) -> None:self.set_start_date(2013,10,7)self.set_end_date(2013,10,11)self.add_equity("SPY", Resolution.SECOND)# Create a QuantConnect indicator and a python custom indicator for comparisonself._sma = self.sma("SPY", 60, Resolution.MINUTE)self._custom = CustomSimpleMovingAverage('custom', 60)# The python custom class must inherit from PythonIndicator to enable Updated event handlerself._custom.updated += self.custom_updatedself._custom_window = RollingWindow(5)self.register_indicator("SPY", self._custom, Resolution.MINUTE)self.plot_indicator('CSMA', self._custom)def custom_updated(self, sender: object, updated: IndicatorDataPoint) -> None:self._custom_window.add(updated)def on_data(self, data: Slice) -> None:if not self.portfolio.invested:self.set_holdings("SPY", 1)if self.time.second == 0:self.log(f" sma -> IsReady: {self._sma.is_ready}. Value: {self._sma.current.value}")self.log(f"custom -> IsReady: {self._custom.is_ready}. Value: {self._custom.value}")# Regression test: test fails with an early quitdiff = abs(self._custom.value - self._sma.current.value)if diff > 1e-10:self.quit(f"Quit: indicators difference is {diff}")def on_end_of_algorithm(self) -> None:for item in self._custom_window:self.log(f'{item}')# Python implementation of SimpleMovingAverage.# Represents the traditional simple moving average indicator (SMA).class CustomSimpleMovingAverage(PythonIndicator):def __init__(self, name: str, period: int) -> None:super().__init__()self.name = nameself.value = 0self._queue = deque(maxlen=period)# Update method is mandatorydef update(self, input: IndicatorDataPoint) -> bool:self._queue.appendleft(input.value)count = len(self._queue)self.value = np.sum(self._queue) / countreturn count == self._queue.maxlen
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。