Hi, I am new to Quantconnect and feeling lost when trying to construct a final universe for a long/short portfolio in which equal number of stocks are selected. Current I am able to filter based on price, volume, market, stock type etc in building the universe. Backtesting with the final long/short candidates sometimes happens to have non tradable stocks as reported by debug info. This will create an unbalanced long/short portfolio. How can I exclude those non tradable or delisted ones before placing my trades? Thanks
Mia Alissi
To address your challenge of constructing a universe that excludes non-tradable stocks and maintains a balanced long/short portfolio, here's a structured approach using QuantConnect's capabilities:
Coarse Universe Selection: Use the coarse selection method to filter stocks based on criteria like price and dollar volume, ensuring only liquid stocks are included. Ensure that
CoarseFundamental.HasFundamentalData
isTrue
to exclude delisted stocks.Fine Universe Selection: Once the coarse universe is determined, use the fine selection method to filter further based on more detailed criteria such as P/E ratio or market cap.
Monitor Security Updates: Implement the
OnSecuritiesChanged
method. This method allows you to react when securities are added or removed from the universe. Specifically, you can manage your portfolio by liquidating positions in securities that become non-tradable.Ensure Tradability in
OnData
: Before executing trades, check if a security is tradable using theSecurity.IsTradable
property. This step ensures you don't attempt to trade stocks that can't be traded, thus preserving the balanced nature of your long/short portfolio.Handle Exceptions: Implement logic to rebalance your portfolio whenever securities are found to be non-tradable. This can involve liquidating such positions and adjusting weights on remaining tradable securities to maintain balance between long and short positions.
Here's a conceptual outline of how you might set this up in your algorithm:
By using these methods, you can effectively manage a universe that minimizes the inclusion of non-tradable or delisted stocks, maintaining an equal number of long and short positions in your portfolio.
Sources: Key Concepts - QuantConnect.com, Fundamental Universes - QuantConnect.com, Coarse Fine Fundamental Combo Algorithm, Coarse Fundamental Top3 Algorithm, Key Concepts - QuantConnect.com
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.
Fimmu smu
I have tried the above. First, data.securities.items() in OnData function caused an error:
"Runtime Error: 'PythonSlice' object has no attribute 'securities'
 at on_data
  for symbol, security in data.securities.items():"
Then I tried this in OnData:
for symbol, bar in data.Bars.items():
    security = self.Securities[symbol]
    if not security.is_tradable:
      continue
Some stocks are still having data issues like:
Skipping shorting SON R735QTJ8XC9X - no data available (2015-02-20 15:30:00)
Skipping shorting KPTI VLD9RVF1PE1X - no data available (2015-02-23 15:30:00)
……..
Louis Szeto
Hi Fimmu smu
The universe selection tool will not return any delisted stocks on that day. Even the ones being subscribed will be removed after its delisting. It is likely that no reference price can be used to fill orders, so you may add a last-known-price to “initiate" the security:
Best
Louis
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.
Fimmu smu
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!