xmd79

Gaussian Fisher Transform Price Reversals

May 5th, 2023
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.05 KB | None | 0 0
  1. // © Amjad_S
  2. //@version=5
  3. //------------------------------------------------------------------------------
  4. // Indictort, Library Import & User Inputs
  5. //-----------------------------------------------------------------------------{
  6. indicator("Gaussian Fisher Transform Price Reversals", shorttitle = "FTR", overlay = true)
  7.  
  8. // Use Inputs //
  9. src = input.source(hl2, title = "Source of transformation")
  10. lkb = input.int(14, title = "Fisher Transformation lookback period")
  11. lkb_volu = input.int(14, "Volume Average and stdev lookback")
  12. lkb_avg = input.int(5, title = "Lagged Fisher Transformation Trigger lookback period")
  13. alpha = input.float(0.33, title = "Normalized Price smoothing filter", minval = 0, maxval = 1)
  14. sigma = input.float(1.00, title = "Standard deviation / Fisher value for valid S&R zone")
  15. Q = input.int(20, title = "Max labels")
  16. fr_col = input.color(color.red, title = "Fisher Resistance color")
  17. fs_col = input.color(color.green, title = "Fisher Support color")
  18.  
  19.  
  20. //------------------------------------------------------------------------------
  21. // Function Deriviations
  22. //-----------------------------------------------------------------------------{
  23. // Gussian Fisher Transform with Fisher Transform Average
  24. FT(src_, lkb_, lkb_avg_) =>
  25. // max and min of time series
  26. float maxH = ta.highest(high, lkb_)
  27. float minL = ta.lowest(low, lkb_)
  28. // Gussian normal distribution Transform
  29. var float val1 = 0
  30. // alpha smoothing of 0.33
  31. val1 := na(val1[1]) ? alpha * 2 * ((src_ - minL) / (maxH - minL) - 0.5) + (0.67 * nz(val1[1], 0)) :
  32. alpha * 2 * ((src_ - minL) / (maxH - minL) - 0.5) + (0.67 * val1[1])
  33. if val1 > 0.99
  34. val1 := 0.999
  35. else if val1 < -0.99
  36. val1 := -0.999
  37. // fish transform
  38. var float fish_trsf = 0
  39. fish_trsf := na(fish_trsf[1]) ? 0.5 * math.log((1 + val1) / (1 - val1)) + (0.5 * nz(fish_trsf[1], 0)) :
  40. 0.5 * math.log((1 + val1) / (1 - val1)) + (0.5 * fish_trsf[1])
  41. // Trigger line
  42. fish_trsf_lag = ta.sma(fish_trsf, lkb_avg)
  43. // Function Sub function call
  44. [fish_trsf, fish_trsf_lag]
  45.  
  46. //------------------------------------------------------------------------------
  47. // Function Calls / Calculations and plots
  48. //-----------------------------------------------------------------------------{
  49. [fish_trsf, fish_trsf_lag] = FT(src, lkb, lkb_avg)
  50.  
  51. // Calculate volatility bars //
  52. i(q) =>
  53. barstate.isrealtime ?
  54. q[1] : q[0]
  55.  
  56. vol = ta.sma(volume, lkb_volu)
  57. dev = ta.stdev(volume, lkb_volu)
  58.  
  59. // pos
  60. posdev1 = vol + (dev)
  61. posdev2 = vol + (dev*2)
  62. posdev3 = vol + (dev*3)
  63. // neg
  64. negdev1 = vol - (dev)
  65. negdev2 = vol - (dev*2)
  66. negdev3 = vol - (dev*3)
  67.  
  68. // plot relative volume bars
  69. barpos1 = color.rgb(255, 19, 157)
  70. barpos2 = color.rgb(204, 0, 255)
  71. barpos3 = color.rgb(111, 0, 255)
  72. barneg1 = #0066ff
  73. barneg2 = #00e1ff
  74. barneg3 = #91f2ff
  75.  
  76. barcolor(volume >= i(posdev1) ? barpos1 :
  77. volume >= i(posdev2) ? barpos2 :
  78. volume >= i(posdev3) ? barpos3 :
  79. volume <= i(negdev1) ? barneg1 :
  80. volume <= i(negdev2) ? barneg2 :
  81. volume <= i(negdev3) ? barneg3 :
  82. na)
  83.  
  84. // Reversals & Reversal plots //
  85. res = ta.crossunder(fish_trsf, fish_trsf_lag) and fish_trsf >= sigma
  86. sup = ta.crossover(fish_trsf, fish_trsf_lag) and fish_trsf <= -1*(sigma)
  87.  
  88. // support and resistance lines
  89. var line[] linetop = array.new<line>()
  90. var line[] linebot = array.new<line>()
  91. // labels
  92. var label[] linetop_label = array.new<label>()
  93. var label[] linebot_label = array.new<label>()
  94.  
  95. // resistance reversal
  96. if res[1]
  97. array.unshift(linetop, line.new(bar_index, high, bar_index + 100, high, extend = extend.none, color = fr_col))
  98. array.unshift(linetop_label, label.new(bar_index, high, yloc = yloc.abovebar, text = "FT : "+ str.tostring(math.round(fish_trsf[1], 2)) +" ", color = na, textcolor = fr_col, style = label.style_none, size = size.small))
  99. // support reversal
  100. if sup[1]
  101. array.unshift(linebot, line.new(bar_index, low, bar_index + 100, low, extend = extend.none, color = fs_col))
  102. array.unshift(linebot_label, label.new(bar_index, low, yloc = yloc.belowbar,text = "FT : "+ str.tostring(math.round(fish_trsf[1], 2)) +" ", color = na, textcolor = fs_col, style = label.style_none, size = size.small))
  103.  
  104. // ploting resistance reversal
  105. for i = (array.size(linetop) > 0 ? array.size(linetop) - 1 : na) to 0
  106. l = line.get_price(array.get(linetop, i), bar_index)
  107. if high > l
  108. line.set_x2(array.get(linetop, i), bar_index)
  109. array.remove(linetop, i)
  110. else if high < l
  111. line.set_x2(array.get(linetop, i), bar_index)
  112.  
  113. // ploting support reversal
  114. for i = (array.size(linebot) > 0 ? array.size(linebot) - 1 : na) to 0
  115. l = line.get_price(array.get(linebot, i), bar_index)
  116. if low < l
  117. line.set_x2(array.get(linebot, i), bar_index)
  118. array.remove(linebot, i)
  119. else if low > l
  120. line.set_x2(array.get(linebot, i), bar_index)
  121.  
  122. if array.size(label.all) > Q
  123. label.delete(array.get(label.all, 0))
  124.  
  125.  
  126.  
Advertisement
Add Comment
Please, Sign In to add comment