Hi Edward,
The reason this algorithm has such huge returns is because it borrows and invests about 50x its capital base, so it's leveraged to the hilt. That kind of leverage is unrealistic in the equities market, but the backtesting engine assumes the user manages the margin/leverage accounting and allows you to borrow as much as you please.
Your code is actually correct as written, the reason the borrowing occurred is because orders were being placed when there were already open orders that had not filled yet due the slippage model used. I added a line to check for open orders, and recorded your leverage, that seems to fix the issue.
The various metrics like Sharpe and drawdown are pretty important, they give you an idea of how consistent a strategy is. For example, imagine you could borrow infinite capital and you invested in your algorithm above, there was a drawdown of 271% at one point, that would be pretty gut wrenching to experience in a real trading scenario.
Thanks for sharing!
best,
David