Squeeze Momentum MTF [LPWN]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Lupown

//@version=5
indicator(shorttitle='Squeeze Momentum MTF [LPWN]', title='Squeeze Momentum MTF [LPWN]', overlay=false)
//squeeze momoentum by lazy bear
import hapharmonic/ConverterTF/1 as X
cipher = "------------------------ CIPHER DOTS ------------------------"
rsiGrp = "---------------------------- RSI ----------------------------"
szqGrp = "-------------------------- Squeeze --------------------------"
adxGrp = "---------------------------- ADX ----------------------------"
divGrp = "------------------------- Divergences -------------------------"
htfGrp = "---------------------------- HTF ----------------------------"
momGrp = "---------------------------- Momentums ----------------------------" 
pryGrp = "---------------------------- Proyection ----------------------------" 
tblGrp = "---------------------------- Table ----------------------------" 
// —————————— PineCoders MTF Selection Framework functions
// ————— Converts current "timeframe.multiplier" plus the TF into minutes of type float.
f_resInMinutes() =>
    _resInMinutes = timeframe.multiplier * (timeframe.isseconds ? 1. / 60. : timeframe.isminutes ? 1. : timeframe.isdaily ? 1440. : timeframe.isweekly ? 10080. : timeframe.ismonthly ? 43800. : na)
    _resInMinutes

// ————— Returns resolution of _resolution period in minutes.
f_tfResInMinutes(_resolution) =>
    // _resolution: resolution of any TF (in "timeframe.period" string format).
    request.security(syminfo.tickerid, _resolution, f_resInMinutes())

// ————— Given current resolution, returns next step of HTF.
f_resNextStep(_res,mom2,info) =>
    // _res: current TF in fractional minutes.
    if mom2 or info
        _res <= 1 ? '5' : _res <= 5 ? '9' : _res <= 15 ? '30' : _res <= 30 ? '60' : _res <= 60 ? '240' : _res <= 360 ? '1D' : _res <= 1440 ? '1W' : '2W'//_res <= 10080 ? '1M' : '1M'
    else
        str.tostring(_res)
f_resNextStep2(_res,mom3,info) =>
    // _res: current TF in fractional minutes.
    if mom3 or info
        _res <= 1 ? '9' : _res <= 5 ? '15' : _res <= 15 ? '45' :  _res <= 30 ? '120' : _res <= 60 ? '480' : _res <= 360 ? '2D' : _res <= 1440 ? '1W' : '2W'//_res <= 10080 ? '1M' : '2M'
    else
        str.tostring(_res)
// ————— ORIGINAL
// f_resNextStep(_res) =>
//     // _res: current TF in fractional minutes.
//     _res    <= 1        ? "15"  :
//       _res  <= 5        ? "60"  :
//       _res  <= 30       ? "240" :
//       _res  <= 60       ? "1D"  :
//       _res  <= 360      ? "3D"  :
//       _res  <= 1440     ? "1W"  :
//       _res  <= 10080    ? "1M"  : "12M"

// ————— Returns a multiple of current resolution as a string in "timeframe.period" format usable with "security()".
f_multipleOfRes(_res, _mult) =>
    // _res:  current resolution in minutes, in the fractional format supplied by f_resInMinutes() companion function.
    // _mult: Multiple of current TF to be calculated.
    // Convert current float TF in minutes to target string TF in "timeframe.period" format.
    _targetResInMin = _res * math.max(_mult, 1)
    _targetResInMin <= 0.083 ? '5S' : _targetResInMin <= 0.251 ? '15S' : _targetResInMin <= 0.501 ? '30S' : _targetResInMin <= 1440 ? str.tostring(math.round(_targetResInMin)) : _targetResInMin <= 43800 ? str.tostring(math.round(math.min(_targetResInMin / 1440, 365))) + 'D' : str.tostring(math.round(math.min(_targetResInMin / 43800, 12))) + 'M'

