Hi J,
Glad to hear you're trading this algo, I really like the concept. I have some other variations on the idea that I can share as well. The check for open orders is not a big issue for this algorithm because it is lower frequency in nature. All open orders are cancelled at the end of the trading day by default, and this algorithm will trade at most once per day, so you shouldn't run into open orders when it comes time to rebalance.
Depending on the securities and amount of capital you are using, the only possible issue I could see would be if an order did not fill completely by the end of the trading day. You could calculate the target number of shares at rebalance, and order the difference the following day if any orders did not completely fill. The odds of this are pretty low if you are using the ETFs or highly liquid stocks.
Here's a function to convert percents to target position sizes.
def targets_from_weights(context, W):
"""
context: obj
contains current portfolio state
W: pandas Series
target weights vector indexed by sid.
returns: Series
target position sizes required to achieve target weights.
"""
equity = context.portfolio.portfolio_value
current_prices = history(1, '1d', 'price').iloc[-1] # .iloc is just to convert the DataFrame to a Series, there is only one row.
# Use floor division to get whole shares
return (W * equity) // current_prices
As for increasing the capital in the account, I'd say you have the right idea, you can just deposit the money and it will use the new capital at the next rebalance, or you can kill it and redeploy to get that cash in the market faster. Also note that the days between rebalance is the number of trading days, not calendar days.
Good to have you live trading, feel free to message me if you have any questions.
David