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:
Louis Szeto
2025-04-14 20:43:03 +08:00
committed by GitHub
parent fe46e5ec3b
commit 020cf013df
55 changed files with 717 additions and 738 deletions
@@ -18,8 +18,8 @@ from Orders.Slippage.VolumeShareSlippageModel import VolumeShareSlippageModel
### Example algorithm implementing VolumeShareSlippageModel.
### </summary>
class VolumeShareSlippageModelAlgorithm(QCAlgorithm):
longs = []
shorts = []
_longs = []
_shorts = []
def initialize(self) -> None:
self.set_start_date(2020, 11, 29)
@@ -27,27 +27,24 @@ class VolumeShareSlippageModelAlgorithm(QCAlgorithm):
# To set the slippage model to limit to fill only 30% volume of the historical volume, with 5% slippage impact.
self.set_security_initializer(lambda security: security.set_slippage_model(VolumeShareSlippageModel(0.3, 0.05)))
# Create SPY symbol to explore its constituents.
spy = Symbol.create("SPY", SecurityType.EQUITY, Market.USA)
self.universe_settings.resolution = Resolution.DAILY
# Add universe to trade on the most and least weighted stocks among SPY constituents.
self.add_universe(self.universe.etf(spy, universe_filter_func=self.selection))
self.add_universe(self.universe.etf("SPY", universe_filter_func=self.selection))
def selection(self, constituents: List[ETFConstituentUniverse]) -> List[Symbol]:
def selection(self, constituents: list[ETFConstituentUniverse]) -> list[Symbol]:
sorted_by_weight = sorted(constituents, key=lambda c: c.weight)
# Add the 10 most weighted stocks to the universe to long later.
self.longs = [c.symbol for c in sorted_by_weight[-10:]]
self._longs = [c.symbol for c in sorted_by_weight[-10:]]
# Add the 10 least weighted stocks to the universe to short later.
self.shorts = [c.symbol for c in sorted_by_weight[:10]]
self._shorts = [c.symbol for c in sorted_by_weight[:10]]
return self.longs + self.shorts
return self._longs + self._shorts
def on_data(self, slice: Slice) -> None:
# Equally invest into the selected stocks to evenly dissipate capital risk.
# Dollar neutral of long and short stocks to eliminate systematic risk, only capitalize the popularity gap.
targets = [PortfolioTarget(symbol, 0.05) for symbol in self.longs]
targets += [PortfolioTarget(symbol, -0.05) for symbol in self.shorts]
targets = [PortfolioTarget(symbol, 0.05) for symbol in self._longs]
targets += [PortfolioTarget(symbol, -0.05) for symbol in self._shorts]
# Liquidate the ones not being the most and least popularity stocks to release fund for higher expected return trades.
self.set_holdings(targets, liquidate_existing_holdings=True)