Advertisement
xmd79

Historical Pattern Matcher [Trendoscope]

Aug 12th, 2023
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.68 KB | None | 0 0
  1. // This work is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
  2. // © Trendoscope Pty Ltd
  3. // ░▒
  4. // ▒▒▒ ▒▒
  5. // ▒▒▒▒▒ ▒▒
  6. // ▒▒▒▒▒▒▒░ ▒ ▒▒
  7. // ▒▒▒▒▒▒ ▒ ▒▒
  8. // ▓▒▒▒ ▒ ▒▒▒▒▒▒▒▒▒▒▒
  9. // ▒▒▒▒▒▒▒▒▒▒▒ ▒ ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  10. // ▒ ▒ ░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░
  11. // ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░▒▒▒▒▒▒▒▒
  12. // ▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ ▒▒
  13. // ▒▒▒▒▒ ▒▒▒▒▒▒▒
  14. // ▒▒▒▒▒▒▒▒▒
  15. // ▒▒▒▒▒ ▒▒▒▒▒
  16. // ░▒▒▒▒ ▒▒▒▒▓ ████████╗██████╗ ███████╗███╗ ██╗██████╗ ██████╗ ███████╗ ██████╗ ██████╗ ██████╗ ███████╗
  17. // ▓▒▒▒▒ ▒▒▒▒ ╚══██╔══╝██╔══██╗██╔════╝████╗ ██║██╔══██╗██╔═══██╗██╔════╝██╔════╝██╔═══██╗██╔══██╗██╔════╝
  18. // ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ ██║ ██████╔╝█████╗ ██╔██╗ ██║██║ ██║██║ ██║███████╗██║ ██║ ██║██████╔╝█████╗
  19. // ▒▒▒▒▒ ▒▒▒▒▒ ██║ ██╔══██╗██╔══╝ ██║╚██╗██║██║ ██║██║ ██║╚════██║██║ ██║ ██║██╔═══╝ ██╔══╝
  20. // ▒▒▒▒▒ ▒▒▒▒▒ ██║ ██║ ██║███████╗██║ ╚████║██████╔╝╚██████╔╝███████║╚██████╗╚██████╔╝██║ ███████╗
  21. // ▒▒ ▒
  22. //@version=5
  23. import HeWhoMustNotBeNamed/DrawingTypes/2 as dr
  24. import HeWhoMustNotBeNamed/DrawingMethods/2
  25. import HeWhoMustNotBeNamed/ZigzagTypes/5 as zg
  26. import HeWhoMustNotBeNamed/ZigzagMethods/6
  27.  
  28. import HeWhoMustNotBeNamed/FibRatios/1 as fibs
  29.  
  30.  
  31. indicator("Historical Pattern Matcher [Trendoscope]", "HPM[Trendoscope]", overlay = true, max_lines_count=500, max_labels_count=500, max_bars_back = 1000)
  32. zigzagLength = input.int(13, step=5, minval=3, title='Length', group='Zigzag', tooltip='Zigzag length', display = display.none)
  33. numberOfPivots = input.int(6, "Number of Pivots", group='Zigzag', minval = 5, tooltip = 'Number of pivots to match for pattern', display = display.none)
  34.  
  35. projectionBullishColor = input.color(color.lime, 'Bullish Colors', inline='bullc', display = display.none, group='Display')
  36. labelBullishColor = input.color(color.green, '', inline='bullc', display = display.none, tooltip = 'Projection and label colors for bullish projections', group='Display')
  37. projectionBearishColor = input.color(color.orange, 'Bullish Colors', inline='bearc', display = display.none, group='Display')
  38. labelBearishColor = input.color(color.red, '', inline='bearc', display = display.none, tooltip = 'Projection and label colors for bearish projections', group='Display')
  39. type PatternData
  40. float[] ratios
  41. array<zg.Pivot> lastOccurence
  42.  
  43. method tostring(PatternData this)=>
  44. strValue = "Last Occurence : "+str.format_time(this.lastOccurence.get(1).point.bartime) +
  45. "\nLast retracement :"+str.tostring(this.ratios.last()) +
  46. "\nMax retracement :"+str.tostring(this.ratios.max()) +
  47. "\nMin retracement :"+str.tostring(this.ratios.min()) +
  48. "\nMedian retracement :"+str.tostring(this.ratios.median()) +
  49. "\nNumber of Occurences :"+str.tostring(this.ratios.size())
  50. strValue
  51. type PatternDrawing
  52. array<dr.Line> currentPattern
  53. array<dr.Line> lastOccurence
  54. dr.Box projectionRange
  55. dr.Label projectionDetails
  56. dr.Line medianProjection
  57.  
  58. depth = numberOfPivots + 2
  59. useRealTimeBars = true
  60.  
  61. offset = useRealTimeBars? 0 : 1
  62.  
  63. indicators = matrix.new<float>()
  64. indicatorNames = array.new<string>()
  65.  
  66. var zg.Zigzag zigzag = zg.Zigzag.new(zigzagLength, depth, offset)
  67. zigzag.calculate(array.from(high, low), indicators, indicatorNames)
  68.  
  69. dr.LineProperties bullishPatternProperties = dr.LineProperties.new(xloc.bar_time, color = labelBullishColor, width = 2)
  70. dr.LineProperties bearishPatternProperties = dr.LineProperties.new(xloc.bar_time, color = labelBearishColor, width = 2)
  71.  
  72. dr.LineProperties bullishProjectionProperties = dr.LineProperties.new(color = labelBullishColor, style = line.style_arrow_right, width = 1)
  73. dr.LineProperties bearishProjectionProperties = dr.LineProperties.new(color = labelBearishColor, style = line.style_arrow_right, width = 1)
  74.  
  75. dr.BoxProperties bullishBoxProperties = dr.BoxProperties.new(color.new(projectionBullishColor, 80), color.new(projectionBullishColor, 90))
  76. dr.BoxProperties bearishBoxProperties = dr.BoxProperties.new(color.new(projectionBearishColor, 80), color.new(projectionBearishColor, 90))
  77. dr.LabelProperties bullishlblProperties = dr.LabelProperties.new(textcolor = chart.fg_color, style = label.style_label_lower_left, color = color.new(labelBullishColor, 90))
  78. dr.LabelProperties bearishLblProperties = dr.LabelProperties.new(textcolor = chart.fg_color, style = label.style_label_upper_left, color = color.new(labelBearishColor, 90))
  79. method getMatchString(array<zg.Pivot> this, startIndex=1)=>
  80. matchstring = ''
  81. for i=startIndex to this.size()-2
  82. for j=i+1 to this.size()-1
  83. matchstring+= this.get(i).point.price > this.get(j).point.price ? '1' : '0'
  84. matchstring
  85.  
  86. method ratios(array<zg.Pivot> this)=>
  87. ratios = array.new<float>()
  88. for pivot in this
  89. ratios.push(pivot.ratio)
  90. ratios
  91.  
  92. method tostring(array<float> this)=> str.tostring(this)
  93. method tostring(array<int> this)=> str.tostring(this)
  94. method update(map<string, PatternData> this, array<zg.Pivot> pivots)=>
  95. matchstring = pivots.getMatchString()
  96.  
  97. PatternData currentObj = na
  98. if(this.contains(matchstring))
  99. currentObj := this.get(matchstring)
  100. else
  101. currentObj := PatternData.new(array.new<float>())
  102. this.put(matchstring, currentObj)
  103.  
  104. currentObj.ratios.push(pivots.get(0).ratio)
  105. currentObj.lastOccurence := pivots
  106.  
  107. method draw(array<zg.Pivot> pivots, PatternData patternInfo, int startIndex=1)=>
  108. var drawing = PatternDrawing.new(array.new<dr.Line>(), array.new<dr.Line>(), dr.Box.new(),
  109. dr.Label.new(), dr.Line.new())
  110. drawing.currentPattern.clear()
  111. drawing.lastOccurence.clear()
  112. drawing.medianProjection.delete()
  113. drawing.projectionDetails.delete()
  114. currentDir = pivots.get(startIndex).dir
  115. for i=startIndex to pivots.size()-2
  116. drawing.currentPattern.push(pivots.get(i).point.createLine(pivots.get(i+1).point, currentDir < 0? bullishPatternProperties : bearishPatternProperties))
  117. for i=1 to patternInfo.lastOccurence.size()-2
  118. drawing.lastOccurence.push(patternInfo.lastOccurence.get(i).point.createLine(patternInfo.lastOccurence.get(i+1).point, currentDir < 0? bullishPatternProperties : bearishPatternProperties))
  119. drawing.currentPattern.draw()
  120. drawing.lastOccurence.draw()
  121. ratioMin = patternInfo.ratios.min()
  122. ratioMax = patternInfo.ratios.max()
  123.  
  124. lastPrice = pivots.get(startIndex+1).point.price
  125. currentPrice = pivots.get(startIndex).point.price
  126.  
  127.  
  128. lastBar = pivots.get(startIndex+1).point.bar
  129. currentBar = pivots.get(startIndex).point.bar
  130. priceMin = fibs.retracement(lastPrice, currentPrice, ratioMin)
  131. priceMax = fibs.retracement(lastPrice, currentPrice, ratioMax)
  132.  
  133. barMin = int(currentBar + ratioMin*(currentBar-lastBar))+1
  134. barMax = int(currentBar + ratioMax*(currentBar-lastBar))+1
  135.  
  136. drawing.projectionRange.p1 := dr.Point.new(priceMin, barMin)
  137. drawing.projectionRange.p2 := dr.Point.new(priceMax, barMax)
  138. drawing.projectionRange.properties := currentDir < 0? bullishBoxProperties : bearishBoxProperties
  139. drawing.projectionRange.draw()
  140.  
  141. drawing.projectionDetails.lblText := patternInfo.tostring()
  142. drawing.projectionDetails.point := dr.Point.new(priceMax, barMax)
  143. drawing.projectionDetails.properties := currentDir < 0? bullishlblProperties : bearishLblProperties
  144. drawing.projectionDetails.draw()
  145.  
  146. ratioMedium = patternInfo.ratios.median()
  147. priceMedian = fibs.retracement(lastPrice, currentPrice, ratioMedium)
  148. barMedian = int(currentBar + ratioMedium*(currentBar-lastBar))+1
  149. drawing.medianProjection := pivots.get(startIndex).point.createLine(dr.Point.new(priceMedian, barMedian), currentDir < 0? bullishProjectionProperties : bearishProjectionProperties)
  150. drawing.medianProjection.draw()
  151.  
  152. var patternMap = map.new<string, PatternData>()
  153.  
  154. var lastPatternBar = 0
  155. if zigzag.flags.newPivot and zigzag.zigzagPivots.size() == depth
  156. lastConfirmedPivot = zigzag.zigzagPivots.get(1)
  157. if(lastConfirmedPivot.point.bar > lastPatternBar)
  158. snapshot = zigzag.zigzagPivots.copy()
  159. snapshot.shift()
  160. patternMap.update(snapshot)
  161. lastPatternBar := lastConfirmedPivot.point.bar
  162.  
  163. currentSnapshot = zigzag.zigzagPivots.copy()
  164. currentSnapshot.pop()
  165. matchstring = currentSnapshot.getMatchString()
  166. if(patternMap.contains(matchstring))
  167. patternInfo = patternMap.get(matchstring)
  168. currentSnapshot.draw(patternInfo)
  169.  
  170. latestSnapshot = zigzag.zigzagPivots.copy()
  171. latestSnapshot.pop()
  172. latestSnapshot.pop()
  173. latestMatch = latestSnapshot.getMatchString(0)
  174. if(patternMap.contains(latestMatch))
  175. patternInfo = patternMap.get(latestMatch)
  176. latestSnapshot.draw(patternInfo, 0)
  177.  
  178.  
  179.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement