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