Does anyone here have any example of how to use leverage in an algorithm framework? I tried calling SetLeverage, the overload of AddEquity that has a leverage input parameter, UniverseSettings.Leverage and none of them seem to add leverage upon execution of the order. I am using the plain vanilla EqualWeightingPortfolioConstructionModel and ImmediateExecutionModel. The only way I know how to add leverage is via SetHoldings but that is not using the algorithm framework. Please advise.
Thanks!
Jon Quant
I figured it out so I'm going to answer my own question. You just have to override the DetermineTargetPercent function on the PortfolioConstructionModel and change the 1m to your new leverage:
protected override Dictionary<Insight, double> DetermineTargetPercent(List<Insight> activeInsights) { var result = new Dictionary<Insight, double>(); // give equal weighting to each security var count = activeInsights.Count(x => x.Direction != InsightDirection.Flat && RespectPortfolioBias(x)); var percent = count == 0 ? 0 : 1m / count; foreach (var insight in activeInsights) { result[insight] = (double)((int)(RespectPortfolioBias(insight) ? insight.Direction : InsightDirection.Flat) * percent); } return result; }
Jared Broad
Thank you for sharing it back Jon :) You are correct that it is the PCM's job to determine the positions weighing up the leverage of the securities.
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.
Derek Melchin
Hi Jon,
The EqualWeightingPortfolioConstructionModel does not store a reference to the algorithm object and the reference is not provided to the DetermineTargetPercent method. Therefore, we have no way of accessing the UniverseSettings property.
However, we can access the algorithm object and utilize its UniverseSettings by creating a custom portfolio construction model and overriding the CreateTargets method. Refer to my comment on this thread for an example.
Best,
Derek Melchin
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.
Jon Quant
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!