Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=3
- study("TD SetUp & CountDown", overlay = true, max_bars_back = 1000, precision = 0)
- //############################################################################################################################### Declarations ##########################################################################################################
- setup_count = 0
- countdown = 0
- countdown_start_up = false
- countdown_start_down = false
- countdown_start = false
- count_direction = na
- countdowns_completed = 0 // Since direction change
- countable_setups = 0 // Since direction change
- direction_change_up = false
- direction_change_down = false
- direction_change = false
- eight = false
- //############################################################################################################################### END Declarations ########################################################################################################
- //############################################################################################################################### Setup Functions #########################################################################################################
- increment_setup(num) =>
- // Restart count if setup was completeled prev candle
- return = iff(nz(num) == 9, 1, nz(num) + 1)
- perfect_setup_up() => return = max(high, high[1]) > max(high[2], high[3])
- perfect_setup_down() => return = min(low, low[1]) < min(low[2], low[3])
- perfect_setup(up) => return = iff(up, perfect_setup_up(), perfect_setup_down())
- //############################################################################################################################### END Setup Functions #####################################################################################################
- //############################################################################################################################### Setup Logic #############################################################################################################
- setup_direction_up = close > close[4]
- setup_direction_down = close < close[4] // Also needed since setup isn't valid if close == close[4]
- setup_color = setup_direction_up ? green: red
- if (setup_direction_up)
- setup_count := iff(nz(setup_direction_up[1]), increment_setup(setup_count[1]), 1)
- if (setup_direction_down)
- setup_count := iff(nz(setup_direction_down[1]), increment_setup(setup_count[1]), 1)
- setup_complete = setup_count == 9
- is_perfect = perfect_setup(setup_direction_up)
- //############################################################################################################################### END Setup Logic #########################################################################################################
- //############################################################################################################################### Setup Plot ##############################################################################################################
- plotchar(setup_count == 1, char = '1', color = setup_color, show_last = 13)
- plotchar(setup_count == 2, char = '2', color = setup_color, show_last = 13)
- plotchar(setup_count == 3, char = '3', color = setup_color, show_last = 13)
- plotchar(setup_count == 4, char = '4', color = setup_color, show_last = 13)
- plotchar(setup_count == 5, char = '5', color = setup_color, show_last = 13)
- plotchar(setup_count == 6, char = '6', color = setup_color, show_last = 13)
- plotchar(setup_count == 7, char = '7', color = setup_color, show_last = 13)
- plotchar(setup_count == 8, char = '8', color = setup_color, show_last = 13)
- 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)))
- 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)))
- //############################################################################################################################### END Setup Plot ##########################################################################################################
- //############################################################################################################################### Countdown Functions #####################################################################################################
- periods_since_direction_change(direction_change) =>
- periods = 0
- for i = 0 to min(500, max(n - 1, 0))
- if (nz(direction_change[i]))
- periods := i
- break
- return = periods
- // Returns number of "counts" since start
- recount(start, count) =>
- num_counts = 0
- for i = 0 to start
- if (nz(count[max(i, 0)]))
- num_counts := num_counts + 1
- return = num_counts
- // Start at index of direction change, and determine the earliest index of a countdown start where the belonging countdown hasn't already been completed
- determine_countdown_start(direction_change, countdowns_completed, count, countdown_start) =>
- start_index = periods_since_direction_change(direction_change)
- skip = countdowns_completed - 1 // TODO: Simplify if possible (depending on how Pine script scopes work)
- start = 0
- for i = (start_index - 1) to 0
- if(countdown_start[max(i, 0)])
- if (skip > 0)
- skip := skip -1
- continue
- start := i
- break
- return = recount(start, count)
- increment_count(countdown) =>
- return = iff(nz(countdown[1]) == 13, 0, nz(countdown[1]) + 1)
- periods_since_eight(eight_series) =>
- periods = 1
- for i = 1 to (n - 1)
- if (nz(eight_series[max(i, 1)]))
- periods := i
- break
- return = max(periods, 1)
- perfect_countdown_up(eight_series) => return = high >= close[periods_since_eight(eight_series)]
- perfect_countdown_down(eight_series) => return = low <= close[periods_since_eight(eight_series)]
- perfect_countdown(up, eight_series) => return = iff(up, perfect_countdown_up(eight_series), perfect_countdown_down(eight_series))
- //############################################################################################################################### END Countdown Functions #################################################################################################
- //############################################################################################################################### Countdown Logic #########################################################################################################
- countdown_start_up := setup_complete and setup_direction_up
- countdown_start_down := setup_complete and setup_direction_down
- countdown_start := countdown_start_up or countdown_start_down
- count_direction := iff(countdown_start_up,
- true,
- iff(countdown_start_down, false, count_direction[1]))
- direction_change_up := count_direction and not count_direction[1]
- direction_change_down := count_direction[1] and not count_direction
- direction_change := direction_change_up or direction_change_down
- countable_setups := iff(direction_change, 1, iff(countdown_start, nz(countable_setups[1]) + 1, nz(countable_setups[1])))
- countdown_color = iff(count_direction, green, red)
- higher = close >= high[2]
- lower = close < low[2]
- increment_countdown = (count_direction and higher) or (lower and not count_direction)
- countdown_complete = increment_countdown and nz(countdown[1]) == 12
- // Incremented for every countdown that completes in the same direction. Reset to 0 if there is a direction change
- countdowns_completed := iff(direction_change, 0, iff(countdown_complete, nz(countdowns_completed[1]) + 1, nz(countdowns_completed[1])))
- // If there's a direction change: set countdown to 1 or 0 depending on whether current tick should be counted.
- // If there isn't a direction change: determine_countdown_start() if current countdown is complete, otherwise +=1
- prev_countdown_complete = nz(countdown[1]) == 13 or nz(countdown[1]) == 0
- prev_countdown = nz(countdown[1])
- start_queued_countdown = prev_countdown_complete and countdowns_completed < countable_setups
- increment_by_one = increment_countdown and prev_countdown >= 1 and not prev_countdown_complete
- countdown := iff(direction_change,
- iff(increment_countdown, 1, 0),
- iff(increment_countdown,
- iff(prev_countdown_complete,
- iff(countable_setups > countdowns_completed,
- determine_countdown_start(direction_change, countdowns_completed, increment_countdown, countdown_start),
- 0),
- prev_countdown + 1),
- prev_countdown))
- eight := countdown == 8 and not nz(eight[1]) // True only when countdown reaches eight. Used to determine whether or not countdowns are "perfect".
- perfect = perfect_countdown(count_direction, eight)
- //############################################################################################################################### END Countdown Logic #####################################################################################################
- //############################################################################################################################### Countdown Plot ##########################################################################################################
- // Weird unicode 9's used because the regular ones are almost invisible. char= can't be used since some numbers are >1 char
- plotchar(countdown == 1 and increment_countdown, char='', color = countdown_color, text = '๐ญ', textcolor = countdown_color, location = location.belowbar, show_last = 13)
- plotchar(countdown == 2 and increment_countdown, char='', color = countdown_color, text = '๐ฎ', textcolor = countdown_color, location = location.belowbar, show_last = 13)
- plotchar(countdown == 3 and increment_countdown, char='', color = countdown_color, text = '๐ฏ', textcolor = countdown_color, location = location.belowbar, show_last = 13)
- plotchar(countdown == 4 and increment_countdown, char='', color = countdown_color, text = '๐ฐ', textcolor = countdown_color, location = location.belowbar, show_last = 13)
- plotchar(countdown == 5 and increment_countdown, char='', color = countdown_color, text = '๐ฑ', textcolor = countdown_color, location = location.belowbar, show_last = 13)
- plotchar(countdown == 6 and increment_countdown, char='', color = countdown_color, text = '๐ฒ', textcolor = countdown_color, location = location.belowbar, show_last = 13)
- plotchar(countdown == 7 and increment_countdown, char='', color = countdown_color, text = '๐ณ', textcolor = countdown_color, location = location.belowbar, show_last = 13)
- plotchar(countdown == 8 and increment_countdown, char='', color = countdown_color, text = '๐ด', textcolor = countdown_color, location = location.belowbar, show_last = 13)
- plotchar(countdown == 9 and increment_countdown, char='', color = countdown_color, text = '๐ต', textcolor = countdown_color, location = location.belowbar, show_last = 13)
- plotchar(countdown == 10 and increment_countdown, char='', color = countdown_color, text = '๐ญ๐ฌ', textcolor = countdown_color, location = location.belowbar, show_last = 13)
- plotchar(countdown == 11 and increment_countdown, char='', color = countdown_color, text = '๐ญ๐ญ', textcolor = countdown_color, location = location.belowbar, show_last = 13)
- plotchar(countdown == 12 and increment_countdown, char='', color = countdown_color, text = '๐ญ๐ฎ', textcolor = countdown_color, location = location.belowbar, show_last = 13)
- 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)
- 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)
- //############################################################################################################################### END Countdown Plot ######################################################################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement