Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Dynamic Leverage Calculation

Hi everyone,

I am trying to limit my algo's max drawdown by using less leverage when my account value is low relatively to my account's highest value, and more leverage when my account's value at its highest.

The code would look something like this:

max_leverage = 2  
min_leverage = 1

context.highest_account_value = ???  
context.current_account_value = context.portfolio.portfolio_value

if context.current_account_value >= context.highest_account_value:  
    context.leverage = max_leverage  
elif context.current_account_value <= context.highest_account_value * (min_leverage/max_leverage):  
    context.leverage = min_leverage  
else:  
    context.leverage = max_leverage * (context.current_account_value/context.highest_account_value)  

The problem is, I have no clue how to record my account's value through time. The best solution would be to use the "history" function provided by quantopian, but it only provides information on equity objects, not account objects.

Does anyone have a way to do this? Big thank you in advance.

4 responses

I'm not sure to correctly understand what you asked, if you want to track the maximum value of context.portfolio.portfolio_value you need just two lines:

def initialize(context):  
    […]  
    # init variable to a sane value  
    context.highest_account_value = context.portfolio.portfolio_value

def handle_data(context, data):  
    […]  
    # maximum is the bigger between current max and current value, store and carry it to next iteration  
    context.highest_account_value = max(context.highest_account_value, context.portfolio.portfolio_value)

    record(max_account = context.highest_account_value  

You should have made the question clearer, as un-requested advice I suggest to read Eric Raymond's how to about asking questions.
Also I suggest to use "code sample block" with proper indentation when showing code.

What markdown flavor is this anyway, github's?

Yes! That's exactly the answer I was looking for, I was really overcomplicating this. Thanks for taking the time to answer, I'll definitely read Eric Raymond's article before posting again.

Some other possibly useful variables:

context.account.leverage (after start, above or below 1)  
context.portfolio.cash (the amount of cash in the account at any given moment)  
context.portfolio.starting_cash (same as context.portfolio.portfolio_value at the start, does not change)  
context.portfolio.positions[stock].amount (number of shares)  
context.portfolio.positions[stock].cost_basis (cumulative cost of buys. Edit: Correction from below, it's the average)  
context.portfolio.positions_value (value of stocks, aside from cash)  

Use site:quantopian.com when searching, like this:
https://www.google.com/#q=%22context.account.leverage%22+site:quantopian.com

Note that context.portfolio.positions[stock].cost_basis is averaged, not cumulative.