// ————— Print a label at end of chart.
f_htfLabel(_txt, _y, _color, _offsetLabels) =>
    _t = int(time + f_resInMinutes() * _offsetLabels * 60000)
    var _lbl = label.new(_t, _y, _txt, xloc.bar_time, yloc.price, #00000000, label.style_none, color.gray, size.large)
    if barstate.islast
        label.set_xy(_lbl, _t, _y)
        label.set_text(_lbl, _txt)
        label.set_textcolor(_lbl, _color)
// }


// —————  INPUTS




TF0 = 'None'
TF1 = 'Auto-Steps (15min, 60min, 4H, 1D, 3D, 1W, 1M, 12M)'
TF2 = 'Multiple Of Current TF'
TF3 = 'Fixed TF'
i_htfType = input.string(TF1, 'Higher Timeframe Selection', options=[TF0, TF1, TF2, TF3],group = htfGrp)
i_htfType2 = input.float(3., '  Multiple of Current TF 1', minval=1,group = htfGrp)
i_htfType22 = input.float(5., '  Multiple of Current TF 2', minval=1,group = htfGrp)
i_htfType3 = input.timeframe('240', '  Fixed TF 1',group = htfGrp)
i_htfType33 = input.timeframe('D', '  Fixed TF 2',group = htfGrp)


 

useHistorial = true//input(true, 'Usar historial')
historial = 500//input(500, 'Historial')


proyec = false//input(false, 'Mostrar proyeccion')
predict = input(true, 'Show predict')


armoniColor = input(false, 'Momentum color')
colorbar = input(true, 'Candle color with')

int lengthM = input.int(20, title='MOM Length', minval=1, step=1, group = momGrp)
srcM = input(close, title='MOM Source', group = momGrp)

mom2 = input(true, title='Momentum HTF 1 ', group = momGrp)
doble2 = input(true, 'Show momentum 1 histogram', group = momGrp)
smoothM2 = input(1, 'Smooth momentum 1', group = momGrp)
farMom2 = input(0, title='Away from 0 point 1 momentum', group = momGrp)



mom3 = input(true, title='Momentum HTF 2', group = momGrp)
doble3 = input(true, 'Show momentum 2 histogram', group = momGrp)
smoothM3 = input(1, 'Smooth Momentum 2', group = momGrp)
farMom3 = input(0, title='Away from 0 point 2 momentum', group = momGrp)



int length = input.int(20, title='SQZ Length', minval=1, step=1,group = szqGrp)
src = input(close, title='SQZ Source',group = szqGrp)
lengths = input(20, title='BB Length',group = szqGrp)
mult = input(2.0,title="BB MultFactor",group = szqGrp)
lengthKC = input(20, title='KC Length',group = szqGrp)
multKC = input(1.5, title='KC MultFactor',group = szqGrp)
scale = input(75.0, title='General scale',group = szqGrp)


//DivergenceS BUENNNNNNNAS
show_div = input(true, title='Show Divergences',group = divGrp)
lbR = input(title='Pivot Lookback Right', defval=1,group = divGrp)
lbL = input(title='Pivot Lookback Left', defval=1,group = divGrp)
rangeUpper = input(title='Max of Lookback Range', defval=60,group = divGrp)
rangeLower = input(title='Min of Lookback Range', defval=1,group = divGrp)
plotBull = input(title='Plot Bullish', defval=true,group = divGrp)
plotHiddenBull = input(title='Plot Hidden Bullish', defval=true,group = divGrp)
plotBear = input(title='Plot Bearish', defval=true,group = divGrp)
plotHiddenBear = input(title='Plot Hidden Bearish', defval=true,group = divGrp)


show_ADX = input(false, title='Show ADX',group = adxGrp)
scaleADX = input(2.0, title='ADX scale',group = adxGrp)
show_di = input(false, title='    Show +DI -DI',group = adxGrp)
show_aa = input.bool(false, 'Show 23 level apart', inline='adx line',group = adxGrp)
far = input.int(-7, 'Separation', inline='adx line',group = adxGrp)
adxlen = input(14, title='ADX Smoothing',group = adxGrp)
dilen = input(14, title='DI Length',group = adxGrp)
keyLevel = input(23, title='Key level for ADX',group = adxGrp)
show_bg = input(false, title='Show bg color',group = adxGrp)




//RSI
show_rsi = input(false, title='Show RSI',group = rsiGrp)
show_RSIfondo = input(true, title='Show RSI background',group = rsiGrp)
rsiApart = input(0, title='RSI separation',group = rsiGrp)
len = input.int(14, minval=1, title='Length RSI ',group = rsiGrp)
upperR = input(70, title='Upper band',group = rsiGrp)
middleR = input(50, title='Middle band',group = rsiGrp)
lowerR = input(30, title='Lower band',group = rsiGrp)

show_cphr = input(true, title='Show Cipher dots',group = cipher)
n1 = input(9, title='Channel Length',group = cipher)
n2 = input(12, title='Average Length',group = cipher)
smooth = input(3, title='Smooth Factor',group = cipher)


showTable = input.bool(true,"Show Table",group = tblGrp)
showAdxTbl = input.bool(true,"Show ADX info",group = tblGrp)
showMomInfo = input.bool(false,"Show MOM info always",group = tblGrp)
TblY = input.string('top', 'Table Position', inline='11', options=['top', 'middle', 'bottom'],group = tblGrp)
TblX = input.string('right', '', inline='11', options=['left', 'center', 'right'],group = tblGrp)


// STEP 1. Make an input with drop-down menu
sizeOption = input.string(title="Table Size",options=["Auto", "Huge", "Large", "Normal", "Small", "Tiny"], defval="Normal",group = tblGrp)

// STEP 2. Convert the input to valid label sizes
labelSize = (sizeOption == "Huge") ? size.huge : (sizeOption == "Large") ? size.large :
     (sizeOption == "Small") ? size.small :
     (sizeOption == "Tiny") ? size.tiny :
     (sizeOption == "Auto") ? size.auto : size.normal

ca1 = input.color(#00FF00,title = "Bullish 1",group = "Colors",inline = "mom1")
cb1 = input.color(#FF0000,title = "Bearish 1",group = "Colors",inline = "mom1")
crs1 = input.color(#008000,title = "sideway-Bulish 1",group = "Colors",inline = "mom1")
crc1 = input.color(#800000,title = "sideway-Bearish 1",group = "Colors",inline = "mom1")

ca2 = input.color(#66bb6a,title = "Bullish 2",group = "Colors",inline = "mom2")
cb2 = input.color(#f75555,title = "Bearish 2",group = "Colors",inline = "mom2")
crs2 = input.color(#056656,title = "sideway-Bulish 2",group = "Colors",inline = "mom2")
crc2 = input.color(#a90044,title = "sideway-Bearish 2",group = "Colors",inline = "mom2")


ca3 = input.color(#74d18b,title = "Bullish 3",group = "Colors",inline = "mom3")
cb3 = input.color(#ff8888,title = "Bearish 3",group = "Colors",inline = "mom3")
crs3 = input.color(#406640,title = "sideway-Bulish 3",group = "Colors",inline = "mom3")
crc3 = input.color(#8d0707,title = "sideway-Bearish 3",group = "Colors",inline = "mom3")

show_Momen = true //input(true, title='-----------Show squeeze momentum-------')

// HTF is being used.
var htfOn = i_htfType != TF0


// ————— HTF calcs
// Get current resolution in float minutes.
var resInMinutes = f_resInMinutes()
// Get HTF from user-defined mode.
var htf = i_htfType == TF0 ? timeframe.period : i_htfType == TF1 ? f_resNextStep(resInMinutes,mom2,showMomInfo) : i_htfType == TF2 ? f_multipleOfRes(resInMinutes, i_htfType2) : i_htfType3

var htf2 = i_htfType == TF0 ? timeframe.period : i_htfType == TF1 ? f_resNextStep2(resInMinutes,mom3,showMomInfo) : i_htfType == TF2 ? f_multipleOfRes(resInMinutes, i_htfType22) : i_htfType33













//plotshape(show_Momen?0:na,style=shape.xcross,color=scolor,location=location.absolute)



///// ADX


//SQUEEZE


useTrueRange = true



// Calculate BB
source = close
basis = ta.sma(source, lengths)
dev = multKC * ta.stdev(source, lengths)
upperBB = basis + dev
lowerBB = basis - dev

// Calculate KC
ma = ta.sma(source, lengthKC)
range_1 = useTrueRange ? ta.tr : high - low
rangema = ta.sma(range_1, lengthKC)
upperKC = ma + rangema * multKC
lowerKC = ma - rangema * multKC

sqzOn = lowerBB > lowerKC and upperBB < upperKC
sqzOff = lowerBB < lowerKC and upperBB > upperKC
noSqz = sqzOn == false and sqzOff == false

scolor = noSqz ? color.blue : sqzOn ? #000000 : color.gray
//plotshape(show_Momen? true : na, color=scolor, style=shape.xcross)
plot(show_Momen ? 0 : na, title='Squeeze Zero Line', color=scolor, linewidth=2, style=plot.style_cross)



dirmov(len) =>
    up = ta.change(high)
    down = -ta.change(low)
    truerange = ta.rma(ta.tr, len)
    plus = fixnan(100 * ta.rma(up > down and up > 0 ? up : 0, len) / truerange)
    minus = fixnan(100 * ta.rma(down > up and down > 0 ? down : 0, len) / truerange)
    [plus, minus]

adx(dilen, adxlen) =>
    [plus, minus] = dirmov(dilen)
    sum = plus + minus
    adx = 100 * ta.rma(math.abs(plus - minus) / (sum == 0 ? 1 : sum), adxlen)
    [adx, plus, minus]

[adxValue, diplus, diminus] = adx(dilen, adxlen)



biggest(series) =>
    max = 0.0
    max := nz(max[1], series)
    if series > max
        max := series
        max
    max



//FUNCTIONS
plFound(osc) =>
    na(ta.pivotlow(osc, lbL, lbR)) ? false : true
phFound(osc) =>
    na(ta.pivothigh(osc, lbL, lbR)) ? false : true





sz = ta.linreg(srcM - math.avg(math.avg(ta.highest(high, lengthM), ta.lowest(low, lengthM)), ta.sma(close, lengthM)), lengthM, 0)


// Calculate ADX Scale
ni = biggest(sz)
far1 = far * ni / scale
adx_scale = (adxValue - keyLevel) * ni / scale

adx_scale2 = (adxValue - keyLevel + far) * ni / scale



sz2 = request.security(syminfo.tickerid, htf, ta.linreg(srcM - math.avg(math.avg(ta.highest(high, lengthM), ta.lowest(low, lengthM)), ta.sma(close, lengthM)), lengthM, 0))

sz3 = request.security(syminfo.tickerid, htf2, ta.linreg(srcM - math.avg(math.avg(ta.highest(high, lengthM), ta.lowest(low, lengthM)), ta.sma(close, lengthM)), lengthM, 0))


adx2 = request.security(syminfo.tickerid, htf, adxValue)
adx3 = request.security(syminfo.tickerid, htf2, adxValue)

sz2ws = sz2 + farMom2 * ni / scale
sz3ws = sz3 + farMom3 * ni / scale
sz22 = ta.ema(ta.ema(ta.ema(sz2ws, smoothM2), smoothM2), smoothM2)
sz33 = ta.ema(ta.ema(ta.ema(sz3ws, smoothM3), smoothM3), smoothM3)


sc1 = sz >= 0
sc2 = sz < 0
sc3 = sz >= sz[1]
sc4 = sz < sz[1]


clr = sc1 and sc3 ? ca1 : sc1 and sc4 ? crs1 : sc2 and sc4 ? cb1 : sc2 and sc3 ? crc1 : color.gray
clr1 = sc1 and sc3 ? ca1 : sc1 and sc4 ? crc1 : sc2 and sc4 ? cb1 : sc2 and sc3 ? crs1 : color.gray

iMom = sc1 and sc3 ? '↗️' : sc1 and sc4 ? '↘️' : sc2 and sc4 ? '↘️' : sc2 and sc3 ? '↗️' : '-'  //ORIGNAL

sc12 = sz2 >= 0
sc22 = sz2 < 0
sc32 = plFound(sz2)
sc42 = phFound(sz2)
iMom2 = ''
iMom3 = ''
clr2 = color.gray
clr3 = color.white
clr22 = color.gray
clr33 = color.white
clr2 := sc12 and sc32 ? ca2 : sc12 and sc42 ? crs2 : sc22 and sc42 ? cb2 : sc22 and sc32 ? crc2 : clr2[1]

clr22 := sc12 and sc32 ? ca1 : sc12 and sc42 ? crc1 : sc22 and sc42 ? cb1 : sc22 and sc32 ? crs1 : clr22[1]

color2 = clr22
color21 = clr2
iMom2 := sc12 and sc32 ? '↗️' : sc12 and sc42 ? '↘️' : sc22 and sc42 ? '↘️' : sc22 and sc32 ? '↗️' : iMom2[1]  //ORIGNAL


sc13 = sz3 >= 0
sc23 = sz3 < 0
sc33 = plFound(sz3)
sc43 = phFound(sz3)


clr3 := sc13 and sc33 ? ca3 : sc13 and sc43 ? crs3 : sc23 and sc43 ? cb3 : sc23 and sc33 ? crc3 : clr3[1]

clr33 := sc13 and sc33 ? ca1 : sc13 and sc43 ? crc1 : sc23 and sc43 ? cb1 : sc23 and sc33 ? crs1 : clr33[1]

color3 = clr33

color31 = clr3

iMom3 := sc13 and sc33 ? '↗️' : sc13 and sc43 ? '↘️' : sc23 and sc43 ? '↘️' : sc23 and sc33 ? '↗️' : iMom3[1]  //ORIGNAL


clr3Av = clr3 == ca3
clr2Av = clr2 == ca2
clrAv = clr == ca1
clr3Ar = clr3 == crc3
clr2Ar = clr2 == crc2
clrAr = clr == crc1

clr3Av2 = clr3 == cb3
clr2Av2 = clr2 == cb2
clrAv2 = clr == cb1
clr3Ar2 = clr3 == crs3
clr2Ar2 = clr2 == crs2
clrAr2 = clr == crs1

clrA = clr3Av and clr2Av and clrAv or clr3Ar and clr2Ar and clrAr or clr3Av and clr2Av and clrAr or clrAv and clr2Av and clr3Ar or clr3Av and clrAv and clr2Ar or clr3Ar and clr2Ar and clrAv or clrAr and clr2Ar and clr3Av or clr3Ar and clrAr and clr2Av ? ca1 : clr3Av2 and clr2Av2 and clrAv2 or clr3Ar2 and clr2Ar2 and clrAr2 or clr3Av2 and clr2Av2 and clrAr2 or clrAv2 and clr2Av2 and clr3Ar2 or clr3Av2 and clrAv2 and clr2Ar2 or clr3Ar2 and clr2Ar2 and clrAv2 or clrAr2 and clr2Ar2 and clr3Av2 or clr3Ar2 and clrAr2 and clr2Av2 ? cb1 : #e4e4e4

//clrA = sz > sz[1] and sz2 >= sz2[1] and sz3 >= sz3[1] ? ca1 :  sz < sz[1] and sz2 <= sz2[1] and sz3 <= sz3[1] ? cb1 : #423432
barcolor(colorbar ? clrA : na)


plot(doble3 and mom3 ? sz33 : na, title='Momentum 3 doble', style=plot.style_histogram, color=color31)



plot(doble2 and mom2 ? sz22 : na, title='Momentum 2 doble', style=plot.style_histogram, color=color21)




plot(show_Momen ? sz : na, title='Squeeze Momentum', color=armoniColor ? clrA : clr, style=plot.style_area)

plotmom2 = plot(mom2 ? sz22 : na, title='Squeeze Momentum 2', color=clr2, style=plot.style_line, linewidth=3)
band2 = plot(mom2 ? farMom2 * ni / scale : na, color=color.new(color.white, 60))
fill(plotmom2, band2, color=mom2 ? color.new(clr2,90) : na, title='MOM2 Fill')

plotmom3 = plot(mom3 ? sz33 : na, title='Squeeze Momentum 3', color=color.new(clr3, 0), style=plot.style_line, linewidth=4)
band3 = plot(mom3 ? farMom3 * ni / scale : na, color=color.new(color.white, 60))
fill(plotmom3, band3, color=mom2 ? color.new(clr3,90) : na, title='MOM3 Fill')









var npl = array.new_float(historial, 0)
var nph = array.new_float(historial, 0)
var hh = 1
var ll = 1
var contH = 0
var contL = 0
var totH = 1
var totL = 1
var promH = 0.0
var promL = 0.0
szint = math.round(sz)


contH += 1
contL += 1
if phFound(sz)
    hh += contH
    contH := 0
    //contL := 0
    totH += 1
    totH
if plFound(sz)
    ll += contL
    contL := 0
    //contH := 0
    totL += 1
    totL
//label.new(bar_index,0,tostring(ll[0]))
//label.new(bar_index[1],0,tostring(ll[1]))
//label.new(bar_index[2],0,tostring(ll[2]))
//label.new(bar_index[3],0,tostring(ll[3]))

promH := hh / totH
promL := ll / totL


var al = line.new(0, 0, 0, 0)
var bl = line.new(0, 0, 0, 0)

if phFound(sz) and predict
    //label.new(bar_index + round(promL),0,text =  "🤑",color = color.green )
    al := line.new(bar_index + math.round(promL), 0, bar_index + math.round(promL), 0.001, extend=extend.both, color=color.green)
    line.delete(bl)

if plFound(sz) and predict
    bl := line.new(bar_index + math.round(promH), 0, bar_index + math.round(promH), 0.001, extend=extend.both, color=color.red)
    line.delete(al)
    //label.new(bar_index + round(promH),0,text =  "🤑" ,color = color.red)
// if barstate.islast

// 	for i = historial to 0
// 		hh := hh + sz[i]
// plot (sma(hh,historial),style = plot.style_columns,show_last = historial)








////////////////////////////////////
//ADX
/////////////////////////////

dip = (diplus - keyLevel) * ni / scale
dim = (diminus - keyLevel) * ni / scale
plot(show_di ? dip * scaleADX : na, color=color.new(color.green, 0), title='DI+')
plot(show_di ? dim * scaleADX : na, color=color.new(color.red, 0), title='DI-')

color_ADX = adxValue > adxValue[1] ? #ffffff : #a09a9a

bgcolor(adxValue > adxValue[1] and adxValue > 23 and show_Momen and show_bg ? clr : na)



rsi = ta.rsi(src, len)
rsiColor = rsi <= lowerR ? color.green : rsi >= upperR ? color.red : #da00ff
rsi_scale = (rsi + rsiApart) * ni / scale


b1s = (rsiApart + upperR) * ni / scale
bm = (rsiApart + middleR) * ni / scale
b0s = (rsiApart + lowerR) * ni / scale

//rsiColor2 = divergence > 0 ? color.lime:color.red
plot(show_rsi ? rsi_scale : na, 'RSI', color=show_rsi ? rsiColor : na)

band1 = plot(show_rsi ? b1s : na, color=bar_index % 2 == 0 ? color.rgb(255, 255, 255, 38) : #00000000)
band0 = plot(show_rsi ? b0s : na, color=bar_index % 2 == 0 ? color.rgb(255, 255, 255, 38) : #00000000)
bandm = plot(show_rsi ? bm : na, color=bar_index % 2 == 0 ? color.rgb(255, 255, 255, 38) : #00000000)
fill(band1, band0, color=show_rsi and show_RSIfondo ? color.rgb(155, 39, 176, 84) : na, title='RSI background')
//////////////////////
////////////////////////////////////////////
//////////////////////

//WT the cipher dots use a indicator by lazy bear


    //wt
CALC(n1, n2, smooth) =>
    ap = hlc3
    esa = ta.ema(ap, n1)
    dw = ta.ema(math.abs(ap - esa), n1)
    ci = (ap - esa) / (0.015 * dw)
    tci = ta.ema(ci, n2)

    wt1 = tci
    wt2 = ta.sma(wt1, smooth)
    wave = wt1 - wt2
    wavecolor = wave >= 0 ? color.lime : color.red
    [wave, wavecolor]

    //CIPHERR
[wave, wavecolor] = CALC(n1, n2, smooth)

middle = 0
condWt = ta.cross(wave, middle) ? true : na
wtjeje = sz  //show_Momen ?  : show_ao ? ao : show_macd ? macd : na
plot(condWt and show_cphr ? wtjeje : na, title='Buy and sell circle', color=wavecolor, style=plot.style_circles, linewidth=3)

/////plot ADX

plot(show_aa ? far1 * scaleADX : na, color=color.new(color.white, 0), title='Punto 23')
p1 = plot(show_aa ? adx_scale2 * scaleADX : show_ADX ? adx_scale * scaleADX : na, color=color_ADX, title='ADX', linewidth=2)

///
///////////////
// ESTADOOO
show_status = input(false, title='------Show STATUS----------')
dist = input(10, title='Monitor distance')
dashColor = input.color(color.new(#696969, 90), 'label Color', inline='Dash Line')
dashTextColor = input.color(color.new(#ffffff, 0), 'Text Color', inline='Dash Line')


if adxValue > adxValue[1] and adxValue > 23
    iadx = 'ADX con pendiente positiva por encima punto 23'
    iadx
if adxValue > adxValue[1] and adxValue < 23
    iadx = 'ADX con pendiente positiva por debajo punto 23'
    iadx
if adxValue < adxValue[1] and adxValue < 23
    iadx = 'ADX con pendiente negativa por debajo punto 23'
    iadx
if adxValue < adxValue[1] and adxValue > 23
    iadx = 'ADX con pendiente negativa por encima punto 23'
    iadx




a1 = adxValue >= 23
a2 = adxValue < 23
a3 = adxValue >= adxValue[1]
a4 = adxValue < adxValue[1]


iAdx = a1 and a3 ? 'Pendiente positiva ↑ 23' : a1 and a4 ? 'Pendiente negativa ↑ 23' : a2 and a4 ? 'Pendiente negativa ↓ 23' : a2 and a3 ? 'Pendiente positiva ↓ 23' : '-'

iAdx1 = a1 and a3 ? '↗️ ↑ 23' : a1 and a4 ? '↘️ ↑ 23' : a2 and a4 ? '↘️ ↓ 23' : a2 and a3 ? '↗️ ↓ 23' : '-'
iAdx2 = a1 and a3 ? '↗️ ↑ 23' : a1 and a4 ? '↘️ ↑ 23' : a2 and a4 ? '↘️ ↓ 23' : a2 and a3 ? '↗️ ↓ 23' : '-'
iAdx3 = a1 and a3 ? '↗️ ↑ 23' : a1 and a4 ? '↘️ ↑ 23' : a2 and a4 ? '↘️ ↓ 23' : a2 and a3 ? '↗️ ↓ 23' : '-'




// iMom = sc1 and sc3 ? 'Direccionalidad Bullish' : sc1 and sc4 ? 'Direccionalidad Bearish' : 
//    sc2 and sc4 ? 'Direccionalidad Bearish' : sc2 and sc3 ? 'Direccinalidad Bullish' : '-'

igral = a1 and a3 and sc1 and sc3 ? 'Fuerte movimiento Bullish' : a1 and a3 and sc1 and sc4 ? 'Monitor muestra sideway-Bearish pero\nel movimiento tiene fuerza' : a1 and a3 and sc2 and sc4 ? 'Fuerte movimiento Bearish' : a1 and a3 and sc2 and sc3 ? 'Monitor muestra sideway-Bulish pero\nel movimiento tiene fuerza' : a1 and a4 and sc1 and sc3 ? 'Movimiento Bullish sin fuerza' : a1 and a4 and sc1 and sc4 ? 'Monitor muestra sideway-Bearish\n pendiente negativa en ADX ' : a1 and a4 and sc2 and sc4 ? 'Movimiento Bearish sin fuerza' : a1 and a4 and sc2 and sc3 ? 'Monitor muestra sideway-Bulish con \npendiente negativa en ADX ' : a2 and a4 and sc1 and sc3 ? 'Movimiento Bullish sin fuerza' : a2 and a4 and sc1 and sc4 ? 'Monitor muestra sideway-Bearish sin fuerza ' : a2 and a4 and sc2 and sc4 ? 'Movimiento Bearish sin fuerza' : a2 and a4 and sc2 and sc3 ? 'Monitor muestra sideway-Bulish sin fuerza ' : a2 and a3 and sc1 and sc3 ? 'Movimiento Bullish que \n quiere agarrar fuerza' : a2 and a3 and sc1 and sc4 ? 'Monitor muestra sideway-Bearish,\n el movimiento quiere agarrar fuerza' : a2 and a3 and sc2 and sc4 ? 'Movimiento Bearish que \n quiere agarrar fuerza' : a2 and a3 and sc2 and sc3 ? 'Monitor muestra sideway-Bulish,\n el movimiento quiere agarrar fuerza' : '-'


s = '\n'
scr_label = 'Info ADX: ' + iAdx + s + 'Info monitor: ' + iMom + s + 'Info general:' + s + igral
// Plot Label on the chart
// Plot Label on the chart

if show_status

    lab_l = label.new(bar_index + dist, 0, '\t Estatus segun estrategia\n\t' + scr_label, color=dashColor, textcolor=dashTextColor, style=label.style_label_left, yloc=yloc.price)
    label.delete(lab_l[1])

// Send alert only if screener is not empty
if scr_label != ''
    alert('Estatus segun estrategia\n' + scr_label, freq=alert.freq_once_per_bar_close)




//////////////////////////////////





bearColor = cb1
bullColor = #1bff00
hiddenBullColor = #a4ff99
hiddenBearColor = #ff9e9e
textColor = color.white
noneColor = color.new(color.white, 100)



_inRange(cond) =>
    bars = ta.barssince(cond == true)
    rangeLower <= bars and bars <= rangeUpper


_findDivRB(osc) =>
    // Osc: Higher Low
    oscHL = osc[lbR] > ta.valuewhen(plFound(osc), osc[lbR], 1) and _inRange(plFound(osc)[1])

    // Price: Lower Low
    priceLL = low[lbR] < ta.valuewhen(plFound(osc), low[lbR], 1)

    bullCond = plotBull and priceLL and oscHL and plFound(osc)


    //------------------------------------------------------------------------------
    // Hidden Bullish

    // Osc: Lower Low
    oscLL = osc[lbR] < ta.valuewhen(plFound(osc), osc[lbR], 1) and _inRange(plFound(osc)[1])

    // Price: Higher Low
    priceHL = low[lbR] > ta.valuewhen(plFound(osc), low[lbR], 1)

    hiddenBullCond = plotHiddenBull and priceHL and oscLL and plFound(osc)



    //------------------------------------------------------------------------------
    // Regular Bearish

    // Osc: Lower High
    oscLH = osc[lbR] < ta.valuewhen(phFound(osc), osc[lbR], 1) and _inRange(phFound(osc)[1])

    // Price: Higher High
    priceHH = high[lbR] > ta.valuewhen(phFound(osc), high[lbR], 1)

    bearCond = plotBear and priceHH and oscLH and phFound(osc)



    //------------------------------------------------------------------------------
    // Hidden Bearish

    // Osc: Higher High
    oscHH = osc[lbR] > ta.valuewhen(phFound(osc), osc[lbR], 1) and _inRange(phFound(osc)[1])

    // Price: Lower High
    priceLH = high[lbR] < ta.valuewhen(phFound(osc), high[lbR], 1)

    hiddenBearCond = plotHiddenBear and priceLH and oscHH and phFound(osc)

    [bullCond, hiddenBullCond, bearCond, hiddenBearCond]




[sz_bullCond, sz_hiddenBullCond, sz_bearCond, sz_hiddenBearCond] = _findDivRB(sz)


foundDivBSZ = plFound(sz) and show_Momen and show_div ? true : false
colordivBSZ = sz_bullCond ? bullColor : sz_hiddenBullCond ? hiddenBullColor : noneColor
foundDivBeSZ = phFound(sz) and show_Momen and show_div ? true : false
colordivBeSZ = sz_bearCond ? bearColor : sz_hiddenBearCond ? hiddenBearColor : noneColor



plot(foundDivBSZ ? sz[lbR] : na, offset=-lbR, title='Regular Bullish', linewidth=1, color=colordivBSZ)
plot(foundDivBeSZ ? sz[lbR] : na, offset=-lbR, title='Regular Bullish', linewidth=1, color=colordivBeSZ)


////////////////////RSI DIV//////////////////


[rsi_bullCond, rsi_hiddenBullCond, rsi_bearCond, rsi_hiddenBearCond] = _findDivRB(rsi_scale)


foundDivBRSI = plFound(rsi_scale) and show_rsi and show_div ? true : false
colordivBRSI = rsi_bullCond ? bullColor : rsi_hiddenBullCond ? hiddenBullColor : noneColor
foundDivBeRSI = phFound(rsi_scale) and show_rsi and show_div ? true : false
colordivBeRSI = rsi_bearCond ? bearColor : rsi_hiddenBearCond ? hiddenBearColor : noneColor



plot(foundDivBRSI ? rsi_scale[lbR] : na, offset=-lbR, title='Regular Bullish', linewidth=1, color=colordivBRSI)
plot(foundDivBeRSI ? rsi_scale[lbR] : na, offset=-lbR, title='Regular Bullish', linewidth=1, color=colordivBeRSI)




//Table 


tTable = table.new(TblY + '_' + TblX, columns=4, rows=4, border_width=1, border_color=color.rgb(255, 255, 255), frame_width=1, frame_color=color.rgb(255, 255, 255), bgcolor=color.gray)

if (barstate.islastconfirmedhistory or barstate.isrealtime) and showTable
// SMA table

//  X.GetTF( timeframe.period )
// 	X.GetTF( htf )
// 	X.GetTF( htf2 )
    table.cell(table_id=tTable, column=0, row=0, text='🎩',  text_color=color.rgb(0, 0, 0),text_size= labelSize)
    table.cell(table_id=tTable, column=1, row=0, text=X.CTF( str.tostring(resInMinutes) ),  text_color=color.rgb(0, 0, 0),text_size= labelSize)
    table.cell(table_id=tTable, column=0, row=1, text='MOM',  text_color=color.rgb(0, 0, 0),text_size= labelSize)
    if showAdxTbl
        table.cell(table_id=tTable, column=0, row=2, text='ADX',  text_color=color.rgb(0, 0, 0),text_size= labelSize)

    table.cell(table_id=tTable, column=1, row=1, text=iMom,  bgcolor=clr1, text_color=color.rgb(0, 0, 0),text_size= labelSize)
    if showAdxTbl
        table.cell(table_id=tTable, column=1, row=2, text=iAdx1,  bgcolor=clr1, text_color=color.rgb(0, 0, 0),text_size= labelSize)
    if mom2 or showMomInfo
        table.cell(table_id=tTable, column=2, row=0, text=X.CTF( htf ),  text_color=color.rgb(0, 0, 0),text_size= labelSize)
        table.cell(table_id=tTable, column=2, row=1, text=iMom2,  bgcolor=color2, text_color=color.rgb(0, 0, 0),text_size= labelSize)
        if showAdxTbl
            table.cell(table_id=tTable, column=2, row=2, text=iAdx2,  bgcolor=color2, text_color=color.rgb(0, 0, 0),text_size= labelSize)
        
    if mom3 or showMomInfo
        table.cell(table_id=tTable, column=3, row=0, text=X.CTF( htf2 ),  text_color=color.rgb(0, 0, 0),text_size= labelSize)
        table.cell(table_id=tTable, column=3, row=1, text=iMom3,  bgcolor=color3, text_color=color.rgb(0, 0, 0),text_size= labelSize)
        if showAdxTbl
            table.cell(table_id=tTable, column=3, row=2, text=iAdx3,  bgcolor=color3, text_color=color.rgb(0, 0, 0),text_size= labelSize)



