xmd79

Murrey Math Lines Indicator

Dec 1st, 2022
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.30 KB | None | 0 0
  1. //© ╦ ╦═╗ ╦
  2. //© ║ ╠╦╝ ║
  3. //© ╚╝ ╩╚═ ╩═╝
  4. //
  5. // Murrey Math Lines Indicator
  6.  
  7. //@version=5
  8.  
  9. // General information on trading with Murrey Math Lines can be found
  10. // here: (https://blog.roboforex.com/blog/2020/08/14/murrey-math-lines-how-to-set-and-trade-them/)
  11. // ---------------------------------------------------------------------------------------------------
  12. // If you'd like to go down the rabbit hole on the math formulas, more information can be found
  13. // here: (https://www.sierrachart.com/index.php?page=doc/StudiesReference.php&ID=238&Name=Murrey_Math)
  14. // and here: (https://www.forexfactory.com/attachment.php/3732227?attachmentid=3732227&d=1599144167)
  15.  
  16. indicator(title='Murrey Math Lines', shorttitle='MM Lines [JRL]', precision=5, overlay=true)
  17.  
  18.  
  19.  
  20. // ##############################################################################################
  21. // INPUTS #######################################################################################
  22. // ##############################################################################################
  23.  
  24. showLabels = input.string(title='Show Labels', defval='Closest to Price Only', options=['All', 'Closest to Price Only', 'None'])
  25.  
  26. // MM Lines
  27. altRes = input(title='Use Alternative Timeframe', defval=false)
  28. resolution = input.timeframe(title='Alternative Timeframe for MML', defval='60')
  29. priceSrc = input.string(title='Source', defval='High/Low', options=['High/Low', 'Open/Close'])
  30. frame = input.int(title='Frame Size', defval=64, minval=8, maxval=256)
  31. multiplier = input.float(title='Frame Multiplier', defval=1.5, minval=1.0, maxval=2.0, step=0.5)
  32. addXtraLines = input(title='Use Extended Octave (i.e., add extra interval lines at top and bottom)', defval=true)
  33. numXtraLines = input.string(title='Number of Additional Lines', defval='4', options=['2', '4', '6'])
  34.  
  35. // Colors
  36. labelCol = input(#ffa726, 'Color of MML label text')
  37. majorCol = input(color.blue, 'Color of major lines (0/8, 4/8, and 8/8)')
  38. rangeCol = input(color.green, 'Color of trading range lines (3/8 and 5/8)')
  39. pivotCol = input(color.orange, 'Color of pivot lines (2/8 and 6/8)')
  40. weakCol = input(color.maroon, 'Color of weak support/resistance lines (1/8 and 7/8')
  41. xtraCol = input(color.silver, 'Color of additional lines')
  42. fiblabelCol = input(color.white, 'Color of MML label text')
  43. pvtCol = input(color.white, 'Color of fibonacci pivot')
  44. fibSCol = input(color.lime, 'Color of fibonacci support lines')
  45. fibRCol = input(color.red, 'Color of fibonacci resistance lines')
  46.  
  47.  
  48.  
  49.  
  50. // ##############################################################################################
  51. // CALCULATIONS #################################################################################
  52. // ##############################################################################################
  53.  
  54. // Finding Prices and Range
  55. pHigh = priceSrc == 'High/Low' ? high : math.max(open, close)
  56.  
  57. pLow = priceSrc == 'High/Low' ? low : math.min(open, close)
  58.  
  59. h = ta.highest(pHigh, math.round(frame * multiplier))
  60. l = ta.lowest(pLow, math.round(frame * multiplier))
  61.  
  62. nRes = altRes ? resolution : timeframe.period
  63.  
  64. nHigh = request.security(syminfo.tickerid, nRes, h)
  65. nLow = request.security(syminfo.tickerid, nRes, l)
  66.  
  67. range_1 = nHigh - nLow
  68.  
  69. // Determining Applicable Fractal Value
  70. fractal = if nHigh <= 250000 and nHigh > 25000
  71. 100000
  72. else if nHigh <= 25000 and nHigh > 2500
  73. 10000
  74. else if nHigh <= 2500 and nHigh > 250
  75. 1000
  76. else if nHigh <= 250 and nHigh > 25
  77. 100
  78. else if nHigh <= 25 and nHigh > 6.25
  79. 12.5
  80. else if nHigh <= 6.25 and nHigh > 3.125
  81. 6.25
  82. else if nHigh <= 3.125 and nHigh > 1.5625
  83. 3.125
  84. else if nHigh <= 1.5625 and nHigh > 0.390625
  85. 1.5625
  86. else
  87. 0.1953125
  88.  
  89. // Calculating the Octave
  90. sum = math.floor(math.log(fractal / range_1) / math.log(2))
  91. octave = fractal * math.pow(0.5, sum)
  92. minimum = math.floor(nLow / octave) * octave
  93. maximum = minimum + octave > nHigh ? minimum + octave : minimum + 2 * octave
  94.  
  95. // Finding the Top
  96. t2 = if nLow >= 3 * (maximum - minimum) / 16 + minimum and nHigh <= 9 * (maximum - minimum) / 16 + minimum
  97. minimum + (maximum - minimum) / 2
  98. else
  99. 0
  100.  
  101. t1 = if nLow >= minimum - (maximum - minimum) / 8 and nHigh <= 5 * (maximum - minimum) / 8 + minimum and t2 == 0
  102. minimum + (maximum - minimum) / 2
  103. else
  104. 0
  105.  
  106. t4 = if nLow >= minimum + 7 * (maximum - minimum) / 16 and nHigh <= 13 * (maximum - minimum) / 16 + minimum
  107. minimum + 3 * (maximum - minimum) / 4
  108. else
  109. 0
  110.  
  111. t5 = if nLow >= minimum + 3 * (maximum - minimum) / 8 and nHigh <= 9 * (maximum - minimum) / 8 + minimum and t4 == 0
  112. maximum
  113. else
  114. 0
  115.  
  116. t3 = if nLow >= minimum + (maximum - minimum) / 8 and nHigh <= 7 * (maximum - minimum) / 8 + minimum and t1 == 0 and t2 == 0 and t4 == 0 and t5 == 0
  117. minimum + 3 * (maximum - minimum) / 4
  118. else
  119. 0
  120.  
  121. t6 = if t1 + t2 + t3 + t4 + t5 == 0
  122. maximum
  123. else
  124. 0
  125.  
  126. top = t1 + t2 + t3 + t4 + t5 + t6
  127.  
  128. // Finding the Bottom
  129. b1 = if t1 > 0
  130. minimum
  131. else
  132. 0
  133.  
  134. b2 = if t2 > 0
  135. minimum + (maximum - minimum) / 4
  136. else
  137. 0
  138.  
  139. b3 = if t3 > 0
  140. minimum + (maximum - minimum) / 4
  141. else
  142. 0
  143.  
  144. b4 = if t4 > 0
  145. minimum + (maximum - minimum) / 2
  146. else
  147. 0
  148.  
  149. b5 = if t5 > 0
  150. minimum + (maximum - minimum) / 2
  151. else
  152. 0
  153.  
  154. b6 = if top > 0 and b1 + b2 + b3 + b4 + b5 == 0
  155. minimum
  156. else
  157. 0
  158.  
  159. bottom = b1 + b2 + b3 + b4 + b5 + b6
  160.  
  161. // MMI
  162. mmi = (top - bottom) / 8
  163.  
  164. // Set MM Lines
  165. plus3 = bottom + 11 * mmi
  166. plus2 = bottom + 10 * mmi
  167. plus1 = bottom + 9 * mmi
  168. eight = bottom + 8 * mmi
  169. seven = bottom + 7 * mmi
  170. six = bottom + 6 * mmi
  171. five = bottom + 5 * mmi
  172. four = bottom + 4 * mmi
  173. three = bottom + 3 * mmi
  174. two = bottom + 2 * mmi
  175. one = bottom + mmi
  176. zero = bottom
  177. minus1 = bottom - mmi
  178. minus2 = bottom - 2 * mmi
  179. minus3 = bottom - 3 * mmi
  180.  
  181.  
  182.  
  183. // ##############################################################################################
  184. // MML PLOTS AND LABELS #########################################################################
  185. // ##############################################################################################
  186.  
  187. plotPlus3 = addXtraLines and (numXtraLines == '8' or numXtraLines == '6') ? plus3 : na
  188. plotPlus2 = addXtraLines and (numXtraLines == '8' or numXtraLines == '6' or numXtraLines == '4') ? plus2 : na
  189. plotPlus1 = addXtraLines ? plus1 : na
  190. plotMinus3 = addXtraLines and (numXtraLines == '8' or numXtraLines == '6') ? minus3 : na
  191. plotMinus2 = addXtraLines and (numXtraLines == '8' or numXtraLines == '6' or numXtraLines == '4') ? minus2 : na
  192. plotMinus1 = addXtraLines ? minus1 : na
  193.  
  194. // Plot Lines
  195. plot(plotPlus3, title='11/8 Line', style=plot.style_line, color=xtraCol, linewidth=2, trackprice=true, offset=-9999)
  196. plot(plotPlus2, title='10/8 Line', style=plot.style_line, color=xtraCol, linewidth=2, trackprice=true, offset=-9999)
  197. plot(plotPlus1, title='9/8 Line', style=plot.style_line, color=xtraCol, linewidth=2, trackprice=true, offset=-9999)
  198. plot(eight, title='8/8 Line', style=plot.style_line, color=majorCol, linewidth=4, trackprice=true, offset=-9999)
  199. plot(seven, title='7/8 Line', style=plot.style_line, color=weakCol, linewidth=2, trackprice=true, offset=-9999)
  200. plot(six, title='6/8 Line', style=plot.style_line, color=pivotCol, linewidth=2, trackprice=true, offset=-9999)
  201. plot(five, title='5/8 Line', style=plot.style_line, color=rangeCol, linewidth=2, trackprice=true, offset=-9999)
  202. plot(four, title='4/8 Line', style=plot.style_line, color=majorCol, linewidth=4, trackprice=true, offset=-9999)
  203. plot(three, title='3/8 Line', style=plot.style_line, color=rangeCol, linewidth=2, trackprice=true, offset=-9999)
  204. plot(two, title='2/8 Line', style=plot.style_line, color=pivotCol, linewidth=2, trackprice=true, offset=-9999)
  205. plot(one, title='1/8 Line', style=plot.style_line, color=weakCol, linewidth=2, trackprice=true, offset=-9999)
  206. plot(zero, title='0/8 Line', style=plot.style_line, color=majorCol, linewidth=4, trackprice=true, offset=-9999)
  207. plot(plotMinus1, title='-1/8 Line', style=plot.style_line, color=xtraCol, linewidth=2, trackprice=true, offset=-9999)
  208. plot(plotMinus2, title='-2/8 Line', style=plot.style_line, color=xtraCol, linewidth=2, trackprice=true, offset=-9999)
  209. plot(plotMinus3, title='-3/8 Line', style=plot.style_line, color=xtraCol, linewidth=2, trackprice=true, offset=-9999)
  210.  
  211. // Labels
  212. plus3Label = addXtraLines and (showLabels == 'All' or showLabels == 'Closest to Price Only' and close > plus2) ? plus3 : na
  213. plotshape(plus3Label, style=shape.labeldown, location=location.absolute, color=#00000000, textcolor=labelCol, show_last=1, text='+3', offset=10)
  214. plus2Label = addXtraLines and (showLabels == 'All' or showLabels == 'Closest to Price Only' and close > plus1 and close < plus3) ? plus2 : na
  215. plotshape(plus2Label, style=shape.labeldown, location=location.absolute, color=#00000000, textcolor=labelCol, show_last=1, text='+2', offset=10)
  216. plus1Label = addXtraLines and (showLabels == 'All' or showLabels == 'Closest to Price Only' and close > eight and close < plus2) ? plus1 : na
  217. plotshape(plus1Label, style=shape.labeldown, location=location.absolute, color=#00000000, textcolor=labelCol, show_last=1, text='+1', offset=10)
  218. eightLabel = showLabels == 'All' or showLabels == 'Closest to Price Only' and close > seven and close < plus1 ? eight : na
  219. plotshape(eightLabel, style=shape.labeldown, location=location.absolute, color=#00000000, textcolor=labelCol, show_last=1, text='8/8', offset=10)
  220. sevenLabel = showLabels == 'All' or showLabels == 'Closest to Price Only' and close > six and close < eight ? seven : na
  221. plotshape(sevenLabel, style=shape.labeldown, location=location.absolute, color=#00000000, textcolor=labelCol, show_last=1, text='7/8', offset=10)
  222. sixLabel = showLabels == 'All' or showLabels == 'Closest to Price Only' and close > five and close < seven ? six : na
  223. plotshape(sixLabel, style=shape.labeldown, location=location.absolute, color=#00000000, textcolor=labelCol, show_last=1, text='6/8', offset=10)
  224. fiveLabel = showLabels == 'All' or showLabels == 'Closest to Price Only' and close > four and close < six ? five : na
  225. plotshape(fiveLabel, style=shape.labeldown, location=location.absolute, color=#00000000, textcolor=labelCol, show_last=1, text='5/8', offset=10)
  226. fourLabel = showLabels == 'All' or showLabels == 'Closest to Price Only' and close > three and close < five ? four : na
  227. plotshape(fourLabel, style=shape.labeldown, location=location.absolute, color=#00000000, textcolor=labelCol, show_last=1, text='4/8', offset=10)
  228. threeLabel = showLabels == 'All' or showLabels == 'Closest to Price Only' and close > two and close < four ? three : na
  229. plotshape(threeLabel, style=shape.labeldown, location=location.absolute, color=#00000000, textcolor=labelCol, show_last=1, text='3/8', offset=10)
  230. twoLabel = showLabels == 'All' or showLabels == 'Closest to Price Only' and close > one and close < three ? two : na
  231. plotshape(twoLabel, style=shape.labeldown, location=location.absolute, color=#00000000, textcolor=labelCol, show_last=1, text='2/8', offset=10)
  232. oneLabel = showLabels == 'All' or showLabels == 'Closest to Price Only' and close > zero and close < two ? one : na
  233. plotshape(oneLabel, style=shape.labeldown, location=location.absolute, color=#00000000, textcolor=labelCol, show_last=1, text='1/8', offset=10)
  234. zeroLabel = showLabels == 'All' or showLabels == 'Closest to Price Only' and close > minus1 and close < one ? zero : na
  235. plotshape(zeroLabel, style=shape.labeldown, location=location.absolute, color=#00000000, textcolor=labelCol, show_last=1, text='0/8', offset=10)
  236. minus1Label = addXtraLines and (showLabels == 'All' or showLabels == 'Closest to Price Only' and close > minus2 and close < zero) ? minus1 : na
  237. plotshape(minus1Label, style=shape.labeldown, location=location.absolute, color=#00000000, textcolor=labelCol, show_last=1, text='-1', offset=10)
  238. minus2Label = addXtraLines and (showLabels == 'All' or showLabels == 'Closest to Price Only' and close > minus3 and close < minus1) ? minus2 : na
  239. plotshape(minus2Label, style=shape.labeldown, location=location.absolute, color=#00000000, textcolor=labelCol, show_last=1, text='-2', offset=10)
  240. minus3Label = addXtraLines and (showLabels == 'All' or showLabels == 'Closest to Price Only' and close < minus2) ? minus3 : na
  241. plotshape(minus3Label, style=shape.labeldown, location=location.absolute, color=#00000000, textcolor=labelCol, show_last=1, text='-3', offset=10)
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
Advertisement
Add Comment
Please, Sign In to add comment