I found two ways of doing rebalancing. One straightforward way is:
for stock in data:
order_target_percent(stock, 1.0 / stockCount)
Another way is first exiting all positions:
for stock in context.portfolio.positions:
order_target(stock, 0)
for stock in context.portfolio.positions:
order_target_percent(stock, 1.0 / stockCount)
My guess is that, 2nd way is better and more stable as it give enough cash after exiting. Is that correct?