Back

SetHoldings not working as intended, I’m stumped

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.

Update Backtest








0

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.


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

0

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.


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.

0


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.

0

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

0

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.


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?

0

Hi The Dude,

  1. As long as we follow Insight by another Insight with the same direction, the holdings in the previous Insight is extended. To have a 30 year Insight, set the duration to 30 * 252 = 7560 days (~252 trading days in a year). 
  2. This is true, however, the best way to liquidate a position is to use a Flat Insight. A Down Insight will liquidate a position, but then also take a short position (please see my previous comment on the Down Insight). When an Up/Down Insight is followed by a Flat Insight, the position will be liquidated.
  3. Emit an arbitrarily long insight (e.g. 99999999 days) and emit a Flat Insight when the ROIC is below .7, (Fine Fundamental data can now be accessed through the Security object, see this thread)  

Best,
Shile Wen

0

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.


Update Backtest





0

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.


Loading...

This discussion is closed