Advertisement
xmd79

Multi-Timeframe Recursive Zigzag [Trendoscope®]

Jan 3rd, 2024
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.39 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 Trendoscope/Zigzag/6 as zg
  24. import HeWhoMustNotBeNamed/utils/1 as ut
  25. import HeWhoMustNotBeNamed/RecursiveAlerts/2 as ra
  26.  
  27. indicator("Multi-Timeframe Recursive Zigzag [Trendoscope®]", "RZigzag-MTF[Trendoscope®]", overlay = true, max_lines_count=500, max_labels_count=500, max_bars_back = 1000)
  28. theme = input.string('Dark', title='Theme', options=['Light', 'Dark'], group='Generic Settings',
  29. tooltip='Chart theme settings. Line and label colors are generted based on the theme settings. If dark theme is selected, '+
  30. 'lighter colors are used and if light theme is selected, darker colors are used.', display=display.none)
  31.  
  32. timeframe = input.timeframe("", "Timeframe", group="Zigzag", tooltip = 'Timeframe on which the zigzag needs to be calculated', display = display.none)
  33. zigzagLength = input.int(5, step=5, minval=1, title='Length', group='Zigzag', tooltip='Zigzag length for level 0 zigzag', display=display.none)
  34. depth = input.int(200, "Depth", step=25, maxval=500, group='Zigzag', tooltip='Zigzag depth refers to max number of pivots to show on chart for level 0 zigzag.'+
  35. ' This number will also impact the recursive calculation and number of levels of zigzag displayed on the chart.', display=display.none)
  36. highlight = input.int(1, "Highlight Level", group='Zigzag', minval = 0, tooltip = 'Highlight one level of the zigzag out of the available', display=display.none)
  37. useRealTimeBars = input.bool(true, 'Use Real Time Bars', group='Zigzag', tooltip = 'If enabled real time bars are used for calculation. Otherwise, only confirmed bars are used', display=display.none)
  38. curvedZigzag = input.bool(false, 'Curved Zigzag', group='Zigzag', tooltip = 'If enabled, prints curved zigzag instead of straight lines', display=display.none)
  39. enableRsi = input.bool(true, 'RSI', group='Indicators', inline='rsi', display=display.none)
  40. rsiLength = input.int(14, '', group='Indicators', inline='rsi', tooltip='Enable and configure RSI indicator', display=display.none)
  41.  
  42. enableMfi = input.bool(true, 'MFI', group='Indicators', inline='mfi', display=display.none)
  43. mfiLength = input.int(14, '', group='Indicators', inline='mfi', tooltip='Enable and configure MFI indicator', display=display.none)
  44.  
  45. enableObv = input.bool(true, 'OBV', group='Indicators', inline='obv', tooltip = 'Enable and configure OBV indicator', display=display.none)
  46.  
  47. enableCustom = input.bool(false, '', group='Custom External Indicator', inline='custom', display=display.none)
  48. customName = input.string('Custom', '', group='Custom External Indicator', inline='custom', display=display.none)
  49. customValue = input.source(close, '', group='Custom External Indicator', inline='custom', tooltip = 'Enable and configure custom external indicator', display=display.none)
  50. showLabel = false
  51.  
  52. alertTooltip = 'Confirmed Pivot refers to last but one pivot which is confirmed. Alerting on confirmed pivot means alerts are triggered only when a new confirmed pivot is formed. '+
  53. 'This also means there will be lag in the alert. On the contry, if you select Last Pivot Update alerts will be on real time. But, remember that last pivot of the Zigzag always repaint. Hence, there will be lots of alerts and repaints'
  54. alertType = input.string('Confirmed Pivot Update', 'Alert Type', ['Confirmed Pivot Update', 'Last Pivot Update'], tooltip = alertTooltip, group='Alert', display=display.none)
  55.  
  56. alertPivot = alertType == 'Confirmed Pivot Update'? 1 : 0
  57. defaultAlertTemplate = '{
  58. \n\t"type" : "{alertType}",
  59. \n\t"level": "{level}",
  60. \n\t"pivot" : {pivot}
  61. \n}'
  62. alertTemplate = input.text_area (defaultAlertTemplate, '', group='Alert', display=display.none)
  63. offset = useRealTimeBars? 0 : 1
  64.  
  65. showLabel := enableRsi or enableMfi or enableObv or (enableCustom and customValue!=close)
  66.  
  67. themeColors = ut.getColors(theme)
  68.  
  69. getZigzag()=>
  70. indicators = matrix.new<float>()
  71. indicatorNames = array.new<string>()
  72. if(enableRsi)
  73. indicatorNames.unshift('RSI'+str.tostring(rsiLength))
  74. indicators.add_row(0, array.from(ta.rsi(high, rsiLength), ta.rsi(low, rsiLength), ta.rsi(close, rsiLength)))
  75.  
  76. if(enableMfi)
  77. indicatorNames.unshift('MFI'+str.tostring(mfiLength))
  78. indicators.add_row(0, array.from(ta.mfi(high, mfiLength), ta.mfi(low, mfiLength), ta.mfi(close, mfiLength)))
  79.  
  80. if(enableObv)
  81. indicatorNames.unshift('OBV')
  82. indicators.add_row(0, array.from(ta.obv, ta.obv, ta.obv))
  83.  
  84. if(enableCustom and (customValue!=close or na(customValue)))
  85. indicatorNames.unshift(customName)
  86. indicators.add_row(0, array.from(customValue, customValue, customValue))
  87.  
  88. var zg.Zigzag zigzag = zg.Zigzag.new(zigzagLength, depth, offset)
  89. [lh, ll, lt] = request.security_lower_tf("", "", [high, low, time])
  90. lowTime = time
  91. highTime = time
  92. if(lt.size() > 0)
  93. highestIndex = lh.sort_indices(order.descending).get(0)
  94. lowestIndex = ll.sort_indices(order.ascending).get(0)
  95. lowTime := lt.get(lowestIndex)
  96. highTime := lt.get(highestIndex)
  97.  
  98. zigzag.calculate(array.from(high, low), indicators, indicatorNames, highTime, lowTime)
  99. if(zigzag.zigzagPivots.size() < 0)
  100. zigzag.zigzagPivots := zigzag.zigzagPivots.slice(0, zigzag.zigzagPivots.size())
  101. barstate.islast? zigzag : na
  102.  
  103. indicatorTf = timeframe.in_seconds("") > timeframe.in_seconds(timeframe) ? "" : timeframe
  104. zigzag = request.security("", indicatorTf, getZigzag())
  105.  
  106. if(barstate.islast)
  107. var array<zg.ZigzagDrawingPL> drawingArray = array.new<zg.ZigzagDrawingPL>()
  108. var firstDraw = true
  109. var lastPivotBar = 0
  110. if((firstDraw or zigzag.flags.newPivot))
  111. drawingArray.clear()
  112. firstDraw := false
  113.  
  114. mlzigzag = zigzag
  115. varip print = true
  116.  
  117. rowNum = 0
  118. var legend = table.new(position=position.top_right, columns=2, rows=100, border_width=1)
  119. table.clear(legend, 0, 0, 1, 99)
  120. while(mlzigzag.zigzagPivots.size() > 3)
  121. labelColor = themeColors.remove(0)
  122. themeColors.push(labelColor)
  123. highlightLevel = rowNum == highlight
  124. lineWidth = highlightLevel ? 2 : 0
  125. lineStyle = highlightLevel ? line.style_solid:line.style_dotted
  126. lineColor = highlightLevel ? labelColor : color.new(labelColor, 30)
  127. zg.ZigzagProperties props = zg.ZigzagProperties.new(lineColor, lineWidth, lineStyle, highlightLevel, maxObjects = depth, curved=curvedZigzag)
  128. zg.ZigzagDrawingPL drawing = zg.ZigzagDrawingPL.new(mlzigzag, props)
  129. drawing.drawplain()
  130. drawingArray.push(drawing)
  131. if(highlightLevel)
  132. alertPivotPoint = mlzigzag.zigzagPivots.get(alertPivot)
  133. if(lastPivotBar < alertPivotPoint.point.index)
  134. lastPivotBar := alertPivotPoint.point.index
  135. keys = array.from('{alertType}', '{level}', '{pivot}')
  136. values = array.from(alertType, str.tostring(highlight), alertPivotPoint.tostring())
  137. currentAlert = ra.updateAlertTemplate(alertTemplate, keys, values)
  138. alert(currentAlert, alert.freq_once_per_bar_close)
  139. table.cell(legend, 0, rowNum, 'Level'+str.tostring(rowNum), text_color=lineColor,
  140. bgcolor=color.new(labelColor, highlightLevel? 90 : 50),
  141. text_size = highlightLevel?size.normal : size.small)
  142. mlzigzag := mlzigzag.nextlevel()
  143. rowNum+=1
  144.  
  145.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement