xmd79

DynSR with harmonics

Nov 30th, 2021
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.79 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('Support Resistance MTF', overlay=true, max_bars_back=4900)
  6. src = input.string(defval='High/Low', title='Source', options=['High/Low', 'Close/Open'])
  7. TimeframeU = input.string(defval='Auto', title='Higher Time Frame', options=['Auto', '15', '30', '60', '120', '180', '240', '720', 'D', 'W', '2W', 'M', '3M', '6M', '12M'])
  8. prd = input.int(defval=10, title='Period for Highest/Lowest Bars', minval=1)
  9. resistancecol = input(defval=color.red, title='Resistance Color')
  10. supportcol = input(defval=color.lime, title='Support Color')
  11. Transp = input.int(defval=80, title='Transparency for Zones', minval=0, maxval=100)
  12. extendlines = input(defval=false, title='Extend Lines')
  13.  
  14. Timeframe = timeframe.period
  15. if TimeframeU == 'Auto'
  16. Timeframe := timeframe.period == '1' ? '15' : timeframe.period == '3' ? '15' : timeframe.period == '5' ? '60' : timeframe.period == '15' ? '60' : timeframe.period == '30' ? '120' : timeframe.period == '45' ? '120' : timeframe.period == '60' ? '240' : timeframe.period == '120' ? 'D' : timeframe.period == '180' ? 'D' : timeframe.period == '240' ? 'D' : timeframe.period == 'D' ? 'W' : timeframe.period == 'W' ? 'M' : '12M'
  17. Timeframe
  18. else
  19. Timeframe := TimeframeU
  20. Timeframe
  21.  
  22. var float hc = na
  23. var float lc = na
  24. srch = src == 'High/Low' ? high : math.max(close, open)
  25. srcl = src == 'High/Low' ? low : math.min(close, open)
  26. hc := ta.highestbars(srch, prd) == 0 ? srch : hc
  27. lc := ta.lowestbars(srcl, prd) == 0 ? srcl : lc
  28.  
  29. hcp = plot(hc, color=hc == hc[1] ? resistancecol : na)
  30. lcp = plot(lc, color=lc == lc[1] ? supportcol : na)
  31.  
  32. bool newbar = ta.change(time(Timeframe)) != 0
  33. var float htfh = na
  34. var float htfl = na
  35. if newbar
  36. htfh := srch
  37. htfl := srcl
  38. htfl
  39. else
  40. htfh := math.max(htfh, srch)
  41. htfl := math.min(htfl, srcl)
  42. htfl
  43.  
  44. highestbar(src, len) =>
  45. ll = 1
  46. if len > 1
  47. for x = 0 to 4000 by 1
  48. if na(newbar[x]) or na(close[x + 1])
  49. break
  50. if newbar[x]
  51. if src <= src[x + 1]
  52. break
  53. ll += 1
  54. if ll >= len
  55. break
  56. ret = ll >= len
  57. ret
  58.  
  59. lowestbar(src, len) =>
  60. ll = 1
  61. if len > 1
  62. for x = 0 to 4000 by 1
  63. if na(newbar[x]) or na(close[x + 1])
  64. break
  65. if newbar[x]
  66. if src >= src[x + 1]
  67. break
  68. ll += 1
  69. if ll >= len
  70. break
  71. ret = ll >= len
  72. ret
  73.  
  74. var float hh = 0
  75. var float ll = 0
  76. bool hbar = highestbar(htfh, prd)
  77. bool lbar = lowestbar(htfl, prd)
  78. hh := hbar ? htfh : hh
  79. ll := lbar ? htfl : ll
  80.  
  81. hhp = plot(hh, color=hh == hh[1] ? resistancecol : na)
  82. llp = plot(ll, color=ll == ll[1] ? supportcol : na)
  83.  
  84. fill(hhp, hcp, color=hc == hc[1] and hh == hh[1] ? color.new(resistancecol, Transp) : na, transp=90)
  85. fill(llp, lcp, color=lc == lc[1] and ll == ll[1] ? color.new(supportcol, Transp) : na, transp=90)
  86.  
  87. // extend lines
  88. if extendlines
  89. var line s1 = na
  90. var line s2 = na
  91. var line r1 = na
  92. var line r2 = na
  93. line.delete(s1)
  94. line.delete(s2)
  95. line.delete(r1)
  96. line.delete(r2)
  97. s1 := line.new(bar_index, lc, bar_index - 1, lc, color=supportcol, extend=extend.left)
  98. s2 := line.new(bar_index, ll, bar_index - 1, ll, color=supportcol, extend=extend.left)
  99. r1 := line.new(bar_index, hc, bar_index - 1, hc, color=resistancecol, extend=extend.left)
  100. r2 := line.new(bar_index, hh, bar_index - 1, hh, color=resistancecol, extend=extend.left)
  101. r2
  102.  
  103. //end of this part
  104.  
  105. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  106. // © LeeManTat
  107.  
  108. //@version=5
  109. //indicator('Multi ZigZag Harmonic Patterns', shorttitle='MZigZag - HP', overlay=true, max_bars_back=1000, max_lines_count=500, max_labels_count=500)
  110.  
  111. max_pivot_size = input.int(100, step=10)
  112.  
  113. showZigZag1 = input(true)
  114. zigzag1Length = input.int(5, step=5, minval=1)
  115. zigzag1Color = input(color.teal)
  116. zigzag1Width = 1
  117. zigzag1Style = line.style_solid
  118.  
  119. showZigZag2 = input(true)
  120. zigzag2Length = input.int(10, step=5, minval=1)
  121. zigzag2Color = input(color.olive)
  122. zigzag2Width = 1
  123. zigzag2Style = line.style_solid
  124.  
  125. showZigZag3 = input(true)
  126. zigzag3Length = input.int(15, step=5, minval=1)
  127. zigzag3Color = input(color.lime)
  128. zigzag3Width = 1
  129. zigzag3Style = line.style_solid
  130.  
  131. showZigZag4 = input(true)
  132. zigzag4Length = input.int(20, step=5, minval=1)
  133. zigzag4Color = input(color.fuchsia)
  134. zigzag4Width = 1
  135. zigzag4Style = line.style_solid
  136.  
  137. abcdClassic = input(true)
  138. abEQcd = input(true)
  139. abcdExt = input(true)
  140. gartley = input(true)
  141. crab = input(true)
  142. deepCrab = input(true)
  143. bat = input(true)
  144. butterfly = input(true)
  145. shark = input(true)
  146. cypher = input(true)
  147. threeDrives = input(true)
  148. fiveZero = input(true)
  149. doubleBottomTop = input(true)
  150. errorPercent = input.int(10, minval=5, step=5, maxval=20)
  151. MaxRiskPerReward = input.int(30, title='Max Risk Per Reward (Double Top/Bottom)', step=10, minval=0)
  152.  
  153. showStatTable = false
  154. waitForConfirmation = input(true)
  155.  
  156. bullishColor = input(color.green)
  157. bearishColor = input(color.red)
  158.  
  159. err_min = (100 - errorPercent) / 100
  160. err_max = (100 + errorPercent) / 100
  161.  
  162. var zigzagpivots1 = array.new_float(0)
  163. var zigzagpivotbars1 = array.new_int(0)
  164. var zigzagpivotdirs1 = array.new_int(0)
  165.  
  166. var zigzagpivots2 = array.new_float(0)
  167. var zigzagpivotbars2 = array.new_int(0)
  168. var zigzagpivotdirs2 = array.new_int(0)
  169.  
  170. var zigzagpivots3 = array.new_float(0)
  171. var zigzagpivotbars3 = array.new_int(0)
  172. var zigzagpivotdirs3 = array.new_int(0)
  173.  
  174. var zigzagpivots4 = array.new_float(0)
  175. var zigzagpivotbars4 = array.new_int(0)
  176. var zigzagpivotdirs4 = array.new_int(0)
  177.  
  178. var wmlines1 = array.new_line(8)
  179. var wmtype1 = array.new_int(2, 1)
  180. var wmLabels1 = array.new_bool(13, false)
  181. var wmLabel1 = array.new_label(1)
  182.  
  183. var wmlines2 = array.new_line(8)
  184. var wmtype2 = array.new_int(2, 1)
  185. var wmLabels2 = array.new_bool(13, false)
  186. var wmLabel2 = array.new_label(1)
  187.  
  188. var wmlines3 = array.new_line(8)
  189. var wmtype3 = array.new_int(2, 1)
  190. var wmLabels3 = array.new_bool(13, false)
  191. var wmLabel3 = array.new_label(1)
  192.  
  193. var wmlines4 = array.new_line(8)
  194. var wmtype4 = array.new_int(2, 1)
  195. var wmLabels4 = array.new_bool(13, false)
  196. var wmLabel4 = array.new_label(1)
  197.  
  198. pivots(length) =>
  199. float phigh = ta.highestbars(high, length) == 0 ? high : na
  200. float plow = ta.lowestbars(low, length) == 0 ? low : na
  201. dir = 0
  202. iff_1 = plow and na(phigh) ? -1 : dir[1]
  203. dir := phigh and na(plow) ? 1 : iff_1
  204. [dir, phigh, plow]
  205.  
  206. zigzag(length, zigzagpivots, zigzagpivotbars, zigzagpivotdirs) =>
  207. [dir, phigh, plow] = pivots(length)
  208. dirchanged = ta.change(dir)
  209.  
  210. if phigh or plow
  211. value = dir == 1 ? phigh : plow
  212. bar = bar_index
  213. newDir = dir
  214. if not dirchanged and array.size(zigzagpivots) >= 1
  215. pivot = array.shift(zigzagpivots)
  216. pivotbar = array.shift(zigzagpivotbars)
  217. pivotdir = array.shift(zigzagpivotdirs)
  218. useNewValues = value * pivotdir < pivot * pivotdir
  219. value := useNewValues ? pivot : value
  220. bar := useNewValues ? pivotbar : bar
  221. bar
  222.  
  223. if array.size(zigzagpivots) >= 2
  224. LastPoint = array.get(zigzagpivots, 1)
  225. newDir := dir * value > dir * LastPoint ? dir * 2 : dir
  226. newDir
  227.  
  228. array.unshift(zigzagpivots, value=value)
  229. array.unshift(zigzagpivotbars, bar)
  230. array.unshift(zigzagpivotdirs, newDir)
  231.  
  232. if array.size(zigzagpivots) > max_pivot_size
  233. array.pop(zigzagpivots)
  234. array.pop(zigzagpivotbars)
  235. array.pop(zigzagpivotdirs)
  236.  
  237. get_harmonic_label(wmLabels, dir, price, bar) =>
  238. isGartley = array.get(wmLabels, 0)
  239. isCrab = array.get(wmLabels, 1)
  240. isDeepCrab = array.get(wmLabels, 2)
  241. isBat = array.get(wmLabels, 3)
  242. isButterfly = array.get(wmLabels, 4)
  243. isShark = array.get(wmLabels, 5)
  244. isCypher = array.get(wmLabels, 6)
  245. is3Drives = array.get(wmLabels, 7)
  246. isFiveZero = array.get(wmLabels, 8)
  247. isAbcd = array.get(wmLabels, 9)
  248. isAbEqCd = array.get(wmLabels, 10)
  249. isAbcdExt = array.get(wmLabels, 11)
  250. isDoubleTop = array.get(wmLabels, 12) and dir < 0
  251. isDoubleBottom = array.get(wmLabels, 12) and dir > 0
  252.  
  253. labelText = isGartley ? 'Gartley' : ''
  254. labelText += (isCrab ? (labelText == '' ? '' : '\n') + 'Crab' : '')
  255. labelText += (isDeepCrab ? (labelText == '' ? '' : '\n') + 'Deep Crab' : '')
  256. labelText += (isBat ? (labelText == '' ? '' : '\n') + 'Bat' : '')
  257. labelText += (isButterfly ? (labelText == '' ? '' : '\n') + 'Butterfly' : '')
  258. labelText += (isShark ? (labelText == '' ? '' : '\n') + 'Shark' : '')
  259. labelText += (isCypher ? (labelText == '' ? '' : '\n') + 'Cypher' : '')
  260. labelText += (is3Drives ? (labelText == '' ? '' : '\n') + '3 Drive' : '')
  261. labelText += (isFiveZero ? (labelText == '' ? '' : '\n') + '5-0' : '')
  262. labelText += (isAbcd ? (labelText == '' ? '' : '\n') + 'ABCD' : '')
  263. labelText += (isAbEqCd ? (labelText == '' ? '' : '\n') + 'AB=CD' : '')
  264. labelText += (isAbcdExt ? (labelText == '' ? '' : '\n') + 'ABCD Extension' : '')
  265. labelText += (isDoubleTop ? (labelText == '' ? '' : '\n') + 'Double Top' : '')
  266. labelText += (isDoubleBottom ? (labelText == '' ? '' : '\n') + 'Double Bottom' : '')
  267. trendColor = dir > 0 ? bullishColor : bearishColor
  268.  
  269. baseLabel = label.new(x=bar, y=price, text=labelText, yloc=yloc.price, color=trendColor, style=dir < 1 ? label.style_label_down : label.style_label_up, textcolor=color.black, size=size.normal)
  270. baseLabel
  271.  
  272. detect_harmonic_pattern(zigzagpivots, zigzagpivotbars, zigzagpivotdirs, wmlines, wmlabel, wmtype, wmLabels, zigzagColor, zigzagWidth, zigzagStyle, showZigZag) =>
  273. start = waitForConfirmation ? 1 : 0
  274. wm_pattern = false
  275. abcd_pattern = false
  276. double_pattern = false
  277. if array.size(zigzagpivots) >= 6 + start and showZigZag
  278.  
  279. d = array.get(zigzagpivots, start + 0)
  280. dBar = array.get(zigzagpivotbars, start + 0)
  281. dDir = array.get(zigzagpivotdirs, start + 0)
  282.  
  283. c = array.get(zigzagpivots, start + 1)
  284. cBar = array.get(zigzagpivotbars, start + 1)
  285. cDir = array.get(zigzagpivotdirs, start + 1)
  286.  
  287. b = array.get(zigzagpivots, start + 2)
  288. bBar = array.get(zigzagpivotbars, start + 2)
  289. bDir = array.get(zigzagpivotdirs, start + 2)
  290.  
  291. a = array.get(zigzagpivots, start + 3)
  292. aBar = array.get(zigzagpivotbars, start + 3)
  293. aDir = array.get(zigzagpivotdirs, start + 3)
  294.  
  295. x = array.get(zigzagpivots, start + 4)
  296. xBar = array.get(zigzagpivotbars, start + 4)
  297. xDir = array.get(zigzagpivotdirs, start + 4)
  298.  
  299. y = array.get(zigzagpivots, start + 5)
  300. yBar = array.get(zigzagpivotbars, start + 5)
  301. yDir = array.get(zigzagpivotdirs, start + 5)
  302.  
  303. highPoint = math.max(x, a, b, c, d)
  304. lowPoint = math.min(x, a, b, c, d)
  305. dir = c > d ? 1 : -1
  306.  
  307. xabRatio = math.abs(b - a) / math.abs(x - a)
  308. abcRatio = math.abs(c - b) / math.abs(a - b)
  309. bcdRatio = math.abs(d - c) / math.abs(b - c)
  310. xadRatio = math.abs(d - a) / math.abs(x - a)
  311. yxaRatio = math.abs(a - x) / math.abs(y - x)
  312.  
  313. abTime = math.abs(aBar - bBar)
  314. cdTime = math.abs(cBar - dBar)
  315. abPrice = math.abs(a - b)
  316. cdPrice = math.abs(c - d)
  317.  
  318. time_ratio = cdTime / abTime
  319. price_ratio = cdPrice / abPrice
  320. abcdDirection = a < b and a < c and c < b and c < d and a < d and b < d ? 1 : a > b and a > c and c > b and c > d and a > d and b > d ? -1 : 0
  321.  
  322. risk = math.abs(b - d)
  323. reward = math.abs(c - d)
  324. riskPerReward = risk * 100 / (risk + reward)
  325.  
  326. if b < highPoint and b > lowPoint
  327. //gartley
  328. if gartley and xabRatio >= 0.618 * err_min and xabRatio <= 0.618 * err_max and abcRatio >= 0.382 * err_min and abcRatio <= 0.886 * err_max and (bcdRatio >= 1.272 * err_min and bcdRatio <= 1.618 * err_max or xadRatio >= 0.786 * err_min and xadRatio <= 0.786 * err_max)
  329. wm_pattern := true
  330. array.set(wmLabels, 0, true)
  331. else
  332. array.set(wmLabels, 0, false)
  333. //Crab
  334. if crab and xabRatio >= 0.382 * err_min and xabRatio <= 0.618 * err_max and abcRatio >= 0.382 * err_min and abcRatio <= 0.886 * err_max and (bcdRatio >= 2.24 * err_min and bcdRatio <= 3.618 * err_max or xadRatio >= 1.618 * err_min and xadRatio <= 1.618 * err_max)
  335. wm_pattern := true
  336. array.set(wmLabels, 1, true)
  337. else
  338. array.set(wmLabels, 1, false)
  339. //Deep Crab
  340. if deepCrab and xabRatio >= 0.886 * err_min and xabRatio <= 0.886 * err_max and abcRatio >= 0.382 * err_min and abcRatio <= 0.886 * err_max and (bcdRatio >= 2.00 * err_min and bcdRatio <= 3.618 * err_max or xadRatio >= 1.618 * err_min and xadRatio <= 1.618 * err_max)
  341. wm_pattern := true
  342. array.set(wmLabels, 2, true)
  343. else
  344. array.set(wmLabels, 2, false)
  345. //Bat
  346. if bat and xabRatio >= 0.382 * err_min and xabRatio <= 0.50 * err_max and abcRatio >= 0.382 * err_min and abcRatio <= 0.886 * err_max and (bcdRatio >= 1.618 * err_min and bcdRatio <= 2.618 * err_max or xadRatio >= 0.886 * err_min and xadRatio <= 0.886 * err_max)
  347. wm_pattern := true
  348. array.set(wmLabels, 3, true)
  349. else
  350. array.set(wmLabels, 3, false)
  351. //Butterfly
  352. if butterfly and xabRatio >= 0.786 * err_min and xabRatio <= 0.786 * err_max and abcRatio >= 0.382 * err_min and abcRatio <= 0.886 * err_max and (bcdRatio >= 1.618 * err_min and bcdRatio <= 2.618 * err_max or xadRatio >= 1.272 * err_min and xadRatio <= 1.618 * err_max)
  353. wm_pattern := true
  354. array.set(wmLabels, 4, true)
  355. else
  356. array.set(wmLabels, 4, false)
  357. //Shark
  358. if shark and abcRatio >= 1.13 * err_min and abcRatio <= 1.618 * err_max and bcdRatio >= 1.618 * err_min and bcdRatio <= 2.24 * err_max and xadRatio >= 0.886 * err_min and xadRatio <= 1.13 * err_max
  359. wm_pattern := true
  360. array.set(wmLabels, 5, true)
  361. else
  362. array.set(wmLabels, 5, false)
  363. //Cypher
  364. if cypher and xabRatio >= 0.382 * err_min and xabRatio <= 0.618 * err_max and abcRatio >= 1.13 * err_min and abcRatio <= 1.414 * err_max and (bcdRatio >= 1.272 * err_min and bcdRatio <= 2.00 * err_max or xadRatio >= 0.786 * err_min and xadRatio <= 0.786 * err_max)
  365. wm_pattern := true
  366. array.set(wmLabels, 6, true)
  367. else
  368. array.set(wmLabels, 6, false)
  369. //3 drive
  370. if threeDrives and yxaRatio >= 0.618 * err_min and yxaRatio <= 0.618 * err_max and xabRatio >= 1.27 * err_min and xabRatio <= 1.618 * err_max and abcRatio >= 0.618 * err_min and abcRatio <= 0.618 * err_max and bcdRatio >= 1.27 * err_min and bcdRatio <= 1.618 * err_max
  371. wm_pattern := true
  372. array.set(wmLabels, 7, true)
  373. else
  374. array.set(wmLabels, 7, false)
  375. //5-0
  376. if fiveZero and xabRatio >= 1.13 * err_min and xabRatio <= 1.618 * err_max and abcRatio >= 1.618 * err_min and abcRatio <= 2.24 * err_max and bcdRatio >= 0.5 * err_min and bcdRatio <= 0.5 * err_max
  377. wm_pattern := true
  378. array.set(wmLabels, 8, true)
  379. else
  380. array.set(wmLabels, 8, false)
  381. //ABCD Classic
  382. if abcdClassic and abcRatio >= 0.618 * err_min and abcRatio <= 0.786 * err_max and bcdRatio >= 1.272 * err_min and bcdRatio <= 1.618 * err_max and abcdDirection != 0
  383. abcd_pattern := true
  384. array.set(wmLabels, 9, true)
  385. else
  386. array.set(wmLabels, 9, false)
  387. //AB=CD
  388. if abEQcd and time_ratio >= err_min and time_ratio <= err_max and price_ratio >= err_min and price_ratio <= err_max and abcdDirection != 0
  389. abcd_pattern := true
  390. array.set(wmLabels, 10, true)
  391. else
  392. array.set(wmLabels, 10, false)
  393. //ABCD Ext
  394. if abcdExt and price_ratio >= 1.272 * err_min and price_ratio <= 1.618 * err_max and abcRatio >= 0.618 * err_min and abcRatio <= 0.786 * err_max and abcdDirection != 0
  395. abcd_pattern := true
  396. array.set(wmLabels, 11, true)
  397. else
  398. array.set(wmLabels, 11, false)
  399. //Double Top/Bottom
  400. if doubleBottomTop and (dDir == 1 and bDir == 2 and cDir == -1 or dDir == -1 and bDir == -2 and cDir == 1) and riskPerReward < MaxRiskPerReward
  401. double_pattern := true
  402. array.set(wmLabels, 12, true)
  403. else
  404. array.set(wmLabels, 12, false)
  405.  
  406. cancelW = false
  407. cancelA = false
  408. cancelD = false
  409. if wm_pattern[1] and x == x[1] and a == a[1] and b == b[1] and c == c[1]
  410. line.delete(array.get(wmlines, 0))
  411. line.delete(array.get(wmlines, 1))
  412. line.delete(array.get(wmlines, 2))
  413. line.delete(array.get(wmlines, 3))
  414. line.delete(array.get(wmlines, 4))
  415. line.delete(array.get(wmlines, 5))
  416. line.delete(array.get(wmlines, 6))
  417. line.delete(array.get(wmlines, 7))
  418. label.delete(array.get(wmlabel, 0))
  419. cancelW := true
  420. cancelW
  421.  
  422. if abcd_pattern[1] and a == a[1] and b == b[1] and c == c[1]
  423. line.delete(array.get(wmlines, 1))
  424. line.delete(array.get(wmlines, 2))
  425. line.delete(array.get(wmlines, 3))
  426. label.delete(array.get(wmlabel, 0))
  427. cancelA := true
  428. cancelA
  429.  
  430. if double_pattern[1] and a == a[1] and b == b[1] and c == c[1]
  431. line.delete(array.get(wmlines, 5))
  432. label.delete(array.get(wmlabel, 0))
  433. cancelD := true
  434. cancelD
  435.  
  436. if wm_pattern
  437. xa = line.new(y1=x, y2=a, x1=xBar, x2=aBar, color=zigzagColor, width=zigzagWidth, style=zigzagStyle)
  438. ab = line.new(y1=a, y2=b, x1=aBar, x2=bBar, color=zigzagColor, width=zigzagWidth, style=zigzagStyle)
  439. bc = line.new(y1=b, y2=c, x1=bBar, x2=cBar, color=zigzagColor, width=zigzagWidth, style=zigzagStyle)
  440. cd = line.new(y1=c, y2=d, x1=cBar, x2=dBar, color=zigzagColor, width=zigzagWidth, style=zigzagStyle)
  441. xb = line.new(y1=x, y2=b, x1=xBar, x2=bBar, color=zigzagColor, width=zigzagWidth, style=zigzagStyle)
  442. bd = line.new(y1=b, y2=d, x1=bBar, x2=dBar, color=zigzagColor, width=zigzagWidth, style=zigzagStyle)
  443. xd = line.new(y1=x, y2=d, x1=xBar, x2=dBar, color=zigzagColor, width=zigzagWidth, style=zigzagStyle)
  444. ac = line.new(y1=a, y2=c, x1=aBar, x2=cBar, color=zigzagColor, width=zigzagWidth, style=zigzagStyle)
  445. array.set(wmlines, 0, xa)
  446. array.set(wmlines, 1, ab)
  447. array.set(wmlines, 2, bc)
  448. array.set(wmlines, 3, cd)
  449. array.set(wmlines, 4, xb)
  450. array.set(wmlines, 5, bd)
  451. array.set(wmlines, 6, xd)
  452. array.set(wmlines, 7, ac)
  453. array.set(wmtype, 0, dir)
  454. if abcd_pattern and not wm_pattern
  455. ab = line.new(y1=a, y2=b, x1=aBar, x2=bBar, color=zigzagColor, width=zigzagWidth, style=zigzagStyle)
  456. bc = line.new(y1=b, y2=c, x1=bBar, x2=cBar, color=zigzagColor, width=zigzagWidth, style=zigzagStyle)
  457. cd = line.new(y1=c, y2=d, x1=cBar, x2=dBar, color=zigzagColor, width=zigzagWidth, style=zigzagStyle)
  458. array.set(wmlines, 1, ab)
  459. array.set(wmlines, 2, bc)
  460. array.set(wmlines, 3, cd)
  461. array.set(wmtype, 0, dir)
  462. if double_pattern and not wm_pattern
  463. bd = line.new(y1=b, y2=d, x1=bBar, x2=dBar, color=zigzagColor, width=zigzagWidth, style=zigzagStyle)
  464. array.set(wmlines, 5, bd)
  465. array.set(wmtype, 0, dir)
  466.  
  467. if wm_pattern or abcd_pattern or double_pattern
  468. array.set(wmlabel, 0, get_harmonic_label(wmLabels, dir, d, dBar))
  469.  
  470. pattern = wm_pattern and not wm_pattern[1] or abcd_pattern and not abcd_pattern[1] or double_pattern and not double_pattern[1]
  471. pattern
  472.  
  473. zigzag(zigzag1Length, zigzagpivots1, zigzagpivotbars1, zigzagpivotdirs1)
  474. zigzag(zigzag2Length, zigzagpivots2, zigzagpivotbars2, zigzagpivotdirs2)
  475. zigzag(zigzag3Length, zigzagpivots3, zigzagpivotbars3, zigzagpivotdirs3)
  476. zigzag(zigzag4Length, zigzagpivots4, zigzagpivotbars4, zigzagpivotdirs4)
  477.  
  478. wm_pattern1 = detect_harmonic_pattern(zigzagpivots1, zigzagpivotbars1, zigzagpivotdirs1, wmlines1, wmLabel1, wmtype1, wmLabels1, zigzag1Color, zigzag1Width, zigzag1Style, showZigZag1)
  479. wm_pattern2 = detect_harmonic_pattern(zigzagpivots2, zigzagpivotbars2, zigzagpivotdirs2, wmlines2, wmLabel2, wmtype2, wmLabels2, zigzag2Color, zigzag2Width, zigzag2Style, showZigZag2)
  480. wm_pattern3 = detect_harmonic_pattern(zigzagpivots3, zigzagpivotbars3, zigzagpivotdirs3, wmlines3, wmLabel3, wmtype3, wmLabels3, zigzag3Color, zigzag3Width, zigzag3Style, showZigZag3)
  481. wm_pattern4 = detect_harmonic_pattern(zigzagpivots4, zigzagpivotbars4, zigzagpivotdirs4, wmlines4, wmLabel4, wmtype4, wmLabels4, zigzag4Color, zigzag4Width, zigzag4Style, showZigZag4)
  482.  
  483. alertcondition(wm_pattern1 or wm_pattern2 or wm_pattern3 or wm_pattern4, title='New harmonic pattern alert', message='New harmonic pattern detected on {{ticker}}')
  484.  
  485. var stats = table.new(position=position.top_right, columns=8, rows=max_pivot_size + 2, border_width=1)
  486.  
  487. if barstate.islast and showStatTable
  488. if showZigZag1
  489. table.cell(table_id=stats, column=0, row=0, text='Zigzag ' + str.tostring(zigzag1Length), bgcolor=color.black, text_color=color.white)
  490. table.cell(table_id=stats, column=0, row=1, text='Price', bgcolor=color.black, text_color=color.white)
  491. table.cell(table_id=stats, column=1, row=1, text='BarIndex', bgcolor=color.black, text_color=color.white)
  492.  
  493. for i = 0 to array.size(zigzagpivots1) - 1 by 1
  494. bgcolor = array.get(zigzagpivotdirs1, i) == 1 ? color.lime : color.orange
  495. table.cell(table_id=stats, column=0, row=i + 2, text=str.tostring(array.get(zigzagpivots1, i)), bgcolor=bgcolor)
  496. table.cell(table_id=stats, column=1, row=i + 2, text=str.tostring(array.get(zigzagpivotbars2, i)), bgcolor=bgcolor)
  497.  
  498. if showZigZag2
  499. table.cell(table_id=stats, column=2, row=0, text='Zigzag ' + str.tostring(zigzag2Length), bgcolor=color.black, text_color=color.white)
  500. table.cell(table_id=stats, column=2, row=1, text='Price', bgcolor=color.black, text_color=color.white)
  501. table.cell(table_id=stats, column=3, row=1, text='BarIndex', bgcolor=color.black, text_color=color.white)
  502.  
  503. for i = 0 to array.size(zigzagpivots2) - 1 by 1
  504. bgcolor = array.get(zigzagpivotdirs2, i) == 1 ? color.lime : color.orange
  505. table.cell(table_id=stats, column=2, row=i + 2, text=str.tostring(array.get(zigzagpivots2, i)), bgcolor=bgcolor)
  506. table.cell(table_id=stats, column=3, row=i + 2, text=str.tostring(array.get(zigzagpivotbars2, i)), bgcolor=bgcolor)
  507.  
  508. if showZigZag3
  509. table.cell(table_id=stats, column=4, row=0, text='Zigzag ' + str.tostring(zigzag3Length), bgcolor=color.black, text_color=color.white)
  510. table.cell(table_id=stats, column=4, row=1, text='Price', bgcolor=color.black, text_color=color.white)
  511. table.cell(table_id=stats, column=5, row=1, text='BarIndex', bgcolor=color.black, text_color=color.white)
  512.  
  513. for i = 0 to array.size(zigzagpivots3) - 1 by 1
  514. bgcolor = array.get(zigzagpivotdirs3, i) == 1 ? color.lime : color.orange
  515. table.cell(table_id=stats, column=4, row=i + 2, text=str.tostring(array.get(zigzagpivots3, i)), bgcolor=bgcolor)
  516. table.cell(table_id=stats, column=5, row=i + 2, text=str.tostring(array.get(zigzagpivotbars3, i)), bgcolor=bgcolor)
  517.  
  518. if showZigZag4
  519. table.cell(table_id=stats, column=6, row=0, text='Zigzag ' + str.tostring(zigzag4Length), bgcolor=color.black, text_color=color.white)
  520. table.cell(table_id=stats, column=6, row=1, text='Price', bgcolor=color.black, text_color=color.white)
  521. table.cell(table_id=stats, column=7, row=1, text='BarIndex', bgcolor=color.black, text_color=color.white)
  522.  
  523. for i = 0 to array.size(zigzagpivots4) - 1 by 1
  524. bgcolor = array.get(zigzagpivotdirs4, i) == 1 ? color.lime : color.orange
  525. table.cell(table_id=stats, column=6, row=i + 2, text=str.tostring(array.get(zigzagpivots4, i)), bgcolor=bgcolor)
  526. table.cell(table_id=stats, column=7, row=i + 2, text=str.tostring(array.get(zigzagpivotbars4, i)), bgcolor=bgcolor)
  527.  
  528.  
Advertisement
Add Comment
Please, Sign In to add comment