Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Why is the leverage so low?

Hello, I'm trying to create my first code here, on Quantopian.

My first attempt was to mimic a buy-and-hold approach with the following code (not sure how to share it properly):

def initialize(context):  
    context.spy = sid(8554)  
    context.invested = False  
    schedule_function(initialize_my_portfolio, date_rules.every_day(), time_rules.market_open())  
    schedule_function(record_leverage, date_rules.every_day(), time_rules.market_close())  


def record_leverage(context, data):  
    record('Leverage', context.account.leverage)  

def initialize_my_portfolio(context, data):  
    if not context.invested:  
        order_target_percent(context.spy, 1.0)  
        context.invested = True  

I made a short backtest on a period between 01/03/2017 and 07/15/2018. The strategy shows a return of 26.9% but this is not important.

It is the level of leverage what caught my attention. Correct me if I am wrong but if I start with 100 USD and my account goes up by 27%, my leverage should go down from 1 (fully invested at the beginning) to something like 0.79 (100 / 126.9). However, my code shows that my leverage remains nearly intact. Not sure why is that.

I'd say there is some problem with my code but I cannot figure out where and why. Perhaps someone could spot my mistake easily.

Thank you for every hint!

Mike

2 responses

Leverage is defined as

leverage = (market value of longs + abs(market value of shorts)) / net liquidation value

Or, in the case where there are only long positions

leverage = market value of longs  / ( market value of longs + cash)

So, in a very simple case of holding a stock with no cash in the account, the leverage is 1 and always stays at 1 as long as no cash is added to the account. The stock going up in value doesn't change the leverage. The leverage (for a long only portfolio) only changes when the cash changes.

leverage = market value of longs  / market value of longs = 1

This reason the leverage moves down in the above algo is that each quarter dividends are added as cash to the account. This makes the denominator slightly larger and the leverage slightly smaller. Notice the steps in the leverage occur every 3 months (ie every quarter).

Once again Dan has beat me to the punch.

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.