I coded this strategy after reading this website https://www.babypips.com/trading/forex-hlhb-system-explained, where the logic is best described.
Basically it enters LONG when a Fast Ema crosses over a Slow Ema AND Rsi crosses over its mid line. The reverse for SHORT trades.
I used the same settings for Ema’s (5 and 10), while I changed Rsi periods from 10 to 8 because the optimization granted better results. I also optimized TP’s and SL’s.
I coded it on DAX 1-minute default TF and 1-hour TF for signals.
Many inner workings are commented and easy to understand and change.
I will describe what I think deserves a deeper insight:
- Line 10 sets the default TF (1-minute).
- Line 12 allows to choose how the Trailing Stop code should behave (0 will use CLOSE to check prices, 1 will use HIGH/LOW).
- Lines 21-157 allow to choose trading hours, days and months.
- Lines 160 and 161 are the basic conditions for Long/Short trades.
- At line 163 TF 1-hour is started, that’s where the signals are returned.
- Lines 184-190 set data to be used as signals (two Ema’s + Rsi).
- Lines 192-195 prepare the setup for LONG tradses, while lines 196-200 prepare the seup for SHORT trades.
- The 1-hour TF has finished its tasks and it’s time to resort again to the default TF (1-minute) at line 202.
- Lines 203-210 are used to avoid entering again after a signal, if a trade is exited before the next hour begins (the signal is still set, but entering at such a late time may cause SL to be hit).
- lines 211-230 enter trades and set flag TradeON to 0, to finish what lines 203-210 have started.
- The last snippet starts at line 233, it’s the TRAILING STOP. Lines 233-343 set the reference to either CLOSE or HIGH/LOW according to one of the first options encountered earlier:
TRAILSTART is the number of pips after which trailing starts.
BASEPERCENT is the base percentage of profits to be secured when Trailing begins.
STEPSIZE is the number of pips needed to increment SL.
PERCENTINC is the percentage that BASEPERCENT needs to be incremented each STEPSIZE pips. So that if BASEPERCENT is 10% initially, after each STEPSIZE chunk will be incremented by this PERCENTINC, if it is 20%, then BASEPERCENT will rise to 12% the first time, then another 20%, rising to 14.4% the second time and so on… as profits grow.
PRICEDISTANCE is the minimum distance the broker requires for an instrument when a pending order is entered (I use 7 for DAX, despite it is actually slightly lower).
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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
//************************************************************************ // HLHB Trend Catcher - DAX mtf // // https://www.babypips.com/trading/forex-hlhb-system-explained //************************************************************************ // DEFPARAM CumulateOrders = False DEFPARAM PreLoadBars = 2000 //////////////////////////////////////////////////////////////////////// TIMEFRAME (default) ONCE nLots = 1 ONCE TrailHILO = 0 //0=use CLOSE 1=use HiLo to trailSL ONCE LongTrading = 1//w1//1 //1=allowed 0=banned ONCE ShortTrading = 1//w2//1 //1=allowed 0=banned // Target Profit & Stop Loss settings ONCE TPlong = 262 //262 ONCE SLlong = 39 //39 ONCE TPshort = 26 //26 ONCE SLshort = 50 //50 // OpenJan = 0//w3//0 //0=not forbidden 1=forbidden OpenFeb = 0//w4//0 //0=not forbidden 1=forbidden OpenMar = 0//w5//0 //0=not forbidden 1=forbidden OpenApr = 0//w6//0 //0=not forbidden 1=forbidden OpenMay = 0//w7//0 //0=not forbidden 1=forbidden OpenJun = 0//w8//0 //0=not forbidden 1=forbidden OpenJul = 0//w9//0 //0=not forbidden 1=forbidden OpenAug = 0//w10//0 //0=not forbidden 1=forbidden OpenSep = 0//w11//0 //0=not forbidden 1=forbidden OpenOct = 0//w12//0 //0=not forbidden 1=forbidden OpenNov = 0//w13//0 //0=not forbidden 1=forbidden OpenDec = 0//w14//0 //0=not forbidden 1=forbidden MonthsForbidden = 0 IF OpenJan = 1 AND OpenMonth = 1 THEN MonthsForbidden = 1 ELSIF OpenFeb = 1 AND OpenMonth = 2 THEN MonthsForbidden = 1 ELSIF OpenMar = 1 AND OpenMonth = 3 THEN MonthsForbidden = 1 ELSIF OpenApr = 1 AND OpenMonth = 4 THEN MonthsForbidden = 1 ELSIF OpenMay = 1 AND OpenMonth = 5 THEN MonthsForbidden = 1 ELSIF OpenJun = 1 AND OpenMonth = 6 THEN MonthsForbidden = 1 ELSIF OpenJul = 1 AND OpenMonth = 7 THEN MonthsForbidden = 1 ELSIF OpenAug = 1 AND OpenMonth = 8 THEN MonthsForbidden = 1 ELSIF OpenSep = 1 AND OpenMonth = 9 THEN MonthsForbidden = 1 ELSIF OpenOct = 1 AND OpenMonth = 10 THEN MonthsForbidden = 1 ELSIF OpenNov = 1 AND OpenMonth = 11 THEN MonthsForbidden = 1 ELSIF OpenDec = 1 AND OpenMonth = 12 THEN MonthsForbidden = 1 ENDIF // OpenMon = 0//w1//0 //0=not forbidden 1=forbidden OpenTue = 0//w2//0 //0=not forbidden 1=forbidden OpenWed = 0//w3//0 //0=not forbidden 1=forbidden OpenThu = 0//w4//0 //0=not forbidden 1=forbidden OpenFri = 0//w5//0 //0=not forbidden 1=forbidden OpenSat = 0//w6//1 //0=not forbidden 1=forbidden OpenSun = 0//w7//1 //0=not forbidden 1=forbidden DaysForbidden = 0 IF OpenMon = 1 AND OpenDayOfWeek = 1 THEN DaysForbidden = 1 ELSIF OpenTue = 1 AND OpenDayOfWeek = 2 THEN DaysForbidden = 1 ELSIF OpenWed = 1 AND OpenDayOfWeek = 3 THEN DaysForbidden = 1 ELSIF OpenThu = 1 AND OpenDayOfWeek = 4 THEN DaysForbidden = 1 ELSIF OpenFri = 1 AND OpenDayOfWeek = 5 THEN DaysForbidden = 1 ELSIF OpenSat = 1 AND OpenDayOfWeek = 6 THEN DaysForbidden = 1 ELSIF OpenSun = 1 AND OpenDayOfWeek = 0 THEN DaysForbidden = 1 ENDIF // OpenH0 = 1 //0=not forbidden 1=forbidden OpenH1 = 1 //0=not forbidden 1=forbidden OpenH2 = 1 //0=not forbidden 1=forbidden OpenH3 = 1 //0=not forbidden 1=forbidden OpenH4 = 1 //0=not forbidden 1=forbidden OpenH5 = 1 //0=not forbidden 1=forbidden OpenH6 = 1 //0=not forbidden 1=forbidden OpenH7 = 1 //0=not forbidden 1=forbidden OpenH8 = 1 //0=not forbidden 1=forbidden OpenH9 = 0//w1//0 //0=not forbidden 1=forbidden OpenH10 = 0//w2//0 //0=not forbidden 1=forbidden OpenH11 = 0//w3//0 //0=not forbidden 1=forbidden OpenH12 = 0//w4//0 //0=not forbidden 1=forbidden OpenH13 = 0//w5//0 //0=not forbidden 1=forbidden OpenH14 = 0//w6//0 //0=not forbidden 1=forbidden OpenH15 = 0//w7//0 //0=not forbidden 1=forbidden OpenH16 = 0//w8//0 //0=not forbidden 1=forbidden OpenH17 = 0//w9//0 //0=not forbidden 1=forbidden OpenH18 = 0//w10//0 //0=not forbidden 1=forbidden OpenH19 = 0//w11//0 //0=not forbidden 1=forbidden OpenH20 = 1 //0=not forbidden 1=forbidden OpenH21 = 1 //0=not forbidden 1=forbidden OpenH22 = 1 //0=not forbidden 1=forbidden OpenH23 = 1 //0=not forbidden 1=forbidden TimeForbidden = 0 IF OpenH0 = 1 AND OpenHour = 0 THEN TimeForbidden = 1 ELSIF OpenH1 = 1 AND OpenHour = 1 THEN TimeForbidden = 1 ELSIF OpenH2 = 1 AND OpenHour = 2 THEN TimeForbidden = 1 ELSIF OpenH3 = 1 AND OpenHour = 3 THEN TimeForbidden = 1 ELSIF OpenH4 = 1 AND OpenHour = 4 THEN TimeForbidden = 1 ELSIF OpenH5 = 1 AND OpenHour = 5 THEN TimeForbidden = 1 ELSIF OpenH6 = 1 AND OpenHour = 6 THEN TimeForbidden = 1 ELSIF OpenH7 = 1 AND OpenHour = 7 THEN TimeForbidden = 1 ELSIF OpenH8 = 1 AND OpenHour = 8 THEN TimeForbidden = 1 ELSIF OpenH9 = 1 AND OpenHour = 9 THEN TimeForbidden = 1 ELSIF OpenH10 = 1 AND OpenHour = 10 THEN TimeForbidden = 1 ELSIF OpenH11 = 1 AND OpenHour = 11 THEN TimeForbidden = 1 ELSIF OpenH12 = 1 AND OpenHour = 12 THEN TimeForbidden = 1 ELSIF OpenH13 = 1 AND OpenHour = 13 THEN TimeForbidden = 1 ELSIF OpenH14 = 1 AND OpenHour = 14 THEN TimeForbidden = 1 ELSIF OpenH15 = 1 AND OpenHour = 15 THEN TimeForbidden = 1 ELSIF OpenH16 = 1 AND OpenHour = 16 THEN TimeForbidden = 1 ELSIF OpenH17 = 1 AND OpenHour = 17 THEN TimeForbidden = 1 ELSIF OpenH18 = 1 AND OpenHour = 18 THEN TimeForbidden = 1 ELSIF OpenH19 = 1 AND OpenHour = 19 THEN TimeForbidden = 1 ELSIF OpenH20 = 1 AND OpenHour = 20 THEN TimeForbidden = 1 ELSIF OpenH21 = 1 AND OpenHour = 21 THEN TimeForbidden = 1 ELSIF OpenH22 = 1 AND OpenHour = 22 THEN TimeForbidden = 1 ELSIF OpenH23 = 1 AND OpenHour = 23 THEN TimeForbidden = 1 ENDIF // TradeCond = (Not DaysForbidden) AND (Not TimeForbidden) AND (Not MonthsForbidden) LongCond = TradeCond AND LongTrading ShortCond = TradeCond AND ShortTrading //////////////////////////////////////////////////////////////////////// TIMEFRAME (1 hour, updateonclose) //h1 IF Not OnMarket THEN BarCount = 0 ELSE BarCount = BarCount + 1 ENDIF // Define some candle features to be used Bullish = open < close Bearish = open > close Body = abs(close - open) HiWick = high - max(close,open) LoWick = min(close,open) - low TotalWicks = HiWick + LoWick LongCandle = Body > TotalWicks LongBullish = LongCandle AND Bullish LongBearish = LongCandle AND Bearish BigBullish = (Body > (range * 0.67)) AND LongBullish BigBearish = (Body > (range * 0.67)) AND LongBearish HugeBullish = (Body > (range * 0.85)) AND BigBullish HugeBearish = (Body > (range * 0.85)) AND BigBearish // ONCE AvgType= 1 //1 = ema ONCE FastMA = 5 //5 ONCE SlowMA = 10 //10 FastEma = average[FastMA,AvgType](close) SlowEma = average[SlowMA,AvgType](close) ONCE RsiMid = 50 MyRsi = Rsi[8](MedianPrice) //8 //------------------------------------------------------------------------------------ // --- LONG a1 = HugeBullish OR BigBullish OR LongBullish// OR Bullish// OR 1 a2 = FastEma CROSSES OVER SlowEma a3 = MyRsi CROSSES OVER RsiMid // --- SHORT b1 = HugeBearish OR BigBearish OR LongBearish// OR Bearish// OR 1 b2 = FastEma CROSSES UNDER SlowEma b3 = MyRsi CROSSES UNDER RsiMid //////////////////////////////////////////////////////////////////////// TIMEFRAME (default) //1 min ONCE TradeON = 1 IF IntraDayBarIndex = 0 THEN TradeON = 1 ENDIF TradeBar = BarCount IF Not OnMarket AND TradeBar <> TradeBar[1] THEN TradeON = 1 ENDIF //************************************************************************ // LONG trades //************************************************************************ ax = a1 AND a2 AND a3 IF ax AND Not OnMarket AND TradeON AND LongCond THEN SET TARGET pPROFIT TPlong SET STOP pLOSS SLlong BUY nLots CONTRACT AT MARKET TradeON = 0 ENDIF //************************************************************************ // SHORT trades //************************************************************************ bx = b1 AND b2 AND b3 IF bx AND Not OnMarket AND TradeON AND ShortCond THEN SET TARGET pPROFIT TPshort SET STOP pLOSS SLshort SELLSHORT nLots CONTRACT AT MARKET TradeON = 0 ENDIF ////////////////////////////////////////////////////////////////////////////////////////////////////////// // Trailing Stop TrailSlPrice = close TrailExitPrice = close IF TrailHILO THEN IF LongOnMarket THEN TrailSlPrice = high TrailExitPrice = low ELSIF ShortOnMarket THEN TrailSlPrice = low TrailExitPrice = high ENDIF ENDIF //------------------------------------------------------------------------------------ IF Not OnMarket THEN TrailStart = 12 //12 Start trailing profits from this point BasePerCent = 0.094 //09.4% Profit to keep StepSize = 6 //6 Pips chunks to increase Percentage PerCentInc = 0.102 //10.2% PerCent increment after each StepSize chunk RoundTO = -0.5 //-0.5 rounds to Lower integer, +0.4 rounds to Higher integer PriceDistance = 7 * pipsize //7 minimun distance from current price y1 = 0 y2 = 0 ProfitPerCent = BasePerCent ELSIF LongOnMarket AND TrailExitPrice > (TradePrice + (y1 * pipsize)) THEN //LONG x1 = (TrailExitPrice - tradeprice) / pipsize //convert price to pips IF x1 >= TrailStart THEN //go ahead only if N+ pips Diff1 = abs(TrailStart - x1) Chunks1 = max(0,round((Diff1 / StepSize) + RoundTO)) ProfitPerCent = BasePerCent + (BasePerCent * (Chunks1 * PerCentInc)) ProfitPerCent = max(ProfitPerCent[1],min(100,ProfitPerCent)) y1 = max(x1 * ProfitPerCent, y1) //y = % of max profit ENDIF ELSIF ShortOnMarket AND TrailExitPrice < (TradePrice - (y2 * pipsize)) THEN //SHORT x2 = (tradeprice - TrailExitPrice) / pipsize //convert price to pips IF x2 >= TrailStart THEN //go ahead only if N+ pips Diff2 = abs(TrailStart - x2) Chunks2 = max(0,round((Diff2 / StepSize) + RoundTO)) ProfitPerCent = BasePerCent + (BasePerCent * (Chunks2 * PerCentInc)) ProfitPerCent = max(ProfitPerCent[1],min(100,ProfitPerCent)) y2 = max(x2 * ProfitPerCent, y2) //y = % of max profit ENDIF ENDIF IF y1 THEN //Place pending STOP order when y>0 SellPrice = Tradeprice + (y1 * pipsize) //convert pips to price IF abs(TrailSlPrice - SellPrice) > PriceDistance THEN IF TrailSlPrice >= SellPrice THEN SELL AT SellPrice STOP ELSE SELL AT SellPrice LIMIT ENDIF ELSE SELL AT Market ENDIF ENDIF IF y2 THEN //Place pending STOP order when y>0 ExitPrice = Tradeprice - (y2 * pipsize) //convert pips to price IF abs(TrailSlPrice - ExitPrice) > PriceDistance THEN IF TrailSlPrice <= ExitPrice THEN EXITSHORT AT ExitPrice STOP ELSE EXITSHORT AT ExitPrice LIMIT ENDIF ELSE EXITSHORT AT Market ENDIF ENDIF |
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 robertogozzi, may i know the timezone for this stratety.
Besides, it would be great if somebody could test it for a longer period.
it also seems the take profit for long vs short side is a bit unbalanced. Do you know why?
TZ is UTC+2 (also called CET)
I think because DAX is mainly in an uptrend recently.
I added options to trade both LONG and SHORT positions, you may choose not to go SHORT.
wonderful, thanks a lot Robertogozzi :-). Yes, if we could backtest on 200k, it would be nice !
Ciao Roberto……ottimo
si potrebbe avere un TS simile per tradare anche i cfd future americani?
MINI SP 500 NASDAQ WALL STREET
Grazie
grateful if anyone could provide 200k backtest results! thx
Is there a switch to start / stop the Trailing SL / TP as when I GRAPH SL & TP neither appear to trail … i.e. I get straight lines / values.
Is there a discussion topic for this System?
If Yes, be good to put the Link on here?
Great Work Roberto, as usual!
No GraHal, there’s no topic open. It would be great if you could start one and post that link here. Thank you.
Discussion Topic started here …
https://www.prorealcode.com/topic/discussion-re-auto-system-hlhb-trend-catcher-dax-mtf/
Hi robertogozzi – thank you very much for sharing this strategy. I have performed various back tests and found that, for my preference, running it on a 5 minute time-frame generates a good number of orders. I also tested the TP for the long and short. For Long I use 75 points and for shorts I use 20, as Governments are manipulating markets to the upside through Quant Easing, the down periods are “bailed out” by central banks now.
Am now running live on an IG spread betting account, accounting for their DAX spreads and will let you know.
Thanks once again, super grateful.
S.
Thank you samsampop.
Hello guys I really appreciate this coding effort but can I use this code for Mt5 Forex Trading, or does it only apply to Dax and PRC