A lot of algorithms are using record() to keep track of leverage. The problem is that record() only shows the last value seen per day and drops the rest. Leverage can change in any minute so 389 out of 390 minutes of the trading day are missed. If record() is set to operate on just the first and second minute of the day, the second minute will show up, it works like that.

Why is leverage so important? Because leverage above 1 often means margin, spending beyond the amount in the account. If you start with $100,000 and the code hits a leverage of 1.5 at any time during the run, it typically means you would have had to supply$150,000 to achieve that result by borrowing from the broker, requiring a margin account with fees/interest. (It's a little more involved, that's just the general idea).

A lot can happen intraday (occurring within/throughout a day). To catch leverage jumps (new highs), here is some minimal code for copy/paste. This will read leverage on every minute processed and chart the maximum every day, looks like a stair-step showing when leverage increases happen. Quick and easy.

Edit, from below ...

def initialize(context):

...

context.mxlv = 0
for i in range(1, 391):
schedule_function(mxlv, date_rules.every_day(), time_rules.market_open(minutes=i))

def mxlv(context, data):
if context.account.leverage > context.mxlv:
context.mxlv = context.account.leverage
record(MxLv = context.mxlv)


Earlier ...

def handle_data(context, data):
if 'mx_lvrg' not in context:             # Max leverage
context.mx_lvrg = 0                  # Init this instead in initialize() for better efficiency
if context.account.leverage > context.mx_lvrg:
context.mx_lvrg = context.account.leverage
record(MxLv = context.mx_lvrg)       # Record maximum leverage encountered

7 responses

Gary,

Thanks for this contribution (and your others)! I have been using the once-a-day leverage number, but I agree that everyone should use max leverage instead. I will start using your code snippet in all my algos.

Tristan

The max per day idea is a good one, thanks.

garyha,

I added this code to the unofficial Quantopian-Community repository on Github. Anyone is welcome to share code on there.

https://github.com/tristanbob/Quantopian-Community/

BTW, I can't find where you posted your PvR function (profit vs risk) function. Can you send a link?

Tristan

Hi Tristan, thanks, I wasn't aware of the repository. I'll make a post for PvR at some point (has intraday max leverage built into it) and in the meantime there's an example in the Source Code tab here. The section that goes with 'def pvr' in initialize() is marked.

Edit: PvR is here now: https://www.quantopian.com/posts/pvr

Gary, you raised an important point and you're right, there isn't an easy way to access intraday leverage. I submitted an internal request for the feature and I think it'll be a good API addition.

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.

Illustrating that the recording of maximum intraday leverage is useful because of chart smoothing.
Here, the ordinary daily leverage (lv) chart curve winds up smoothed so that the spikes, which reached and caused the stair step MxLv values, appear to be much lower. One can easily be mis-led by merely recording leverage once per day.

Easier maximum leverage code ...

def initialize(context):

...

context.mxlv = 0
for i in range(1, 391):
schedule_function(mxlv, date_rules.every_day(), time_rules.market_open(minutes=i))

def mxlv(context, data):
if context.account.leverage > context.mxlv:
context.mxlv = context.account.leverage
record(MxLv = context.mxlv)