Back to Community
Why is leverage not part of backtest metrics or graphs?

I cannot find the leverage in backtest results/graphs/metrics. Am I missing something?

I believe leverage is an important part of an algorithm and I expected to see it somewhere at the end of a backtest. I usually record(...) the leverage, but this is far from the perfect solution. Ideally I would like to see daily mean/min/max/mean absolute deviation of the leverage. Without those information you can easily under/over leverage your algorithm.

I understand that for weekly/monthly rebalancing this is not much of an issue, as the leverage you might record daily is probably close enough to the real leverage. The issue could be with algorithms that perform daily or intraday rebalancing, or algorithms that enter/exit positions dynamically without having periodic rebalancing phases. For those latter scenarios it could be easy to under/over leverage the portfolio.

What is your opinion in regard?

7 responses

It's a great feature request. We should definitely make leverage more prominent.

Have you been running tearsheets on your backtests? The tearsheet does include information about leverage, and a number of other important metrics. I believe it has surpassed the backtest screen at this point.

The underlying answer to your question is that you're seeing the evolution of the product. The backtest screen was one of the first things we built back in 2012. Leverage wasn't as high a priority consideration to us back then. Tearsheets, on the other hand, represent a much more up-to-date version of what we think is important.

That backtest screen will be updated, or replaced, in time.

Disclaimer

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 Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian 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.

Thanks Dan, I use the tearsheets and it's great. I see the leverage in the backtest screen as a help while developing or debugging an algorithm. No more than a help but still quite useful.

Hi Luca,

For the time being, you can manually record your leverage in the plot using record(leverage = context.account.leverage) in handle_data. This should help you visualize your algo's leverage in backtesting!

Disclaimer

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 Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian 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.

Unfortunately recording the leverage once per day doesn't give me much information with my algorithm, as the leverage can vary along the day.
This is what I do in my code:

def handle_data(context, data):  
    context.daily_leverage = context.daily_leverage.append(pd.Series(context.account.leverage))


def before_trading_close(context, data):  
    record(mean_leverage=context.daily_leverage.mean())  
    record(min_leverage=context.daily_leverage.min())  
    record(max_leverage=context.daily_leverage.max())  
    record(mad_leverage=context.daily_leverage.mad())  
    context.daily_leverage = pd.Series()  

I guess that making context.account.leverage a pandas Series would give us enough information without having the hassle of keeping track by ourselves of the leverage along the day.

Yes, while it wouldn't be as nice visually, you could log your Series at the end of each day.

FWIW, a major problem with how leverage, and recording, works in general, is that in backtests, you can only get daily samples. If you have an intraday event such as August 24th, it's completely invisible in backtest, unless you manually add logging and exceptions to trigger on events - anything recorded or plotted will hide the event.