Hey all,
So I'm brand new to coding with Quantopian, but am excited about the capabilities of the tool, so I'm wondering if I might be able to get some help coding my first strategy? I find that it's easier for me to see where each feature lies once it's made when I know each step of the strategy already. Trying to code it learning from scratch is a bit of a headache as I have no clue what I'm doing yet.
Anyway, if I can explain it in terms of boolean/pseudo logic, the strategy works like this. It is a weekly strategy that will only use stocks from the Russell 2000 index, so we can set this as the Universe, I'm assuming based on market cap is the best way. The strategy buys first thing Monday morning, and sells before market close on Friday (based on the US Equities calendar, not trading holidays). Basically, it looks for stocks gapping down more than 2% from the previous Friday's close, but here's the kicker. The strategy needs to pick the top 5 tickers (or sid's) with the highest historical successful weekly gap fill percentage based on the ticker's previous 80 weeks of trading history. So for example, if a particular sid has gapped down 10 times in the last 80 weeks, and then successfully filled within those particular trading weeks 8 times, it's gap fill percentage would be 80%. We will only want to consider stocks that have a gap fill percentage of more than 70%. We will allow a max trading size per stock of $20,000, meaning each week we will be risking $100,000 of capital, splitting the volume slippage using the default quantopian setting. The strategy, in a perfect world, would make a list of stocks with a > 70% gapfill% with a minimum amount of gaps being 5, and then sort those into a descending order with the highest % fillers at the top. If there is a tie, the winner would be the stock that has more gaps in its history.
I hope this sort of makes sense. I read about this strategy over at StockFetcher, written by username Kevin_in_GA. You can view the exact screen/strategy as the first post on this page:
http://www.stockfetcher.com/forums/Filter-Exchange/MONDAY-GAP-UP-AND-GAP-DOWN-TRADING-SYSTEMS/104555
Have a read and let me know what you guys think. I wanted to write out my summary of the strategy first before just posting the link in an attempt to show that I'm actually interested in learning how to code using Quantopian and seeing this thing through, and wanting to understand each step of its development, rather than just posting the link and saying "someone build this for me." I also wanted to show that I have been reading the getting started and how to's in the learning sections and understand the basics, but where each section of code belongs in the script is where I need help, as well coding the logic of the script as I'm more used to coding in MQL4/boolean style for the Forex traders reading this.
I like this code for buying on Monday morning, and selling before Friday's close, but rather than buying SPY, it would need to start screening for the 5 stocks from within the Russell 2000 to trade instead:
def initialize(context):
context.spy = sid(8554)
schedule_function(open_positions, date_rules.week_start(), time_rules.market_open())
schedule_function(close_positions, date_rules.week_end(), time_rules.market_close(minutes=30))
def open_positions(context, data):
order_target_percent(context.spy, 0.10)
def close_positions(context, data):
order_target_percent(context.spy, 0)
Anyway, let me know what you think and thanks for the help!