@Grant
Good catch. You are correct in noting the 'latest_price' factor is the prior day close and that what we really want is the current minute data. This price is always updated (ie over-written) to be the current minute price in the 'update_stock_data' function with the following code
# Get the latest prices for all our securities
# May want to account for possibility of price being NaN or 0?
output_df.latest_price = data.current(output_df.index, 'price')
That factor could be taken out of the pipeline definition. Just make sure you add 'latest_price' as a column in the 'before_trading_start'. Maybe something like this
# Add other columns to the dataframe for storing qty of shares held, etc
context.output = context.output.assign(
latest_price = 0.0
held_shares = 0,
target_shares = 0,
order_shares = 0,
target_value = 0.0,
order_value = 0.0,
weight = MY_ETFS.weight,
)
You will probably note that the pipeline isn't doing anything for us now (ie not fetching any data). It could be removed in the above algorithm example but it's handy to keep in place and used as an algorithm framework. One can then easily add factors and reference them in the code. For example one version of this I've tried adds a 'volatility' factor and uses that to adjust the weight (rather than a fixed weight).
As far as a limit order potentially not filling. Yes, that's a possibility and I've experienced it. Depending upon ones algorithm though this may not be a problem. You wondered if "this could really muck things up if an order hangs" . Not really, The logic in the above algorithm always checks available cash before posting any subsequent buys. The logic doesn't require a complete fill. If a limit order didn't fill then there simply won't be the associated available cash. Any buys then just get adjusted (and that limit order still remains until the end of day).
I've actually tried placing sell limit orders for a .5% above the current price and buy limit orders for .5% below the current price in the above algorithm. One could also try adjusting the price by perhaps the standard deviation or something more 'statistical'. This actually decreased beta and increased returns a bit. Again, depends upon ones algorithm though.