// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © rhapsodyz
//@version=5
indicator("TD Sequential 9/13", shorttitle="TD", overlay=true)
// Function to calculate TD Setup
tdSetup() =>
var int setupCount = 0
var int setupDirection = 0
if close > close[4]
if setupDirection != 1
setupCount := 1
setupDirection := 1
else
setupCount += 1
else if close < close[4]
if setupDirection != -1
setupCount := -1
setupDirection := -1
else
setupCount -= 1
else
setupCount := 0
[setupCount, setupDirection]
// Function to calculate TD Countdown
tdCountdown(setupCount, setupDirection) =>
var int countdown = 0
if math.abs(setupCount) == 9
for i = 1 to 13
if setupDirection > 0 and low < low[2]
countdown += 1
else if setupDirection < 0 and high > high[2]
countdown += 1
if countdown >= 13
break
countdown
// Calculating TD Setup and TD Countdown
[setup, direction] = tdSetup()
countdown = tdCountdown(setup, direction)
// Plotting
plotshape(series=countdown == 9 and direction > 0, location=location.abovebar, color=color.red, style=shape.labelup, text="9D")
plotshape(series=countdown == 9 and direction < 0, location=location.belowbar, color=color.green, style=shape.labeldown, text="9U")
plotshape(series=countdown == 13 and direction > 0, location=location.abovebar, color=color.purple, style=shape.labelup, text="13D")
plotshape(series=countdown == 13 and direction < 0, location=location.belowbar, color=color.blue, style=shape.labelup, text="13U")