I have a “SPY” ETF Constituents Universe added, and have my own ETFConstituentsFilter defined (example code snippet attached, which can reprocude the issue). Based on the document I am expecting the filter function will be called on daily basis. But in the debug log, I only see it called once on Jan, 1, 2020. Why is that? Whats the expected behaviour for the filter calling frequency?
from AlgorithmImports import *
class ETFConstituentsUniverseAlgorithm(QCAlgorithm):
symbol_data_by_symbol = {}
def Initialize(self):
self.SetCash(100000)
self.SetStartDate(2020, 1, 1)
self.SetEndDate(2020, 2, 28)
self.UniverseSettings.Asynchronous = True
self.UniverseSettings.Resolution = Resolution.Daily
self.AddUniverse(self.Universe.ETF("SPY", Market.USA), self.ETFConstituentsFilter)
def ETFConstituentsFilter(self, constituents: List[ETFConstituentData]) -> List[Symbol]:
self.Debug(f"[INFO::Portfolio]: Starts ETF filter update ...")
for c in constituents:
pass
return Universe.Unchanged
Mia Alissi
The
ETFConstituentsFilterfunction is indeed expected to be called on a daily basis as per the documentation. However, in your code, you are returningUniverse.Unchangedwhich means that the universe members remain the same as they were on the previous day. This could be the reason why you are not seeing the debug log message on a daily basis.If you want to see the debug log message every day, you should return a list of symbols from the
ETFConstituentsFilterfunction. For example, you can return all the constituents as follows:This will select all the constituents of the ETF for your universe and you should see the debug log message every day.
Please note that the
ETFConstituentsFilterfunction is called asynchronously as you have setself.UniverseSettings.Asynchronous = True. This means that the function is called in parallel with your algorithm's main event loop, and the timing of the debug log messages may not align exactly with your algorithm's daily events.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.
BRANDON CHEN
Still I dont think its helping, I have the following code after modified, and I dont see any other calls to the filter except on day 1. Also, the OnSecuritiesChanged hook is never triggered as well (not a single log). Which I expect it to be triggered frequently as I am simply returning a random list which contains single random element from first couple constituents of the list.
Mia Alissi
The
ETFConstituentsFilterfunction is indeed expected to be called on a daily basis. However, theOnSecuritiesChangedmethod is only called when there are changes to the securities in your universe. In your case, you are selecting a random constituent from the first 10 constituents of the ETF. If the selected constituent is the same as the one selected on the previous day, thenOnSecuritiesChangedwill not be called because there are no changes to the securities in your universe.If you want to see the
OnSecuritiesChangedmethod being called more frequently, you could modify yourETFConstituentsFilterfunction to select a different constituent each day. For example, you could select a constituent based on the current day of the month:This will select a different constituent from the first 10 constituents each day, which should result in the
OnSecuritiesChangedmethod being called more frequently.Please note that the
ETFConstituentsFilterfunction is called asynchronously as you have setself.UniverseSettings.Asynchronous = True. This means that the function is called in parallel with your algorithm's main event loop, and the timing of the debug log messages may not align exactly with your algorithm's daily events.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.
BRANDON CHEN
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!