Thomas,
Welcome to Quantopian, I took some time go through your algorithm. I got it using minute data and talib rather than the ta transforms. I also found an accounting error that was allowing you to buy more securities than the imposed limit of 10.
When handle_data is called, context.portfolio contains the current account values as of the time when the function was called. The portfolio variables will not be updated until handle data is called again. That means context.portfolio.cash will not change within that bar, even if you place several orders. You have to do a little extra work to track any money spent within handle_data calls. Here is an example of how you can do that.
# Managing cash spent within a call to handle_data
cash = context.portfolio.cash # Cash as of the beginning of the handle_data call
for stock in data:
price = data[stock].price
if some_buy_signal:
order(stock, 10)
cash -= 10 * price
if some_sell_signal:
order(stock, -10)
cash += 10 * price
The algo respects its 10 stock limit now, so the rest of your logic was good. It trades less often now also, only when a new slot opens up. Maybe you can add something to select the 'more ideal' stocks to fill those open slots, whatever that might mean : )
happy tinkering,
David