I’m using coarse/fine universe to screen for stocks, which get added to a watchlist (“self.invested_stocks”). When the universe changes, symbols are added/removed from this watchlist based on certain criteria. Then I’m iterating through the symbols in the watchlist and equally purchasing the stocks. Each time new stocks are added or removed from the portfolio, I’m also attempting to rebalance the portfolio to an equal weighting of all holdings. I’m doing this with “self.SetHoldings([PortfolioTarget… “ and have also tried “self.SetHoldings(security.Symbol…”
For some reason, both are failing to equal-weight the positions in the portfolio. In the log I’m printing the value of holdings for each stock, anytime there are changes to the portfolio, and values are showing to be quite different. I can’t figure out how to correct this.
Additionally the portfolio continues to hold more cash, above the 10% cash threshold I set. By the end of the backtest it's only 76% invested.
A second issue I’m having is that the stock PDLI keeps getting added to the portfolio, but there appears to be an error in the QC data or algorithm. Although there is a stock price for PDLI, QC incorrectly shows a price of $0. I’ve submitted a support ticket in the data issues section of the QC site, but haven’t heard back yet. In the meantime, how can I manually remove this stock from my watchlist? I’ve tried adding a condition “and security.Symbol.Value is not str("PDLI R735QTJ8XC9X”)” as a requirement before stocks are appended to my watchlist, but this doesn’t seem to work. Perhaps there's a more elegant way to do this.
Any help with these two issues would be hugely appreciated! Thanks in advance.
Shile Wen
Hi The Dude,
To address excluding PDLI, in our coarse filter, we can change our return statement from:
return [i.Symbol for i in top_liquid_coarse]
to
return [i.Symbol for i in top_liquid_coarse if i.Symbol.Value != 'PDLI']
Furthermore, instead of doing complicated SetHoldings math, we can instead use an EqualWeightingPortfolioConstructionModel to handle this logic for us, and we just need to emit Insights monthly. I’ve shown the changes in the attached backtest.
Also, to have a scheduled event 2 hours after market open, we need to have the data on a minute resolution.I've also added self.recently_rebalanced to check if we've updated the securities list before emitting new insights.
Best,
Shile Wen
Bodhi
I don't think this accomplishes what I'm setting out to do. By introducing alpha insights with portfolio construction model, stocks are being held for a pre-defined/finite period of time. This is not correct.
This is a buy/hold strategy where stocks will potentially be held for an infinite amount of time, even if they no longer meet the requirements of Universe Selection. In other words, the coarse/fine universe selection sets the requirements to add them to the portfolio, but there's a completely different set of requirements to liquidate them from the portfolio. This is why I think emitting insights is not best for this strategy.
Why isn't SetHoldings working properly?
I've updated the algorithm to remove PDLI from the universe, per your suggestion and have attached an updated backtest here.
When you look at the Log, also notice positions are not being held equal-weighted. By the end of the backtest (later months), some positions are over twice the value of other positions.
Bodhi
I have a correction to my last comment - now that PDLI has been removed, SetHoldings now appropriately invests 90% of the portfolio (keeping 10% cash buffer) and rebalances the portfolio correctly.
Now the only question I have remaining is related to using Alpha Insights (see above comment). To clarify, I'd like to know if using alpha insights with equal weighting construction portfolio can work for this strategy, given this is a buy/hold strategy. The coarse/fine universe selection sets the requirements to add stocks to the portfolio, but there's a completely different set of requirements to liquidate them from the portfolio., and therefore there is no time duration for an insight. A stock could be potentially be held indefinitely.
Shile Wen
Hi The Dude,
Buy and hold can be accomplished using Insights. To do this, we can set the Insight duration to an arbitrarily long duration. Furthermore, if an Up Insight is followed by another Up Insight before the previous Insight expires, the previous position is held. This means, as long as we keep emitting insights with the same direction for a stock, we buy and hold that stock until an Insight is either not emitted or the Insight Direction changes for that stock (a Flat insight liquidates, while a Down Insight flips the position). In the algorithm I posted earlier, the latter method is used (note: I accidentally used a Down Insight instead of a Flat Insight, so I'll attach a fixed version).
Best,
Shile Wen
Bodhi
Hi Shile, what you're saying makes sense, but I''m still unclear on several things:
1. You state we can set an arbitrarily long expiration date for the insight, but the code you posted appears to have expiration set at month-end. That's not long enough. How could we set the expiration for 30 years?
2. If the insight expiration is set at 30 years, the stock would be sold if at any point during this timespan a down insight is emitted for that same stock, is that correct? That would be ideal.
3. You state: "as long as we keep emitting insights with the same direction for a stock, we buy and hold that stock until an Insight is either not emitted or the Insight Direction changes for that stock" --> If an up insight is emitted for a stock and is therefore purchased, I want that stock to be continued to be held even if we don't continue emitting up direction insights for that stock. For example, if a stock has a ROIC over 0.7 and is in the bottom quartile for EV-to-EBIT, then an up insight is emitted and the stock is initially purchased. But going forward, now that the stock is being held, it will be sold only if ROIC goes below 0.7, but not if it's no longer in the bottom quartile for EV-to-EBIT. We only care about EV-to-EBIT for the initial purchase, but it's not a metric that's looked at to determine when to sell it. How could this be done with insights?
Shile Wen
Hi The Dude,
Best,
Shile Wen
Bodhi
I've made updates based on your comments here, but I'm seeing something strange. Trades are executed 1 month later then they should be.
For example, if you view the Log of the attached backtest, you'll notice on 4/2/2002 the alpha emits a flat insight for GGG, but it's not sold until 5/2/2002. This happens for every flat insight. Stock is sold 1 month later than it should be.
How can I correct this so the trade is placed the same day the insight is emitted?
Shile Wen
Hi The Dude,
OnSecuritiesChanged refers to changes to the stocks in our Universe, and not changes to the securities we hold in our portfolio. The tracking of selling of securities should be done in OnOrderEvent,
Best,
Shile Wen
Bodhi
Thanks for your response. That helps me better understand how to log Debug messages, but I don't think it addresses my question.
To clarify, I've completely deleted OnSecuritiesChanged since it was just logging Debug messages. Notice in the logs that ISSC is given a flat signal on 1/3/2001, but the algorithm doesn't sell it until 2/2/2001. And GGG is given a flat signal on 4/2/2002, but not sold until 5/2/2002.
Why is there a one month delay between the flat signal and when the trade is placed?
Thank you very much for helping me out here.
Jared Broad
self.Settings.RebalancePortfolioOnInsightChanges = False # Rebalance when the insights state changes/added/expire. self.Settings.RebalancePortfolioOnSecurityChanges = True # Rebalance when new assets are added to the universe.
It's not updating because it's explicitly disabled; and so it is only updated when the universe rebalances.
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.
Bodhi
Thanks! That makes sense.
Bodhi
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!