Advertisement
xmd79

Function Cosine Wave

Jan 5th, 2023 (edited)
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 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. // © DarkPoolTrading
  3. // Based on the Function Sine Wave Indicator by RicardoSantos
  4.  
  5. //@version=5
  6. indicator(title='Function Cosine Wave', precision=12)
  7.  
  8. method1 = input.string('Median', options=['EMA', 'Median', 'SMA'])
  9. length1 = input(5)
  10. mult1 = input.int(1, minval=0, maxval=1)
  11.  
  12. method2 = input.string('Median', options=['EMA', 'Median', 'SMA'])
  13. length2 = input(7)
  14. mult2 = input.int(1, minval=0, maxval=1)
  15.  
  16. method3 = input.string('Median', options=['EMA', 'Median', 'SMA'])
  17. length3 = input(9)
  18. mult3 = input.int(1, minval=0, maxval=1)
  19. //----
  20. Avg(x, length, method) =>
  21. sma_1 = ta.sma(x, length)
  22. ema_1 = ta.ema(x, length)
  23. percentile_linear_interpolation_1 = ta.percentile_linear_interpolation(x, length, 50)
  24. method == 'SMA' ? sma_1 : method == 'EMA' ? ema_1 : percentile_linear_interpolation_1
  25. //----
  26. a1 = ta.highest(length1) - math.max(close, open)
  27. b1 = math.min(close, open) - ta.lowest(length1)
  28. c1 = math.max(close, open) + a1 * mult1
  29. d1 = math.min(close, open) - b1 * mult1
  30.  
  31. a2 = ta.highest(length2) - math.max(close, open)
  32. b2 = math.min(close, open) - ta.lowest(length2)
  33. c2 = math.max(close, open) + a2 * mult2
  34. d2 = math.min(close, open) - b2 * mult2
  35.  
  36. a3 = ta.highest(length3) - math.max(close, open)
  37. b3 = math.min(close, open) - ta.lowest(length3)
  38. c3 = math.max(close, open) + a3 * mult3
  39. d3 = math.min(close, open) - b3 * mult3
  40. //----
  41. e1 = Avg(c1, length1, method1)
  42. f1 = Avg(d1, length1, method1)
  43. g1 = 0
  44. cross_1 = ta.cross(close, f1)
  45. g1 := ta.cross(close, e1) ? 1 : cross_1 ? 0 : nz(g1[1])
  46.  
  47. e2 = Avg(c2, length2, method2)
  48. f2 = Avg(d2, length2, method2)
  49. g2 = 0
  50. cross_2 = ta.cross(close, f2)
  51. g2 := ta.cross(close, e2) ? 1 : cross_2 ? 0 : nz(g2[1])
  52.  
  53. e3 = Avg(c3, length3, method3)
  54. f3 = Avg(d3, length3, method3)
  55. g3 = 0
  56. cross_3 = ta.cross(close, f3)
  57. g3 := ta.cross(close, e3) ? 1 : cross_3 ? 0 : nz(g3[1])
  58. //---
  59. hilo1 = g1 * f1 + (1 - g1) * e1
  60. css1 = g1 == 1 ? color.green : color.red
  61. plot(hilo1, color=css1, style=plot.style_line, show_last=1, linewidth=1, trackprice=true, transp=0)
  62.  
  63. hilo2 = g2 * f2 + (1 - g2) * e2
  64. css2 = g2 == 1 ? color.green : color.red
  65. plot(hilo2, color=css2, style=plot.style_line, show_last=1, linewidth=1, trackprice=true, transp=0)
  66.  
  67. hilo3 = g3 * f3 + (1 - g3) * e3
  68. css3 = g3 == 1 ? color.green : color.red
  69. plot(hilo3, color=css3, style=plot.style_line, show_last=1, linewidth=1, trackprice=true, transp=0)
  70.  
  71.  
  72. //end of this part
  73.  
  74.  
  75.  
  76. wave_height = input(100)
  77. wave_duration = input(161)
  78. wave_duration_bigwave = input(217)
  79.  
  80. n = bar_index
  81.  
  82. f_cosine_wave(_wave_height, _wave_duration) =>
  83. _pi = 3.14159265359
  84. _w = 2 * _pi / _wave_duration
  85. _cosine_wave = _wave_height * math.sin(_w * n + _pi / 2)
  86. _cosine_wave
  87.  
  88. f_cosine_wave2(_wave_height, _wave_duration_bigwave) =>
  89. _pi = 3.14159265359
  90. _w2 = 2 * _pi / _wave_duration_bigwave
  91. _cosine_wave2 = _wave_height * math.sin(_w2 * n + _pi / 2)
  92. _cosine_wave2
  93.  
  94. wave = f_cosine_wave(wave_height, wave_duration)
  95. //wave2 = f_cosine_wave(wave_height, close)
  96. big_wave = f_cosine_wave2(wave_height, wave_duration_bigwave)
  97.  
  98. plot(series=wave, title='Normal', color=color.new(#FFFFFF, 0))
  99. plot(series=big_wave, title='Normal', color=color.new(#FFFFFF, 0))
  100.  
  101. //plot(series=wave2, title='Price as cycle duration', color=#C9A0DC)
  102.  
  103. //end of this part
  104.  
  105.  
  106. //end of this part
  107.  
  108.  
  109. //study(title="[RS]Compounded Risk Score", shorttitle="CRS")
  110. // if you took a trade for every bars close for the duration of the length
  111. // how would those trades fair atm?
  112. // values closer to (100 | -100) are better for its (bull | bear), closer to 0 will be closer to break eaven.
  113. // this indicator is optimal for trending markets.
  114.  
  115. length = input(100)
  116. avg_length = input(25)
  117.  
  118. f_compound_risk(_source, _length) =>
  119. _total = 0.0
  120. _absolute = 0.0
  121. for _i = 1 to _length by 1
  122. _container0 = _total
  123. _container1 = _absolute
  124. _total := _container0 + _source[0] - _source[_i]
  125. _absolute := _container1 + math.abs(_source[0] - _source[_i])
  126. _absolute
  127. if _total > 0
  128. _container = _total
  129. _total := _total / _absolute
  130. _total
  131. else
  132. _container = _total
  133. _total := 0 - math.abs(_container) / _absolute
  134. _total
  135. _total * 100
  136.  
  137. //inp = input(close)
  138.  
  139. cr = f_compound_risk(wave, length)
  140. ma = ta.ema(cr, avg_length)
  141.  
  142. plot(series=cr, color=color.new(color.black, 0))
  143. plot(series=ma, color=color.new(color.navy, 0))
  144. hline(100)
  145. hline(50)
  146. hline(0)
  147. hline(-50)
  148. hline(-100)
  149.  
  150. //end of this part
  151.  
  152.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement