Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.35 KB | None | 0 0
  1. //@version=3
  2. study("TD SetUp & CountDown", overlay = true, max_bars_back = 1000, precision = 0)
  3.  
  4. //############################################################################################################################### Declarations ##########################################################################################################
  5. setup_count = 0
  6. countdown = 0
  7. countdown_start_up = false
  8. countdown_start_down = false
  9. countdown_start = false
  10. count_direction = na
  11. countdowns_completed = 0 // Since direction change
  12. countable_setups = 0 // Since direction change
  13. direction_change_up = false
  14. direction_change_down = false
  15. direction_change = false
  16. eight = false
  17. //############################################################################################################################### END Declarations ########################################################################################################
  18. //############################################################################################################################### Setup Functions #########################################################################################################
  19. increment_setup(num) =>
  20. // Restart count if setup was completeled prev candle
  21. return = iff(nz(num) == 9, 1, nz(num) + 1)
  22.  
  23. perfect_setup_up() => return = max(high, high[1]) > max(high[2], high[3])
  24. perfect_setup_down() => return = min(low, low[1]) < min(low[2], low[3])
  25. perfect_setup(up) => return = iff(up, perfect_setup_up(), perfect_setup_down())
  26. //############################################################################################################################### END Setup Functions #####################################################################################################
  27. //############################################################################################################################### Setup Logic #############################################################################################################
  28. setup_direction_up = close > close[4]
  29. setup_direction_down = close < close[4] // Also needed since setup isn't valid if close == close[4]
  30. setup_color = setup_direction_up ? green: red
  31.  
  32. if (setup_direction_up)
  33. setup_count := iff(nz(setup_direction_up[1]), increment_setup(setup_count[1]), 1)
  34. if (setup_direction_down)
  35. setup_count := iff(nz(setup_direction_down[1]), increment_setup(setup_count[1]), 1)
  36.  
  37. setup_complete = setup_count == 9
  38. is_perfect = perfect_setup(setup_direction_up)
  39. //############################################################################################################################### END Setup Logic #########################################################################################################
  40. //############################################################################################################################### Setup Plot ##############################################################################################################
  41. plotchar(setup_count == 1, char = '1', color = setup_color, show_last = 13)
  42. plotchar(setup_count == 2, char = '2', color = setup_color, show_last = 13)
  43. plotchar(setup_count == 3, char = '3', color = setup_color, show_last = 13)
  44. plotchar(setup_count == 4, char = '4', color = setup_color, show_last = 13)
  45. plotchar(setup_count == 5, char = '5', color = setup_color, show_last = 13)
  46. plotchar(setup_count == 6, char = '6', color = setup_color, show_last = 13)
  47. plotchar(setup_count == 7, char = '7', color = setup_color, show_last = 13)
  48. plotchar(setup_count == 8, char = '8', color = setup_color, show_last = 13)
  49. plotshape(setup_count == 9 and setup_direction_up, text = '๐Ÿ—', textcolor = green, location = location.abovebar, style = shape.arrowdown, color = iff(is_perfect, color(#b30000, 0), color(orange, 0)))
  50. plotshape(setup_count == 9 and setup_direction_down, text = '๐Ÿ—', textcolor = red, location = location.abovebar, style = shape.arrowup, color = iff(is_perfect, color(#006400, 0), color(lime, 0)))
  51. //############################################################################################################################### END Setup Plot ##########################################################################################################
  52. //############################################################################################################################### Countdown Functions #####################################################################################################
  53. periods_since_direction_change(direction_change) =>
  54. periods = 0
  55. for i = 0 to min(500, max(n - 1, 0))
  56. if (nz(direction_change[i]))
  57. periods := i
  58. break
  59. return = periods
  60.  
  61. // Returns number of "counts" since start
  62. recount(start, count) =>
  63. num_counts = 0
  64. for i = 0 to start
  65. if (nz(count[max(i, 0)]))
  66. num_counts := num_counts + 1
  67.  
  68. return = num_counts
  69.  
  70. // Start at index of direction change, and determine the earliest index of a countdown start where the belonging countdown hasn't already been completed
  71. determine_countdown_start(direction_change, countdowns_completed, count, countdown_start) =>
  72. start_index = periods_since_direction_change(direction_change)
  73. skip = countdowns_completed - 1 // TODO: Simplify if possible (depending on how Pine script scopes work)
  74. start = 0
  75.  
  76. for i = (start_index - 1) to 0
  77. if(countdown_start[max(i, 0)])
  78. if (skip > 0)
  79. skip := skip -1
  80. continue
  81. start := i
  82. break
  83.  
  84. return = recount(start, count)
  85.  
  86. increment_count(countdown) =>
  87. return = iff(nz(countdown[1]) == 13, 0, nz(countdown[1]) + 1)
  88.  
  89. periods_since_eight(eight_series) =>
  90. periods = 1
  91. for i = 1 to (n - 1)
  92. if (nz(eight_series[max(i, 1)]))
  93. periods := i
  94. break
  95. return = max(periods, 1)
  96.  
  97. perfect_countdown_up(eight_series) => return = high >= close[periods_since_eight(eight_series)]
  98. perfect_countdown_down(eight_series) => return = low <= close[periods_since_eight(eight_series)]
  99. perfect_countdown(up, eight_series) => return = iff(up, perfect_countdown_up(eight_series), perfect_countdown_down(eight_series))
  100. //############################################################################################################################### END Countdown Functions #################################################################################################
  101. //############################################################################################################################### Countdown Logic #########################################################################################################
  102. countdown_start_up := setup_complete and setup_direction_up
  103. countdown_start_down := setup_complete and setup_direction_down
  104. countdown_start := countdown_start_up or countdown_start_down
  105.  
  106. count_direction := iff(countdown_start_up,
  107. true,
  108. iff(countdown_start_down, false, count_direction[1]))
  109. direction_change_up := count_direction and not count_direction[1]
  110. direction_change_down := count_direction[1] and not count_direction
  111. direction_change := direction_change_up or direction_change_down
  112.  
  113. countable_setups := iff(direction_change, 1, iff(countdown_start, nz(countable_setups[1]) + 1, nz(countable_setups[1])))
  114. countdown_color = iff(count_direction, green, red)
  115. higher = close >= high[2]
  116. lower = close < low[2]
  117. increment_countdown = (count_direction and higher) or (lower and not count_direction)
  118. countdown_complete = increment_countdown and nz(countdown[1]) == 12
  119. // Incremented for every countdown that completes in the same direction. Reset to 0 if there is a direction change
  120. countdowns_completed := iff(direction_change, 0, iff(countdown_complete, nz(countdowns_completed[1]) + 1, nz(countdowns_completed[1])))
  121.  
  122. // If there's a direction change: set countdown to 1 or 0 depending on whether current tick should be counted.
  123. // If there isn't a direction change: determine_countdown_start() if current countdown is complete, otherwise +=1
  124. prev_countdown_complete = nz(countdown[1]) == 13 or nz(countdown[1]) == 0
  125. prev_countdown = nz(countdown[1])
  126. start_queued_countdown = prev_countdown_complete and countdowns_completed < countable_setups
  127. increment_by_one = increment_countdown and prev_countdown >= 1 and not prev_countdown_complete
  128.  
  129. countdown := iff(direction_change,
  130. iff(increment_countdown, 1, 0),
  131. iff(increment_countdown,
  132. iff(prev_countdown_complete,
  133. iff(countable_setups > countdowns_completed,
  134. determine_countdown_start(direction_change, countdowns_completed, increment_countdown, countdown_start),
  135. 0),
  136. prev_countdown + 1),
  137. prev_countdown))
  138.  
  139. eight := countdown == 8 and not nz(eight[1]) // True only when countdown reaches eight. Used to determine whether or not countdowns are "perfect".
  140. perfect = perfect_countdown(count_direction, eight)
  141. //############################################################################################################################### END Countdown Logic #####################################################################################################
  142. //############################################################################################################################### Countdown Plot ##########################################################################################################
  143. // Weird unicode 9's used because the regular ones are almost invisible. char= can't be used since some numbers are >1 char
  144. plotchar(countdown == 1 and increment_countdown, char='', color = countdown_color, text = '๐Ÿญ', textcolor = countdown_color, location = location.belowbar, show_last = 13)
  145. plotchar(countdown == 2 and increment_countdown, char='', color = countdown_color, text = '๐Ÿฎ', textcolor = countdown_color, location = location.belowbar, show_last = 13)
  146. plotchar(countdown == 3 and increment_countdown, char='', color = countdown_color, text = '๐Ÿฏ', textcolor = countdown_color, location = location.belowbar, show_last = 13)
  147. plotchar(countdown == 4 and increment_countdown, char='', color = countdown_color, text = '๐Ÿฐ', textcolor = countdown_color, location = location.belowbar, show_last = 13)
  148. plotchar(countdown == 5 and increment_countdown, char='', color = countdown_color, text = '๐Ÿฑ', textcolor = countdown_color, location = location.belowbar, show_last = 13)
  149. plotchar(countdown == 6 and increment_countdown, char='', color = countdown_color, text = '๐Ÿฒ', textcolor = countdown_color, location = location.belowbar, show_last = 13)
  150. plotchar(countdown == 7 and increment_countdown, char='', color = countdown_color, text = '๐Ÿณ', textcolor = countdown_color, location = location.belowbar, show_last = 13)
  151. plotchar(countdown == 8 and increment_countdown, char='', color = countdown_color, text = '๐Ÿด', textcolor = countdown_color, location = location.belowbar, show_last = 13)
  152. plotchar(countdown == 9 and increment_countdown, char='', color = countdown_color, text = '๐Ÿต', textcolor = countdown_color, location = location.belowbar, show_last = 13)
  153. plotchar(countdown == 10 and increment_countdown, char='', color = countdown_color, text = '๐Ÿญ๐Ÿฌ', textcolor = countdown_color, location = location.belowbar, show_last = 13)
  154. plotchar(countdown == 11 and increment_countdown, char='', color = countdown_color, text = '๐Ÿญ๐Ÿญ', textcolor = countdown_color, location = location.belowbar, show_last = 13)
  155. plotchar(countdown == 12 and increment_countdown, char='', color = countdown_color, text = '๐Ÿญ๐Ÿฎ', textcolor = countdown_color, location = location.belowbar, show_last = 13)
  156. plotshape(countdown == 13 and increment_countdown and count_direction, style = shape.arrowdown, color = iff(perfect, color(#b30000, 0), color(orange, 0)), text = '๐Ÿ๐Ÿ‘', textcolor = green, location = location.belowbar)
  157. plotshape(countdown == 13 and increment_countdown and not count_direction, style = shape.arrowup, color = iff(perfect, color(#006400, 0), color(lime, 0)), text = '๐Ÿ๐Ÿ‘', textcolor = red, location = location.belowbar)
  158. //############################################################################################################################### END Countdown Plot ######################################################################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement