Advertisement
xmd79

Support and Resistance: Triangles [YinYangAlgorithms]

Sep 29th, 2023
468
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.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. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  3. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  4. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ,@@@@@@@@@@@@@@@@@@@@@@@
  5. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@
  6. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@
  7. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@
  8. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@
  9. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ .@@@@@@@@@@@@@@@ @@@@@@@@
  10. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ *@@@@@@@@@@@@@@ @@@@@@@
  11. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@ @@@@@
  12. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@ @@@@
  13. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@
  14. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@. @@
  15. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@
  16. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@. @
  17. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @
  18. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@, @
  19. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @
  20. // @@@@@@@@@@@@@@@@@@@@@@@@@@@ @
  21. // @@@@@@@@@@@@@@@@@@@@@@@@@ @@
  22. // @@@@@@@@@@@@@@@@@@@@@@@ @@
  23. // @@@@@@@@@@@@@@@@@@@@@@ @@@
  24. // @@@@@@@@@@@@@@@@@@@@@* @@@@@ @@@@
  25. // @@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@ @@@@@
  26. // @@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@ @@@@@@@
  27. // @@@@@@@@@@@@@@@@@@@@@ @@@@@@@@% @@@@@@@@
  28. // @@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@
  29. // @@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@
  30. // @@@@@@@@@@@@@@@@@@@@@@@@ %@@@@@@@@@@@@@@@
  31. // @@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@
  32. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@
  33. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  34. // © YinYangAlgorithms
  35.  
  36. //@version=5
  37. indicator("Support and Resistance: Triangles [YinYangAlgorithms]", overlay=true)
  38. // ~~~~~~~~ INPUTS ~~~~~~~~ //
  39. showTriangles = input.bool(true, "Show Triangles", tooltip="Should we plot triangles on the screen?")
  40. createZonesFor = input.string("Both", "Triangle Zones", options=["Upwards", "Downwards", "Both", "Neither"], tooltip="What types of triangles should we create our zones for?")
  41. dev = input.float(0.0001, "Max Deviation Allowed", tooltip="Maximum Deviation up or down from the last bars High/Low for potential to create a Triangle")
  42. lookBack = input.int(50, "Lookback Distance", tooltip="How far back we look to see for potential of a High/Low within Deviation range")
  43. minDistance = input.int(10, "Min Distance", tooltip="This is so triangles are spaced properly and not from 2 bars beside each other. Min distance allocated between 2 points to create a Triangle")
  44. eachBarPercentIncrease = input.float(0.005, "Bar Percent Increase", tooltip="How much % multiplier do we apply for each bar spacing of the triangle. 0.005 creates a close to Equilateral Triangle, but other values like 0.004 and 0.006 seem to work well too.")
  45.  
  46. // ~~~~~~~~ VARIABLES ~~~~~~~~ //
  47. createUpwardsTriangle = createZonesFor == "Both" or createZonesFor == "Upwards"
  48. createDownwardsTriangle = createZonesFor == "Both" or createZonesFor == "Downwards"
  49. float lt = 0.
  50. float lb = 0.
  51. float ht = 0.
  52. float hb = 0.
  53.  
  54. // ~~~~~~~~ FUNCTIONS ~~~~~~~~ //
  55. //Create the triangles and send back its Support and Resistance locations
  56. createTriangle(_up, _src, _color) =>
  57. //Calculate the maximum upwards and downwards deviation allowed based on source
  58. srcDown = _src[1] - (_src[1] * dev)
  59. srcUp = _src[1] + (_src[1] * dev)
  60. //see if any of the same source between minDistance and lookBack are within the deviation allowment
  61. triangleDev = 0.
  62. triangleIndex = 0
  63. for i = minDistance to lookBack
  64. cur = _src[i]
  65. if cur >= srcDown and cur <= srcUp
  66. triangleDev := cur
  67. triangleIndex := i
  68. break
  69. //calculate the support and resistance locations based on this triangle
  70. triangleTop = -1.
  71. triangleBot = -1.
  72. //did we find a valid triangle?
  73. if triangleDev != 0
  74. //the middle of the triangle (bar index location)
  75. midDist = triangleIndex / 2
  76. //how much of a % change is there (we generally want to create an Equilateral Triangle)
  77. percentIncrease = triangleIndex * eachBarPercentIncrease
  78. //modify the mid point based on if its an upward or downward triangle
  79. midPoint = _up ? triangleDev * (1 + percentIncrease) : triangleDev * (1 - percentIncrease)
  80. //create the triangle
  81. if showTriangles and ((_up and createUpwardsTriangle) or (not _up and createDownwardsTriangle))
  82. line.new(bar_index - triangleIndex, triangleDev, bar_index[1], _src[1], color=_color, width=1)
  83. line.new(bar_index - triangleIndex, triangleDev, bar_index - midDist, midPoint, color=_color, width=1)
  84. line.new(bar_index - midDist, midPoint, bar_index[1], _src[1], color=_color, width=1)
  85. //save the support and resistance locations
  86. triangleTop := midPoint
  87. triangleBot := triangleDev
  88. //return the support and resistance locations (if there were any)
  89. [triangleTop, triangleBot]
  90.  
  91. // ~~~~~~~~ CALCULATIONS ~~~~~~~~ //
  92. [lowTop, lowBot] = createTriangle(true, low, color.green)
  93. [highTop, highBot] = createTriangle(false, high, color.red)
  94.  
  95. lt := lowTop != -1 ? lowTop : lt[1]
  96. lb := lowBot != -1 ? lowBot : lb[1]
  97. ht := highTop != -1 ? highTop : ht[1]
  98. hb := highBot != -1 ? highBot : hb[1]
  99.  
  100. // ~~~~~~~~ PLOTS ~~~~~~~~ //
  101. plot(createUpwardsTriangle ? lt : na, color=color.red, style=plot.style_circles)
  102. plot(createUpwardsTriangle ? lb : na, color=color.orange, style=plot.style_circles)
  103. plot(createDownwardsTriangle ? ht : na, color=color.green, style=plot.style_circles)
  104. plot(createDownwardsTriangle ? hb : na, color=color.blue, style=plot.style_circles)
  105.  
  106. // ~~~~~~~~ END ~~~~~~~~ //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement