//@version=5
indicator("[TNR][PCR] Signals & Overlays [4.1]", overlay=true,
max_labels_count=500)
// Get user settings
showBuySell = input(true, "Show Buy & Sell", group="BUY & SELL SIGNALS")
sensitivity = input.float(1.3, "Sensitivity (1-6)", 1, 6, group="BUY & SELL
SIGNALS")
percentStop = input.float(0, "Stop Loss % (0 to Disable)", 0, group="BUY &
SELL SIGNALS")
offsetSignal = input.float(5, "Signals Offset", 0, group="BUY & SELL SIGNALS")
showDashboard = input(true, "Show Dashboard", group="TREND DASHBOARD")
locationDashboard = input.string("Top Right", "Table Location", ["Top Right",
"Middle Right", "Bottom Right", "Top Center", "Middle Center", "Bottom Center",
"Top Left", "Middle Left", "Bottom Left"], group="TREND DASHBOARD")
tableTextColor = input(color.white, "Table Text Color", group="TREND DASHBOARD")
tableBgColor = input(#000000, "Table Background Color", group="TREND
DASHBOARD")
sizeDashboard = input.string("Small", "Table Size", ["Large", "Normal",
"Small", "Tiny"], group="TREND DASHBOARD")
showRevBands = input.bool(true, "Show Reversal Bands", group="REVERSAL BANDS")
lenRevBands = input.int(30, "Length", group="REVERSAL BANDS")
// Functions
smoothrng(x, t, m) =>
wper = t * 2 - 1
avrng = ta.ema(math.abs(x - x[1]), t)
smoothrng = ta.ema(avrng, wper) * m
rngfilt(x, r) =>
rngfilt = x
rngfilt := x > nz(rngfilt[1]) ? x - r < nz(rngfilt[1]) ? nz(rngfilt[1]) : x - r
: x + r > nz(rngfilt[1]) ? nz(rngfilt[1]) : x + r
percWidth(len, perc) => (ta.highest(len) - ta.lowest(len)) * perc / 100
securityNoRep(sym, res, src) => request.security(sym, res, src, barmerge.gaps_off,
barmerge.lookahead_on)
swingPoints(prd) =>
pivHi = ta.pivothigh(prd, prd)
pivLo = ta.pivotlow (prd, prd)
last_pivHi = ta.valuewhen(pivHi, pivHi, 1)
last_pivLo = ta.valuewhen(pivLo, pivLo, 1)
hh = pivHi and pivHi > last_pivHi ? pivHi : na
lh = pivHi and pivHi < last_pivHi ? pivHi : na
hl = pivLo and pivLo > last_pivLo ? pivLo : na
ll = pivLo and pivLo < last_pivLo ? pivLo : na
[hh, lh, hl, ll]
f_chartTfInMinutes() =>
float _resInMinutes = timeframe.multiplier * (
timeframe.isseconds ? 1 :
timeframe.isminutes ? 1. :
timeframe.isdaily ? 60. * 24 :
timeframe.isweekly ? 60. * 24 * 7 :
timeframe.ismonthly ? 60. * 24 * 30.4375 : na)
f_kc(src, len, sensitivity) =>
basis = ta.sma(src, len)
span = ta.atr(len)
[basis + span * sensitivity, basis - span * sensitivity]
wavetrend(src, chlLen, avgLen) =>
esa = ta.ema(src, chlLen)
d = ta.ema(math.abs(src - esa), chlLen)
ci = (src - esa) / (0.015 * d)
wt1 = ta.ema(ci, avgLen)
wt2 = ta.sma(wt1, 3)
[wt1, wt2]
f_top_fractal(src) => src[4] < src[2] and src[3] < src[2] and src[2] > src[1] and
src[2] > src[0]
f_bot_fractal(src) => src[4] > src[2] and src[3] > src[2] and src[2] < src[1] and
src[2] < src[0]
f_fractalize (src) => f_top_fractal(src) ? 1 : f_bot_fractal(src) ? -1 : 0
f_findDivs(src, topLimit, botLimit) =>
fractalTop = f_fractalize(src) > 0 and src[2] >= topLimit ? src[2] : na
fractalBot = f_fractalize(src) < 0 and src[2] <= botLimit ? src[2] : na
highPrev = ta.valuewhen(fractalTop, src[2], 0)[2]
highPrice = ta.valuewhen(fractalTop, high[2], 0)[2]
lowPrev = ta.valuewhen(fractalBot, src[2], 0)[2]
lowPrice = ta.valuewhen(fractalBot, low[2], 0)[2]
bearSignal = fractalTop and high[2] > highPrice and src[2] < highPrev
bullSignal = fractalBot and low[2] < lowPrice and src[2] > lowPrev
[bearSignal, bullSignal]
// Get components
source = close
smrng1 = smoothrng(source, 27, 1.5)
smrng2 = smoothrng(source, 55, sensitivity)
smrng = (smrng1 + smrng2) / 2
filt = rngfilt(source, smrng)
up = 0.0, up := filt > filt[1] ? nz(up[1]) + 1 : filt < filt[1] ? 0 :
nz(up[1])
dn = 0.0, dn := filt < filt[1] ? nz(dn[1]) + 1 : filt > filt[1] ? 0 :
nz(dn[1])
bullCond = bool(na), bullCond := source > filt and source > source[1] and up > 0
or source > filt and source < source[1] and up > 0
bearCond = bool(na), bearCond := source < filt and source < source[1] and dn > 0
or source < filt and source > source[1] and dn > 0
lastCond = 0, lastCond := bullCond ? 1 : bearCond ? -1 : lastCond[1]
bull = bullCond and lastCond[1] == -1
bear = bearCond and lastCond[1] == 1
countBull = ta.barssince(bull)
countBear = ta.barssince(bear)
trigger = nz(countBull, bar_index) < nz(countBear, bar_index) ? 1 : 0
rsi = ta.rsi(close, 21)
rsiOb = rsi > 70 and rsi > ta.ema(rsi, 10)
rsiOs = rsi < 30 and rsi < ta.ema(rsi, 10)
dHigh = securityNoRep(syminfo.tickerid, "D", high [1])
dLow = securityNoRep(syminfo.tickerid, "D", low [1])
dClose = securityNoRep(syminfo.tickerid, "D", close[1])
ema = ta.ema(close, 144)
emaBull = close > ema
equal_tf(res) => str.tonumber(res) == f_chartTfInMinutes() and not
timeframe.isseconds
higher_tf(res) => str.tonumber(res) > f_chartTfInMinutes() or timeframe.isseconds
too_small_tf(res) => (timeframe.isweekly and res=="1") or (timeframe.ismonthly and
str.tonumber(res) < 10)
securityNoRep1(sym, res, src) =>
bool bull_ = na
bull_ := equal_tf(res) ? src : bull_
bull_ := higher_tf(res) ? request.security(sym, res, src, barmerge.gaps_off,
barmerge.lookahead_on) : bull_
bull_array = request.security_lower_tf(syminfo.tickerid, higher_tf(res) ?
str.tostring(f_chartTfInMinutes()) + (timeframe.isseconds ? "S" : "") :
too_small_tf(res) ? (timeframe.isweekly ? "3" : "10") : res, src)
if array.size(bull_array) > 1 and not equal_tf(res) and not higher_tf(res)
bull_ := array.pop(bull_array)
array.clear(bull_array)
bull_
TF1Bull = securityNoRep1(syminfo.tickerid, "1" , emaBull)
TF3Bull = securityNoRep1(syminfo.tickerid, "3" , emaBull)
TF5Bull = securityNoRep1(syminfo.tickerid, "5" , emaBull)
TF15Bull = securityNoRep1(syminfo.tickerid, "15" , emaBull)
TF30Bull = securityNoRep1(syminfo.tickerid, "30" , emaBull)
TF60Bull = securityNoRep1(syminfo.tickerid, "60" , emaBull)
TF120Bull = securityNoRep1(syminfo.tickerid, "120" , emaBull)
TF240Bull = securityNoRep1(syminfo.tickerid, "240" , emaBull)
TF480Bull = securityNoRep1(syminfo.tickerid, "480" , emaBull)
TFDBull = securityNoRep1(syminfo.tickerid, "1440", emaBull)
[upperKC1, lowerKC1] = f_kc(close, lenRevBands, 3)
[upperKC2, lowerKC2] = f_kc(close, lenRevBands, 4)
[upperKC3, lowerKC3] = f_kc(close, lenRevBands, 5)
[upperKC4, lowerKC4] = f_kc(close, lenRevBands, 6)
[wt1, wt2] = wavetrend(hlc3, 9, 12)
[wtDivBear1, wtDivBull1] = f_findDivs(wt2, 15, -40)
[wtDivBear2, wtDivBull2] = f_findDivs(wt2, 45, -65)
wtDivBull = wtDivBull1 or wtDivBull2
wtDivBear = wtDivBear1 or wtDivBear2
// Colors
cyan = #089981, cyan30 = color.new(cyan, 70)
pink = #f23645, pink30 = color.new(pink, 70)
red = #f23645, red30 = color.new(red , 70)
// Plot
off = percWidth(300, offsetSignal)
if showBuySell and bull
b = label.new(bar_index, low-off, "△", color = color.green, size = size.tiny,
yloc = yloc.belowbar, style = label.style_triangleup)
label.delete(b[2])
if showBuySell and bear
b=label.new(bar_index, low-off, "△", color = color.red, size = size.tiny, yloc
= yloc.abovebar, style = label.style_triangledown)
label.delete(b[2])
if ta.crossover(wt1, wt2) and wt2 <= -53
b=label.new(bar_index, low-off, "△", color = color.green, size = size.tiny,
yloc = yloc.belowbar, style = label.style_circle)
label.delete(b[2])
if ta.crossunder(wt1, wt2) and wt2 >= 53
b=label.new(bar_index, low-off, "△", color = color.red, size = size.tiny, yloc
= yloc.abovebar, style = label.style_circle)
label.delete(b[2])
//s1=label.new(showBuySell and bull ? low - off : na, "Buy Label" , shape.circle ,
location.absolute, cyan, 0, "" , color.white, size=size.tiny)
//s2=label.new(showBuySell and bear ? high + off : na, "Sell Label", shape.circle,
location.absolute, pink, 0, "", color.white, size=size.tiny)
//s3=label.new(ta.crossover(wt1, wt2) and wt2 <= -53, "Mild Buy" ,
shape.triangleup, location.belowbar, cyan, size=size.tiny)
//s4=label.new(ta.crossunder(wt1, wt2) and wt2 >= 53, "Mild Sell",
shape.triangledown, location.abovebar, pink, size=size.tiny)
barcolor(up > dn ? cyan : pink)
srcStop = close
atrBand = srcStop * (percentStop / 100)
atrStop = trigger ? srcStop - atrBand : srcStop + atrBand
lastTrade(src) => ta.valuewhen(bull or bear, src, 0)
entry_y = lastTrade(srcStop)
stop_y = lastTrade(atrStop)
tp1_y = (entry_y - lastTrade(atrStop)) * 1 + entry_y
tp2_y = (entry_y - lastTrade(atrStop)) * 2 + entry_y
tp3_y = (entry_y - lastTrade(atrStop)) * 3 + entry_y
labelTpSl(y, txt, color) =>
label labelTpSl = percentStop != 0 ? label.new(bar_index + 1, y, txt,
xloc.bar_index, yloc.price, color, label.style_label_left, color.white,
size.normal) : na
label.delete(labelTpSl[1])
labelTpSl(entry_y, "Entry: " + str.tostring(math.round_to_mintick(entry_y)),
color.gray)
labelTpSl(stop_y , "Stop Loss: " + str.tostring(math.round_to_mintick(stop_y)),
color.new(#c2185b,0))
labelTpSl(tp1_y, "Take Profit 1: " + str.tostring(math.round_to_mintick(tp1_y)),
color.new(#03aeca,0))
labelTpSl(tp2_y, "Take Profit 2: " + str.tostring(math.round_to_mintick(tp2_y)),
color.new(#03aeca,0))
labelTpSl(tp3_y, "Take Profit 3: " + str.tostring(math.round_to_mintick(tp3_y)),
color.new(#03aeca,0))
lineTpSl(y, color) =>
line lineTpSl = percentStop != 0 ? line.new(bar_index - (trigger ? countBull :
countBear) + 4, y, bar_index + 1, y, xloc.bar_index, extend.none, color,
line.style_solid) : na
line.delete(lineTpSl[1])
lineTpSl(entry_y, color.gray)
lineTpSl(stop_y, color.new(#f23645,0))
lineTpSl(tp1_y, color.new(#089981,0))
lineTpSl(tp2_y, color.new(#089981,0))
lineTpSl(tp3_y, color.new(#089981,0))
var dashboard_loc = locationDashboard == "Top Right" ? position.top_right :
locationDashboard == "Middle Right" ? position.middle_right : locationDashboard ==
"Bottom Right" ? position.bottom_right : locationDashboard == "Top Center" ?
position.top_center : locationDashboard == "Middle Center" ? position.middle_center
: locationDashboard == "Bottom Center" ? position.bottom_center : locationDashboard
== "Top Left" ? position.top_left : locationDashboard == "Middle Left" ?
position.middle_left : position.bottom_left
var dashboard_size = sizeDashboard == "Large" ? size.large : sizeDashboard ==
"Normal" ? size.normal : sizeDashboard == "Small" ? size.small : size.tiny
var dashboard = showDashboard ? table.new(dashboard_loc, 2, 15, tableBgColor,
#000000, 2, tableBgColor, 1) : na
dashboard_cell(column, row, txt, signal=false) => table.cell(dashboard, column,
row, txt, 0, 0, signal ? #000000 : tableTextColor, text_size=dashboard_size)
dashboard_cell_bg(column, row, col) => table.cell_set_bgcolor(dashboard, column,
row, col)
if barstate.islast and showDashboard
dashboard_cell(0, 0 , "PunkAlgo")
dashboard_cell(0, 1 , "Current Position")
dashboard_cell(0, 2 , "Current Trend")
dashboard_cell(0, 3 , "Volume")
dashboard_cell(0, 4 , "Timeframe")
dashboard_cell(0, 7 , "5 min:")
dashboard_cell(0, 8 , "15 min:")
dashboard_cell(0, 9 , "30 min:")
dashboard_cell(0, 10, "1 H:")
dashboard_cell(0, 11, "2 H:")
dashboard_cell(0, 12, "4 H:")
dashboard_cell(0, 13, "8 H:")
dashboard_cell(0, 14, "Daily:")
dashboard_cell(1, 0 , "V.3")
dashboard_cell(1, 1 , trigger ? "Buy" : "Sell", true), dashboard_cell_bg(1, 1,
trigger ? color.new(cyan, 10): color.red)
dashboard_cell(1, 2 , emaBull ? "Bullish" : "Bearish", true),
dashboard_cell_bg(1, 2, emaBull ? color.new(cyan, 10) : color.red)
dashboard_cell(1, 3 , str.tostring(volume))
dashboard_cell(1, 4 , "Trends")
dashboard_cell(1, 7 , TF5Bull ? "Bullish" : "Bearish", true),
dashboard_cell_bg(1, 7 , TF5Bull ? color.new(cyan, 10) : color.red)
dashboard_cell(1, 8 , TF15Bull ? "Bullish" : "Bearish", true),
dashboard_cell_bg(1, 8 , TF15Bull ? color.new(cyan, 10) : color.red)
dashboard_cell(1, 9 , TF30Bull ? "Bullish" : "Bearish", true),
dashboard_cell_bg(1, 9 , TF30Bull ? color.new(cyan, 10): color.red)
dashboard_cell(1, 10, TF60Bull ? "Bullish" : "Bearish", true),
dashboard_cell_bg(1, 10, TF60Bull ? color.new(cyan, 10) : color.red)
dashboard_cell(1, 11, TF120Bull ? "Bullish" : "Bearish", true),
dashboard_cell_bg(1, 11, TF120Bull ? color.new(cyan, 10) : color.red)
dashboard_cell(1, 12, TF240Bull ? "Bullish" : "Bearish", true),
dashboard_cell_bg(1, 12, TF240Bull ? color.new(cyan, 10) : color.red)
dashboard_cell(1, 13, TF480Bull ? "Bullish" : "Bearish", true),
dashboard_cell_bg(1, 13, TF480Bull ? color.new(cyan, 10): color.red)
dashboard_cell(1, 14, TFDBull ? "Bullish" : "Bearish", true),
dashboard_cell_bg(1, 14, TFDBull ? color.new(cyan, 10) : color.red)
plot(showRevBands ? upperKC1 : na, "Rev.Zone Upper 1", red30)
plot(showRevBands ? upperKC2 : na, "Rev.Zone Upper 2", red30)
plot(showRevBands ? upperKC3 : na, "Rev.Zone Upper 3", red30)
plot(showRevBands ? upperKC4 : na, "Rev.Zone Upper 4", red30)
plot(showRevBands ? lowerKC4 : na, "Rev.Zone Lower 4", cyan30)
plot(showRevBands ? lowerKC3 : na, "Rev.Zone Lower 3", cyan30)
plot(showRevBands ? lowerKC2 : na, "Rev.Zone Lower 2", cyan30)
plot(showRevBands ? lowerKC1 : na, "Rev.Zone Lower 1", cyan30)
// Alerts
alert02 = bull
alert03 = wtDivBull
alert04 = wtDivBear
alert05 = bull or bear
alert06 = ta.crossover(wt1, wt2) and wt2 <= -53
alert07 = ta.crossunder(wt1, wt2) and wt2 >= 53
alert09 = rsiOb or rsiOs
alert10 = bear
alerts(sym) =>
if alert02 or alert03 or alert04 or alert06 or alert07 or alert10
alert_text = alert02 ? "Buy Signal PunkAlgo" : alert03 ? "Strong Buy Signal
PunkAlgo" : alert04 ? "Strong Sell Signal PunkAlgo" : alert06 ? "Mild Buy Signal
PunkAlgo" : alert07 ? "Mild Sell Signal PunkAlgo" : "Sell Signal PunkAlgo"
alert(alert_text, alert.freq_once_per_bar_close)
alerts(syminfo.tickerid)
alertcondition(alert02, "Buy Signal", "Buy Signal PunkAlgo")
alertcondition(alert05, "Either Buy or Sell Signal", "PunkAlgo Signal")
alertcondition(alert06, "Mild Buy Alert", "Mild Buy Signal PunkAlgo,
TimeFrame={{interval}}")
alertcondition(alert07, "Mild Sell Alert", "Mild Sell Signal PunkAlgo,
TimeFrame={{interval}}")
alertcondition(alert09, "Reversal Signal", "Reversal Signal")
alertcondition(alert10, "Sell Signal", "Sell Signal PunkAlgo")
// Trendlines Modules
//inputs
show_trendlines = input.bool(true, 'Show Trendlines', inline = 'tl_1',group='🧊
Trendlines')
upper_trendline_color = input.color(#9598a1, '', inline =
'tl_1',group='Trendlines')
lower_trendline_color = input.color(#9598a1, '', inline =
'tl_1',group='Trendlines')
extendLine = input.bool(true, 'Extend', inline = 'tl_1',group='Trendlines')
linestyle_curr_inp = input.string(defval = 'Solid', title = "Style", options =
['Solid', 'Dotted', 'Dashed'],inline = "tl_3",group='Trendlines')
line_width_curr = input.int(1, 'Width', step = 1, minval = 1,maxval =
4,inline = "tl_3",group='Trendlines')
pivLen_L = input.int(20, 'Lookback', step = 1, minval = 1, inline
='tl_5',group='Trendlines')
pivLen_R = pivLen_L//input.int(20, '/', step = 1, minval = 1, inline
='tl_5',group='Trendlines')
hideCrossed = not(input.bool(false, 'Show Broken', inline =
'tl_2',group='Trendlines'))
broken_color_up = input.color(#9598a1, '', inline =
'tl_2',group='Trendlines')
broken_color_down = input.color(#9598a1, '', inline =
'tl_2',group='Trendlines')
extendLine_B = input.bool(true, 'Extend', inline = 'tl_2',group='Trendlines')
show_signals = input.bool(false, 'Show Signals', inline =
'tl_s',group='Trendlines')
broken_color_up_signal = input.color(color.yellow, '', inline =
'tl_s',group='Trendlines')
broken_color_down_signal = input.color(color.yellow, '', inline =
'tl_s',group='Trendlines')
maxLines = input.int(3, 'Max Broken', step = 1, minval = 1, maxval = 50,
inline = 'tl_2_B',group='Trendlines')
Source_tl = input.string('Close', 'Mitigation', options = ['Close',
'High/Low'], inline ='tl_2_B',group='Trendlines')
linestyle_broken_inp = input.string(defval = 'Dashed', title = "Style (Broken)",
options = ['Solid', 'Dotted', 'Dashed'],inline = "tl_2-1",group='Trendlines')
line_width_broken = input.int(1, 'Width', step = 1, minval = 1,maxval =
4,inline = "tl_2-1",group='Trendlines')
lineStyle_curr= linestyle_curr_inp == 'Solid' ? line.style_dotted :
linestyle_curr_inp == 'Dotted' ? line.style_dotted : line.style_dashed
lineStyle_broken= linestyle_broken_inp == 'Solid' ? line.style_dotted :
linestyle_broken_inp == 'Dotted' ? line.style_dotted : line.style_dashed
s_close = request.security(ticker.standard(syminfo.tickerid), timeframe.period,
close)
s_open = request.security(ticker.standard(syminfo.tickerid), timeframe.period,
open)
s_high = request.security(ticker.standard(syminfo.tickerid), timeframe.period,
high)
s_low = request.security(ticker.standard(syminfo.tickerid), timeframe.period, low)
var line[] pivot_high_array = array.new_line(),var line[] pivot_low_array =
array.new_line()
ph2_M = ta.pivothigh(s_high, pivLen_L, pivLen_R)
pl2_M = ta.pivotlow(s_low, pivLen_L, pivLen_R)
ph2 = ta.pivothigh(s_high, pivLen_L, pivLen_R)
pl2 = ta.pivotlow(s_low, pivLen_L, pivLen_R)
var float prev_close_H = na, var float curr_close_H = na
var float prev_close_L = na, var float curr_close_L = na
var int X_prev_low = na, var float Y_prev_low = na, var int X_curr_low = na, var
float Y_curr_low = na
var int X_curr_high = na, var float Y_curr_high = na, var int X_prev_high = na, var
float Y_prev_high = na
maxLines := hideCrossed ? 0 : maxLines
tl_array_size_up = maxLines/2
tl_array_size_dn = maxLines%2==0? maxLines/2 : (maxLines/2)+1
//functions
newTrendLine(ptype, x1, y1, x2, y2)=>
new_trendline = line.new(x1, y1, x2, y2, extend = extendLine ? extend.right :
extend.none, color = ptype == 'ph2' ? upper_trendline_color :
lower_trendline_color, width = line_width_curr,style = lineStyle_curr,xloc =
xloc.bar_index)
if ptype == 'ph2'
pivot_high_array.unshift(new_trendline)
else
pivot_low_array.unshift(new_trendline)
SlopeOfLine(line)=>
slopeph2 = (line.get_y2(line) - line.get_y1(line))/(line.get_x2(line) -
line.get_x1(line))
extendedph2 = line.get_y2(line) - slopeph2 * (line.get_x2(line) - bar_index)
extendedph2
up_trend_line_formed= false
down_trend_line_formed = false
if pl2
X_prev_low := X_curr_low, Y_prev_low := Y_curr_low, prev_close_L :=
curr_close_L
X_curr_low := bar_index[pivLen_R], Y_curr_low := s_low[pivLen_R],
curr_close_L:= s_close[pivLen_R]
if Y_prev_low < Y_curr_low and show_trendlines and Y_curr_low > prev_close_L
newTrendLine('pl2', X_prev_low, Y_prev_low, X_curr_low, Y_curr_low)
down_trend_line_formed:=true
if ph2
X_prev_high := X_curr_high, Y_prev_high := Y_curr_high, prev_close_H :=
curr_close_H
X_curr_high := bar_index[pivLen_R], Y_curr_high := s_high[pivLen_R],
curr_close_H:= s_close[pivLen_R]
if Y_prev_high > Y_curr_high and show_trendlines and prev_close_H > Y_curr_high
newTrendLine('ph2', X_prev_high, Y_prev_high, X_curr_high, Y_curr_high)
up_trend_line_formed := true
if close > Y_prev_high
Y_prev_high:=0.000000
if close < Y_prev_low
Y_prev_low:= 9999999999.9999
// alertcondition(up_trend_line_formed,'Up TrendLine Formed','Up TrendLine Formed')
// alertcondition(down_trend_line_formed,'Down TrendLine Formed','Down TrendLine
Formed')
alertcondition(down_trend_line_formed or up_trend_line_formed,'Trendline
Formed','Trendline Formed')
up_trend_line_broken = false
down_trend_line_broken = false
for x in pivot_low_array
var line [] Down_Trend_Lines = array.new_line(tl_array_size_up)
var label [] Down_Trend_Labels = array.new_label(tl_array_size_up)
src = Source_tl == 'Close' ? s_close : s_low
x.set_xy2(bar_index, SlopeOfLine(x))
if x.get_x2() - x.get_x1() > 300
x.delete()
if src < line.get_y2(x)
tl_line = line.new(line.get_x1(x), line.get_y1(x), line.get_x2(x),
line.get_y2(x), color = broken_color_down, style = lineStyle_broken, width =
line_width_broken,xloc = xloc.bar_index, extend = extendLine_B ? extend.right :
extend.none)
down_trend_line_broken:=true
Down_Trend_Lines.unshift(tl_line)
line.delete(x)
if Down_Trend_Lines.size() > (tl_array_size_up)
line.delete(Down_Trend_Lines.pop())
for x in pivot_high_array
var line [] Up_Trend_Lines = array.new_line(tl_array_size_dn)
var label [] Up_Trend_Labels = array.new_label(tl_array_size_dn)
src = Source_tl == 'Close' ? s_close : s_high
x.set_xy2(bar_index, SlopeOfLine(x))
if x.get_x2() - x.get_x1() > 300
x.delete()
if src > line.get_y2(x)
tl_line = line.new(line.get_x1(x), line.get_y1(x), line.get_x2(x),
line.get_y2(x), color = broken_color_up, style = lineStyle_broken, width =
line_width_broken,xloc = xloc.bar_index, extend = extendLine_B ? extend.right :
extend.none)
up_trend_line_broken:=true
Up_Trend_Lines.unshift(tl_line)
line.delete(x)
if Up_Trend_Lines.size() > (tl_array_size_dn)
line.delete(Up_Trend_Lines.pop())
plotshape(show_signals and up_trend_line_broken? low : na, title='Trendline Broken
Up', style=shape.xcross, textcolor=color.new(color.white, 0), size=size.small,
location=location.belowbar, color=broken_color_up_signal,display= display.all -
display.status_line, editable = false)
plotshape(show_signals and down_trend_line_broken? high : na, title='Trendline
Broken Down', style=shape.xcross, textcolor=color.new(color.white, 0),
size=size.small, location=location.abovebar,
color=broken_color_down_signal,display= display.all - display.status_line,
editable = false)
// alertcondition(up_trend_line_broken,'Up TrendLine Broken','Up TrendLine Broken')
// alertcondition(down_trend_line_broken,'Down TrendLine Broken','Down TrendLine
Broken')
alertcondition(up_trend_line_broken or down_trend_line_broken,'Trendline
Broken','Trendline Broken')
// Liquidity Sweeps
//INPUTS
cooldownPeriod = input.int(10,title="Cooldown Period", minval=0, group= "🧊 Swing
Detaction")
lbLeft = 20
lbRight = 20
showSwing = input.bool(true,title="🧊 Show Swing Detaction", inline="s_1", group =
'')
swingClr = input.color(color.new(#b2b5be, 0), title='', inline="s_1", group = '🧊
Swing Detaction')
bullWidth = input.int(1, title='Line Width:', group='Bullish Sweep')
bullStyle = input.string('Dashed', title='Line Style:', options=['Solid', 'Dotted',
'Dashed'], group='🧊 Bullish Sweep')
bullColor = input.color(color.new(#089981, 0), title='Bullish Color:',
group='Bullish Sweep')
bearWidth = input.int(1, title='Line Width:', group='Bearish Sweep')
bearStyle = input.string('Dashed', title='Line Style:', options=['Solid', 'Dotted',
'Dashed'], group='🧊 Bearish Sweep')
bearColor = input.color(color.new(#f23645, 0), title='Bearish Color:', group='🧊
Bearish Sweep')
//FUNCTIONS
lineStyle(s) =>
if s == 'Solid'
line.style_solid
else if s == 'Dotted'
line.style_dotted
else
line.style_dashed
//VARS
var int bullSignalIndex = 0
var int bearSignalIndex = 0
var line bullLine = na
var line bearLine = na
var line highLine = na
var line lowLine = na
var label swingHighLbl = na
var label swingLowLbl = na
var label swingHighLblTxt = na
var label swingLowLblTxt = na
var float swingLowVal = na
var float swingHighVal = na
//CALCULATIONS
pLow = ta.pivotlow(low, lbLeft, lbRight)
pHigh = ta.pivothigh(high, lbLeft, lbRight)
pLowVal = ta.valuewhen(not na(pLow), low[lbRight], 0)
pHighVal = ta.valuewhen(not na(pHigh), high[lbRight], 0)
prevLowIndex = ta.valuewhen(not na(pLow), bar_index[lbRight], 0)
prevHighIndex = ta.valuewhen(not na(pHigh), bar_index[lbRight], 0)
lp = ta.lowest(low, lbLeft)
hp = ta.highest(high, lbLeft)
highestClose = ta.highest(close, lbLeft)
lowestClose = ta.lowest(close, lbLeft)
bullishSFP = low < pLowVal and close > pLowVal and open > pLowVal and low == lp and
lowestClose >= pLowVal
bearishSFP = high > pHighVal and close < pHighVal and open < pHighVal and high ==
hp and highestClose <= pHighVal
bullcond1 = bullishSFP[3] and (close > pLowVal) and (close[1] > pLowVal[1]) and
(close[2] > pLowVal[2]) and bar_index >= bullSignalIndex + cooldownPeriod
bearCond1 = bearishSFP[3] and (close < pHighVal) and (close[1] < pHighVal[1]) and
(close[2] < pHighVal[2]) and bar_index >= bearSignalIndex + cooldownPeriod
//Check Swing H/L Stopper
var int swingLowCounter = 0
var int swingHighCounter = 0
var bool isSwingLowCheck = false
var bool isSwingHighCheck = false
var bool stopPrintingLow = false
var bool stopPrintingHigh = false
if high < swingLowVal and isSwingLowCheck
swingLowCounter := swingLowCounter+1
if low > swingHighVal and isSwingHighCheck
swingHighCounter := swingHighCounter+1
if ta.crossunder(close, swingLowVal) and isSwingLowCheck == false
isSwingLowCheck := true
swingLowCounter := 1
if ta.crossover(close, swingHighVal) and isSwingHighCheck == false
isSwingHighCheck := true
swingHighCounter := 1
if swingLowCounter == 5 and isSwingLowCheck
stopPrintingLow := true
isSwingLowCheck := false
line.set_x2(lowLine,bar_index[4])
if swingHighCounter == 5 and isSwingHighCheck
stopPrintingHigh := true
isSwingHighCheck := false
line.set_x2(highLine,bar_index[4])
//Draw sweep lines
if bullcond1
bullSignalIndex := bar_index
bullLine := line.new(prevLowIndex, pLowVal, bar_index-3, pLowVal,
color=bullColor, width=bullWidth, style=lineStyle(bullStyle))
if bearCond1
bearSignalIndex := bar_index
bearLine := line.new(prevHighIndex, pHighVal, bar_index-3, pHighVal,
color=bearColor, width=bearWidth, style=lineStyle(bearStyle))
var swingHighArr = array.new_label(0)
var swingHighTextArr = array.new_label(0)
var swingLowArr = array.new_label(0)
var swingLowTextArr = array.new_label(0)
if array.size(swingHighArr) >= 3
label.delete(array.shift(swingHighArr))
label.delete(array.shift(swingHighTextArr))
if array.size(swingLowArr) >= 3
label.delete(array.shift(swingLowArr))
label.delete(array.shift(swingLowTextArr))
//Draw range lines
if showSwing
if stopPrintingHigh == false
line.set_x2(highLine,bar_index+5)
if stopPrintingLow == false
line.set_x2(lowLine,bar_index+5)
if showSwing and not na(pHigh) and bearishSFP[lbRight] == false
stopPrintingHigh := false
swingHighVal := high[lbRight]
line.delete(highLine)
highLine := line.new(bar_index[lbRight], high[lbRight], bar_index+10,
high[lbRight], color = swingClr, width = 2)
swingHighLbl := label.new(bar_index[lbRight], high[lbRight], text="",
yloc=yloc.abovebar, color = swingClr, textcolor = swingClr, style =
label.style_triangledown, size = size.auto)
swingHighLblTxt := label.new(bar_index[lbRight], high[lbRight], text="Swing\
nH", yloc=yloc.abovebar, color = swingClr, textcolor = swingClr, style =
label.style_none, size = size.small)
array.push(swingHighArr, swingHighLbl)
array.push(swingHighTextArr, swingHighLblTxt)
if showSwing and not na(pLow) and bullishSFP[lbRight] == false
stopPrintingLow := false
swingLowVal := low[lbRight]
line.delete(lowLine)
lowLine := line.new(bar_index[lbRight], low[lbRight], bar_index+10,
low[lbRight], color = swingClr, width = 2)
swingLowLbl := label.new(bar_index[lbRight], low[lbRight], text="",
yloc=yloc.belowbar, color = swingClr, textcolor = swingClr, style =
label.style_triangleup, size = size.auto)
swingLowLblTxt := label.new(bar_index[lbRight], low[lbRight], text="Swing\nL",
yloc=yloc.belowbar, color = swingClr, textcolor = swingClr, style =
label.style_none, size = size.small)
array.push(swingLowArr, swingLowLbl)
array.push(swingLowTextArr, swingLowLblTxt)
//PLOTS
plotshape(bullcond1, text='Sweep', color=bullColor, textcolor=bullColor,
location=location.belowbar, offset = -3)
plotshape(bearCond1, text='Sweep', color=bearColor, textcolor=bearColor,
location=location.abovebar, offset = -3)
//ALERTS
alertcondition(bullishSFP, title='Bullish Sweep', message='{{ticker}} Bullish
Sweep, Price:{{close}}')
alertcondition(bearishSFP, title='Bearish Sweep', message='{{ticker}} Bearish
Sweep, Price:{{close}}')
showRibbon = input(title='Show Smart Ribbon', defval=false, group= "🧊 Smart
Ribbon")
string Core = "Core Settings"
string visual = "Visual Settings"
int ShortPriod = input.int (26, 'Slow
Period', 1,group=Core)
int LongPriod = input.int (14, 'Fast
Period', 2,group=Core)
int Neighbours = math.floor(math.sqrt(input.int
(250,'Neighbours Count', 5,group=Core)))
float BarRange = high - low
var array<float> feature1 = array.new_float(0)
var array<float> feature2 = array.new_float(0)
var array<int> directions = array.new_int(0)
var array<float> data = array.new_float()
var array<int> predictions = array.new_int(0)
float max = input.float(5.0,"Long
Sensitivity",tooltip = "The sensitivity level directly affects the number of class
generated for uptrend",step = 0.1,maxval = 5.0,minval = 1.0) * 10
float max2 = input.float(9,"Short
Sensitivity",tooltip = "The sensitivity level directly affects the number of class
generated for uptrend",step = 0.1,maxval = 12.0,minval = 5.0) * 10
color Bull = input.color(#1f4037,title =
"Bull",group=visual,inline = "005")
color Bull1 = input.color(#99f2c8,title =
"",group=visual,inline = "005")
color bear11 = input.color(#FF416C,title =
"Bear",group=visual,inline = "005")
color Bear1 = input.color(#FF4B2B,title =
"",group=visual,inline = "005")
var int signal = 0
var float prediction = 0.0
var LLong = false
var SShort = false
Range_MA(period)=>
// Calculate weighted ma
weight = BarRange / math.sum(BarRange, period)
TotalSum = math.sum(close * weight, period)
TotalW= math.sum(weight, period)
rwma = TotalSum / TotalW
prediction_data(float src , int dir) =>
// RSI Data Calculation
up = ta.rma(math.max(ta.change(src), 0), dir)
down = ta.rma(-math.min(ta.change(src), 0), dir)
_ISR = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up /
down))
mf = ta.mfi(src, dir)
_ISR_mfi = math.abs( _ISR + mf / 2 )
_ISR_mfi
LongData = prediction_data(hlc3,LongPriod)
ShortData = prediction_data(hlc3,ShortPriod)
longcon = LongData < max
shorton = ShortData > max2
int Class = shorton ? -1 : longcon ? 1 : 0
// Nearest Neighbor Calculation
array.push(feature1, LongData)
array.push(feature2, ShortData)
array.push(directions, Class)
int size = array.size(directions)
float dust = -999.0
for i = 0 to size - 1
// Calculate the Manhattan distance of the current point to all historic
points.
float ManD = math.abs(LongData - array.get(feature1, i)) + math.abs(ShortData -
array.get(feature2, i))
if ManD > dust
dust := ManD
if array.size(predictions) >= Neighbours
array.shift(predictions)
array.push(predictions, array.get(directions, i))
// Prediction and coloring Conditions
prediction := array.sum(predictions) * 5
C = not (prediction > 0) and not (prediction < 0)
signal := prediction > 0 ? 1 : prediction < 0 ? -1 : C ? 0 : nz(signal[1])
int changed = ta.change(signal)
bool Uptrend = changed and signal==1
bool DownTrend = changed and signal==-1
if Uptrend //and close > Range_MA(10)
LLong:= true
SShort:= false
if DownTrend //and close < Range_MA(10)
LLong:= false
SShort:= true
_color =
LLong ? color.new(color.from_gradient(prediction,0,55,Bull,Bull1),30) :
SShort ? color.new(color.from_gradient(-prediction,-55,-1,bear11,Bear1),30) :
na
first = plot(showRibbon ? Range_MA(10): na , color = color.new(_color,80),linewidth
= 1,editable = false)
plot(showRibbon ? Range_MA(11) : na ,color = color.new(_color,80),linewidth =
1,editable = false)
plot(showRibbon ?Range_MA(12): na,color = color.new(_color,80),linewidth =
1,editable = false)
plot(showRibbon ?Range_MA(13): na,color = color.new(_color,80),linewidth =
1,editable = false)
plot(showRibbon ?Range_MA(14): na,color = color.new(_color,80),linewidth =
1,editable = false)
plot(showRibbon ?Range_MA(15): na,color = color.new(_color,80),linewidth =
1,editable = false)
plot(showRibbon ?Range_MA(16): na,color = color.new(_color,80),linewidth =
1,editable = false)
plot(showRibbon ?Range_MA(17): na,color = color.new(_color,80),linewidth =
1,editable = false)
plot(showRibbon ?Range_MA(18): na,color = color.new(_color,80),linewidth =
1,editable = false)
last = plot(showRibbon ? Range_MA(20): na,color = color.new(_color,80),linewidth =
1,editable = false)
fill(first,last,color= color.new(_color,90))