Back

Leverage Control ?

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

Update Backtest








We follow the more standardized multi-asset way of dealing with this: Margin. There is a total margin available in your algorithm and each purchase decrements the margin available. This is described here:

https://www.quantconnect.com/forum/discussion/2317/migrating-from-quantopian-to-quantconnect/p2/comment-7488

Set holdings takes the buying power; so if leverage is 2; SetHoldings("IBM", 2) will max out your leverage. Leverage is actually just a helper function in LEAN. Under the surface we're setting the "Margin Required" to purchase the asset (just like a real brokerage does).

Does this help?

0

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.


Hi Jared, Thank you for your comment. I get to know about 'Margin' method at QC, and found that we could use  'CalculateOrderQuantity' method very usefully. By using this method, we get the maximum quantity to get the pre-defined target percent (capped target percent) of total portfolio value. And we can add a helper function that returns quantity available to get our defined target percent. With this method and a helper function, we can control leverage of each security (or pair) and total portfolio as well. Thank you :) 

# Get quantity to reach our target percent of total portfolio value

# We get the quantity of SPY to get 100% of total portfolio value
# This quantity is changing as total portfolio value changes

self.CalculateOrderQuantity("SPY", 1.0))
self.CalculateOrderQuantity("AAPL", 1.0))

1

self.CalculateOrderQuantity("SPY", 1.0)) 

Genius! 

0

Update Backtest





0

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.


Loading...

This discussion is closed