I'm trying to balance a portfolio based on the stocks on a list: If it's on the list, buy X percent of portfolio value; if it's no longer on the list, drop it entirely.
The problem is that, although when I record cash, it stays above 0, the list (context.held), recorded in the attached backtest, just keeps on growing.
I use the "if stock not in open_orders" filter and I do take stocks out (line 56) but I still can't figure out what's going on with context.held.
I should also mention that I intend for X (percentage allocation) to be dynamic but I just used 1 / len(context.held) and simplified the get_q function to share this. Could ayone lend me a hand?
Thanks in advance,
Aitor