order_target_percent(stock, 0) is causing short position by January 15th.
I think it is because "context.portfolio.positions[stock].amount > 0" is true in line 42 even after the initial "order_target_percent(stock, 0)" from the previous minute if the market is not liquid enough. In that case, you will end up sending "order_target_percent(stock, 0)" again while the first (previous minute) "order_target_percent(stock, 0)" is still partially queued up to get ride of everything.
Is there any clever way to build a variable such as "context.current_position_ind" so that it will flip once for each buy all or buy sell order in order to avoid issue described above. Also I need "context.current_position_ind" such that it can handle multiple securities. I am not so well versed in Python yet. I would think I need to build "context.current_position_ind" either as a list or pandas.
Thank you very much.