Gap Trading Strategies (How To Trade Gaps With Backtested Examples) (Amibroker And Tradestation Code)

The code in plain English:

We tested two strategies:

The first one is a day trade strategy:

  1. The open must be lower than 0.15% compared to the close the day before, but higher than minus 0.7%.
  2. Today’s open must be higher than than yesterday’s moving average of the last 25 closing prices.
  3. If 1 and 2 are true, then but at the open of the day.
  4. Sell at the close of day or if the 5-minute bar is higher than yesterday’s closing price.

The second was an EOD strategy:

  1. The S&P 500 must gap down at least 0.15%.
  2. Yesterday’s IBS must have been 0.25 or lower.
  3. Yesterday’s 5-day RSI must be 0.45 or lower.
  4. If 1-3 are true, then buy today’s open.
  5. Exit at the close if the close is higher than yesterday’s close.

Amibroker code:

This is the code for the 5-minute intraday bars:

SetOption(“initialEquity”,100000);
Leverage=1.0 ;
SetOption(“accountmargin”,100/leverage);
SetPositionSize(99.75*leverage,spsPercentOfEquity);
setOption(“MaxOpenPositions”,1);

TimeFrameSet(inDaily);
Averages=MA(Close,25);
TimeFrameRestore();
Averages1=Ref(TimeFrameExpand(Averages,inDaily),-1);

dailyOpen = TimeFrameGetPrice(“O”, inDaily,0);
dailyPrevClose = TimeFrameGetPrice(“C”, inDaily, -1);
gapDiff = dailyOpen / dailyPrevClose – 1;

Buy= gapDiff<-.0015 AND gapdiff>-.007 AND TimeNum()==083000 AND Open>averages1;
BuyPrice=Open;
Sell=TimeNum()==145500 OR Close>TimeFrameGetPrice(“C”, inDaily, -1) ;
SellPrice=Close;

Gap strategy on daily bars:

gapdiff=(O-Ref(C,-1))/Ref(C,-1) ;

ibs=(C-L)/(H-L) ;

Buy= gapdiff<-.0015 AND Ref(ibs,-1)<.25 AND Ref(RSI(5),-1)<45;
BuyPrice=Open;
Sell= C>Ref(C,-1);
SellPrice=Close;

Tradestation code:

{
Gap trading strategy (Strategy 79B_ENTRY)
The S&P 500 must gap down at least 0.15%.
Yesterday's IBS must have been 0.25 or lower.
Yesterday's 5-day RSI must be 0.45 or lower.
If 1-3 are true, then buy today's open.
Exit at the close if the close is higher than yesterday's close.
It is not possible to combine next bar's open price and on close order in the same Strategy (Error #30215).
For this reason this strategy defines entry rule only. 
To backtest properly add the both 79B_ENTRY and 79B_EXIT strategies to the list and then run backtest.
}

Inputs:
	GapTreshold(-0.0015),
	IbsTreshold(0.25),
	RsiTreshold(45),
	RsiLookback(5);
	
Vars:
	GapDiff(0),
	Ibs(0);

GapDiff = (Open next bar - Close)/Close;
Ibs = (C - L) / (H - L);

if GapDiff < GapTreshold and Ibs < IbsTreshold and  RSI(Close, RsiLookback) < RsiTreshold Then
	Buy next bar on open;


---------------------------------------------------------------

{
Gap trading strategy (Strategy 79B_EXIT)
The S&P 500 must gap down at least 0.15%.
Yesterday's IBS must have been 0.25 or lower.
Yesterday's 5-day RSI must be 0.45 or lower.
If 1-3 are true, then buy today's open.
Exit at the close if the close is higher than yesterday's close.
It is not possible to combine next bar's open price and on close order in the same Strategy (Error #30215).
For this reason this strategy defines exit rule only. 
To backtest properly add the both 79B_ENTRY and 79B_EXIT strategies to the list and then run backtest.
}

if Close > Close[1] then
	sell this bar on close; 
{
Strategy 79 - Gap Trading Strategies 
The open must be lower than 0.15% compared to the close the day before, but higher than minus 0.7%.
Today's open must be higher than than yesterday's moving average of the last 25 closing prices.
If 1 and 2 are true, then but at the open of the day.
Sell at the close of day or if the 5-minute bar is higher than yesterday's closing price.

Before running this backtest, two dataseries for the required symbol have to be placed on the chart.
The first data series (Data1) with interval 5 minutes (or event 1 minute)
The second data series (Data2) with interval daily
Set reasonable data range for the both series. 200 days, for example.
Do not forget to set Strategy properties for all strategies on cart - Maximum number of bars the study will reference to 26 or more
}

Inputs:
	AverageLookback(25),
	GapMax(-0.15),
	GapMin(-0.70);
	
Vars:
	Averages(0),
	YesterdayClose(0),
	GapDiff(0);
	
Averages = Average(Close of Data2, AverageLookback);
YesterdayClose = Close of Data2;
	
if Date <> Date[1]  then 
begin
	GapDiff = (Open / YesterdayClose - 1) * 100;
	if GapDiff > GapMin and GapDiff < GapMax then 
		buy this bar on close;
end;

if Close > YesterdayClose then
	sell this bar on close;

Setexitonclose;

Disclaimer: We are not financial advisors. Please do your own due diligence and investment research or consult a financial professional. All articles are our opinions – they are not suggestions to buy or sell any securities.