Some needed clarification of my earlier message:
Why is max leverage 3.05 while context.account.leverage is far lower? Maybe because record() is only the end of day value? Daily level.
So, couldn't we be ordering way into negative cash at noon, return to a leverage of 1 by close, and mistakenly figure we are fine?
I think I was trying to demonstrate that problem above by forcing into negative cash, margin, and wasn't clear.
We can be thinking we have an awesome result and leverage around 1 when actually it would have required 3 times the starting capital in the Interactive Brokers account to achieve that result, we missed a leverage spike.
Due to this, shouldn't we be watching max_leverage, not just current leverage, since record() happens only once per day?
I presume if we run record() every minute, all except the last one per day are ignored. True?
When looking at maximums, it isn't necessary to run record() every minute, just when there is a new high.
Reading context.account.leverage still has to happen every minute to see if there is a new maximum.
This can do that ...
# Record maximum leverage encountered
if 'mx_lvrg' not in context:
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(mx_lvrg = context.mx_lvrg)