xmd79

ehlers_inverse_fisher_transform_of_adaptive_stochastic

Jan 10th, 2023
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.08 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. // © palitoj_endthen
  3.  
  4. //@version=5
  5. indicator(title = 'ehlers_inverse_fisher_transform_of_adaptive_stochastic', shorttitle = 'inverse_fisher_adaptive_stochastic', overlay = false)
  6.  
  7. // input
  8. hp_period = input.int(defval = 48, title = 'High-pass Period', group = 'Value', tooltip = 'Determines the length of High-pass Period')
  9. src = input.source(defval = close, title = 'Source', group = 'Options', tooltip = 'Determines the source of input data, default to close')
  10.  
  11.  
  12. // variable
  13. avg_length = 3
  14. m = 0
  15. n = 0
  16. x = 0.00
  17. y = 0.00
  18. alpha1 = 0.00
  19. hp = 0.00
  20. a1 = 0.00
  21. b1 = 0.00
  22. c1 = 0.00
  23. c2 = 0.00
  24. c3 = 0.00
  25. filt = 0.00
  26. sx = 0.00
  27. sy = 0.00
  28. sxx = 0.00
  29. syy = 0.00
  30. sxy = 0.00
  31. sp = 0.00
  32. spx = 0.00
  33. max_pwr = 0.00
  34. dominant_cycle = 0.00
  35.  
  36.  
  37. // arrays
  38. var corr = array.new_float(50)
  39. var cosine_part = array.new_float(50)
  40. var sine_part = array.new_float(50)
  41. var sq_sum = array.new_float(50)
  42. var r1 = array.new_float(50)
  43. var r2 = array.new_float(50)
  44. var pwr = array.new_float(50)
  45.  
  46.  
  47. // compute inferse fisher transform of adaptive stochastic
  48. // highpass filter cyclic components whose periods are shorter than 48 bars
  49. pi = 2*math.asin(1)
  50. alpha1 := (math.cos(.707*2*pi/hp_period) + math.sin(.707*2*pi/hp_period)-1)/math.cos(.707*2*pi/hp_period)
  51. hp := (1-alpha1/2)*(1-alpha1/2)*(src-2*src[1]+src[2])+2*(1-alpha1)*nz(hp[1])-(1-alpha1)*(1-alpha1)*nz(hp[2])
  52.  
  53. // smooth with super smoother filter
  54. a1 := math.exp(-1.414*3.14159/10)
  55. b1 := 2*a1*math.cos(1.414*180/10)
  56. c2 := b1
  57. c3 := -a1*a1
  58. c1 := 1-c2-c3
  59. filt := c1*(hp+hp[1])/2+c2*nz(filt[1])+c3*nz(filt[2])
  60.  
  61. // pearson correlation for each value of lag
  62. for lag = 0 to 48
  63. // set the averaging length as m
  64. m := avg_length
  65. if avg_length == 0
  66. m := lag
  67. // initialize correlation sums
  68. sx := 0.00
  69. sy := 0.00
  70. sxx := 0.00
  71. syy := 0.00
  72. sxy := 0.00
  73. // advance samples of both data streams and sum pearson components
  74. for count = 0 to m-1
  75. x := filt[count]
  76. y := filt[lag+count]
  77. sx := sx+x
  78. sy := sy+y
  79. sxx := sxx+x*x
  80. sxy := sxy+x*y
  81. syy := syy+y*y
  82. // compute correlation for each value of lag
  83. if (m*sxx-sx*sx)*(m*syy-sy*sy) > 0
  84. array.set(corr, lag, ((m*sxy-sx*sy)/math.sqrt((m*sxx-sx*sx)*(m*syy-sy*sy))))
  85.  
  86. for period = 10 to 48
  87. array.set(cosine_part, period, 0)
  88. array.set(sine_part, period, 0)
  89. for n2 = 3 to 48
  90. array.set(cosine_part, period, nz(array.get(cosine_part, period))+nz(array.get(corr, n2))*math.cos(360*n2/period))
  91. array.set(sine_part, period, nz(array.get(sine_part, period))+nz(array.get(corr, n2))*math.sin(360*n2/period))
  92. array.set(sq_sum, period, nz(array.get(cosine_part, period)) * nz(array.get(cosine_part, period))+nz(array.get(sine_part, period))*nz(array.get(sine_part, period)))
  93.  
  94. for period2 = 10 to 48
  95. array.set(r2, period2, nz(array.get(r1, period2)))
  96. array.set(r1, period2, .2*nz(array.get(sq_sum, period2))*nz(array.get(sq_sum, period2))+.8*nz(array.get(r2, period2)))
  97.  
  98. // find maximum power level for normalization
  99. max_pwr := .995*max_pwr
  100.  
  101. for period3 = 10 to 48
  102. if nz(array.get(r1, period3)) > max_pwr
  103. max_pwr := nz(array.get(r1, period3))
  104.  
  105. for period4 = 3 to 48
  106. array.set(pwr, period4, nz(array.get(r1, period4))/max_pwr)
  107.  
  108. // compute the dominant cycle using the cg of the spectrum
  109. for period5 = 10 to 48
  110. if nz(array.get(pwr, period5)) >= .5
  111. spx := spx+period5*nz(array.get(pwr, period5))
  112. sp := sp+nz(array.get(pwr, period5))
  113.  
  114. if sp != 0
  115. dominant_cycle := spx/sp
  116. if dominant_cycle < 10
  117. dominant_cycle := 10
  118. if dominant_cycle > 48
  119. dominant_cycle := 48
  120.  
  121. // stochastic compuation start here
  122. highest = 0.00
  123. lowest = 0.00
  124. stoc = 0.00
  125. smooth_num = 0.00
  126. smooth_denom = 0.00
  127. adaptive_stoc = 0.00
  128.  
  129. highest := filt
  130. lowest := filt
  131.  
  132. for i = 0 to dominant_cycle-1
  133. if filt[i] > highest
  134. highest := filt[i]
  135. if filt[i] < lowest
  136. lowest := filt[i]
  137.  
  138. stoc := (filt-lowest)/(highest-lowest)
  139. adaptive_stoc := c1*(stoc+stoc[1])/2+c2*nz(adaptive_stoc[1])+c3*nz(adaptive_stoc[2])
  140.  
  141. value1 = 0.00
  142. ifish = 0.00
  143. trigger = 0.00
  144.  
  145. value1 := 2*(adaptive_stoc-.5)
  146. ifish := (math.exp(2*3*value1)-1)/(math.exp(2*3*value1)+1)
  147. trigger := .9*ifish[1]
  148.  
  149. // visualize
  150. color_con = ifish > trigger and ifish > ifish[1] ? color.green : color.red
  151. color_con_ = ifish > trigger and ifish > ifish[1]
  152.  
  153. plot(ifish, color = color_con, linewidth = 3)
  154. plot(trigger, color = color.new(color.aqua, 50))
  155.  
  156.  
  157. // create alert
  158. alertcondition((not color_con_[1] and color_con_), title = 'Entry', message = 'Buy/Long entry detected')
  159. alertcondition((color_con_ and not color_con_), title = 'Close', message = 'Sell/Short entry detected')
  160.  
  161.  
  162. // // strategy test
  163. // long_condition = not (color_con_[1] and color_con_)
  164. // if long_condition
  165. // strategy.entry('long', strategy.long)
  166.  
  167. // short_condition = color_con_[1] and not color_con_
  168. // if short_condition
  169. // strategy.exit('exit', 'long', profit = 10, loss = 1)
  170.  
Advertisement
Add Comment
Please, Sign In to add comment