QUANTCONNECT COMMUNITY
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.
Mia Alissi
To help you backtest a long-short portfolio in QuantConnect, you can follow these steps to ensure that the allowable weight for shorting a symbol is greater than or equal to the intended weight:
Initialize Algorithm: Set up your algorithm with the necessary start and end dates, initial cash, and add the securities you want to trade.
Calculate Order Quantity: Use
self.calculate_order_quantity(symbol, W)
to determine the number of shares you intend to short based on the weightW
. This method calculates the order quantity to achieve target-percent holdings.Check Shortability: For each candidate stock, use
self.shortable(symbol, short_quantity)
to verify if the calculated short quantity is shortable by the brokerage.Retrieve Shortable Quantity: Use
self.shortable_quantity(symbol)
to get the maximum quantity that can be shorted for each symbol.Compare Weights: For each symbol, compare the calculated order quantity with the shortable quantity to ensure the allowable weight is greater than or equal to the intended weight.
Select Eligible Stocks: Filter and select the top
N
stocks from your list ofM
candidates that meet the shortability criteria.Execute Orders: Place market orders for the selected stocks using
self.market_order(symbol, -short_quantity)
to execute the short trades.Here's a sample implementation:
This code initializes a long-short portfolio, calculates the shortable quantity for each candidate stock, checks if the intended short weight is fillable, and executes the short orders for the top
N
eligible stocks. Adjust theself.symbols
andself.weights
lists according to your specific requirements.Sources: Shorting - QuantConnect.com, Key Concepts - QuantConnect.com, A Risk Parity Approach to Leveraged ETFs, ROE Effect Within Stocks, Long Only Alpha Stream Algorithm
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!