//@version=5
indicator('CISD After Sweeps - Last Signal Only [Modified]', shorttitle='CISD Sweep
Last', max_labels_count=500, max_lines_count=500, overlay=true)
// Settings
space = ' '
length = [Link]( 15, 'Swing Length' , minval=1
)
Minimum_Sequence_Length = [Link]( 0, 'Minimum CISD Duration' , minval=0
)
Maximum_Sequence_Length = [Link](100, 'Maximum Swing Validity' , minval=1,
maxval=1000
, tooltip = 'Maximum allowed swing level duration
without a new detected sweep' )
show_last_only = [Link](true, 'Show Only Last CISD Signal',
tooltip='When enabled, only the most recent bullish or bearish CISD will be
displayed')
textSize = [Link]([Link]( 'Tiny', 'Label/Text Size' , group='Style',
options =['Tiny', 'Small']))
cBull = [Link]([Link] , 'Bullish' ,
group='Style' )
cBear = [Link]([Link] , 'Bearish' , group='Style'
)
cSweepH = [Link]([Link], 'Sweeps' + space,
group='Style', inline="S" )
cSweepL = [Link]([Link], '' , group='Style',
inline="S", tooltip='Sweep High/Low')
//Defined Types
type bin
line ln
bool active
[Link] cp1
[Link] cp2
bool broken = false
type swing
[Link] cp
line ln
line wick
bool active = false
type cisd_signal
line ln
label lbl
label arrow
bool is_bullish
type sweep_signal
line ln
line wick
//Constants and general variables
INV = color(na)
n = bar_index
//Variables
bull = close > open
bear = close < open
var int trend = 0
var array<bin> arrBull = [Link]<bin>()
var array<bin> arrBear = [Link]<bin>()
var array<swing>swingsH = [Link]<swing>()
var array<swing>swingsL = [Link]<swing>()
var [Link] cp_lastPh = [Link].from_index(n , high)
var [Link] cp_lastPl = [Link].from_index(n , low )
var [Link] trackPriceBull = [Link].from_index(na, na )
var [Link] trackPriceBear = [Link].from_index(na, na )
// Variables for tracking last CISD signals
var cisd_signal last_bullish_cisd = na
var cisd_signal last_bearish_cisd = na
// Variables for tracking last sweep signals
var sweep_signal last_bullish_sweep = na // Last low sweep (enables bullish CISD)
var sweep_signal last_bearish_sweep = na // Last high sweep (enables bearish CISD)
// Variables for tracking sweep conditions
var bool high_swept = false // Track if a high has been swept (required for
bearish CISD)
var bool low_swept = false // Track if a low has been swept (required for bullish
CISD)
// Function to clear old CISD signal
clear_cisd_signal(signal) =>
if not na(signal)
if not na([Link])
[Link]()
if not na([Link])
[Link]()
if not na([Link])
[Link]()
// Function to clear old sweep signal
clear_sweep_signal(signal) =>
if not na(signal)
if not na([Link])
[Link]()
if not na([Link])
[Link]()
//Execution
ph = [Link](length, 1)
pl = [Link] (length, 1)
if not na(ph)
[Link]([Link]([Link].from_index(n-1, ph)))
cp_lastPh := [Link].from_index(n-1, ph)
if not na(pl)
[Link]([Link]([Link].from_index(n-1, pl)))
cp_lastPl := [Link].from_index(n-1, pl)
if bull and bear[1]
trackPriceBull := [Link].from_index(n, open)
if bear and bull[1]
trackPriceBear := [Link].from_index(n, open)
//Reset when last Swing is broken
if close > cp_lastPh.price
cp_lastPh := [Link].from_index(na, na)
if close < cp_lastPl.price
cp_lastPl := [Link].from_index(na, na)
//Sweep Detection and CISD Logic
//#region
if [Link]() > 0
max_bars_back(close, 1000)
for i = [Link]() -1 to 0
get = [Link](i)
x = [Link]
y = [Link]
//Too far back
if n - x > Maximum_Sequence_Length
[Link](i)
else
//if line swing available
if not na([Link])
//if broken
if close > [Link].get_y2()
[Link](i)
else
if close > y
[Link](i)
else
//Show swing/sweeps
//First time sweep of line
if not [Link]
if high > [Link] and close < [Link]
// Clear previous bearish sweep if showing last only
if show_last_only
clear_sweep_signal(last_bearish_sweep)
sweep_line = [Link](x, [Link], n, [Link],
color=cSweepH)
sweep_wick = [Link](n, [Link], n, high,
color=cSweepH, width=3)
[Link] := sweep_line
[Link] := sweep_wick
[Link] := true
// Store as last bearish sweep
if show_last_only
last_bearish_sweep := sweep_signal.new(sweep_line,
sweep_wick)
// Mark that a high has been swept (enables bearish
CISD)
high_swept := true
//check if not broken already
good = true
for j = 0 to n - [Link]
if close[j] < [Link]
good := false
break
if good
[Link]([Link](
[Link]([Link],
[Link], n, [Link], color=[Link](cBull, 40))
, true
, [Link].from_index(cp_lastPh.index,
cp_lastPh.price)
, [Link].from_index(n, cp_lastPh.price)
)
)
if [Link]() > 0
max_bars_back(close, 1000)
for i = [Link]() -1 to 0
get = [Link](i)
x = [Link]
y = [Link]
//Too far back
if n - x > Maximum_Sequence_Length
[Link](i)
else
//if line swing available
if not na([Link])
//if broken
if close < [Link].get_y2()
[Link](i)
else
if close < y
[Link](i)
else
//Show swing/sweeps
if not [Link]
if low < [Link] and close > [Link]
// Clear previous bullish sweep if showing last only
if show_last_only
clear_sweep_signal(last_bullish_sweep)
sweep_line = [Link](x, [Link], n, [Link],
color=cSweepL)
sweep_wick = [Link](n, [Link], n, low ,
color=cSweepL, width=3)
[Link] := sweep_line
[Link] := sweep_wick
[Link] := true
// Store as last bullish sweep
if show_last_only
last_bullish_sweep := sweep_signal.new(sweep_line,
sweep_wick)
// Mark that a low has been swept (enables bullish
CISD)
low_swept := true
//check if not broken already
good = true
for j = 0 to n - [Link]
if close[j] > [Link]
good := false
break
if good
[Link]([Link](
[Link]([Link],
[Link], n, [Link], color=[Link](cBear, 40))
, true
, [Link].from_index(cp_lastPl.index,
cp_lastPl.price)
, [Link].from_index(n, cp_lastPl.price)
)
)
// Process Bullish CISD (only after low sweep)
if [Link]() > 0
for i = [Link]() -1 to 0
get = [Link](i)
[Link].set_x2(n)
x1 = [Link].get_x1()
if n - x1 > Maximum_Sequence_Length
[Link]()
[Link](i)
else
if close < [Link].get_y2()
if n - x1 >= Minimum_Sequence_Length
// Only create bearish CISD if a high has been swept first
if high_swept
// Clear previous bearish CISD if showing last only
if show_last_only
clear_cisd_signal(last_bearish_cisd)
[Link].set_color(cBear)
if trend ==-1
[Link].set_style(line.style_dashed)
trend := -1
x = [Link]([Link](x1, n))
// Create new bearish CISD signal
cisd_lbl = [Link](x, [Link].get_y2()
, style= label.style_label_up
, textcolor=cBear, text='CISD'
, size=textSize
, color=INV
)
cisd_arrow = [Link](n, high
, style=label.style_label_down
, textcolor=cBear, text='▼'
, size=textSize
, color=INV
)
// Store as last bearish CISD
if show_last_only
last_bearish_cisd := cisd_signal.new([Link], cisd_lbl,
cisd_arrow, false)
// Reset high_swept flag after creating bearish CISD
high_swept := false
else
// If no high sweep occurred, just delete the line
[Link]()
else
[Link]()
[Link](i)
// Process Bearish CISD (only after high sweep)
if [Link]() > 0
for i = [Link]() -1 to 0
get = [Link](i)
[Link].set_x2(n)
x1 = [Link].get_x1()
if n - x1 > Maximum_Sequence_Length
[Link]()
[Link](i)
else
if close > [Link].get_y2()
if n - x1 >= Minimum_Sequence_Length
// Only create bullish CISD if a low has been swept first
if low_swept
// Clear previous bullish CISD if showing last only
if show_last_only
clear_cisd_signal(last_bullish_cisd)
[Link].set_color(cBull)
if trend == 1
[Link].set_style(line.style_dashed)
trend := 1
x = [Link]([Link](x1, n))
// Create new bullish CISD signal
cisd_lbl = [Link](x, [Link].get_y2()
, style= label.style_label_down
, textcolor=cBull, text='CISD'
, size=textSize
, color=INV
)
cisd_arrow = [Link](n, low
, style=label.style_label_up
, textcolor=cBull, text='▲'
, size=textSize
, color=INV
)
// Store as last bullish CISD
if show_last_only
last_bullish_cisd := cisd_signal.new([Link], cisd_lbl,
cisd_arrow, true)
// Reset low_swept flag after creating bullish CISD
low_swept := false
else
// If no low sweep occurred, just delete the line
[Link]()
else
[Link]()
[Link](i)