xmd79

Follow The Ranging Hull

Jun 24th, 2022
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.88 KB | None | 0 0
  1. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  2. // © flygalaxies
  3.  
  4. // Strategy based on the Follow Line Indicator by Dreadblitz, Hull Suite by InSilico and Range Filter Buy and Sell 5 min by guikroth
  5. // Designed for the purpose of back testing
  6. // Strategy:
  7. // - When the Range Filter Color Changes, And the HULL Suite is in that direction, Enter In that direction
  8. // - e.g Range Filter Changes color from red to green, and Hull Suite is in Green. Enter Long
  9. // - e.g Range Filter Changes color from green to red, and Hull Suite is in red. Enter Short
  10. //
  11. // - When the Following Line Color Changes, And the HULL Suite is in that direction, Enter In that direction
  12. // - e.g Following Line Changes color from red to green, and Hull Suite is in Green. Enter Long
  13. // - e.g Following Line Changes color from green to red, and Hull Suite is in red. Enter Short
  14. //
  15. // Credits:
  16. // Hull Suite by InSilico https://www.tradingview.com/u/InSilico/
  17. // Range Filter Buy and Sell 5 min https://www.tradingview.com/u/guikroth/
  18. // Follow Line Indicator by Dreadblitz https://www.tradingview.com/u/Dreadblitz/
  19.  
  20. //@version=5
  21. // strategy("Follow The Ranging Hull", overlay=true, initial_capital = 50000, process_orders_on_close = true)
  22. indicator("Follow The Ranging Hull", overlay=true)
  23.  
  24.  
  25. /////////////////////////
  26. // STRATEGY SETTINGS ///
  27. ///////////////////////
  28. fromDate = input.time(title="From", defval=timestamp("01 Jan 2022 00:00 GMT+2"), inline="1", group="Date Range")
  29. toDate = input.time(title="To", defval=timestamp("01 Jan 2022 23:00 GMT+2"), inline="1", group="Date Range")
  30. inDate = time >= fromDate and time <= toDate
  31. isStrategy = input.bool(title="IS STRATEGY", defval = false, group="Trading Rules")
  32. isPointRule = input.bool(title="Apply Point Rule", defval = false, group="Trading Rules")
  33. pointRulePoints = input.int(title="Points (The points that it uses to take profit)", defval = 80, group="Trading Rules")
  34.  
  35. ////////////////////////////////////
  36. // FOLLOWING, ENTRY, EXIT RULES ///
  37. //////////////////////////////////
  38. followLineUseATR = input.bool(title="Use ATR for the Following Line (if false, it will use the slow range filter)", defval = false, group="Following, Entry, Exit Rules")
  39. followRngFilterEntry = input.bool(title="use Following line or Slow Range Filter as entry point", defval = false, group="Following, Entry, Exit Rules")
  40. showStatusWindow = input.bool(title="Show Status Window", defval = true, group="Following, Entry, Exit Rules")
  41. hl = input.bool(defval = true, title = "Hide Labels", group = "Following, Entry, Exit Rules")
  42. exitRule = input.session("Hull", title="Exit Rule", options=["Hull", "RangeFilter", "Following/SlowRangeFilter"], group="Following, Entry, Exit Rules")
  43.  
  44. ////////////////////
  45. // COLOR INPUTS ///
  46. //////////////////
  47. rngFilterColorUp = input.color(title="Fast Range Filter Color Up", defval = color.green, group="Range Filter Colors")
  48. rngFilterColorDown = input.color(title="Fast Range Filter Color Down", defval = color.red, group="Range Filter Colors")
  49. rngFilterColorUpSlow = input.color(title="Slow Range Filter Color Up", defval = color.green, group="Range Filter Colors")
  50. rngFilterColorDownSlow = input.color(title="Slow Range Filter Color Down", defval = color.red, group="Range Filter Colors")
  51. hullColorUp = input.color(title="Hull Color Up", defval = color.green, group="Hull Suite Colors")
  52. hullColorDown = input.color(title="Hull Color Down", defval = color.red, group="Hull Suite Colors")
  53. fliColorUp = input.color(title="Follow Line Color Up", defval = color.green, group="Following Line Colors")
  54. fliColorDown = input.color(title="Follow Line Color Down", defval = color.red, group="Following Line Colors")
  55.  
  56.  
  57. ///////////////////////////
  58. // Range Filter INPUTS ///
  59. /////////////////////////
  60. // Fast Inputs
  61. src = input(defval=ohlc4, title="Range Filter Fast Source", group="Range Filter Fast")
  62. per = input.int(defval=33, minval=1, title="Range Filter Fast Sampling Period", group="Range Filter Fast")
  63. mult = input.float(defval=2.1, minval=0.1, title="Range Filter Fast Multiplier", group="Range Filter Fast", step=0.1)
  64. // Slow Inputs
  65. srcSlow = input(defval=ohlc4, title="Range Filter Slow Source", group="Range Filter Slow")
  66. perSlow = input.int(defval=48, minval=1, title="Range Filter Slow Sampling Period", group="Range Filter Slow")
  67. multSlow = input.float(defval=3.4, minval=0.1, title="Range Filter Slow Multiplier", group="Range Filter Slow", step=0.1)
  68.  
  69.  
  70. /////////////////////////
  71. // Hull Suite INPUTS ///
  72. ///////////////////////
  73. srcHull = input(close, title="Source", group="Hull Suite")
  74. modeSwitch = input.session("Ehma", title="Hull Variation", options=["Hma", "Thma", "Ehma"], group="Hull Suite")
  75. length = input(55, title="Length(180-200 for floating S/R , 55 for swing entry)")
  76. switchColor = input(true, "Color Hull according to trend?", group="Hull Suite")
  77. visualSwitch = input(true, title="Show as a Band?", group="Hull Suite")
  78. thicknesSwitch = input(1, title="Line Thickness", group="Hull Suite")
  79. transpSwitch = input.int(40, title="Band Transparency",step=5, group="Hull Suite")
  80.  
  81. //////////////////////////
  82. // FOLLOW LINE INPUTS ///
  83. ////////////////////////
  84. BBperiod = input.int(defval = 21, title = "BB Period", minval = 1, group = "Following Line ")
  85. BBdeviations = input.float(defval = 1.00, title = "BB Deviations", minval = 0.1, step=0.05, group = "Following Line")
  86. UseATRfilter = input.bool(defval = true, title = "ATR Filter", group = "Following Line ")
  87. ATRperiod = input.int(defval = 5, title = "ATR Period", minval = 1, group = "Following Line ")
  88.  
  89. //////////////////////////
  90. // TSI INPUTS ///////////
  91. ////////////////////////
  92. tsiSrc = input(title="Source", defval=close, group = "TSI")
  93. longLength = input.int(title="Long Length", defval=6, group = "TSI")
  94. shortLength = input.int(title="Short Length" , defval=13, group = "TSI")
  95. signalLength = input.int(title="Signal Length" , defval=4, group = "TSI")
  96.  
  97. //////////////////////////
  98. // Range Filter Logic ///
  99. ////////////////////////
  100. // Fast
  101. smoothrng(x, t, m) =>
  102. wper = t * 2 - 1
  103. avrng = ta.ema(math.abs(x - x[1]), t)
  104. smoothrng = ta.ema(avrng, wper) * m
  105. smoothrng
  106. smrng = smoothrng(src, per, mult)
  107.  
  108. rngfilt(x, r) =>
  109. rngfilt = x
  110. rngfilt := x > nz(rngfilt[1]) ? x - r < nz(rngfilt[1]) ? nz(rngfilt[1]) : x - r :
  111. x + r > nz(rngfilt[1]) ? nz(rngfilt[1]) : x + r
  112. rngfilt
  113. filt = rngfilt(src, smrng)
  114.  
  115. upward = 0.0
  116. upward := filt > filt[1] ? nz(upward[1]) + 1 : filt < filt[1] ? 0 : nz(upward[1])
  117. downward = 0.0
  118. downward := filt < filt[1] ? nz(downward[1]) + 1 : filt > filt[1] ? 0 : nz(downward[1])
  119.  
  120. filtcolor = upward > 0 ? rngFilterColorUp : downward > 0 ? rngFilterColorDown : color.orange
  121.  
  122. filtplot = plot(filt, color=filtcolor, linewidth=3, title="Fast Range Filter ")
  123. // Slow
  124. smoothrngSlow(x, t, m) =>
  125. wper = t * 2 - 1
  126. avrng = ta.ema(math.abs(x - x[1]), t)
  127. smoothrngSlow = ta.ema(avrng, wper) * m
  128. smoothrngSlow
  129. smrngSlow = smoothrngSlow(srcSlow, perSlow, multSlow)
  130.  
  131. rngfiltSlow(x, r) =>
  132. rngfilt = x
  133. rngfilt := x > nz(rngfilt[1]) ? x - r < nz(rngfilt[1]) ? nz(rngfilt[1]) : x - r :
  134. x + r > nz(rngfilt[1]) ? nz(rngfilt[1]) : x + r
  135. rngfilt
  136. filtSlow = rngfiltSlow(srcSlow, smrngSlow)
  137.  
  138. upwardSlow = 0.0
  139. upwardSlow := filtSlow > filtSlow[1] ? nz(upwardSlow[1]) + 1 : filtSlow < filtSlow[1] ? 0 : nz(upwardSlow[1])
  140. downwardSlow = 0.0
  141. downwardSlow := filtSlow < filtSlow[1] ? nz(downwardSlow[1]) + 1 : filtSlow > filtSlow[1] ? 0 : nz(downwardSlow[1])
  142.  
  143. filtcolorSlow = upwardSlow > 0 ? rngFilterColorUpSlow : downwardSlow > 0 ? rngFilterColorDownSlow : color.orange
  144.  
  145. filtplotSlow = plot(followLineUseATR == false ? filtSlow : na, color=filtcolorSlow, linewidth=3, title="Slow Range Filter")
  146.  
  147.  
  148. ////////////////////////
  149. // Hull Suite Logic ///
  150. //////////////////////
  151.  
  152.  
  153. HMA(_srcHull, _length) => ta.wma(2 * ta.wma(_srcHull, _length / 2) - ta.wma(_srcHull, _length), math.round(math.sqrt(_length)))
  154. EHMA(_srcHull, _length) => ta.ema(2 * ta.ema(_srcHull, _length / 2) - ta.ema(_srcHull, _length), math.round(math.sqrt(_length)))
  155. THMA(_srcHull, _length) => ta.wma(ta.wma(_srcHull,_length / 3) * 3 - ta.wma(_srcHull, _length / 2) - ta.wma(_srcHull, _length), _length)
  156.  
  157. Mode(modeSwitch, src, len) =>
  158. modeSwitch == "Hma" ? HMA(src, len) :
  159. modeSwitch == "Ehma" ? EHMA(src, len) :
  160. modeSwitch == "Thma" ? THMA(src, len/2) : na
  161.  
  162. _hull = Mode(modeSwitch, src, int(length))
  163. HULL = _hull
  164. MHULL = HULL[0]
  165. SHULL = HULL[2]
  166. // HIGHER TIMEFRAME HULL
  167. HULL1 = request.security(syminfo.ticker, "1", _hull)
  168. HULL5 = request.security(syminfo.ticker, "5", _hull)
  169. HULL15 = request.security(syminfo.ticker, "15", _hull)
  170. HULL3 = request.security(syminfo.ticker, "3", _hull)
  171.  
  172. hullColor = switchColor ? (HULL > HULL[2] ? hullColorUp : hullColorDown) : #ff9800
  173. // Fi1 = plot(MHULL, title="MHULL", color=hullColor, linewidth=thicknesSwitch, transp=50)
  174. Fi1 = plot(MHULL, title="MHULL", color=color.new(hullColor, 50), linewidth=thicknesSwitch)
  175. // Fi2 = plot(visualSwitch ? SHULL : na, title="SHULL", color=hullColor, linewidth=thicknesSwitch, transp=50)
  176. Fi2 = plot(visualSwitch ? SHULL : na, title="SHULL", color=color.new(hullColor, 50), linewidth=thicknesSwitch)
  177.  
  178. fill(Fi1, Fi2, title="Band Filler", color=color.new(hullColor, transpSwitch))
  179.  
  180.  
  181. /////////////////////////
  182. // Follow Line Logic ///
  183. ///////////////////////
  184. BBUpper=ta.sma (close,BBperiod)+ta.stdev(close, BBperiod)*BBdeviations
  185. BBLower=ta.sma (close,BBperiod)-ta.stdev(close, BBperiod)*BBdeviations
  186.  
  187. TrendLine = 0.0
  188. iTrend = 0.0
  189. buy = 0.0
  190. sell = 0.0
  191.  
  192. atr = ta.atr(ATRperiod)
  193.  
  194. BBSignal = close>BBUpper? 1 : close<BBLower? -1 : 0
  195.  
  196. if BBSignal == 1 and UseATRfilter == 1
  197. TrendLine:=low-atr
  198. if TrendLine<TrendLine[1]
  199. TrendLine:=TrendLine[1]
  200. if BBSignal == -1 and UseATRfilter == 1
  201. TrendLine:=high+atr
  202. if TrendLine>TrendLine[1]
  203. TrendLine:=TrendLine[1]
  204. if BBSignal == 0 and UseATRfilter == 1
  205. TrendLine:=TrendLine[1]
  206.  
  207. if BBSignal == 1 and UseATRfilter == 0
  208. TrendLine:=low
  209. if TrendLine<TrendLine[1]
  210. TrendLine:=TrendLine[1]
  211. if BBSignal == -1 and UseATRfilter == 0
  212. TrendLine:=high
  213. if TrendLine>TrendLine[1]
  214. TrendLine:=TrendLine[1]
  215. if BBSignal == 0 and UseATRfilter == 0
  216. TrendLine:=TrendLine[1]
  217.  
  218. iTrend:=iTrend[1]
  219. if TrendLine>TrendLine[1]
  220. iTrend:=1
  221. if TrendLine<TrendLine[1]
  222. iTrend:=-1
  223.  
  224. buy:= iTrend[1]==-1 and iTrend==1 and followLineUseATR == true ? 1 : na
  225. sell:= iTrend[1]==1 and iTrend==-1 and followLineUseATR == true ? 1 : na
  226.  
  227. plot(followLineUseATR == true ? TrendLine : na, color=iTrend > 0? fliColorUp : fliColorDown ,style=plot.style_line,linewidth=2,title="Trend Line")
  228.  
  229.  
  230. //////////////////////////
  231. // TSI Logic ///////////
  232. ////////////////////////
  233.  
  234. mom = ta.change(tsiSrc)
  235. numerator = ta.ema(ta.ema(mom, longLength), shortLength)
  236. denominator = ta.ema(ta.ema(math.abs(mom), longLength), shortLength)
  237. tsi = 100 * numerator / denominator
  238. signal = ta.ema(tsi, signalLength)
  239.  
  240. trendColor = tsi > signal ? hullColorUp : hullColorDown
  241. // tsiColor = applyRibbonFilling ? trendColor : #ff3e7d
  242. // signalColor = applyRibbonFilling ? trendColor : #3c78d8
  243.  
  244. TSI1 = request.security(syminfo.ticker, "1", trendColor)
  245. TSI3 = request.security(syminfo.ticker, "3", trendColor)
  246. TSI5 = request.security(syminfo.ticker, "5", trendColor)
  247. TSI15 = request.security(syminfo.ticker, "15", trendColor)
  248.  
  249.  
  250. ///////////////////////////
  251. // STATUS WINDOW LOGIC ///
  252. /////////////////////////
  253.  
  254.  
  255. if(showStatusWindow == true )
  256. var table statuswindow = table.new(position.top_right, 100, 100, border_width=2)
  257.  
  258. HullTxt1 = " Hull 1 min "
  259. TsiTxt1 = " TSI 1 min "
  260. if(HULL1 > HULL1[2])
  261. table.cell(statuswindow, 1, 1, text=HullTxt1, bgcolor=hullColorUp, text_color=color.white, text_size=size.small)
  262. else
  263. table.cell(statuswindow, 1, 1, text=HullTxt1, bgcolor=hullColorDown, text_color=color.white, text_size=size.small)
  264. table.cell(statuswindow, 1, 2, text=TsiTxt1, bgcolor=TSI1, text_color=color.white, text_size=size.small)
  265.  
  266. HullTxt3 = " Hull 3 min "
  267. TsiTxt3 = " TSI 3 min "
  268. if(HULL3 > HULL3[2])
  269. table.cell(statuswindow, 2, 1, text=HullTxt3, bgcolor=hullColorUp, text_color=color.white, text_size=size.small)
  270. else
  271. table.cell(statuswindow, 2, 1, text=HullTxt3, bgcolor=hullColorDown, text_color=color.white, text_size=size.small)
  272. table.cell(statuswindow, 2, 2, text=TsiTxt3, bgcolor=TSI3, text_color=color.white, text_size=size.small)
  273.  
  274.  
  275. HullTxt5 = " Hull 5 min "
  276. TsiTxt5 = " TSI 5 min "
  277.  
  278. if(HULL5 > HULL5[2])
  279. table.cell(statuswindow, 3, 1, text=HullTxt5, bgcolor=hullColorUp, text_color=color.white, text_size=size.small)
  280. else
  281. table.cell(statuswindow, 3, 1, text=HullTxt5, bgcolor=hullColorDown, text_color=color.white, text_size=size.small)
  282. table.cell(statuswindow, 3, 2, text=TsiTxt5, bgcolor=TSI5, text_color=color.white, text_size=size.small)
  283.  
  284. HullTxt15 = " Hull 15 min "
  285. TsiTxt15 = " TSI 15 min "
  286. if(HULL15 > HULL15[2])
  287. table.cell(statuswindow, 4, 1, text=HullTxt15, bgcolor=hullColorUp, text_color=color.white, text_size=size.small)
  288. else
  289. table.cell(statuswindow, 4, 1, text=HullTxt15, bgcolor=hullColorDown, text_color=color.white, text_size=size.small)
  290. table.cell(statuswindow, 4, 2, text=TsiTxt15, bgcolor=TSI15, text_color=color.white, text_size=size.small)
  291.  
  292.  
  293. ////////////////////////////////////////////
  294. // ALERTS & LABELS BASED ON ENTRY POINT ///
  295. //////////////////////////////////////////
  296.  
  297.  
  298.  
  299. //////////////////////////////////
  300. // STRATEGY ENTRY POINT LOGIC ///
  301. ////////////////////////////////
  302. // if(inDate and isStrategy)
  303. // // RANGE FILTER ENTRY LONG WITH HULL
  304. // if(filtcolor[1] == rngFilterColorDown and filtcolor == rngFilterColorUp)
  305. // strategy.entry("rngFiltLong", strategy.long, when = HULL > HULL[2])
  306. // // RANGE FILTER ENTRY SHORT WITH HULL
  307. // if(filtcolor[1] == rngFilterColorUp and filtcolor == rngFilterColorDown)
  308. // strategy.entry("rngFiltShort", strategy.short, when = HULL < HULL[2])
  309. // // FOLLOW LINE OR SLOW RANGE FILTER ENTRY, IF ENABLED
  310. // if(followRngFilterEntry == true)
  311. // if(followLineUseATR == true)
  312. // // FOLLOWING LINE ENTRY SHORT WITH HULL
  313. // if(buy == 1)
  314. // strategy.entry("fliLong", strategy.long, when = HULL > HULL[2])
  315. // // FOLLOWING LINE ENTRY SHORT WITH HULL
  316. // if(sell == 1)
  317. // strategy.entry("fliShort", strategy.short, when = HULL < HULL[2])
  318. // else
  319. // // SLOW RANGE FILTER ENTRY WITH HULL
  320. // if(filtcolorSlow[1] == rngFilterColorDownSlow and filtcolorSlow == rngFilterColorUpSlow)
  321. // strategy.entry("slowRngFiltLong", strategy.long, when = HULL > HULL[2])
  322. // // SLOW RANGE FILTER ENTRY SHORT WITH HULL
  323. // if(filtcolorSlow[1] == rngFilterColorUpSlow and filtcolorSlow == rngFilterColorDownSlow)
  324. // strategy.entry("slowRngFiltShort", strategy.short, when = HULL < HULL[2])
  325.  
  326. // if(isPointRule)
  327. // strategy.exit('exitRngFiltLong', 'rngFiltLong', profit=pointRulePoints)
  328. // strategy.exit('exitRngFiltShort', 'rngFiltShort', profit=pointRulePoints)
  329. // strategy.exit('exitSlowRngFiltLong', 'slowRngFiltLong', profit=pointRulePoints)
  330. // strategy.exit('exitSlowRngFiltShort', 'slowRngFiltShort', profit=pointRulePoints)
  331. // strategy.exit('exitFliLong', 'fliLong', profit=pointRulePoints, when = sell == 1)
  332. // strategy.exit('exitFliShort', 'fliShort', profit=pointRulePoints , when = buy == 1)
  333.  
  334. // closingRuleLong = exitRule == "Hull" ? HULL > HULL[2] : exitRule == "RangeFilter" ? (filtcolor[2] == rngFilterColorUp and filtcolor == rngFilterColorDown) : exitRule == "Following/SlowRangeFilter" ? followLineUseATR == false? (sell == 1): (filtcolor[2] == rngFilterColorUp and filtcolor == rngFilterColorDown): na
  335. // closingRuleShort = exitRule == "Hull" ? HULL > HULL[2] : exitRule == "RangeFilter" ? (filtcolor[1] == rngFilterColorDown and filtcolor == rngFilterColorUp) : exitRule == "Following/SlowRangeFilter" ? followLineUseATR == true ? (buy == 1) : (filtcolor[2] == rngFilterColorDown and filtcolor == rngFilterColorUp) : na
  336.  
  337. // strategy.close("rngFiltLong", when = closingRuleLong)
  338. // strategy.close("rngFiltShort", when = closingRuleShort)
  339. // strategy.close("slowRngFiltLong", when = closingRuleLong)
  340. // strategy.close("slowRngFiltShort", when = closingRuleShort)
  341. // strategy.close("fliLong", when = closingRuleLong)
  342. // strategy.close("fliShort", when = closingRuleShort)
  343.  
  344.  
  345.  
Advertisement
Add Comment
Please, Sign In to add comment