I want to customize the Universal Selection model, is below code correct ? main.py will call universal selection
main.py
from AlgorithmImports import *
import parameters as parameters
from Logger import Logger
from UniverseSelection import UniverseSelection
# endregion
class DailyBreakOut(QCAlgorithm):
def initialize(self):
self.SetStartDate(2023, 1, 1)
self.SetCash(parameters.INITIAL_CASH)
self.logger = Logger(self)
self.add_universe_selection(UniverseSelection(self, self.logger))
def on_data(self, data):
# Your on_data logic here
pass
UniverseSelection.py
class UniverseSelection(UniverseSelectionModel):
def __init__(self, algorithm: QCAlgorithm, logger: Logger):
super().__init__()
self.algorithm = algorithm
self.logger = logger
self.logger.log("Starting UniverseSelection initialization...")
try:
self.history = {}
self.moving_averages_200 = {}
self.moving_averages_20 = {}
self.previous_close = {}
self.atr = {}
self.volatility = {}
self.avg_volatility = {}
self.logger.log("Finished UniverseSelection initialization.")
except BaseException as e:
self.logger.log(f"Error during UniverseSelection initialization: {str(e)}")
raise e
def CreateUniverses(self) -> List[Universe]:
# This method must return one or more universes
return [self.algorithm.AddUniverse(self.CoarseSelectionFunction)]
def CoarseSelectionFunction(self, coarse):
selected: List[str] = []
for stock in coarse:
if stock.Price > params.PRICE_THRESHOLD and stock.DollarVolume > params.DOLLAR_VOLUME_THRESHOLD:
selected.append(stock.Symbol)
# Initialize the historical data if not already done
if stock.Symbol not in self.history:
self.InitializeIndicators(stock.Symbol)
return selected
Mia Alissi
The code provided by Dharmesh has several issues that need to be addressed:
Import Errors: The modules
parameters,Logger, andUniverseSelectioncannot be found. Ensure these modules are correctly defined and available in your project.Attribute Errors: In
main.py, the methodsSetStartDateandSetCashshould be renamed toset_start_dateandset_cashto follow Python's naming conventions for methods in QuantConnect.Indentation Error: In
UniverseSelection.py, the methodCoarseSelectionFunctionhas an indentation issue. Ensure that the method definition is properly aligned with the other methods in the class.Here's how you can correct these issues:
main.py
UniverseSelection.py
Make sure that the
parameters,Logger, andUniverseSelectionmodules are correctly defined and available in your project.Sources: Stock Selection Strategy Based On Fundamental Factors, Momentum And State Of Market Filters, Custom Data Universe Regression Algorithm, Asset Growth Effect, January Effect In Stocks
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Dharmesh Khalasi
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!