Leverage is the ratio of amount spent vs initial capital. If borrowing occurs, leverage goes above the value of 1 (borrowing, negative cash) and that condition is called margin. As soon as one's first buy occurs, leverage is no longer zero. Examples: The leverage for a first purchase of $5 (including commission) on $10 initial capital would be .5 (one half). Spending $15 on $10 initial capital would be 1.5.
Most of the time when we refer to "leverage" we really mean to be talking about 'margin', negative cash, borrowing.
Here's another way to keep an eye out for margin. It won't tax the logging limits too much because it only logs new highs (or lows in the second one), and can be easily dropped into an algo (it does its own initialization for storing the previous value to be able to compare):
def handle_data(context, data):
# Log any new leverage high
if 'leverage_high' not in context:
context.leverage_high = 0
if context.account.leverage > context.leverage_high:
context.leverage_high = context.account.leverage
log.info('leverage_high {}'.format(context.leverage_high))
Sometimes an easier way to think of it is lowest cash used instead, and this does that:
def handle_data(context, data):
# Log any new lowest cash level
if 'cash_low' not in context:
context.cash_low = context.portfolio.starting_cash
if context.portfolio.cash < context.cash_low:
context.cash_low = context.portfolio.cash
log.info('cash low {}'.format( int(context.cash_low) ))
You used record() for cash, that's great
Side-note: One thing that folks ought to keep in mind is that the custom chart necessarily does smoothing to be able to be so nice, so mousing over it can show averaged values that didn't really actually happen. For example if you record just two values, 0 on some bars and 100 on others, sometimes the chart will show you a value of say, 60, due to smoothing.
Lines like those above provide precision if needed.
By the way, did you know you can click custom chart legend items to toggle them off and on? I just discovered that a few days ago, useful sometimes when two or more recorded sets of values are in widely different ranges from each other.
You'll find that the algo above uses a lot of leverage/margin. It had already spent twice the initial capital five days in (leverage of 2.0040087117). Three times a couple of years later. I didn't have the patience to wait for the full test. It is useful for you to know that unfortunately the chart and metrics currently ignore margin (negative cash, borrowing). On our own we can calculate profit divided by maximum spent (including margin) and that will give us an idea of code merit no matter how much margin happens to be used as code changes are made. And that feels great to not have to be putting forth so much effort to be as close as possible to the utilization of 100% of initial capital. Good luck.