Hi all,
This is a modification to Gap trading Catcher.
It uses Mid-level day indicator and Williams’ 3 bar trailing stop.
Tested on AEX25, 2 minutes timeframe, 200k backtest, spread 0.1
Optimised p1 and p2 in range 1 to 7
Exit positions are made with the help of the Williams 3 bars trailing stop indicator found here:
https://www.prorealcode.com/prorealtime-indicators/williams-3-bar-trailing-stop/
If criteria are met it enters 9u am. Keeps positions overnight but closes at Friday 22u pm.
The strategy takes a while to load.
//-------------------------------------------------------------------------
// Main code : GapCatcherMLD_W
//-------------------------------------------------------------------------
// common rules
DEFPARAM CUMULATEORDERS = false
DEFPARAM PRELOADBARS = 10000
//DEFPARAM FLATBEFORE = 100000
//DEFPARAM FLATAFTER = 200000
// time rules
ONCE entertime = 090000
ONCE lasttime = 090000
ONCE closetime = 240000 //because of 240000 it doesn't close the position
ONCE closetimefriday=220000
tt1 = time >= entertime
tt2 = time <= lasttime
tradetime = tt1 and tt2
// positionsize and stops
positionsize=2
sl=0.66 // % stoploss
ts=0.33 // % trailingstop
// setup number of trades intraday
if IntradayBarIndex = 0 then
longtradecounter = 0
Shorttradecounter = 0
endif
// trade criteria
lc = tradetime and countoflongshares < 1 and longtradecounter < 1
sc = tradetime and countofshortshares < 0 and shorttradecounter < 1
// indicator
p1=6 //6
p2=5 //5
ref=CALL "3 bars trailing stop Williams"
// MID-LEVEL 1 DAY : md1
md1 = (DHigh(1) + DLow(1) ) / 2
// MID-LEVEL 3 DAYS : md3
Newhigh = 0
For i = 1 to p1 do
IF Dhigh(i) > Newhigh THEN
Newhigh = Dhigh(i)
ENDIF
NEXT
Newlow = 1000000000000 // = valeur infinie
For i = 1 to p1 do
IF Dlow(i) < Newlow THEN
Newlow= Dlow(i)
ENDIF
NEXT
md3 = (Newhigh + Newlow) /2
// MID-LEVEL 5 DAYS : md5
Newhigh = 0
For i = 1 to p2 do
IF Dhigh(i) > Newhigh THEN
Newhigh = Dhigh(i)
ENDIF
NEXT
Newlow = 1000000000000
For i = 1 to p2 do
IF Dlow(i) < Newlow THEN
Newlow= Dlow(i)
ENDIF
NEXT
md5 = (Newhigh + Newlow) /2
amplitude = 0
detector = 0
// market BELOW previous day close [ BUY ]
if close < md3 and md1<md5 then
if (abs((high-low[1]) / low[1]) > amplitude) then
detector = 1
endif
endif
// market ABOVE previous day close [ SELL ]
if close > md3 and md1>md5 then
if (abs((low-high[1]) /high[1]) > amplitude) then
detector = -1
endif
endif
// long entry
if lc and detector=1 then
buy positionsize lot at market
longtradecounter=longtradecounter + 1
endif
// short entry
if sc and detector=-1 then
sellshort positionsize lot at market
shorttradecounter=shorttradecounter + 1
endif
// trailing stop
If onmarket and (positionperf*100) >= ts then
profittake=1
elsif not onmarket then
profittake=0
endif
// exit long
if longonmarket then
if detector=-1 and low[1] > ref[1] and high<ref then
sell at market
elsif profittake=1 and low[1] > ref[1] and high<ref then
sell at market
endif
endif
// exit short
if shortonmarket then
if detector=1 and high[1] < ref[1] and low>ref then
exitshort at market
elsif profittake=1 and high[1] < ref[1] and low>ref then
exitshort at market
endif
endif
// exit all
If onmarket then
if time >= closetime then
sell at market
exitshort at market
elsif (CurrentDayOfWeek=5 and time>=closetimefriday) then
sell at market
exitshort at market
endif
endif
SET STOP %LOSS sl
//GRAPH 0 coloured(300,0,0) AS "zeroline"
//GRAPH (positionperf*100)coloured(0,0,0,255) AS "PositionPerformance"