Fix bug/syntax in python examples (#8658)
* CustomDataRegressionAlgorithm * DescendingCustomDataObjectStoreRegressionAlgorithm * CustomDataPropertiesRegressionAlgorithm * DateTime -> should be datetime * KerasNeuralNetworkAlgorithm * OptionIndicatorsMirrorContractsRegressionAlgorithm * BybitCustomDataCryptoRegressionAlgorithm * DropboxBaseDataUniverseSelectionAlgorithm * UserDefinedUniverseAlgorithm * CompleteOrderTagUpdateAlgorithm * BasicTemplateOptionEquityStrategyAlgorithm hint * ETFConstituentUniverseFrameworkRegressionAlgorithm * FutureStopMarketOrderOnExtendedHoursRegressionAlgorithm * SecurityDynamicPropertyPythonClassAlgorithm * hint * hinting * CallbackCommandRegressionAlgorithm * CustomWarmUpPeriodIndicatorAlgorithm * CrunchDAOSignalExportDemonstrationAlgorithm * ExpiryHelperAlphaModelFrameworkAlgorithm * ClassicRenkoConsolidatorAlgorithm * SmaCrossUniverseSelectionAlgorithm * PEP8 Fix: Assigning to a Method * SliceGetByTypeRegressionAlgorithm * MarketOnCloseOrderBufferExtendedMarketHoursRegressionAlgorithm * MarketOnCloseOrderBufferRegressionAlgorithm * CustomIndicatorAlgorithm * ScheduledQueuingAlgorithm * ComboOrdersFillModelAlgorithm * CustomIndicatorWithExtensionAlgorithm * IndicatorWithRenkoBarsRegressionAlgorithm * CoarseFineOptionUniverseChainRegressionAlgorithm * NumeraiSignalExportDemonstrationAlgorithm * DropboxUniverseSelectionAlgorithm * WeeklyUniverseSelectionRegressionAlgorithm * AutoRegressiveIntegratedMovingAverageRegressionAlgorithm * DropboxBaseDataUniverseSelectionAlgorithm * IronCondorStrategyAlgorithm * LongAndShortButterflyPutStrategiesAlgorithm * FutureStopMarketOrderOnExtendedHoursRegressionAlgorithm * LongAndShortCallCalendarSpreadStrategiesAlgorithm * KerasNeuralNetworkAlgorithm * LongAndShortPutCalendarSpreadStrategiesAlgorithm * OptionPriceModelForOptionStylesBaseRegressionAlgorithm * TensorFlowNeuralNetworkAlgorithm * MarketOnCloseOrderBufferRegressionAlgorithm * MarketOnCloseOrderBufferExtendedMarketHoursRegressionAlgorithm * typing * ComboOrderTicketDemoAlgorithm * PytorchNeuralNetworkAlgorithm * MultipleSymbolConsolidationAlgorithm * fixes * revert getattr mypy syntax * address peer review * Addresses Peer-Review --------- Co-authored-by: Alexandre Catarino <AlexCatarino@users.noreply.github.com>
This commit is contained in:
@@ -21,54 +21,54 @@ from collections import deque
|
||||
### <meta name="tag" content="indicator classes" />
|
||||
### <meta name="tag" content="custom indicator" />
|
||||
class CustomIndicatorAlgorithm(QCAlgorithm):
|
||||
def initialize(self):
|
||||
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 comparison
|
||||
self._sma = self.sma("SPY", 60, Resolution.MINUTE)
|
||||
self.custom = CustomSimpleMovingAverage('custom', 60)
|
||||
self._custom = CustomSimpleMovingAverage('custom', 60)
|
||||
|
||||
# The python custom class must inherit from PythonIndicator to enable Updated event handler
|
||||
self.custom.updated += self.custom_updated
|
||||
self._custom.updated += self._custom_updated
|
||||
|
||||
self.custom_window = RollingWindow[IndicatorDataPoint](5)
|
||||
self.register_indicator("SPY", self.custom, Resolution.MINUTE)
|
||||
self.plot_indicator('CSMA', self.custom)
|
||||
self._custom_window = RollingWindow[IndicatorDataPoint](5)
|
||||
self.register_indicator("SPY", self._custom, Resolution.MINUTE)
|
||||
self.plot_indicator('CSMA', self._custom)
|
||||
|
||||
def custom_updated(self, sender, updated):
|
||||
self.custom_window.add(updated)
|
||||
def custom_updated(self, sender: object, updated: IndicatorDataPoint) -> None:
|
||||
self._custom_window.add(updated)
|
||||
|
||||
def on_data(self, data):
|
||||
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}")
|
||||
self.log(f"custom -> IsReady: {self._custom.is_ready}. Value: {self._custom.value}")
|
||||
|
||||
# Regression test: test fails with an early quit
|
||||
diff = abs(self.custom.value - self._sma.current.value)
|
||||
diff = 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):
|
||||
for item in self.custom_window:
|
||||
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, period):
|
||||
def __init__(self, name: str, period: int) -> None:
|
||||
super().__init__()
|
||||
self.name = name
|
||||
self.value = 0
|
||||
self.queue = deque(maxlen=period)
|
||||
self._queue = deque(maxlen=period)
|
||||
|
||||
# Update method is mandatory
|
||||
def update(self, input):
|
||||
self.queue.appendleft(input.value)
|
||||
count = len(self.queue)
|
||||
self.value = np.sum(self.queue) / count
|
||||
return count == self.queue.maxlen
|
||||
def update(self, input: IndicatorDataPoint) -> bool:
|
||||
self._queue.appendleft(input.value)
|
||||
count = len(self._queue)
|
||||
self.value = np.sum(self._queue) / count
|
||||
return count == self._queue.maxlen
|
||||
|
||||
Reference in New Issue
Block a user