The main change might be dropping the use of .div. You can ignore the continue business, some find that style easier to understand and work with, and since I had changed it for my understanding, I left it in place.
Run this and look at the logs. Then you might want to try my two lines and compare the logging output between the two. From that I'm sure you'll figure out where to go from there. Notice start and stop dates are entered for the tracking, out of initialize to make that easy for you to change. Also I added leverage in the output. It is focused on a time when leverage jumps to 8.
The code also contains https://www.quantopian.com/posts/margin. If you turn that on you'll see that your version carries a cumulative 94 million margin overnight if you total them all up which it does.
In the custom chart, you can see that the leverage high of 32 and spending 121 million results in 7% returns adjusted for the amount risked. Replace with my two lines (or perhaps just drop your use of .div, always operate against current cash instead) and returns will be 70% or so.