xmd79

Customized Stochastic

Nov 27th, 2022
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 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. // © jeffreyl166
  3.  
  4. //@version=5
  5. indicator(title="Customized Stochastic", shorttitle="Customized Stochastic", format=format.price, precision=2, timeframe="", timeframe_gaps=true)
  6. periodK = input.int(14, title="%K Length", minval=1)
  7. smoothK = input.int(1, title="%K Smoothing", minval=1)
  8. periodD = input.int(3, title="%D Smoothing", minval=1)
  9. buyzone = input.int(50, title="buyzone from 100 down to =", minval=1)
  10. sellzone = input.int(50, title="sellzone from 0 up to =", minval=1)
  11. overbought_zone = input.int(80, title="overbought zone", minval=1)
  12. oversold_zone = input.int(20, title="oversold zone", minval=1)
  13.  
  14. inp = input(close)
  15.  
  16. k = ta.sma(ta.stoch(inp, high, low, periodK), smoothK)
  17. d = ta.sma(k, periodD)
  18. plot(k, title="%K", color=#2962FF)
  19. plot(d, title="%D", color=#FF6D00)
  20. h0 = hline(100, "Upper Band", color=#787B86)
  21. h1 = hline(buyzone, "middle Band", color=#787B86)
  22. h2 = hline(sellzone, "middle Band", color=#787B86)
  23. h3 = hline(0, "Upper Band", color=#787B86)
  24. fill(h0, h1, color.new(color.green, 85))
  25. fill(h3, h2, color.new(color.red, 85))
  26.  
  27.  
  28. // Divergences
  29.  
  30. pivot_right = 5
  31. pivot_left = 5
  32. max_range = 50
  33. min_range = 5
  34.  
  35. pivot_low_true = na(ta.pivotlow(k, pivot_left, pivot_right)) ? false : true
  36. pivot_high_true = na(ta.pivothigh(k, pivot_left, pivot_right)) ? false : true
  37.  
  38. confirm_range(x) =>
  39. bars = ta.barssince(x == true)
  40. min_range <= bars and bars <= max_range
  41. //
  42.  
  43. CCI_HL_check = k[pivot_right] > ta.valuewhen(pivot_low_true, k[pivot_right], 1) and confirm_range(pivot_low_true[1])
  44. CCI_LL_check = k[pivot_right] < ta.valuewhen(pivot_high_true, k[pivot_right], 1) and confirm_range(pivot_high_true[1])
  45.  
  46. //
  47.  
  48. price_ll_check= low[pivot_right] < ta.valuewhen(pivot_low_true, low[pivot_right], 1)
  49. price_hl_check= high[pivot_right] > ta.valuewhen(pivot_high_true, high[pivot_right], 1)
  50.  
  51. bullcond = price_ll_check and CCI_HL_check and pivot_low_true
  52. bearcond = price_hl_check and CCI_LL_check and pivot_high_true
  53.  
  54. //
  55. plot(
  56. pivot_low_true ? k[pivot_right] : na,
  57. offset=-pivot_right,
  58. linewidth=3,
  59. color=(bullcond ? color.green :color.new(color.white, 100)))
  60.  
  61. plotshape(
  62. bullcond ? k[pivot_right] : na,
  63. offset=-pivot_right,
  64. text="DIV",
  65. style=shape.labelup,
  66. location=location.absolute,
  67. color=color.green,
  68. textcolor=color.white)
  69.  
  70. plot(
  71. pivot_high_true ? k[pivot_right] : na,
  72. offset=-pivot_right,
  73. linewidth=3,
  74. color=(bearcond ? color.red :color.new(color.white, 100)))
  75.  
  76. plotshape(
  77. bearcond ? k[pivot_right] : na,
  78. offset=-pivot_right,
  79. text="DIV",
  80. style=shape.labeldown,
  81. location=location.absolute,
  82. color=color.red,
  83. textcolor=color.white)
  84.  
  85.  
  86. // Stochastic overbought and oversold signals
  87.  
  88. ob_signal = ta.crossover(k, overbought_zone)
  89. os_signal = ta.crossunder(k, oversold_zone)
  90.  
  91. plotshape(ob_signal, style=shape.circle, color=color.red, location=location.top, size=size.tiny)
  92. plotshape(os_signal, style=shape.circle, color=color.green, location=location.bottom, size=size.tiny)
  93.  
  94.  
  95. //Alerts
  96.  
  97. alertcondition(ob_signal, title="Stochastic overbought", message="Stochastic overbought")
  98. alertcondition(os_signal, title="Stochastic oversold", message="Stochastic oversold")
  99.  
  100. alertcondition(bullcond, title="Stochastic bull divergence", message="Stochastic bull divergence")
  101. alertcondition(bearcond, title="Stochastic bear divergence", message="Stochastic bear divergence")
  102.  
  103. alertcondition(ta.crossover(k,buyzone), title="Stochastic crossover to buyzone", message="Stochastic crossover to buyzone")
  104. alertcondition(ta.crossunder(k,sellzone), title="Stochastic crossunder to sellzone", message="Stochastic crossunder to sellzone")
  105.  
  106.  
Advertisement
Add Comment
Please, Sign In to add comment