The algorithm is creating new orders every minute, even if there are current open orders being placed..
Think about it like this:
Let's say Stock J's share price is $1000.
With $1m starting capital, the following will happen.
Start of Minute 1:
# Order 100% of portfolio value
50 seconds after the start of minute 1:
There is an open order for 1000 shares of Stock J,
Start of Minute 2:
500 shares of Stock J have been filled.
# This means that there is still an open order for 1000 shares of Stock J, but it is partially filled.
# Your portfolio value is still $1m, so this targets to $1m based on the number of shares in the portfolio.
# 500 shares of Stock J are ordered
50 seconds after the start of Minute 2:
The previous order from Minute 1 has been completely filled. (1000/1000)
There is an open order for 500 shares of Stock J
Start of Minute 3:
400 Shares of the order were filled. (400/500)
# Portfolio value at $~1.4m
# Order placed to make ~$1.4m to $1m
50 seconds after minute 3:
Order for 500 shares filled.
Open order for -400 shares.
This is all assuming that the price doesn't change from minute to minute, in which case it would rarely converge to a correct price.
If you were to change
handle_data to return when there are open orders, you'll get the correct behavior.
def handle_data(context, data):
This should converge to your target price.