Hi, I'm following the bootcamp and I have a question.
It is about the code below
# region imports
from AlgorithmImports import *
# endregion
class T07LiquidUniverseSelection04BuildingOurPortfolio(QCAlgorithm):
filtered_by_price = None
def initialize(self):
# Locally Lean installs free sample data, to download more data please visit https://www.quantconnect.com/docs/v2/lean-cli/datasets/downloading-data
self.set_start_date(2019, 1,11) # Set Start Date
self.set_end_date(2019, 7, 1) # Set End Date
self.set_cash(100000) # Set Strategy Cash
self.add_universe(self.coarse_selection_filter)
self.universe_settings.resolution = Resolution.Daily
def coarse_selection_filter(self,coarse):
sorted_by_daily_dollar_volume = sorted(coarse, key=lambda c: c.dollar_volume, reverse=True)
symbols_by_price = [c.symbol for c in sorted_by_daily_dollar_volume if c.price > 10]
self.filtered_by_price =symbols_by_price[:8]
return self.filtered_by_price
def on_securities_changed(self, changes):
self.changes=changes
self.log(f"on_securities_changed({self.time}):: (changes)")
# self.log(f"{self.changes}")
for security in changes.removed_securities:
self.debug(f"{self.time}: Removed {security.symbol}")
if security.invested:
self.liquidate(security.symbol)
for security in changes.added_securities:
self.debug(f"{self.time}: Added {security.symbol}")
self.set_holdings(security.symbol, 0.1)
in this example, it seems to me that the code initialize my portfolio in on_securities_changed. However, when I look at the log.txt file after the backtest, I found a behavior I don't understand. When it add the equity from the last three lines, it doesn't print out the log message on top (self.log(f"on_securities_changed({self.time}):: (changes)")) before initializing my portfolio. This is hard to understand to me.
Mia Alissi
The
OnSecuritiesChanged
method is triggered when there are changes in the universe of securities, i.e., when securities are added or removed from the universe. In your code, you are logging the changes and then handling the added and removed securities.The
self.log(f"on_securities_changed({self.time}):: (changes)")
line is supposed to log the changes in the universe. However, you have put(changes)
inside the string, which means it will literally print "(changes)" instead of the actual changes. If you want to print the actual changes, you should useself.log(f"on_securities_changed({self.time}):: {changes}")
.As for the securities being added without the log message appearing, it could be due to the order of operations in your code. The
OnSecuritiesChanged
method is called whenever securities are added or removed. If a security is added and immediately invested in (viaself.set_holdings(security.symbol, 0.1)
), it might be that the order of these operations is causing the log message to not appear before the investment is made.Here is the corrected code:
If you still encounter issues, please provide more details or share the complete log file for further investigation.
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.
Uhahahaha
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!