Hi Richard,
Looks like my hacked together code. You might have a look at https://www.quantopian.com/posts/comparing-olps-algorithms-olmar-up-et-al-dot-on-etfs and contact the author, Paul Perry (https://www.quantopian.com/users/5312f7e2efc731bbf20000fe) to see if he has any insights.
Up front, you may want to consider your objective. Are you trying to "beat" SPY? See if you can improve the performance with mean reversion, without adding risk, and including transaction costs? Or something else?
You could also consider how to incorporate the relative risk. For example, see https://www.quantopian.com/posts/equal-risk-contribution-s-and-p-sector-portfolio-erc.
One suggestion is to compute the optimum portfolio weights every day (or every minute, if you want). Store the weights versus time, smooth them, and periodically rebalance (e.g. weekly/monthly/quarterly) using the smoothed weighting.
Also, for development, I recommend starting with a gross leverage of 1.0. Also, always run the backtest starting as far back as possible. If you go the equal-weight route, maybe use RSP as your benchmark, which would allow you to go back to 4/24/03?
Grant