Does anyone out there have a gap fill strategy they would be willing to share snippets of the code?
Or if there is someone out there willing to help out I need to find how to code the following:
- current day low is above the prior day high - thus creating a gap
- Short at the end of the day (to ensure the low does not close the gap thus nullifying the trade)
- Sell once the Gap has closed (price reaches the prior day high from 1.)
This seems relatively easy to code, but I'm running into a writers block
This is what I have so far:
def initialize(context):
#set_commission(commission.PerShare(cost=0, min_trade_cost=0))
#set_slippage(slippage.FixedSlippage(spread=0))
schedule_function(func= getin,date_rule=date_rules.every_day(),
time_rule=time_rules.market_close(hours=0, minutes=5))
context.stock = sid(24) #AAPL
#set_benchmark(context.stock)
def handle_data(context, data):
#if 'mx_lvrg' not in context: # Max leverage
#context.mx_lvrg = 0 # Init this instead in initialize() for better efficiency
#if context.account.leverage > context.mx_lvrg:
#context.mx_lvrg = context.account.leverage
#record(mx_lvrg = context.mx_lvrg) # Record maximum leverage encountered
#record(leverage=context.account.leverage)
def getin(context,data):
close = prices[-2]
current = data.current(context.stock,'price')
gap = (current-close)/close
thresh = .005
if (gap < -thresh) and (current > mavg):
order_target_percent(context.stock,1)
if (gap > thresh) and (current < mavg):
order_target_percent(context.stock,-1)
def getout(context,data):
order_target_percent(context.stock,0)