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:
@@ -14,45 +14,47 @@
|
||||
from AlgorithmImports import *
|
||||
from collections import deque
|
||||
|
||||
import numpy as np
|
||||
|
||||
### <summary>
|
||||
### Algorithm asserting that security dynamic properties keep Python references to the Python class they are instances of,
|
||||
### specifically when this class is a subclass of a C# class.
|
||||
### </summary>
|
||||
class SecurityDynamicPropertyPythonClassAlgorithm(QCAlgorithm):
|
||||
def initialize(self):
|
||||
def initialize(self) -> None:
|
||||
self.set_start_date(2013, 10, 7)
|
||||
self.set_end_date(2013, 10, 7)
|
||||
|
||||
self.spy = self.add_equity("SPY", Resolution.MINUTE)
|
||||
self._spy = self.add_equity("SPY", Resolution.MINUTE)
|
||||
|
||||
custom_sma = CustomSimpleMovingAverage('custom', 60)
|
||||
self.spy.custom_sma = custom_sma
|
||||
custom_sma.security = self.spy
|
||||
self._spy.custom_sma = custom_sma
|
||||
custom_sma.security = self._spy
|
||||
|
||||
self.register_indicator(self.spy.symbol, self.spy.custom_sma, Resolution.MINUTE)
|
||||
self.register_indicator(self._spy.symbol, self._spy.custom_sma, Resolution.MINUTE)
|
||||
|
||||
def on_warmup_finished(self) -> None:
|
||||
if type(self.spy.custom_sma) != CustomSimpleMovingAverage:
|
||||
if type(self._spy.custom_sma) != CustomSimpleMovingAverage:
|
||||
raise AssertionError("spy.custom_sma is not an instance of CustomSimpleMovingAverage")
|
||||
|
||||
if self.spy.custom_sma.security is None:
|
||||
if not self._spy.custom_sma.security:
|
||||
raise AssertionError("spy.custom_sma.security is None")
|
||||
else:
|
||||
self.debug(f"spy.custom_sma.security.symbol: {self.spy.custom_sma.security.symbol}")
|
||||
self.debug(f"spy.custom_sma.security.symbol: {self._spy.custom_sma.security.symbol}")
|
||||
|
||||
def on_data(self, slice: Slice) -> None:
|
||||
if self.spy.custom_sma.is_ready:
|
||||
self.debug(f"CustomSMA: {self.spy.custom_sma.current.value}")
|
||||
if self._spy.custom_sma.is_ready:
|
||||
self.debug(f"CustomSMA: {self._spy.custom_sma.current.value}")
|
||||
|
||||
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)
|
||||
|
||||
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