xmd79

MMA V2 Indicator - Blast by Malik Muntazir Abbas

Nov 29th, 2022
499
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.15 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. // © LonesomeTheBlue
  3.  
  4. //@version=5
  5. indicator('MMA V2 Indicator - Blast by Malik Muntazir Abbas', 'MMA', overlay=true)
  6. prd = input.int(defval=10, title='Pivot Period', minval=4, maxval=30, group='Setup')
  7. ppsrc = input.string(defval='High/Low', title='Source', options=['High/Low', 'Close/Open'], group='Setup')
  8. maxnumpp = input.int(defval=20, title=' Maximum Number of Pivot', minval=5, maxval=100, group='Setup')
  9. ChannelW = input.int(defval=10, title='Maximum Channel Width %', minval=1, group='Setup')
  10. maxnumsr = input.int(defval=5, title=' Maximum Number of S/R', minval=1, maxval=10, group='Setup')
  11. min_strength = input.int(defval=2, title=' Minimum Strength', minval=1, maxval=10, group='Setup')
  12. labelloc = input.int(defval=20, title='Label Location', group='Colors', tooltip='Positive numbers reference future bars, negative numbers reference histical bars')
  13. linestyle = input.string(defval='Dashed', title='Line Style', options=['Solid', 'Dotted', 'Dashed'], group='Colors')
  14. linewidth = input.int(defval=2, title='Line Width', minval=1, maxval=4, group='Colors')
  15. resistancecolor = input.color(defval=color.red, title='Resistance Color', group='Colors')
  16. supportcolor = input.color(defval=color.lime, title='Support Color', group='Colors')
  17. showpp = input(false, title='Show Point Points')
  18.  
  19. float src1 = ppsrc == 'High/Low' ? high : math.max(close, open)
  20. float src2 = ppsrc == 'High/Low' ? low : math.min(close, open)
  21. float ph = ta.pivothigh(src1, prd, prd)
  22. float pl = ta.pivotlow(src2, prd, prd)
  23.  
  24. plotshape(ph and showpp, text='H', style=shape.labeldown, color=na, textcolor=color.new(color.red, 0), location=location.abovebar, offset=-prd)
  25. plotshape(pl and showpp, text='L', style=shape.labelup, color=na, textcolor=color.new(color.lime, 0), location=location.belowbar, offset=-prd)
  26.  
  27. Lstyle = linestyle == 'Dashed' ? line.style_dashed : linestyle == 'Solid' ? line.style_solid : line.style_dotted
  28.  
  29. //calculate maximum S/R channel zone width
  30. prdhighest = ta.highest(300)
  31. prdlowest = ta.lowest(300)
  32. cwidth = (prdhighest - prdlowest) * ChannelW / 100
  33.  
  34. var pivotvals = array.new_float(0)
  35.  
  36. if ph or pl
  37. array.unshift(pivotvals, ph ? ph : pl)
  38. if array.size(pivotvals) > maxnumpp // limit the array size
  39. array.pop(pivotvals)
  40.  
  41. get_sr_vals(ind) =>
  42. float lo = array.get(pivotvals, ind)
  43. float hi = lo
  44. int numpp = 0
  45. for y = 0 to array.size(pivotvals) - 1 by 1
  46. float cpp = array.get(pivotvals, y)
  47. float wdth = cpp <= lo ? hi - cpp : cpp - lo
  48. if wdth <= cwidth // fits the max channel width?
  49. lo := cpp <= lo ? cpp : lo
  50. hi := cpp > lo ? cpp : hi
  51. numpp += 1
  52. numpp
  53. [hi, lo, numpp]
  54.  
  55. var sr_up_level = array.new_float(0)
  56. var sr_dn_level = array.new_float(0)
  57. sr_strength = array.new_float(0)
  58.  
  59. find_loc(strength) =>
  60. ret = array.size(sr_strength)
  61. for i = ret > 0 ? array.size(sr_strength) - 1 : na to 0 by 1
  62. if strength <= array.get(sr_strength, i)
  63. break
  64. ret := i
  65. ret
  66. ret
  67.  
  68. check_sr(hi, lo, strength) =>
  69. ret = true
  70. for i = 0 to array.size(sr_up_level) > 0 ? array.size(sr_up_level) - 1 : na by 1
  71. //included?
  72. if array.get(sr_up_level, i) >= lo and array.get(sr_up_level, i) <= hi or array.get(sr_dn_level, i) >= lo and array.get(sr_dn_level, i) <= hi
  73. if strength >= array.get(sr_strength, i)
  74. array.remove(sr_strength, i)
  75. array.remove(sr_up_level, i)
  76. array.remove(sr_dn_level, i)
  77. ret
  78. else
  79. ret := false
  80. ret
  81. break
  82. ret
  83.  
  84. var sr_lines = array.new_line(11, na)
  85. var sr_labels = array.new_label(11, na)
  86.  
  87. for x = 1 to 10 by 1
  88. rate = 100 * (label.get_y(array.get(sr_labels, x)) - close) / close
  89. label.set_text(array.get(sr_labels, x), text=str.tostring(label.get_y(array.get(sr_labels, x))) + '(' + str.tostring(rate, '#.##') + '%)')
  90. label.set_x(array.get(sr_labels, x), x=bar_index + labelloc)
  91. label.set_color(array.get(sr_labels, x), color=label.get_y(array.get(sr_labels, x)) >= close ? color.red : color.lime)
  92. label.set_textcolor(array.get(sr_labels, x), textcolor=label.get_y(array.get(sr_labels, x)) >= close ? color.white : color.black)
  93. label.set_style(array.get(sr_labels, x), style=label.get_y(array.get(sr_labels, x)) >= close ? label.style_label_down : label.style_label_up)
  94. line.set_color(array.get(sr_lines, x), color=line.get_y1(array.get(sr_lines, x)) >= close ? resistancecolor : supportcolor)
  95.  
  96. if ph or pl
  97. //because of new calculation, remove old S/R levels
  98. array.clear(sr_up_level)
  99. array.clear(sr_dn_level)
  100. array.clear(sr_strength)
  101. //find S/R zones
  102. for x = 0 to array.size(pivotvals) - 1 by 1
  103. [hi, lo, strength] = get_sr_vals(x)
  104. if check_sr(hi, lo, strength)
  105. loc = find_loc(strength)
  106. // if strength is in first maxnumsr sr then insert it to the arrays
  107. if loc < maxnumsr and strength >= min_strength
  108. array.insert(sr_strength, loc, strength)
  109. array.insert(sr_up_level, loc, hi)
  110. array.insert(sr_dn_level, loc, lo)
  111. // keep size of the arrays = 5
  112. if array.size(sr_strength) > maxnumsr
  113. array.pop(sr_strength)
  114. array.pop(sr_up_level)
  115. array.pop(sr_dn_level)
  116.  
  117. for x = 1 to 10 by 1
  118. line.delete(array.get(sr_lines, x))
  119. label.delete(array.get(sr_labels, x))
  120.  
  121. for x = 0 to array.size(sr_up_level) > 0 ? array.size(sr_up_level) - 1 : na by 1
  122. float mid = math.round_to_mintick((array.get(sr_up_level, x) + array.get(sr_dn_level, x)) / 2)
  123. rate = 100 * (mid - close) / close
  124. array.set(sr_labels, x + 1, label.new(x=bar_index + labelloc, y=mid, text=str.tostring(mid) + '(' + str.tostring(rate, '#.##') + '%)', color=mid >= close ? color.red : color.lime, textcolor=mid >= close ? color.white : color.black, style=mid >= close ? label.style_label_down : label.style_label_up))
  125.  
  126. array.set(sr_lines, x + 1, line.new(x1=bar_index, y1=mid, x2=bar_index - 1, y2=mid, extend=extend.both, color=mid >= close ? resistancecolor : supportcolor, style=Lstyle, width=linewidth))
  127.  
  128. f_crossed_over() =>
  129. ret = false
  130. for x = 0 to array.size(sr_up_level) > 0 ? array.size(sr_up_level) - 1 : na by 1
  131. float mid = math.round_to_mintick((array.get(sr_up_level, x) + array.get(sr_dn_level, x)) / 2)
  132. if close[1] <= mid and close > mid
  133. ret := true
  134. ret
  135. ret
  136.  
  137. f_crossed_under() =>
  138. ret = false
  139. for x = 0 to array.size(sr_up_level) > 0 ? array.size(sr_up_level) - 1 : na by 1
  140. float mid = math.round_to_mintick((array.get(sr_up_level, x) + array.get(sr_dn_level, x)) / 2)
  141. if close[1] >= mid and close < mid
  142. ret := true
  143. ret
  144. ret
  145.  
  146. alertcondition(f_crossed_over(), title='Resistance Broken', message='Resistance Broken')
  147. alertcondition(f_crossed_under(), title='Support Broken', message='Support Broken')
  148.  
Advertisement
Add Comment
Please, Sign In to add comment