Here's an algo that I originally posted on https://www.quantopian.com/posts/interpretation-of-difference-between-minutely-open-price-and-prior-close-price. I thought I'd re-post as a separate thread in case anyone is interested in fiddling with it. The basic outline is:
- Trade intraday only; close open positions before the end of the trading day.
- On a minute-by-minute basis for SPY, compute the difference between the prior closing price and the immediately following open price (delta_oc).
- Accumulate the differences over 390 minutely bars, and then compute the mean (delta_oc_mean) and standard deviation (delta_oc_std).
- Apply trading rules:
if delta_oc > delta_oc_mean + 3*delta_oc_std:
order_target_percent(context.spy, -1) # short
elif delta_oc < delta_oc_mean - 3*delta_oc_std:
order_target_percent(context.spy, 1) # long
Notes:
- No commissions (set_commission(commission.PerTrade(cost=0)).
- Custom slippage model with settings that may idealize the algo performance (but not make it ridiculous).
- The code could be re-factored, making better use of history, Pandas, etc., so if you're needing to learn, give it a shot!
- It takes awhile to run, so if you make adjustments to the code, you might try a shorter timeframe first.
- It's probably not at all realistic, but maybe it'll spawn some insights.
Any idea if the margin is correct going short with order_target_percent(context.spy, -1)? Or would this exceed typical margin rules?