Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Long only optimal portfolio trades more stocks than processed through pipeline and accumulates positions unintentionally

Hey guys,

I have a question regarding a long only algorithm I am building.

I have noticed, that apparently the algorithm accumulates positions, since the number of positions increases gradually and after a while there are more open positions than stocks in the pipeline. I have tried to close the positions manually but this also does not work, since I get the following error when I try to order_target_percent(stock,0) for eachstock in context.portfolio.positions: stock = eachstock.symbol

Error
non-string argument given to symbol()

Does anyone know why positions are being accumulated and i have more positions than output from my pipeline and how to solve it ? Thanks a lot and have a nice day

2 responses

I believe what you are seeing is a combination of a few things.

  1. You are apparently trading rather thin stocks which don't completely fill. Look at the logs and you will see the warnings.
  2. The above isn't too bad except the algorithm only trades once a week. If you don't completely exit a position (because it didn't completely fill) it will stay open through the entire week. This is what you see as 'accumulating positions'. Don't worry, they will close eventually. The optimize method you are using will work.
  3. Finally, you are recording the number of positions in the pipeline every day. The pipeline might return more or less positions than are open but since orders are only placed once a week, then you may see more or less positions in the portfolio during the week.

Make sense?

Totally! Thanks a lot for your time and explanation :)