xmd79

Rolling largest Impulse Murreys Lines

Nov 23rd, 2021
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.96 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. // © RicardoSantos
  3.  
  4. //@version=5
  5. indicator(title='Rolling largest Impulse Murreys Lines', overlay=true, max_bars_back=500)
  6.  
  7. import RicardoSantos/FunctionArrayMaxSubKadanesAlgorithm/1 as msub
  8. import RicardoSantos/ColorExtension/4 as colore
  9.  
  10. // helpers:
  11. line_style_switch (string _style) =>
  12. switch (_style)
  13. ('dashed') => line.style_dashed
  14. ('dotted') => line.style_dotted
  15. ('solid') => line.style_solid
  16. line_extend_switch (string _extend) =>
  17. switch (_extend)
  18. ('left') => extend.left
  19. ('right') => extend.right
  20. ('both') => extend.both
  21. ('none') => extend.none
  22. label_textsize(string _size) =>
  23. switch (_size)
  24. ('auto') => size.auto
  25. ('huge') => size.huge
  26. ('large') => size.large
  27. ('normal') => size.normal
  28. ('small') => size.small
  29. ('tiny') => size.tiny
  30. // inputs:
  31. g_param = 'Parameters'
  32. int window_size = input.int(defval=250, title='Window Size', minval=1, maxval=500, tooltip='size of data to search for largest impulse swing.', group=g_param)
  33. bool direction = 'Bull' == input.string(defval='Bull', title='Direction of impulse', options=['Bull', 'Bear'], tooltip='prefered direction of impulse.', group=g_param)
  34. float i_inversion_rate = input.float(defval=1.0, title='Inversion Rate', minval=0.0, maxval=1.0, tooltip='Inversion rate used to mirror the base line.', group=g_param)
  35.  
  36. g_style = 'Line Options'
  37. i_base_color = input.color(defval=color.silver, title='Base', inline='b', group=g_style)
  38. i_base_extend = line_extend_switch(input.string(defval='none', options=['left', 'right', 'both', 'none'], title='', inline='b', group=g_style))
  39. i_base_style = line_style_switch(input.string(defval='solid', options=['dashed', 'dotted', 'solid'], title='', inline='b', group=g_style))
  40. i_base_width = input.int(defval=1, minval=0, title='', tooltip='Base line properties.', inline='b', group=g_style)
  41. i_invr_color = input.color(defval=color.silver, title='Inverse', inline='i', group=g_style)
  42. i_invr_extend = line_extend_switch(input.string(defval='none', options=['left', 'right', 'both', 'none'], title='', inline='i', group=g_style))
  43. i_invr_style = line_style_switch(input.string(defval='dashed', options=['dashed', 'dotted', 'solid'], title='', inline='i', group=g_style))
  44. i_invr_width = input.int(defval=1, minval=0, title='', tooltip='Inverse line properties.', inline='i', group=g_style)
  45. i_extend = line_extend_switch(input.string(defval='left', options=['left', 'right', 'both', 'none'], title='MML Extend', inline='00', group=g_style))
  46. i_labels = input.bool(defval=true, title='', inline='00', group=g_style)
  47. i_labels_textsize = input.string(defval='small', options=['auto', 'huge', 'large', 'normal', 'small', 'tiny'], title='Labels', inline='00', group=g_style)
  48. i_mm01_color = input.color(defval=color.gray, title='-2/8', inline='01', group=g_style)
  49. i_mm01_style = line_style_switch(input.string(defval='dotted', options=['dashed', 'dotted', 'solid'], title='', inline='01', group=g_style))
  50. i_mm01_width = input.int(defval=1, minval=0, title='', tooltip='-2/8 line properties.', inline='01', group=g_style)
  51. i_mm02_color = input.color(defval=color.gray, title='-1/8', inline='02', group=g_style)
  52. i_mm02_style = line_style_switch(input.string(defval='dashed', options=['dashed', 'dotted', 'solid'], title='', inline='02', group=g_style))
  53. i_mm02_width = input.int(defval=1, minval=0, title='', tooltip='-1/8 line properties.', inline='02', group=g_style)
  54. i_mm03_color = input.color(defval=color.aqua, title=' 0/8', inline='03', group=g_style)
  55. i_mm03_style = line_style_switch(input.string(defval='solid' , options=['dashed', 'dotted', 'solid'], title='', inline='03', group=g_style))
  56. i_mm03_width = input.int(defval=2, minval=0, title='', tooltip=' 0/8 line properties.', inline='03', group=g_style)
  57. i_mm04_color = input.color(defval=color.gray, title=' 1/8', inline='04', group=g_style)
  58. i_mm04_style = line_style_switch(input.string(defval='dashed' , options=['dashed', 'dotted', 'solid'], title='', inline='04', group=g_style))
  59. i_mm04_width = input.int(defval=1, minval=0, title='', tooltip=' 1/8 line properties.', inline='04', group=g_style)
  60. i_mm05_color = input.color(defval=color.gray, title=' 2/8', inline='05', group=g_style)
  61. i_mm05_style = line_style_switch(input.string(defval='solid' , options=['dashed', 'dotted', 'solid'], title='', inline='05', group=g_style))
  62. i_mm05_width = input.int(defval=1, minval=0, title='', tooltip=' 2/8 line properties.', inline='05', group=g_style)
  63. i_mm06_color = input.color(defval=color.gray, title=' 3/8', inline='06', group=g_style)
  64. i_mm06_style = line_style_switch(input.string(defval='dashed' , options=['dashed', 'dotted', 'solid'], title='', inline='06', group=g_style))
  65. i_mm06_width = input.int(defval=1, minval=0, title='', tooltip=' 3/8 line properties.', inline='06', group=g_style)
  66. i_mm07_color = input.color(defval=color.lime, title=' 4/8', inline='07', group=g_style)
  67. i_mm07_style = line_style_switch(input.string(defval='solid' , options=['dashed', 'dotted', 'solid'], title='', inline='07', group=g_style))
  68. i_mm07_width = input.int(defval=2, minval=0, title='', tooltip=' 4/8 line properties.', inline='07', group=g_style)
  69. i_mm08_color = input.color(defval=color.gray, title=' 5/8', inline='08', group=g_style)
  70. i_mm08_style = line_style_switch(input.string(defval='dashed' , options=['dashed', 'dotted', 'solid'], title='', inline='08', group=g_style))
  71. i_mm08_width = input.int(defval=1, minval=0, title='', tooltip=' 5/8 line properties.', inline='08', group=g_style)
  72. i_mm09_color = input.color(defval=color.gray, title=' 6/8', inline='09', group=g_style)
  73. i_mm09_style = line_style_switch(input.string(defval='solid' , options=['dashed', 'dotted', 'solid'], title='', inline='09', group=g_style))
  74. i_mm09_width = input.int(defval=1, minval=0, title='', tooltip=' 6/8 line properties.', inline='09', group=g_style)
  75. i_mm10_color = input.color(defval=color.gray, title=' 7/8', inline='10', group=g_style)
  76. i_mm10_style = line_style_switch(input.string(defval='dashed' , options=['dashed', 'dotted', 'solid'], title='', inline='10', group=g_style))
  77. i_mm10_width = input.int(defval=1, minval=0, title='', tooltip=' 7/8 line properties.', inline='10', group=g_style)
  78. i_mm11_color = input.color(defval=color.aqua, title=' 8/8', inline='11', group=g_style)
  79. i_mm11_style = line_style_switch(input.string(defval='solid' , options=['dashed', 'dotted', 'solid'], title='', inline='11', group=g_style))
  80. i_mm11_width = input.int(defval=2, minval=0, title='', tooltip=' 8/8 line properties.', inline='11', group=g_style)
  81. i_mm12_color = input.color(defval=color.gray, title=' 9/8', inline='12', group=g_style)
  82. i_mm12_style = line_style_switch(input.string(defval='dashed' , options=['dashed', 'dotted', 'solid'], title='', inline='12', group=g_style))
  83. i_mm12_width = input.int(defval=1, minval=0, title='', tooltip=' 9/8 line properties.', inline='12', group=g_style)
  84. i_mm13_color = input.color(defval=color.gray, title='10/8', inline='13', group=g_style)
  85. i_mm13_style = line_style_switch(input.string(defval='dotted' , options=['dashed', 'dotted', 'solid'], title='', inline='13', group=g_style))
  86. i_mm13_width = input.int(defval=1, minval=0, title='', tooltip='10/8 line properties.', inline='13', group=g_style)
  87. // prepare data:
  88. float hl = (direction ? +1.0 : -1.0) * ta.change(high * low)
  89.  
  90. var float[] samples = array.new_float(size=window_size, initial_value=0.0)
  91. array.unshift(samples, hl)
  92. array.pop(samples)
  93.  
  94. // find the largest impulse in the data samples:
  95. [ki_max, ki_start, ki_end] = msub.indices(samples)
  96.  
  97. bool is_new_impulse = ta.change(ki_start + ki_end) != 0
  98.  
  99. // initialize the lines:
  100. var line[] lines = array.new_line(15)
  101. var label[] labels = array.new_label(15)
  102. if barstate.isfirst
  103. // base line:
  104. array.set(lines, 0, line.new(bar_index, 0.0, bar_index, 0.0, xloc.bar_index, i_base_extend, i_base_color, i_base_style, i_base_width))
  105. array.set(lines, 1, line.new(bar_index, 0.0, bar_index, 0.0, xloc.bar_index, i_invr_extend, i_invr_color, i_invr_style, i_invr_width))
  106. //from lowest octave to highest:
  107. array.set(lines, 2, line.new(bar_index, 0.0, bar_index, 0.0, xloc.bar_index, i_extend, i_mm01_color, i_mm01_style, i_mm01_width))
  108. array.set(lines, 3, line.new(bar_index, 0.0, bar_index, 0.0, xloc.bar_index, i_extend, i_mm02_color, i_mm02_style, i_mm02_width))
  109. array.set(lines, 4, line.new(bar_index, 0.0, bar_index, 0.0, xloc.bar_index, i_extend, i_mm03_color, i_mm03_style, i_mm03_width))
  110. array.set(lines, 5, line.new(bar_index, 0.0, bar_index, 0.0, xloc.bar_index, i_extend, i_mm04_color, i_mm04_style, i_mm04_width))
  111. array.set(lines, 6, line.new(bar_index, 0.0, bar_index, 0.0, xloc.bar_index, i_extend, i_mm05_color, i_mm05_style, i_mm05_width))
  112. array.set(lines, 7, line.new(bar_index, 0.0, bar_index, 0.0, xloc.bar_index, i_extend, i_mm06_color, i_mm06_style, i_mm06_width))
  113. array.set(lines, 8, line.new(bar_index, 0.0, bar_index, 0.0, xloc.bar_index, i_extend, i_mm07_color, i_mm07_style, i_mm07_width))
  114. array.set(lines, 9, line.new(bar_index, 0.0, bar_index, 0.0, xloc.bar_index, i_extend, i_mm08_color, i_mm08_style, i_mm08_width))
  115. array.set(lines, 10, line.new(bar_index, 0.0, bar_index, 0.0, xloc.bar_index, i_extend, i_mm09_color, i_mm09_style, i_mm09_width))
  116. array.set(lines, 11, line.new(bar_index, 0.0, bar_index, 0.0, xloc.bar_index, i_extend, i_mm10_color, i_mm10_style, i_mm10_width))
  117. array.set(lines, 12, line.new(bar_index, 0.0, bar_index, 0.0, xloc.bar_index, i_extend, i_mm11_color, i_mm11_style, i_mm11_width))
  118. array.set(lines, 13, line.new(bar_index, 0.0, bar_index, 0.0, xloc.bar_index, i_extend, i_mm12_color, i_mm12_style, i_mm12_width))
  119. array.set(lines, 14, line.new(bar_index, 0.0, bar_index, 0.0, xloc.bar_index, i_extend, i_mm13_color, i_mm13_style, i_mm13_width))
  120. //labels
  121. if i_labels
  122. array.set(labels, 2, label.new(bar_index, 0.0, '-2/8', color=i_mm01_color, style=label.style_label_right, textcolor=colore.invert(i_mm01_color), size=i_labels_textsize))
  123. array.set(labels, 3, label.new(bar_index, 0.0, '-1/8', color=i_mm02_color, style=label.style_label_right, textcolor=colore.invert(i_mm02_color), size=i_labels_textsize))
  124. array.set(labels, 4, label.new(bar_index, 0.0, ' 0/8', color=i_mm03_color, style=label.style_label_right, textcolor=colore.invert(i_mm03_color), size=i_labels_textsize))
  125. array.set(labels, 5, label.new(bar_index, 0.0, ' 1/8', color=i_mm04_color, style=label.style_label_right, textcolor=colore.invert(i_mm04_color), size=i_labels_textsize))
  126. array.set(labels, 6, label.new(bar_index, 0.0, ' 2/8', color=i_mm05_color, style=label.style_label_right, textcolor=colore.invert(i_mm05_color), size=i_labels_textsize))
  127. array.set(labels, 7, label.new(bar_index, 0.0, ' 3/8', color=i_mm06_color, style=label.style_label_right, textcolor=colore.invert(i_mm06_color), size=i_labels_textsize))
  128. array.set(labels, 8, label.new(bar_index, 0.0, ' 4/8', color=i_mm07_color, style=label.style_label_right, textcolor=colore.invert(i_mm07_color), size=i_labels_textsize))
  129. array.set(labels, 9, label.new(bar_index, 0.0, ' 5/8', color=i_mm08_color, style=label.style_label_right, textcolor=colore.invert(i_mm08_color), size=i_labels_textsize))
  130. array.set(labels, 10, label.new(bar_index, 0.0, ' 6/8', color=i_mm09_color, style=label.style_label_right, textcolor=colore.invert(i_mm09_color), size=i_labels_textsize))
  131. array.set(labels, 11, label.new(bar_index, 0.0, ' 7/8', color=i_mm10_color, style=label.style_label_right, textcolor=colore.invert(i_mm10_color), size=i_labels_textsize))
  132. array.set(labels, 12, label.new(bar_index, 0.0, ' 8/8', color=i_mm11_color, style=label.style_label_right, textcolor=colore.invert(i_mm11_color), size=i_labels_textsize))
  133. array.set(labels, 13, label.new(bar_index, 0.0, ' 9/8', color=i_mm12_color, style=label.style_label_right, textcolor=colore.invert(i_mm12_color), size=i_labels_textsize))
  134. array.set(labels, 14, label.new(bar_index, 0.0, '10/8', color=i_mm13_color, style=label.style_label_right, textcolor=colore.invert(i_mm13_color), size=i_labels_textsize))
  135.  
  136. update_label (int _n, int _x, float _y) => //{
  137. label _label = array.get(labels, _n)
  138. label.set_xy(_label, _x, _y)
  139. //}
  140. update_bline (int _n, int _x1, float _y1, int _x2, float _y2) => //{
  141. line _line = array.get(lines, _n)
  142. line.set_xy1(_line, _x1, _y1)
  143. line.set_xy2(_line, _x2, _y2)
  144. //}
  145. update_mline (int _n, int _x1, int _x2, float _y) => //{
  146. line _line = array.get(lines, _n)
  147. line.set_xy1(_line, _x1, _y)
  148. line.set_xy2(_line, _x2, _y)
  149. if i_labels
  150. update_label(_n, _x2, _y)
  151. //}
  152.  
  153. if is_new_impulse
  154. float _start_value = hl2[ki_start]
  155. float _end_value = hl2[ki_end]
  156. int _x1 = bar_index - ki_start
  157. int _x2 = bar_index - ki_end
  158. float _y1 = low[ki_start]
  159. float _y2 = high[ki_end]
  160. if _end_value < _start_value
  161. _y1 := high[ki_start]
  162. _y2 := low[ki_end]
  163.  
  164. float _range = _y2 - _y1
  165.  
  166. update_bline(00, _x1, _y1, _x2, _y2)
  167. update_bline(01, _x1, _y1, _x1+(_x1-_x2), _y1+(_y2-_y1)*i_inversion_rate)
  168. update_mline(02, _x1, _x2, _y1 + _range * -0.250)
  169. update_mline(03, _x1, _x2, _y1 + _range * -0.125)
  170. update_mline(04, _x1, _x2, _y1)
  171. update_mline(05, _x1, _x2, _y1 + _range * 0.125)
  172. update_mline(06, _x1, _x2, _y1 + _range * 0.250)
  173. update_mline(07, _x1, _x2, _y1 + _range * 0.375)
  174. update_mline(08, _x1, _x2, _y1 + _range * 0.500)
  175. update_mline(09, _x1, _x2, _y1 + _range * 0.625)
  176. update_mline(10, _x1, _x2, _y1 + _range * 0.750)
  177. update_mline(11, _x1, _x2, _y1 + _range * 0.875)
  178. update_mline(12, _x1, _x2, _y1 + _range * 1.000)
  179. update_mline(13, _x1, _x2, _y1 + _range * 1.125)
  180. update_mline(14, _x1, _x2, _y1 + _range * 1.250)
  181.  
  182.  
  183.  
Advertisement
Add Comment
Please, Sign In to add comment