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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
//------------------------------------------------------------------------- // 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" |
Share this
No information on this site is investment advice or a solicitation to buy or sell any financial instrument. Past performance is not indicative of future results. Trading may expose you to risk of loss greater than your deposits and is only suitable for experienced investors who have sufficient financial means to bear such risk.
ProRealTime ITF files and other attachments :PRC is also on YouTube, subscribe to our channel for exclusive content and tutorials
Hi Paul, thank you for your code. There are not so many trading strategies for AEX25 around here 🙂
The strategy takes a while to load because of all these unnecessary FOR/NEXT loops in your code, this could be optimized to a few lines and increase the speed of calculation.
First impressions, looks great, backtests well across a number of markets, great on nikei. Only seems to take long trades, can’t see why?
thnx nicolas, will take that advise and see if I can improve the speed.
mrripley99, set countofshortshares < 0 to 1. It can do both long and short same time, or do it separately.
Thanks Paul
Hi Paul, Thank you for your code. I have tested it on several pairs and noticed that it never opens short positions although there is code for it?
Duh. Should learn to read above post :p
It seems that after publishing it started drawdown.
Please forgive the daft question, but why is 3-bars-trailing-stop-williams-3.itf included as an attachment in the OP when CALL “3 bars trailing stop Williams” (no -3) is used in the code??
Hi,
Seems great but what would be the code for a stock ?
Thanks,
Chirs
Paul, what is the best way in your opinion to know if I have over optimized?