Hi all, I want to control leverage of each security and the entire portfolio separately. For example, I trade 2 pairs (4 stocks). Each time of trades , the quantity is changing depending on some factors (hedge ratio and so on.). I want to set the maximum leverage of each pair (combination of 2 stocks in the pair) to 1.0 and entire portfolio to 2.0 separately.
When I use 'SetLeverage' method, it sets the maximum leverage for the security and entire portfolio together at the same time, which is not what I want. For example, as below, if I set the leverage of two securities to 1.0 each, both maximum leverage of those two securities and entire portfolio is 1.0. In other words, if I order to buy each security with target percent 1.0 at the same time, only one of them gets filled because the max leverage of the portfolio is limited to 1.0. What I want here is that I want to buy both securities with target percent 1.0 (the total leverage becomes 2.0.).
- def Initialize(self):
# Set the cash we'd like to use for our backtest
# This is ignored in live trading
self.SetCash(100000)
# Start and end dates for the backtest.
# These are ignored in live trading.
self.SetStartDate(2017,1,1)
self.SetEndDate(2017,10,1)
# Add assets you'd like to see
self.spy = self.AddEquity("SPY", Resolution.Daily).Symbol
self.aapl = self.AddEquity("AAPL", Resolution.Daily).Symbol
self.Securities["SPY"].SetLeverage(1.0)
self.Securities["AAPL"].SetLeverage(1.0)
The quantity is supposed to change all the time. It's not like 'SetHoldings("SPY", 1.0)' but like 'SetHoldings("SPY, weight)'. And the maximum leverage for each security (or each pair) should be set to a pre-defined number (e.g. 0.8 or 1.0 or 1.2.). So below the maximum leverage of each security (or pair), we can use the quantity (weight) as we get. However, if the leverage is over the maximum, we should cap the quantity (weight) of the security (or pair). Can anyone help me on this? Thanks :)
Douglas Stridsberg
Hi Nhat,
SetHoldings does exactly what you've described - it sets the weight of the symbol in question. If you want to buy AAPL a 2x leverage with all your cash, you'd use SetHoldings("AAPL", 2.0).
I think what you're looking for (individual per-stock cap + global cap) is something that can be written into a function. If you trade the two stocks in a pair at the same time, it becomes even easier - just check that the sum of the weights don't add up to more than 1.0 and otherwise scale them down.
If I haven't understood your question, do you mind providing a worked example of what you're trying to achieve? From your post I can't see this being a difficult problem to solve at all, but I may have misunderstood you.
As a side note, you shoud look into building a framework algorithm instead - that way you can pair together investment decisions and have them intelligently managed by a portfolio construction model which would host your leverage capping methodology.
Nhat Luong
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!