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 :)

Author