Advertisement
xmd79

Function cosine wave updated

Jan 3rd, 2023
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.79 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(50)
  78. n = bar_index
  79.  
  80. f_cosine_wave(_wave_height, _wave_duration) =>
  81. _pi = 3.14159265359
  82. _w = 2 * _pi / _wave_duration
  83. _cosine_wave = _wave_height * math.sin(_w * n + _pi / 2)
  84. _cosine_wave
  85.  
  86. wave = f_cosine_wave(wave_height, wave_duration)
  87. wave2 = f_cosine_wave(wave_height, close)
  88. plot(series=wave, title='Normal', color=color.new(#FFFFFF, 0))
  89. //plot(series=wave2, title='Price as cycle duration', color=#C9A0DC)
  90.  
  91. //end of this part
  92.  
  93.  
  94. //end of this part
  95.  
  96.  
  97. //study(title="[RS]Compounded Risk Score", shorttitle="CRS")
  98. // if you took a trade for every bars close for the duration of the length
  99. // how would those trades fair atm?
  100. // values closer to (100 | -100) are better for its (bull | bear), closer to 0 will be closer to break eaven.
  101. // this indicator is optimal for trending markets.
  102.  
  103. length = input(100)
  104. avg_length = input(25)
  105.  
  106. f_compound_risk(_source, _length) =>
  107. _total = 0.0
  108. _absolute = 0.0
  109. for _i = 1 to _length by 1
  110. _container0 = _total
  111. _container1 = _absolute
  112. _total := _container0 + _source[0] - _source[_i]
  113. _absolute := _container1 + math.abs(_source[0] - _source[_i])
  114. _absolute
  115. if _total > 0
  116. _container = _total
  117. _total := _total / _absolute
  118. _total
  119. else
  120. _container = _total
  121. _total := 0 - math.abs(_container) / _absolute
  122. _total
  123. _total * 100
  124.  
  125. //inp = input(close)
  126.  
  127. cr = f_compound_risk(wave, length)
  128. ma = ta.ema(cr, avg_length)
  129.  
  130. plot(series=cr, color=color.new(color.black, 0))
  131. plot(series=ma, color=color.new(color.navy, 0))
  132. hline(100)
  133. hline(50)
  134. hline(0)
  135. hline(-50)
  136. hline(-100)
  137.  
  138. //end of this part
  139.  
  140. //@version=5
  141. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  142. // © Electrified (electrifiedtrading)
  143. //indicator(title='Stochastic RSI+', shorttitle='Stoch RSI+', format=format.price, precision=2, timeframe='') // v2
  144.  
  145. kcolor = #0094FF
  146. dcolor = #FF6A00
  147. WMA = 'WMA'
  148. EMA = 'EMA'
  149. SMA = 'SMA'
  150. VWMA = 'VWMA'
  151. VAWMA = 'VAWMA'
  152.  
  153. ///////////////////////////////////////////////////
  154. // Input
  155.  
  156. k_mode = input.string(SMA, 'K Mode', inline='Source', options=[SMA, EMA, WMA, VWMA, VAWMA])
  157. src = wave
  158. smoothK = input.int(3, 'K', inline='Values', minval=1)
  159. smoothD = input.int(3, 'D', inline='Values', minval=1)
  160.  
  161.  
  162. lengthRSI = input.int(14, 'RSI', group='Lengths', inline='Lengths', minval=1)
  163. lengthStoch = input.int(14, 'Stochastic', group='Lengths', inline='Lengths', minval=1)
  164.  
  165.  
  166. upperBand = input.int(80, 'Upper', group='Band', minval=50, maxval=100)
  167. lowerBand = input.int(20, 'Lower', group='Band', maxval=50, minval=0)
  168.  
  169.  
  170. ///////////////////////////////////////////////////
  171. // Functions
  172. vawma(src, len) =>
  173. sum = 0.0
  174. vol = 0.0
  175. for m = 1 to len by 1 // m = triangular multiple
  176. i = len - m
  177. v = volume[i] * m
  178. vol += v
  179. sum += src[i] * v
  180. sum
  181. sum / vol
  182. ////
  183.  
  184. getMA(series, mode, len) =>
  185. mode == WMA ? ta.wma(series, len) : mode == EMA ? ta.ema(series, len) : mode == VWMA ? ta.vwma(series, len) : mode == VAWMA ? vawma(series, len) : ta.sma(series, len)
  186. ////
  187.  
  188. ///////////////////////////////////////////////////
  189. // Calculation
  190. rsi1 = ta.rsi(src, lengthRSI)
  191. stoch = ta.stoch(rsi1, rsi1, rsi1, lengthStoch)
  192. k = getMA(stoch, k_mode, smoothK)
  193. d = ta.sma(k, smoothD)
  194. k_c = ta.change(k)
  195. d_c = ta.change(d)
  196. kd = k - d
  197.  
  198.  
  199. ///////////////////////////////////////////////////
  200. // Visualization
  201. h0 = hline(upperBand, 'Upper Band', color=#606060)
  202. hline(50, 'Middle Band', color=#606060)
  203. h1 = hline(lowerBand, 'Lower Band', color=#606060)
  204. fill(h0, h1, color=color.new(#9915FF, 95), title='Band Background')
  205.  
  206. signalColor = k > lowerBand and d < upperBand and k > d and k_c > 0 and d_c > 0 ? kcolor : k < upperBand and d > lowerBand and k < d and k_c < 0 and d_c < 0 ? dcolor : na
  207. kp = plot(k, 'K', color=color.new(kcolor, 0))
  208. dp = plot(d, 'D', color=color.new(dcolor, 0))
  209. fill(kp, dp, color=color.new(signalColor, 50), title='K-D')
  210.  
  211. signalUp = not na(signalColor) and kd > 0
  212. signalDown = not na(signalColor) and kd < 0
  213.  
  214. plot(signalUp ? kd : na, 'Signal Up', color=color.new(kcolor, 0), style=plot.style_columns)
  215. plot(signalDown ? kd + 100 : na, 'Signal Down', color=color.new(dcolor, 0), style=plot.style_columns, histbase=100)
  216.  
  217.  
  218. ///////////////////////////////////////////////////
  219. // Alerts
  220. alertcondition(d < lowerBand, '1: Over-sold', 'Stoch RSI+: Over-sold\nD value below lower band. ({{ticker}} {{interval}})')
  221. alertcondition(d < lowerBand and k > d, '2: Over-sold & Rising ▲', 'Stoch RSI+ Over-sold & Rising ▲\nK>D and D value below lower band. ({{ticker}} {{interval}})')
  222. alertcondition(signalUp, '3: Postitive (+) Momentum ▲', 'Stoch RSI+: Postitive (+) Momentum ▲\nK&D rising ▲ within band. ({{ticker}} {{interval}})')
  223. alertcondition(d > upperBand, '4: Over-bought', 'Stoch RSI+: Over-bought\nD value above upper band. ({{ticker}} {{interval}})')
  224. alertcondition(d > upperBand and k < d, '5: Over-bought & Falling ▼', 'Stoch RSI+: Over-bought & Falling ▼\nK<D and D value above upper band. ({{ticker}} {{interval}})')
  225. alertcondition(signalDown, '6: Negative (-) Momentum ▼', 'Stoch RSI+: Negative (-) Momentum ▼\nK&D falling ▼ within band. ({{ticker}} {{interval}})')
  226.  
  227.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement