xmd79

SupportResitanceAndTrend

Oct 5th, 2021
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 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. // © Electrified (electrifiedtrading)
  3.  
  4. // @version=5
  5. // @description Contains utilities for finding key levels of support, resistance and direction of trend.
  6. library("SupportResitanceAndTrend", true)
  7. import Electrified/MovingAverages/2 as ma
  8.  
  9. // @function A more flexible version of SuperTrend that allows for supplying the series used and sensitivity adjustment by confirming close bars.
  10. // @param multiple The multiple to apply to the average true range.
  11. // @param h The high values.
  12. // @param l The low values.
  13. // @param atr The average true range values.
  14. // @param closeBars The number of bars to confirm a change in trend.
  15. // @returns [trend, up, dn, unconfirmed, warn, reversal]
  16. export superTrendPlus(
  17. simple float multiple,
  18. series float h,
  19. series float l,
  20. series float atr,
  21. simple int closeBars = 0) =>
  22. up = l-(multiple*atr)
  23. up1 = nz(up[1], up)
  24. up := l[1] > up1 ? math.max(up, up1) : up
  25. dn = h+(multiple*atr)
  26. dn1 = nz(dn[1], dn)
  27. dn := h[1] < dn1 ? math.min(dn, dn1) : dn
  28.  
  29. var lastU = up1
  30. var lastD = dn1
  31. var trend = 0
  32. var confirmation = 0
  33. var unconfirmed = 0
  34.  
  35. // cover gap cases
  36. if(trend != +1 and l > lastD)
  37. trend := +1
  38. else if(trend != -1 and h < lastU)
  39. trend := -1
  40.  
  41. // confirmed cases
  42. else if(trend != +1 and h > lastD)
  43. unconfirmed += 1
  44. if(confirmation<closeBars and close[1]>lastD)
  45. confirmation += 1
  46. if(confirmation>=closeBars)
  47. trend := +1
  48. else if(trend != -1 and l < lastU)
  49. unconfirmed += 1
  50. if(confirmation<closeBars and close[1]<lastU)
  51. confirmation += 1
  52. if(confirmation>=closeBars)
  53. trend := -1
  54.  
  55. if(trend[1]!=trend)
  56. lastU := up1
  57. lastD := dn1
  58. confirmation := 0
  59. unconfirmed := 0
  60. else
  61. lastU := trend==+1 ? math.max(lastU, up1) : up1
  62. lastD := trend==-1 ? math.min(lastD, dn1) : dn1
  63. // Trend is clearly continuing? Reset confirmation.
  64. if(trend==+1 and dn1>dn1[1] or trend==-1 and up1<up1[1])
  65. confirmation := 0
  66. unconfirmed := 0
  67.  
  68. [trend, lastU, lastD, unconfirmed, unconfirmed[1]==0 and unconfirmed > 0, trend != trend[1]]
  69. ///
  70.  
  71. // @function superTrendPlus with simplified parameters.
  72. // @param multiple The multiple to apply to the average true range.
  73. // @param period The number of bars to measure.
  74. // @param mode The type of moving average to use with the true range.
  75. // @param closeBars The number of bars to confirm a change in trend.
  76. // @returns [trend, up, dn, unconfirmed, warn, reversal]
  77. export superTrend(
  78. simple float multiple,
  79. simple int period,
  80. simple string mode = "WMA",
  81. simple int closeBars = 0) =>
  82. superTrendPlus(multiple, high, low, ma.getMA(mode, period, ta.tr), closeBars)
  83. ///
  84.  
  85. // @function Identifies support and resistance levels by when a stochastic RSI reverses.
  86. // @returns [trend, up, dn, unconfirmed, warn, reversal]
  87. export stochSR(
  88. simple int smoothK,
  89. simple int smoothD,
  90. simple int lengthRSI,
  91. simple int lengthStoch,
  92. series float src = hlc3,
  93. simple float lowerBand = 20,
  94. simple float upperBand = 80,
  95. simple float lowerReversal = 20,
  96. simple float upperReversal = 80) =>
  97. rsi1 = ta.rsi(src, lengthRSI)
  98. stoch = ta.stoch(rsi1, rsi1, rsi1, lengthStoch)
  99. k = ta.wma(stoch, smoothK)
  100. d = ta.sma(k, smoothD)
  101. k_c = ta.change(k)
  102. d_c = ta.change(d)
  103. kd = k - d
  104.  
  105. var hi = high
  106. var lo = low
  107. var phi = high
  108. var plo = low
  109. var state = 0
  110.  
  111. if(d<lowerBand)
  112. phi := high
  113. if(d>upperBand)
  114. plo := low
  115.  
  116. if(high>phi)
  117. phi := high
  118. if(low<plo)
  119. plo := low
  120.  
  121. if(state!=-1 and d<lowerBand)
  122. state := -1
  123. else if(state!=+1 and d>upperBand)
  124. state := +1
  125.  
  126. if(hi>phi and state==+1 and k<d and k<lowerReversal)
  127. hi := phi
  128. if(lo<plo and state==-1 and k>d and k>upperReversal)
  129. lo := plo
  130.  
  131. if(high>hi)
  132. hi := high
  133. if(low<lo)
  134. lo := low
  135.  
  136. [lo, hi]
  137. ///
Advertisement
Add Comment
Please, Sign In to add comment