Hi Altan,
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
order_target_percent(symbol("J"),1)
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.
order_target_percent(symbol("J"), 1)
# 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_target_percent(symbol("J"), 1)
# Order placed to make ~$1.4m to $1m
(-400 shares)
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):
if get_open_orders():
return
order_target_percent(symbol("J"), 1)
This should converge to your target price.
Best,
Lotanna Ezenwa