New Tradability by haji
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © hajjiplays
// This code is now available to the public. People may now copy and reuse the code in bigger projects. 
// However this script property of NoGoodLuck LLC, 
// and may not be copied or redistributed without credit to Elhadji Ouedrago or NoGoodLuck. 

//@version=5
indicator("New Tradability by haji", overlay = false)  
 
ha_t = ticker.heikinashi(syminfo.tickerid)
normal = ticker.new(syminfo.prefix, syminfo.tickerid)

tradabilitybar = "On"
fearbar = "On"
forceshow = "Greed"

areanumber = input.int(2, "True Area Lookback", group = "Settings", minval = 0)

backtracknumber = input.int(1999, "Backtest Bars", group = "Settings", minval = 0)
testingnumberMicro = input.int(1, "Micro Testing Number", group = "Settings Pattern", minval = 1)
testingnumberNormal = input.int(3, "Normal Testing Number", group = "Settings Pattern", minval = 1)
testingnumberMacro = input.int(8, "Macro Testing Number", group = "Settings Pattern", minval = 1)

testingshort = input.bool(true, "Use Short Time Frame")
testingnormal = input.bool(true, "Use Normal Time Frame")
testinglong = input.bool(true, "Use Long Time Frame")

abovebelow = input.string("Above", "Above or Below", ["Above", "Below"], group = "Main Alerts")
alertmewhen = input.string("None", "Alert Me When", ["None", "Pre-Active", "Active", "Mean Reversion"], group = "Main Alerts")
trendarea = input.int(0, title = "Trend Area Score Alert Value", group = "Main Alerts")
tradabilityvalue = input.int(0, title = "Tradability Score Alert Value", group = "Main Alerts")

microqualityalert = input.int(0, title = "Micro Quality Alert Value", group = "Specific Alerts")
qualityalert = input.int(0, title = "Quality Alert Value", group = "Specific Alerts")
macroqualityalert = input.int(0, title = "Macro Quality Alert Value", group = "Specific Alerts")

microtrendalert = input.int(0, title = "Micro Trend Alert Value", group = "Specific Alerts")
trendalert = input.int(0, title = "Trend Value", group = "Specific Alerts")
macrotrendalert = input.int(0, title = "Macro Trend Alert Value", group = "Specific Alerts")

andor = input.string("AND", "AND / OR", ["AND", "OR"], group = "Pattern Alerts")
consistencymicroScoreAlert = input.int(0, title = "Consistency Micro Score", group = "Pattern Alerts")
consistencynormalScoreAlert = input.int(0, title = "Consistency Normal Score", group = "Pattern Alerts")
consistencymacroScoreAlert = input.int(0, title = "Consistency Macro Score", group = "Pattern Alerts")

arrownumber = 4

patterns_short = '30'
patterns_normal = '60'
patterns_long = '240'

overlay = false
showdirection = true
useoldcal = true



timingg1 = false

useV4 = true

timingg = false

if timingg1 == true
    timingg := true


if overlay == false
    fearbar := "Off"
    showdirection := false


williamsalligator = " Williams Alligator is Mixed"
 


resshort = patterns_short
ha_openshort = request.security(ha_t, resshort, open)
ha_closeshort = request.security(ha_t, resshort, close)
ha_difshort = ha_openshort - ha_closeshort
iff_short = ha_difshort < 0 ? 2 : 3
ha_diffshort = ha_difshort > 0 ? 1 : iff_short

resnormal = patterns_normal
ha_opennormal = request.security(ha_t, resnormal, open)
ha_closenormal = request.security(ha_t, resnormal, close)
ha_difnormal = ha_opennormal - ha_closenormal
iff_normal = ha_difnormal < 0 ? 2 : 3
ha_diffnormal = ha_difnormal > 0 ? 1 : iff_normal

reslong = patterns_long
ha_openlong = request.security(ha_t, reslong, open)
ha_closelong = request.security(ha_t, reslong, close)
ha_diflong = ha_openlong - ha_closelong
iff_long = ha_diflong < 0 ? 2 : 3
ha_difflong = ha_diflong > 0 ? 1 : iff_long
 

res15 = '15'
ha_open15 = request.security(ha_t, res15, open)
ha_close15 = request.security(ha_t, res15, close)
ha_dif15 = ha_open15 - ha_close15
iff_15 = ha_dif15 < 0 ? 2 : 3
ha_diff15 = ha_dif15 > 0 ? 1 : iff_15


res60 = '60'
ha_open60 = request.security(ha_t, res60, open)
ha_close60 = request.security(ha_t, res60, close)
ha_dif60 = ha_open60 - ha_close60
iff_60 = ha_dif60 < 0 ? 2 : 3
ha_diff60 = ha_dif60 > 0 ? 1 : iff_60

res240 = '240'
ha_open240 = request.security(ha_t, res240, open)
ha_close240 = request.security(ha_t, res240, close)
ha_dif240 = ha_open240 - ha_close240
iff_240 = ha_dif240 < 0 ? 2 : 3
ha_diff240 = ha_dif240 > 0 ? 1 : iff_240


colorr = color.gray


oneclose200 = (request.security(syminfo.tickerid, "1", ta.sma(request.security(syminfo.tickerid, "1", close), 200)))
oneclose50 = (request.security(syminfo.tickerid, "1", ta.sma(request.security(syminfo.tickerid, "1", close), 50)))
oneclose9 = (request.security(syminfo.tickerid, "1", ta.sma(request.security(syminfo.tickerid, "1", close), 9)))

fifteenclose200 = (request.security(syminfo.tickerid, "15", ta.sma(request.security(syminfo.tickerid, "15", close), 200)))
fifteenclose50 = (request.security(syminfo.tickerid, "15", ta.sma(request.security(syminfo.tickerid, "15", close), 50)))
fifteenclose9 = (request.security(syminfo.tickerid, "15", ta.sma(request.security(syminfo.tickerid, "15", close), 9)))

thirtyclose200 = (request.security(syminfo.tickerid, "30", ta.sma(request.security(syminfo.tickerid, "30", close), 200)))
thirtyclose50 = (request.security(syminfo.tickerid, "30", ta.sma(request.security(syminfo.tickerid, "30", close), 50)))
thirtyclose9 = (request.security(syminfo.tickerid, "30", ta.sma(request.security(syminfo.tickerid, "30", close), 9)))

onehourclose200 = (request.security(syminfo.tickerid, "60", ta.sma(request.security(syminfo.tickerid, "60", close), 200)))
onehourclose50 = (request.security(syminfo.tickerid, "60", ta.sma(request.security(syminfo.tickerid, "60", close), 50)))
onehourclose9 = (request.security(syminfo.tickerid, "60", ta.sma(request.security(syminfo.tickerid, "60", close), 9)))


microquality = (((math.abs(close - oneclose200) * 8) + (math.abs(close - oneclose50) * 4) + (math.abs(close - oneclose9) * 2)) * 2)

quality = (((math.abs(close - fifteenclose200) * 8) + (math.abs(close - fifteenclose50) * 4) + (math.abs(close - fifteenclose9) * 2)))

macroquality = (((math.abs(close - thirtyclose200) * 8) + (math.abs(close - thirtyclose50) * 4) + (math.abs(close - thirtyclose9) * 2)) * 2) + (((math.abs(close - onehourclose200) * 8) + (math.abs(close - onehourclose50) * 4) + (math.abs(close - onehourclose9) * 2)) * 1)

currentmicroquality = microquality[0]
rankedmicroquality = 1
for i = 1 to 1000
    if microquality[i] > currentmicroquality
        rankedmicroquality := rankedmicroquality + 1

percentilemicro = ((rankedmicroquality - 1) * 100) / 1000

currentquality = quality[0]
rankedquality = 1
for i = 1 to 1000
    if quality[i] > currentquality
        rankedquality := rankedquality + 1

percentile = ((rankedquality - 1) * 100) / 1000

currentmacroquality = macroquality[0]
rankedmacroquality = 1
for i = 1 to 1000
    if macroquality[i] > currentmacroquality
        rankedmacroquality := rankedmacroquality + 1

percentilemacro = ((rankedmacroquality - 1) * 100) / 1000




currentAtr = ta.atr(2)[0]
rankedAtr = 1
if tradabilitybar == "On"
    for i = 1 to 1000
        if ta.atr(2)[i] > currentAtr
            rankedAtr := rankedAtr + 1

Volatility = 'Average'

ATRpercentile = (100 -  ((rankedAtr * 100)/1000))

lengthh = 15
mult = 2.0
lengthhKC = 20
multKC = 1.5

useTrueRange = true

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

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

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

val = ta.linreg(source - math.avg(math.avg(ta.highest(high, lengthhKC), ta.lowest(low, lengthhKC)), ta.sma(close, lengthhKC)), lengthhKC, 0)



max = 70
min = 1

overbought = 70
oversold   = 30

src2 = close

N = max-min+1
diff = nz(src2 - src2[1])

var num = array.new_float(N,0)
var den = array.new_float(N,0)

k = 0
overbuy = 0
oversell = 0
avgg = 0.
for i = min to max
    alpha = 1/i
    
    num_rma = alpha*diff + (1-alpha)*array.get(num,k)
    den_rma = alpha*math.abs(diff) + (1-alpha)*array.get(den,k)
    rsi = 50*num_rma/den_rma + 50
    avgg += rsi
    
    overbuy := rsi > overbought ? overbuy + 1 : overbuy
    oversell := rsi < oversold ? oversell + 1 : oversell
    
    array.set(num,k,num_rma)
    array.set(den,k,den_rma)
    k += 1
//----
avg_rsi = avgg/N
buy_rsi_ma = 0.
sell_rsi_ma = 0.

buy_rsi_ma := nz(buy_rsi_ma[1] + overbuy/N*(avg_rsi - buy_rsi_ma[1]),avg_rsi) 
sell_rsi_ma := nz(sell_rsi_ma[1] + oversell/N*(avg_rsi - sell_rsi_ma[1]),avg_rsi) 


mfivariable = 0.0

if ha_diff60 == 2
    if overbuy > 10
        mfivariable := -5

if ha_diff60 == 1
    if oversell > 10
        mfivariable := -5


if ha_diff60 == 2
    if oversell > 10
        mfivariable := 10

if ha_diff60 == 1
    if overbuy > 10
        mfivariable := 10

squeezevariable = 1.0



len = 34
src = close
ref = 13
sqzLen = 5

ma = ta.ema(src, len)
closema = close - ma
refma = ta.ema(src, ref)-ma
sqzma = ta.ema(src, sqzLen)-ma



if noSqz
    squeezevariable := 10
if sqzOn
    squeezevariable := -5

tradability = (((percentile * 2 ) + ATRpercentile) / 3) + (mfivariable + squeezevariable)


percentiletradability = tradability





lengthce = 4
multce = 2.0
showLabels = true
useClose = true
highlightState = true

atr = multce * ta.atr(lengthce)

longStop = (useClose ? ta.highest(close, lengthce) : ta.highest(lengthce)) - atr
longStopPrev = nz(longStop[1], longStop)
longStop := close[1] > longStopPrev ? math.max(longStop, longStopPrev) : longStop

shortStop = (useClose ? ta.lowest(close, lengthce) : ta.lowest(lengthce)) + atr
shortStopPrev = nz(shortStop[1], shortStop)
shortStop := close[1] < shortStopPrev ? math.min(shortStop, shortStopPrev) : shortStop

var int dir = 1
dir := close > shortStopPrev ? 1 : close < longStopPrev ? -1 : dir

lasttime = 0

lasttime2 = 0

if tradabilitybar == "On"
    if dir == 2
        for i = 1 to 100
            if dir[i] == 2
                lasttime += 5
            else
                break
    if dir == 1
        for i = 1 to 100
            if dir[i] == 1
                lasttime += 5
            else
                break

    if ha_diff60 == 2
        for i = 1 to 100
            if ha_diff60 == 1
                lasttime2 += 5
            else
                break
    if ha_diff60 == 1
        for i = 1 to 100
            if ha_diff60 == 2
                lasttime2 += 5
            else
                break

extra = (dir == 1 ? (math.abs(close - longStop)) : (math.abs(close - shortStop)))


madridvariable = 0.0

if closema>= sqzma and closema>=0 and dir == 1
    madridvariable := 10
if closema>= sqzma and closema>=0 and dir == -1
    madridvariable := -10

if closema<= sqzma and closema<=0 and dir == -1
    madridvariable := 10
if closema<= sqzma and closema<=0 and dir == 1
    madridvariable := -10


if closema>= sqzma and closema<=0 and dir == 1
    madridvariable := 5
if closema>= sqzma and closema<=0 and dir == -1
    madridvariable := -5

if closema<= sqzma and closema>=0 and dir == -1
    madridvariable := 5
if closema<= sqzma and closema>=0 and dir == 1
    madridvariable := -5



currentextra = extra[0]
rankedextra = 1

if tradabilitybar == "On"
    for i = 1 to 60
        if extra[i] > currentextra
            rankedextra := rankedextra + 1
 

percentileextra = (((rankedextra - 1) * 100) / 60) 

percentiletradabilityy = (macroquality * 8) + (percentile * 4) + (microquality * 2) / 14


tradabilitybarn = ((percentilemacro * 5) + (percentile * 3) + (percentilemicro * 2)) / 10

currenttradabilitybarn = tradabilitybarn[0]
rankedtradabilitybarn = 1
for i = 1 to 1000
    if tradabilitybarn[i] > currenttradabilitybarn
        rankedtradabilitybarn := rankedtradabilitybarn + 1

percentiletradabilitybarn = ((rankedtradabilitybarn - 1) * 100) / 1000

yellowthing = (tradabilitybarn) + ((mfivariable + squeezevariable + madridvariable) / 3)


yellowcolor = color.rgb(255, 235, 59, 60)

if yellowthing[1] > yellowthing
    yellowcolor := color.rgb(59, 59, 255, 60)


stoploss = (dir == 1 ? longStop : shortStop) 

[macd, signal, histob] = request.security(syminfo.tickerid, '240', ta.macd(close, 12, 26, 9))

bigtime = ''
mediumtime = ''
smalltime = ''

if histob > 0
    bigtime := '⬆'
if histob < 0
    bigtime := '⬇'

[macdm, signalm, histom] = ta.macd(close, 12, 26, 9)

if histom > 0
    mediumtime := '⬆'
if histom < 0 
    mediumtime := '⬇'


[macds, signals, histos] = request.security(syminfo.tickerid, '15', ta.macd(close, 12, 26, 9))

if histos > 0
    smalltime := '⬆'
if histos < 0
    smalltime := '⬇'

fifteen_minute_MFI = request.security(syminfo.tickerid, '15', ta.mfi(hlc3, 14))
one_hour_MFI = request.security(syminfo.tickerid, '240', ta.mfi(hlc3, 14))

if fifteen_minute_MFI > 80
    mediumtime := '⬇'
if fifteen_minute_MFI < 20
    mediumtime := '⬆'

if one_hour_MFI > 80
    bigtime := '⬇'
if one_hour_MFI < 20
    bigtime := '⬆'

defaultcolor = color.rgb(255, 255, 255, 100)

barinndercolor = color.gray
baroutercolor = color.white




tradenowcolor = color.rgb(120, 123, 134, 90)
if ha_diff60 == 2 and dir == 1 and williamsalligator == "Williams Alligator is Bullish ↑"
    tradenowcolor := color.rgb(20, 255, 48, 75)
if ha_diff60 == 1 and dir == -1 and williamsalligator == "Williams Alligator is Bearish ↓"
    tradenowcolor := color.rgb(255, 20, 20, 75)

if showdirection == false
    tradenowcolor := color.rgb(146, 146, 146, 75)

going = color.rgb(120, 123, 134, 70)

if ha_diff15 == 1
    going := color.rgb(255, 82, 82, 70)
if ha_diff15 == 2
    going := color.rgb(76, 175, 79, 70)



going15 = color.rgb(120, 123, 134, 70)

if ha_diff15 == 1
    going15 := color.rgb(255, 82, 82, 70)
if ha_diff15 == 2
    going15 := color.rgb(76, 175, 79, 70)

if showdirection == false
    going := color.rgb(76, 175, 79, 100) 
    going15 := color.rgb(76, 175, 79, 100)

hii = 6
whyy = 10
var stop = table.new(position.middle_center,51, 51, border_width = 0, border_color = color.rgb(255, 255, 255, 100), frame_color = color.rgb(255, 255, 255, 100), frame_width = 2)

if overlay == false
    table.set_position(stop, position.top_center)
if tradabilitybar == "On" and overlay == true
    for i = 1 to 29
        table.cell(stop, 2, i, '', bgcolor=color.rgb(55, 55, 56, 100), text_color = color.gray, height=hii, width=whyy)
    table.cell(stop, 2, 30, 'Tradability Score = '+ str.tostring(yellowthing, '#') + ' %   -   Stop Loss: ' + str.tostring(stoploss) + '  -  ' + williamsalligator, bgcolor=tradenowcolor, text_color = color.white, height=hii, width=whyy, text_size = size.tiny)
    table.cell(stop, 1, 30, ' ', bgcolor=going, text_color = color.white, height=hii, width=whyy, text_size = size.tiny)
    table.cell(stop, 3, 30, ' ', bgcolor=going15, text_color = color.white, height=hii, width=whyy, text_size = size.tiny)
if tradabilitybar == "On" and overlay == false
    hii := 9
    table.cell(stop, 2, 30, 'Tradability Score = '+ str.tostring(yellowthing, '#') + ' %   -   Stop Loss: ' + str.tostring(stoploss), bgcolor=tradenowcolor, text_color = color.white, height=hii, width=whyy, text_size = size.tiny)
    table.cell(stop, 1, 30, ' ', bgcolor=going, text_color = color.white, height=hii, width=whyy, text_size = size.tiny)
    table.cell(stop, 3, 30, ' ', bgcolor=going15, text_color = color.white, height=hii, width=whyy, text_size = size.tiny)


fgtimeframe = '45'

MACDTRADEBUY = smalltime == '⬆' and bigtime == '⬆' and mediumtime == '⬆'

MACDTRADESELL = smalltime == '⬇' and bigtime == '⬇' and mediumtime == '⬇'

fourhourHeikinAshibuy = ha_diff240 == 2
fourhourHeikinAshisell = ha_diff240 == 1

WilliamsAlligatorBuy = williamsalligator == "Williams Alligator is Bullish ↑"
WilliamsAlligatorSell = williamsalligator == "Williams Alligator is Bearish ↓"

ChandelierBuy = dir == 1
ChandelierSell = dir == -1

onehourHeikinAshiBuy = ha_diff60 == 2
onehourHeikinAshiSell = ha_diff60 == 1

MFIbuy = ta.mfi(close, 14)[2] <= 30
MFIsell = ta.mfi(close, 14)[2] >= 70







shortHeikinAshibuy = ha_diff15 == 2
shortHeikinAshisell = ha_diff15 == 1

shortMFIbuy = fifteen_minute_MFI < 30
shortMFIsell = fifteen_minute_MFI > 70

shortMACDbuy = histos > 0
shortMACDsell = histos < 0

shortpivotbuy = request.security(syminfo.tickerid, '15', ta.pivotlow(4, 2))
shortpivotsell = request.security(syminfo.tickerid, '15', ta.pivothigh(4, 2))

shortsarbuy = request.security(syminfo.tickerid, '15', ta.sar(0.02, 0.02,0.2))  < close
shortsarsell = request.security(syminfo.tickerid, '15', ta.sar(0.02, 0.02,0.2))  > close



normalHeikinAshibuy = ha_diff60 == 2
normalHeikinAshisell = ha_diff60 == 1

normalMFIbuy = ta.mfi(close, 14) < 30
normalMFIsell = ta.mfi(close, 14) > 70

normalMACDbuy = histom > 0
normalMACDsell = histom < 0

normalpivotbuy = ta.pivotlow(4, 2) or ta.pivotlow(4, 3)
normalpivotsell = ta.pivothigh(4, 2) or ta.pivothigh(4, 3)

normalsarbuy = ta.sar(0.02, 0.02,0.2)  < close
normalsarsell = ta.sar(0.02, 0.02,0.2)  > close


longHeikinAshibuy = ha_diff240 == 2
longHeikinAshisell = ha_diff240 == 1

longMFIbuy = one_hour_MFI < 30
longMFIsell = one_hour_MFI > 70

longMACDbuy = histob > 0
longMACDsell = histob < 0

longpivotbuy = request.security(syminfo.tickerid, '240', ta.pivotlow(4, 2)) or request.security(syminfo.tickerid, '240', ta.pivotlow(4, 3))
longpivotsell = request.security(syminfo.tickerid, '240', ta.pivothigh(4, 2)) or request.security(syminfo.tickerid, '240', ta.pivothigh(4, 3))

longsarbuy = request.security(syminfo.tickerid, '240', ta.sar(0.02, 0.02,0.2))  < close
longsarsell = request.security(syminfo.tickerid, '240', ta.sar(0.02, 0.02,0.2))  > close



shortbuyarea = 0
normalbuyarea = 0
longbuyarea = 0

shortsellarea = 0
normalsellarea = 0
longsellarea = 0

for i = 0 to areanumber
    if shortHeikinAshibuy[i]
        shortbuyarea += 1
    if shortMFIbuy[i]
        shortbuyarea += 1
    if shortMACDbuy[i]
        shortbuyarea += 1
    if shortpivotbuy[i]
        shortbuyarea += 1
    if shortsarbuy[i]
        shortbuyarea += 1

    if shortHeikinAshisell[i]
        shortsellarea += 1
    if shortMFIsell[i]
        shortsellarea += 1
    if shortMACDsell[i]
        shortsellarea += 1
    if shortpivotsell[i]
        shortsellarea += 1
    if shortsarsell[i]
        shortsellarea += 1


    if normalHeikinAshibuy[i]
        normalbuyarea += 1
    if normalMFIbuy[i]
        normalbuyarea += 1
    if normalMACDbuy[i]
        normalbuyarea += 1
    if normalpivotbuy[i]
        normalbuyarea += 1
    if normalsarbuy[i]
        normalbuyarea += 1

    if normalHeikinAshisell[i]
        normalsellarea += 1
    if normalMFIsell[i]
        normalsellarea += 1
    if normalMACDsell[i]
        normalsellarea += 1
    if normalpivotsell[i]
        normalsellarea += 1
    if normalsarsell[i]
        normalsellarea += 1

    if longHeikinAshibuy[i]
        longbuyarea += 1
    if longMFIbuy[i]
        longbuyarea += 1
    if longMACDbuy[i]
        longbuyarea += 1
    if longpivotbuy[i]
        longbuyarea += 1
    if longsarbuy[i]
        longbuyarea += 1

    if longHeikinAshisell[i]
        longsellarea += 1
    if longMFIsell[i]
        longsellarea += 1
    if longMACDsell[i]
        longsellarea += 1
    if longpivotsell[i]
        longsellarea += 1
    if longsarsell[i]
        longsellarea += 1

shortrendarea = 0
shortrendareacolor = color.gray
if shortbuyarea > shortsellarea
    shortrendarea := (shortbuyarea / (shortbuyarea + shortsellarea)) * 100
    shortrendareacolor := color.green
if shortsellarea > shortbuyarea
    shortrendarea := (shortsellarea / (shortbuyarea + shortsellarea)) * 100
    shortrendareacolor := color.red

shortcolor = color.rgb(120, 123, 134, 70)
if shortbuyarea / (shortbuyarea + shortsellarea) > 0.6
    shortcolor := color.rgb(76, 175, 79, 70)
if shortsellarea / (shortbuyarea + shortsellarea) > 0.6
    shortcolor := color.rgb(175, 76, 76, 70)

line1 = hline(15, '15', color.rgb(255, 255, 255, 100))
line2 = hline(11, '11', color.rgb(255, 255, 255, 100))
fill(line1, line2, color = shortcolor)

normalrendarea = 0
normalrendareacolor = color.gray
if normalbuyarea > normalsellarea
    normalrendarea := (normalbuyarea / (normalbuyarea + normalsellarea)) * 100
    normalrendareacolor := color.green
if normalsellarea > normalbuyarea
    normalrendarea := (normalsellarea / (normalbuyarea + normalsellarea)) * 100
    normalrendareacolor := color.red

normalcolor = color.rgb(120, 123, 134, 70)
if normalbuyarea / (normalbuyarea + normalsellarea) > 0.6
    normalcolor := color.rgb(76, 175, 79, 70)
if normalsellarea / (normalbuyarea + normalsellarea) > 0.6
    normalcolor := color.rgb(175, 76, 76, 70)

line3 = hline(10, '15', color.rgb(255, 255, 255, 100))
line4 = hline(6, '11', color.rgb(255, 255, 255, 100))
fill(line3, line4, color = normalcolor)

longcolor = color.rgb(120, 123, 134, 70)
if longbuyarea / (longbuyarea + longsellarea) > 0.6
    longcolor := color.rgb(76, 175, 79, 70)
if longsellarea / (longbuyarea + longsellarea) > 0.6
    longcolor := color.rgb(175, 76, 76, 70)

longrendarea = 0
longrendareacolor = color.gray
if longbuyarea > longsellarea
    longrendarea := (longbuyarea / (longbuyarea + longsellarea)) * 100
    longrendareacolor := color.green
if longsellarea > longbuyarea
    longrendarea := (longsellarea / (longbuyarea + longsellarea)) * 100
    longrendareacolor := color.red

line5 = hline(5, '15', color.rgb(255, 255, 255, 100))
line6 = hline(1, '11', color.rgb(255, 255, 255, 100))
fill(line5, line6, color = longcolor)

plot(15, color = shortHeikinAshibuy ? color.green : color.red, style = plot.style_line, linewidth = 1)

plot(14, color = shortMFIbuy ? color.green : na, style = plot.style_cross, linewidth = 1)
plot(14, color = shortMFIsell ? color.red : na, style = plot.style_cross, linewidth = 1)

plot(13, color = shortMACDbuy ? color.green : na, style = plot.style_stepline_diamond, linewidth = 1)
plot(13, color = shortMACDsell ? color.red : na, style = plot.style_stepline_diamond, linewidth = 1)

plot(12, color = shortpivotbuy ? color.green : na, style = plot.style_cross, linewidth = 1)
plot(12, color = shortpivotsell ? color.red : na, style = plot.style_cross, linewidth = 1)

plot(11, color = shortsarbuy ? color.green : na, style = plot.style_line, linewidth = 1)
plot(11, color = shortsarsell ? color.red : na, style = plot.style_line, linewidth = 1)

plot(10, color = normalHeikinAshibuy ? color.green : color.red, style = plot.style_line, linewidth = 1)

plot(9, color = normalMFIbuy ? color.green : na, style = plot.style_cross, linewidth = 1)
plot(9, color = normalMFIsell ? color.red : na, style = plot.style_cross, linewidth = 1)

plot(8, color = normalMACDbuy ? color.green : na, style = plot.style_stepline_diamond, linewidth = 1)
plot(8, color = normalMACDsell ? color.red : na, style = plot.style_stepline_diamond, linewidth = 1)

plot(7, color = normalpivotbuy ? color.green : na, style = plot.style_cross, linewidth = 1)
plot(7, color = normalpivotsell ? color.red : na, style = plot.style_cross, linewidth = 1)

plot(6, color = normalsarbuy ? color.green : na, style = plot.style_line, linewidth = 1)
plot(6, color = normalsarsell ? color.red : na, style = plot.style_line, linewidth = 1)

plot(5, color = longHeikinAshibuy ? color.green : color.red, style = plot.style_line, linewidth = 1)

plot(4, color = longMFIbuy ? color.green : na, style = plot.style_cross, linewidth = 1)
plot(4, color = longMFIsell ? color.red : na, style = plot.style_cross, linewidth = 1)

plot(3, color = longMACDbuy ? color.green : na, style = plot.style_stepline_diamond, linewidth = 1)
plot(3, color = longMACDsell ? color.red : na, style = plot.style_stepline_diamond, linewidth = 1)

plot(2, color = longpivotbuy ? color.green : na, style = plot.style_cross, linewidth = 1)
plot(2, color = longpivotsell ? color.red : na, style = plot.style_cross, linewidth = 1)

plot(1, color = longsarbuy ? color.green : na, style = plot.style_line, linewidth = 1)
plot(1, color = longsarsell ? color.red : na, style = plot.style_line, linewidth = 1)

plot(-5, color = color.rgb(255, 255, 255, 100))
plot(19, color = color.rgb(255, 255, 255, 100))

newcolor = color.rgb(120, 123, 134, 90)


microqualitydirection = ' --- '
qualitydirection = ' --- '
macroqualitydirection = ' --- '
ATRpercentiledirection = ' --- '
trueareadirection = ' --- '

if percentilemicro[arrownumber] < percentilemicro[0]
    microqualitydirection := '↑'
if percentilemicro[arrownumber] > percentilemicro[0]
    microqualitydirection := '↓'

if percentile[arrownumber] < percentile[0]
    qualitydirection := '↑'
if percentile[arrownumber] > percentile[0]
    qualitydirection := '↓'

if percentilemacro[arrownumber] < percentilemacro[0]
    macroqualitydirection := '↑'
if percentilemacro[arrownumber] > percentilemacro[0]
    macroqualitydirection := '↓'

if ATRpercentile[arrownumber] < ATRpercentile[0]
    ATRpercentiledirection := '↑'
if ATRpercentile[arrownumber] > ATRpercentile[0]
    ATRpercentiledirection := '↓'

activetrend = 'Mean Reversion' 
activetrendcolor = color.gray
barinndercolor := color.rgb(120, 123, 134, 50)
baroutercolor := color.gray

areatrend = 'None'

if longbuyarea > longsellarea
    areatrend := "Bullish"
if longbuyarea < longsellarea
    areatrend := "Bearish"

if (microqualitydirection == '↓' or qualitydirection == '↓') and ha_diff60 == 2
    activetrend := 'Bullish Potential'
    activetrendcolor := color.rgb(255, 255, 255)
    barinndercolor := color.rgb(255, 255, 255, 50)
    baroutercolor := color.rgb(255, 255, 255)

if (microqualitydirection == '↓' and qualitydirection == '↓') and ha_diff60 == 2 and close < ta.sma(close, 200)
    activetrend := 'Bullish Pre-Active'
    activetrendcolor := color.rgb(39, 155, 176)
    barinndercolor := color.rgb(39, 155, 176, 50)
    baroutercolor := color.rgb(39, 155, 176)

if (microqualitydirection == '↓' or qualitydirection == '↓') and ha_diff60 == 2 and close > ta.sma(close, 200)
    activetrend := 'Bullish Extension'
    activetrendcolor := color.rgb(127, 181, 129)
    barinndercolor := color.rgb(127, 181, 129, 50)
    baroutercolor := color.green

if (microqualitydirection == '↓' or qualitydirection == '↓') and ha_diff60 == 1
    activetrendcolor := color.rgb(255, 255, 255)
    barinndercolor := color.rgb(255, 255, 255, 50)
    baroutercolor := color.rgb(255, 255, 255)

if (microqualitydirection == '↓' and qualitydirection == '↓') and ha_diff60 == 1
    activetrend := 'Bearish Pre-Active'
    activetrendcolor := color.purple
    barinndercolor := color.rgb(155, 39, 176, 50)
    baroutercolor := color.purple

if (microqualitydirection == '↓' or qualitydirection == '↓') and ha_diff60 == 1 and close < ta.sma(close, 200)
    activetrend := 'Bearish Extension'
    activetrendcolor := color.rgb(255, 168, 168)
    barinndercolor := color.rgb(255, 168, 168, 50)
    baroutercolor := color.red

if macroqualitydirection == '↓' and (ha_diff60 == 2)
    activetrend := 'Bullish Active'
    activetrendcolor := color.green
    barinndercolor := color.rgb(76, 175, 79, 50)
    baroutercolor := color.green
if macroqualitydirection == '↓' and (ha_diff60 == 1)
    activetrend := 'Bearish Active'
    activetrendcolor := color.red
    barinndercolor := color.rgb(255, 82, 82, 50)
    baroutercolor := color.red

if microqualitydirection == '↑' and qualitydirection == '↑'
    activetrend := 'Mean Reversion' 
    activetrendcolor := color.gray
    barinndercolor := color.rgb(120, 123, 134, 50)
    baroutercolor := color.gray

statusquoshort = ""

if ha_diffshort[2] == 2
    statusquoshort += 'G'
else
    statusquoshort += 'R'
if ha_diffshort[1] == 2
    statusquoshort += 'G'
else
    statusquoshort += 'R'
if ha_diffshort[0] == 2
    statusquoshort += 'G'
else
    statusquoshort += 'R'

if testingshort == false
    statusquoshort := ""

statusquonormal = ""

if ha_diffnormal[2] == 2
    statusquonormal += 'G'
else
    statusquonormal += 'R'
if ha_diffnormal[1] == 2
    statusquonormal += 'G'
else
    statusquonormal += 'R'
if ha_diffnormal[0] == 2
    statusquonormal += 'G'
else
    statusquonormal += 'R'

if testingnormal== false
    statusquonormal := ""

statusquolong = ""

if ha_difflong[2] == 2
    statusquolong += 'G'
else
    statusquolong += 'R'
if ha_difflong[1] == 2
    statusquolong += 'G'
else
    statusquolong += 'R'
if ha_difflong[0] == 2
    statusquolong += 'G'
else
    statusquolong += 'R'

if testinglong == false
    statusquolong := ""

statusquo = statusquoshort + statusquonormal + statusquolong

PatternSuccessMicro = 0.0
PatternSuccessNormal = 0.0
PatternSuccessMacro = 0.0
PatternFailMicro = 0.0
PatternFailNormal = 0.0
PatternFailMacro = 0.0
BullishFrequencyMicro = 0.0
BearishFrequencyMicro = 0.0
BullishFrequencyNormal = 0.0
BearishFrequencyNormal = 0.0
BullishFrequencyMacro = 0.0
BearishFrequencyMacro = 0.0

if statusquo == statusquoshort + statusquonormal + statusquolong
    for i = testingnumberMacro to backtracknumber
        if statusquo[i] == statusquoshort + statusquonormal + statusquolong
            if close[i] < close[i - testingnumberMicro]
                BullishFrequencyMicro += 1
            if close[i] > close[i - testingnumberMicro]
                BearishFrequencyMicro += 1
            if close[i] < close[i - testingnumberNormal]
                BullishFrequencyNormal += 1
            if close[i] > close[i - testingnumberNormal]
                BearishFrequencyNormal += 1
            if close[i] < close[i - testingnumberMacro]
                BullishFrequencyMacro += 1
            if close[i] > close[i - testingnumberMacro]
                BearishFrequencyMacro += 1

textcolorMicro = color.gray
textcolorNormal = color.gray
textcolorMacro = color.gray

if BullishFrequencyMicro > BearishFrequencyMicro
    PatternSuccessMicro := BullishFrequencyMicro / (BullishFrequencyMicro + BearishFrequencyMicro)
    textcolorMicro := color.green
    
if BullishFrequencyMicro < BearishFrequencyMicro
    PatternSuccessMicro := BearishFrequencyMicro / (BullishFrequencyMicro + BearishFrequencyMicro)
    textcolorMicro := color.red

if BullishFrequencyNormal > BearishFrequencyNormal
    PatternSuccessNormal := BullishFrequencyNormal / (BullishFrequencyNormal + BearishFrequencyNormal)
    textcolorNormal := color.green
    
if BullishFrequencyNormal < BearishFrequencyNormal
    PatternSuccessNormal := BearishFrequencyNormal / (BullishFrequencyNormal + BearishFrequencyNormal)
    textcolorNormal := color.red

if BullishFrequencyMacro > BearishFrequencyMacro
    PatternSuccessMacro := BullishFrequencyMacro / (BullishFrequencyMacro + BearishFrequencyMacro)
    textcolorMacro := color.green
    
if BullishFrequencyMacro < BearishFrequencyMacro
    PatternSuccessMacro := BearishFrequencyMacro / (BullishFrequencyMacro + BearishFrequencyMacro)
    textcolorMacro := color.red


FrequencyOfPattern = (BullishFrequencyMicro + BearishFrequencyMicro)

FrequencyMicro = 0.0


if BullishFrequencyMicro > BearishFrequencyMicro
    FrequencyMicro := BullishFrequencyMicro
    textcolorMicro := color.green
else
    FrequencyMicro := BearishFrequencyMicro
    textcolorMicro := color.red

FrequencyNormal = 0.0

if BullishFrequencyNormal > BearishFrequencyNormal
    FrequencyNormal := BullishFrequencyNormal
    textcolorNormal := color.green
else
    FrequencyNormal := BearishFrequencyNormal
    textcolorNormal := color.red

FrequencyMacro = 0.0

if BullishFrequencyMacro > BearishFrequencyMacro
    FrequencyMacro := BullishFrequencyMacro
    textcolorMacro := color.green
else
    FrequencyMacro := BearishFrequencyMacro
    textcolorMacro := color.red



percentageMicro = (FrequencyMicro / FrequencyOfPattern) * 100
percentageNormal = (FrequencyNormal / FrequencyOfPattern) * 100
percentageMacro = (FrequencyMacro / FrequencyOfPattern) * 100



consistencymicroScore = ((BullishFrequencyMicro + 0) / (FrequencyOfPattern + (100))) * 100

consistencynormalScore = ((BullishFrequencyNormal + 0) / (FrequencyOfPattern + (100))) * 100

consistencymacroScore = ((BullishFrequencyMacro + 0) / (FrequencyOfPattern + (100))) * 100



needed = 0
buyarea = longbuyarea + normalbuyarea + shortbuyarea
sellarea = longsellarea + normalsellarea + shortsellarea


currentbuyarea = buyarea[0]
rankedbuyarea = 1

for i = 1 to 1000
    if buyarea[i] > currentbuyarea
        rankedbuyarea := rankedbuyarea + 1

percentilebuyarea = ((rankedbuyarea - 1) * 100) / 1000

currentsellarea = sellarea[0]
rankedsellarea = 1

for i = 1 to 1000
    if sellarea[i] > currentsellarea
        rankedsellarea := rankedsellarea + 1

percentilesellarea = ((rankedsellarea - 1) * 100) / 1000

patternpercentage = buyarea > sellarea ? percentilesellarea : percentilebuyarea



toughtnesscolor1 = defaultcolor
toughtnesscolor2 = defaultcolor
toughtnesscolor3 = defaultcolor
toughtnesscolor4 = defaultcolor
toughtnesscolor5 = defaultcolor
toughtnesscolor6 = defaultcolor
toughtnesscolor7 = defaultcolor
toughtnesscolor8 = defaultcolor
toughtnesscolor9 = defaultcolor
toughtnesscolor10 = defaultcolor

toughtnesscolor11 = defaultcolor
toughtnesscolor12 = defaultcolor
toughtnesscolor13 = defaultcolor
toughtnesscolor14 = defaultcolor
toughtnesscolor15 = defaultcolor
toughtnesscolor16 = defaultcolor
toughtnesscolor17 = defaultcolor
toughtnesscolor18 = defaultcolor
toughtnesscolor19 = defaultcolor
toughtnesscolor20 = defaultcolor

toughtnesscolor21 = defaultcolor
toughtnesscolor22 = defaultcolor
toughtnesscolor23 = defaultcolor
toughtnesscolor24 = defaultcolor
toughtnesscolor25 = defaultcolor
toughtnesscolor26 = defaultcolor
toughtnesscolor27 = defaultcolor
toughtnesscolor28 = defaultcolor
toughtnesscolor29 = defaultcolor
toughtnesscolor30 = defaultcolor

toughtnesscolor31 = defaultcolor
toughtnesscolor32 = defaultcolor
toughtnesscolor33 = defaultcolor
toughtnesscolor34 = defaultcolor
toughtnesscolor35 = defaultcolor
toughtnesscolor36 = defaultcolor
toughtnesscolor37 = defaultcolor
toughtnesscolor38 = defaultcolor
toughtnesscolor39 = defaultcolor
toughtnesscolor40 = defaultcolor

toughtnesscolor41 = defaultcolor
toughtnesscolor42 = defaultcolor
toughtnesscolor43 = defaultcolor
toughtnesscolor44 = defaultcolor
toughtnesscolor45 = defaultcolor
toughtnesscolor46 = defaultcolor
toughtnesscolor47 = defaultcolor
toughtnesscolor48 = defaultcolor
toughtnesscolor49 = defaultcolor
toughtnesscolor50 = defaultcolor

toughtnesscolor51 = defaultcolor
toughtnesscolor52 = defaultcolor
toughtnesscolor53 = defaultcolor
toughtnesscolor54 = defaultcolor
toughtnesscolor55 = defaultcolor
toughtnesscolor56 = defaultcolor
toughtnesscolor57 = defaultcolor
toughtnesscolor58 = defaultcolor
toughtnesscolor59 = defaultcolor
toughtnesscolor60 = defaultcolor

toughtnesscolor61 = defaultcolor
toughtnesscolor62 = defaultcolor
toughtnesscolor63 = defaultcolor
toughtnesscolor64 = defaultcolor
toughtnesscolor65 = defaultcolor
toughtnesscolor66 = defaultcolor
toughtnesscolor67 = defaultcolor
toughtnesscolor68 = defaultcolor
toughtnesscolor69 = defaultcolor
toughtnesscolor70 = defaultcolor

toughtnesscolor71 = defaultcolor
toughtnesscolor72 = defaultcolor
toughtnesscolor73 = defaultcolor
toughtnesscolor74 = defaultcolor
toughtnesscolor75 = defaultcolor
toughtnesscolor76 = defaultcolor
toughtnesscolor77 = defaultcolor
toughtnesscolor78 = defaultcolor
toughtnesscolor79 = defaultcolor
toughtnesscolor80 = defaultcolor

toughtnesscolor81 = defaultcolor
toughtnesscolor82 = defaultcolor
toughtnesscolor83 = defaultcolor
toughtnesscolor84 = defaultcolor
toughtnesscolor85 = defaultcolor
toughtnesscolor86 = defaultcolor
toughtnesscolor87 = defaultcolor
toughtnesscolor88 = defaultcolor
toughtnesscolor89 = defaultcolor
toughtnesscolor90 = defaultcolor

toughtnesscolor91 = defaultcolor
toughtnesscolor92 = defaultcolor
toughtnesscolor93 = defaultcolor
toughtnesscolor94 = defaultcolor
toughtnesscolor95 = defaultcolor
toughtnesscolor96 = defaultcolor
toughtnesscolor97 = defaultcolor
toughtnesscolor98 = defaultcolor
toughtnesscolor99 = defaultcolor

if tradabilitybar == "On"
    if yellowthing > 0
        toughtnesscolor1 := yellowcolor
    if yellowthing > 1
        toughtnesscolor2 := yellowcolor
    if yellowthing > 2
        toughtnesscolor3 := yellowcolor
    if yellowthing > 3
        toughtnesscolor4 := yellowcolor
    if yellowthing > 4
        toughtnesscolor5 := yellowcolor
    if yellowthing > 5
        toughtnesscolor6 := yellowcolor
    if yellowthing > 6
        toughtnesscolor7 := yellowcolor
    if yellowthing > 7
        toughtnesscolor8 := yellowcolor
    if yellowthing > 8
        toughtnesscolor9 := yellowcolor
    if yellowthing > 9
        toughtnesscolor10 := yellowcolor
    if yellowthing > 10
        toughtnesscolor11 := yellowcolor
    if yellowthing > 11
        toughtnesscolor12 := yellowcolor
    if yellowthing > 12
        toughtnesscolor13 := yellowcolor
    if yellowthing > 13
        toughtnesscolor14 := yellowcolor
    if yellowthing > 14
        toughtnesscolor15 := yellowcolor
    if yellowthing > 15
        toughtnesscolor16 := yellowcolor
    if yellowthing > 16
        toughtnesscolor17 := yellowcolor
    if yellowthing > 17
        toughtnesscolor18 := yellowcolor
    if yellowthing > 18
        toughtnesscolor19 := yellowcolor
    if yellowthing > 19
        toughtnesscolor20 := yellowcolor
    if yellowthing > 20
        toughtnesscolor21 := yellowcolor
    if yellowthing > 21
        toughtnesscolor22 := yellowcolor
    if yellowthing > 22
        toughtnesscolor23 := yellowcolor
    if yellowthing > 23
        toughtnesscolor24 := yellowcolor
    if yellowthing > 24
        toughtnesscolor25 := yellowcolor
    if yellowthing > 25
        toughtnesscolor26 := yellowcolor
    if yellowthing > 26
        toughtnesscolor27 := yellowcolor
    if yellowthing > 27
        toughtnesscolor28 := yellowcolor
    if yellowthing > 28
        toughtnesscolor29 := yellowcolor
    if yellowthing > 29
        toughtnesscolor30 := yellowcolor
    if yellowthing > 30
        toughtnesscolor31 := yellowcolor
    if yellowthing > 31
        toughtnesscolor32 := yellowcolor
    if yellowthing > 32
        toughtnesscolor33 := yellowcolor
    if yellowthing > 33
        toughtnesscolor34 := yellowcolor
    if yellowthing > 34
        toughtnesscolor35 := yellowcolor
    if yellowthing > 35
        toughtnesscolor36 := yellowcolor
    if yellowthing > 36
        toughtnesscolor37 := yellowcolor
    if yellowthing > 37
        toughtnesscolor38 := yellowcolor
    if yellowthing > 38
        toughtnesscolor39 := yellowcolor
    if yellowthing > 39
        toughtnesscolor40 := yellowcolor
    if yellowthing > 40
        toughtnesscolor41 := yellowcolor
    if yellowthing > 41
        toughtnesscolor42 := yellowcolor
    if yellowthing > 42
        toughtnesscolor43 := yellowcolor
    if yellowthing > 43
        toughtnesscolor44 := yellowcolor
    if yellowthing > 44
        toughtnesscolor45 := yellowcolor
    if yellowthing > 45
        toughtnesscolor46 := yellowcolor
    if yellowthing > 46
        toughtnesscolor47 := yellowcolor
    if yellowthing > 47
        toughtnesscolor48 := yellowcolor
    if yellowthing > 48
        toughtnesscolor49 := yellowcolor
    if yellowthing > 49
        toughtnesscolor50 := yellowcolor
    if yellowthing > 50
        toughtnesscolor51 := yellowcolor
    if yellowthing > 51
        toughtnesscolor52 := yellowcolor
    if yellowthing > 52
        toughtnesscolor53 := yellowcolor
    if yellowthing > 53
        toughtnesscolor54 := yellowcolor
    if yellowthing > 54
        toughtnesscolor54 := yellowcolor
    if yellowthing > 54
        toughtnesscolor55 := yellowcolor
    if yellowthing > 55
        toughtnesscolor56 := yellowcolor
    if yellowthing > 56
        toughtnesscolor57 := yellowcolor
    if yellowthing > 57
        toughtnesscolor58 := yellowcolor
    if yellowthing > 58
        toughtnesscolor59 := yellowcolor
    if yellowthing > 59
        toughtnesscolor60 := yellowcolor
    if yellowthing > 60
        toughtnesscolor61 := yellowcolor
    if yellowthing > 61
        toughtnesscolor62 := yellowcolor
    if yellowthing > 62
        toughtnesscolor63 := yellowcolor
    if yellowthing > 63
        toughtnesscolor64 := yellowcolor
    if yellowthing > 64
        toughtnesscolor65 := yellowcolor
    if yellowthing > 65
        toughtnesscolor66 := yellowcolor
    if yellowthing > 66
        toughtnesscolor67 := yellowcolor
    if yellowthing > 67
        toughtnesscolor68 := yellowcolor
    if yellowthing > 68
        toughtnesscolor69 := yellowcolor
    if yellowthing > 69
        toughtnesscolor70 := yellowcolor
    if yellowthing > 70
        toughtnesscolor71 := yellowcolor
    if yellowthing > 71
        toughtnesscolor72 := yellowcolor
    if yellowthing > 72
        toughtnesscolor73 := yellowcolor
    if yellowthing > 73
        toughtnesscolor74 := yellowcolor
    if yellowthing > 74
        toughtnesscolor75 := yellowcolor
    if yellowthing > 75
        toughtnesscolor76 := yellowcolor
    if yellowthing > 76
        toughtnesscolor77 := yellowcolor
    if yellowthing > 77
        toughtnesscolor78 := yellowcolor
    if yellowthing > 78
        toughtnesscolor79 := yellowcolor
    if yellowthing > 79
        toughtnesscolor80 := yellowcolor
    if yellowthing > 80
        toughtnesscolor81 := yellowcolor
    if yellowthing > 81
        toughtnesscolor82 := yellowcolor
    if yellowthing > 82
        toughtnesscolor83 := yellowcolor
    if yellowthing > 83
        toughtnesscolor84 := yellowcolor
    if yellowthing > 84
        toughtnesscolor85 := yellowcolor
    if yellowthing > 85
        toughtnesscolor86 := yellowcolor
    if yellowthing > 86
        toughtnesscolor87 := yellowcolor
    if yellowthing > 87
        toughtnesscolor88 := yellowcolor
    if yellowthing > 88
        toughtnesscolor89 := yellowcolor
    if yellowthing > 89
        toughtnesscolor90 := yellowcolor
    if yellowthing > 90
        toughtnesscolor91 := yellowcolor
    if yellowthing > 91
        toughtnesscolor92 := yellowcolor
    if yellowthing > 92
        toughtnesscolor93 := yellowcolor
    if yellowthing > 93
        toughtnesscolor94 := yellowcolor
    if yellowthing > 94
        toughtnesscolor95 := yellowcolor
    if yellowthing > 95
        toughtnesscolor96 := yellowcolor
    if yellowthing > 96
        toughtnesscolor97 := yellowcolor
    if yellowthing > 97
        toughtnesscolor98 := yellowcolor
    if yellowthing > 98
        toughtnesscolor99 := yellowcolor

    if tradabilitybarn > 0
        toughtnesscolor1 := barinndercolor
    if tradabilitybarn > 1
        toughtnesscolor2 := barinndercolor
    if tradabilitybarn > 2
        toughtnesscolor3 := barinndercolor
    if tradabilitybarn > 3
        toughtnesscolor4 := barinndercolor
    if tradabilitybarn > 4
        toughtnesscolor5 := barinndercolor
    if tradabilitybarn > 5
        toughtnesscolor6 := barinndercolor
    if tradabilitybarn > 6
        toughtnesscolor7 := barinndercolor
    if tradabilitybarn > 7
        toughtnesscolor8 := barinndercolor
    if tradabilitybarn > 8
        toughtnesscolor9 := barinndercolor
    if tradabilitybarn > 9
        toughtnesscolor10 := barinndercolor
    if tradabilitybarn > 10
        toughtnesscolor11 := barinndercolor
    if tradabilitybarn > 11
        toughtnesscolor12 := barinndercolor
    if tradabilitybarn > 12
        toughtnesscolor13 := barinndercolor
    if tradabilitybarn > 13
        toughtnesscolor14 := barinndercolor
    if tradabilitybarn > 14
        toughtnesscolor15 := barinndercolor
    if tradabilitybarn > 15
        toughtnesscolor16 := barinndercolor
    if tradabilitybarn > 16
        toughtnesscolor17 := barinndercolor
    if tradabilitybarn > 17
        toughtnesscolor18 := barinndercolor
    if tradabilitybarn > 18
        toughtnesscolor19 := barinndercolor
    if tradabilitybarn > 19
        toughtnesscolor20 := barinndercolor
    if tradabilitybarn > 20
        toughtnesscolor21 := barinndercolor
    if tradabilitybarn > 21
        toughtnesscolor22 := barinndercolor
    if tradabilitybarn > 22
        toughtnesscolor23 := barinndercolor
    if tradabilitybarn > 23
        toughtnesscolor24 := barinndercolor
    if tradabilitybarn > 24
        toughtnesscolor25 := barinndercolor
    if tradabilitybarn > 25
        toughtnesscolor26 := barinndercolor
    if tradabilitybarn > 26
        toughtnesscolor27 := barinndercolor
    if tradabilitybarn > 27
        toughtnesscolor28 := barinndercolor
    if tradabilitybarn > 28
        toughtnesscolor29 := barinndercolor
    if tradabilitybarn > 29
        toughtnesscolor30 := barinndercolor
    if tradabilitybarn > 30
        toughtnesscolor31 := barinndercolor
    if tradabilitybarn > 31
        toughtnesscolor32 := barinndercolor
    if tradabilitybarn > 32
        toughtnesscolor33 := barinndercolor
    if tradabilitybarn > 33
        toughtnesscolor34 := barinndercolor
    if tradabilitybarn > 34
        toughtnesscolor35 := barinndercolor
    if tradabilitybarn > 35
        toughtnesscolor36 := barinndercolor
    if tradabilitybarn > 36
        toughtnesscolor37 := barinndercolor
    if tradabilitybarn > 37
        toughtnesscolor38 := barinndercolor
    if tradabilitybarn > 38
        toughtnesscolor39 := barinndercolor
    if tradabilitybarn > 39
        toughtnesscolor40 := barinndercolor
    if tradabilitybarn > 40
        toughtnesscolor41 := barinndercolor
    if tradabilitybarn > 41
        toughtnesscolor42 := barinndercolor
    if tradabilitybarn > 42
        toughtnesscolor43 := barinndercolor
    if tradabilitybarn > 43
        toughtnesscolor44 := barinndercolor
    if tradabilitybarn > 44
        toughtnesscolor45 := barinndercolor
    if tradabilitybarn > 45
        toughtnesscolor46 := barinndercolor
    if tradabilitybarn > 46
        toughtnesscolor47 := barinndercolor
    if tradabilitybarn > 47
        toughtnesscolor48 := barinndercolor
    if tradabilitybarn > 48
        toughtnesscolor49 := barinndercolor
    if tradabilitybarn > 49
        toughtnesscolor50 := barinndercolor
    if tradabilitybarn > 50
        toughtnesscolor51 := barinndercolor
    if tradabilitybarn > 51
        toughtnesscolor52 := barinndercolor
    if tradabilitybarn > 52
        toughtnesscolor53 := barinndercolor
    if tradabilitybarn > 53
        toughtnesscolor54 := barinndercolor
    if tradabilitybarn > 54
        toughtnesscolor55 := barinndercolor
    if tradabilitybarn > 55
        toughtnesscolor56 := barinndercolor
    if tradabilitybarn > 56
        toughtnesscolor57 := barinndercolor
    if tradabilitybarn > 57
        toughtnesscolor58 := barinndercolor
    if tradabilitybarn > 58
        toughtnesscolor59 := barinndercolor
    if tradabilitybarn > 59
        toughtnesscolor60 := barinndercolor
    if tradabilitybarn > 60
        toughtnesscolor61 := barinndercolor
    if tradabilitybarn > 61
        toughtnesscolor62 := barinndercolor
    if tradabilitybarn > 62
        toughtnesscolor63 := barinndercolor
    if tradabilitybarn > 63
        toughtnesscolor64 := barinndercolor
    if tradabilitybarn > 64
        toughtnesscolor65 := barinndercolor
    if tradabilitybarn > 65
        toughtnesscolor66 := barinndercolor
    if tradabilitybarn > 66
        toughtnesscolor67 := barinndercolor
    if tradabilitybarn > 67
        toughtnesscolor68 := barinndercolor
    if tradabilitybarn > 68
        toughtnesscolor69 := barinndercolor
    if tradabilitybarn > 69
        toughtnesscolor70 := barinndercolor
    if tradabilitybarn > 70
        toughtnesscolor71 := barinndercolor
    if tradabilitybarn > 71
        toughtnesscolor72 := barinndercolor
    if tradabilitybarn > 72
        toughtnesscolor73 := barinndercolor
    if tradabilitybarn > 73
        toughtnesscolor74 := barinndercolor
    if tradabilitybarn > 74
        toughtnesscolor75 := barinndercolor
    if tradabilitybarn > 75
        toughtnesscolor76 := barinndercolor
    if tradabilitybarn > 76
        toughtnesscolor77 := barinndercolor
    if tradabilitybarn > 77
        toughtnesscolor78 := barinndercolor
    if tradabilitybarn > 78
        toughtnesscolor79 := barinndercolor
    if tradabilitybarn > 79
        toughtnesscolor80 := barinndercolor
    if tradabilitybarn > 80
        toughtnesscolor81 := barinndercolor
    if tradabilitybarn > 81
        toughtnesscolor82 := barinndercolor
    if tradabilitybarn > 82
        toughtnesscolor83 := barinndercolor
    if tradabilitybarn > 83
        toughtnesscolor84 := barinndercolor
    if tradabilitybarn > 84
        toughtnesscolor85 := barinndercolor
    if tradabilitybarn > 85
        toughtnesscolor86 := barinndercolor
    if tradabilitybarn > 86
        toughtnesscolor87 := barinndercolor
    if tradabilitybarn > 87
        toughtnesscolor88 := barinndercolor
    if tradabilitybarn > 88
        toughtnesscolor89 := barinndercolor
    if tradabilitybarn > 89
        toughtnesscolor90 := barinndercolor
    if tradabilitybarn > 90
        toughtnesscolor91 := barinndercolor
    if tradabilitybarn > 91
        toughtnesscolor92 := barinndercolor
    if tradabilitybarn > 92
        toughtnesscolor93 := barinndercolor
    if tradabilitybarn > 93
        toughtnesscolor94 := barinndercolor
    if tradabilitybarn > 94
        toughtnesscolor95 := barinndercolor
    if tradabilitybarn > 95
        toughtnesscolor96 := barinndercolor
    if tradabilitybarn > 96
        toughtnesscolor97 := barinndercolor
    if tradabilitybarn > 97
        toughtnesscolor98 := barinndercolor
    if tradabilitybarn > 98
        toughtnesscolor99 := barinndercolor


textcolorthing = color.black
if yellowthing < 50
    textcolorthing := barinndercolor

var toughness = table.new(position.bottom_center, 101, 2, border_width = 0, border_color = baroutercolor, frame_color = color.white, frame_width = 2)
hi = 9
why = 0.1

locationcolor = color.rgb(0, 0, 0, 100)


if tradabilitybar == "On"
    table.cell(toughness, 20, 0, ' ', bgcolor=toughtnesscolor20, height=hi, width=why)
    table.cell(toughness, 21, 0, ' ', bgcolor=toughtnesscolor21, height=hi, width=why)
    table.cell(toughness, 22, 0, ' ', bgcolor=toughtnesscolor22, height=hi, width=why)
    table.cell(toughness, 22, 0, ' ', bgcolor=toughtnesscolor22, height=hi, width=why)
    table.cell(toughness, 24, 0, ' ', bgcolor=toughtnesscolor24, height=hi, width=why)
    table.cell(toughness, 25, 0, ' ', bgcolor=toughtnesscolor25, height=hi, width=why)
    table.cell(toughness, 26, 0, ' ', bgcolor=toughtnesscolor26, height=hi, width=why)
    table.cell(toughness, 27, 0, ' ', bgcolor=toughtnesscolor27, height=hi, width=why)
    table.cell(toughness, 28, 0, ' ', bgcolor=toughtnesscolor28, height=hi, width=why)
    table.cell(toughness, 29, 0, ' ', bgcolor=toughtnesscolor29, height=hi, width=why)
    table.cell(toughness, 30, 0, ' ', bgcolor=toughtnesscolor40, height=hi, width=why)
    table.cell(toughness, 30, 0, ' ', bgcolor=toughtnesscolor30, height=hi, width=why)
    table.cell(toughness, 31, 0, ' ', bgcolor=toughtnesscolor31, height=hi, width=why)
    table.cell(toughness, 32, 0, ' ', bgcolor=toughtnesscolor32, height=hi, width=why)
    table.cell(toughness, 33, 0, ' ', bgcolor=toughtnesscolor33, height=hi, width=why)
    table.cell(toughness, 34, 0, ' ', bgcolor=toughtnesscolor34, height=hi, width=why)
    table.cell(toughness, 35, 0, ' ', bgcolor=toughtnesscolor35, height=hi, width=why)
    table.cell(toughness, 36, 0, ' ', bgcolor=toughtnesscolor36, height=hi, width=why)
    table.cell(toughness, 37, 0, ' ', bgcolor=toughtnesscolor37, height=hi, width=why)
    table.cell(toughness, 38, 0, ' ', bgcolor=toughtnesscolor38, height=hi, width=why)
    table.cell(toughness, 39, 0, ' ', bgcolor=toughtnesscolor39, height=hi, width=why)
    table.cell(toughness, 40, 0, ' ', bgcolor=toughtnesscolor40, height=hi, width=why)
    table.cell(toughness, 41, 0, ' ', bgcolor=toughtnesscolor41, height=hi, width=why)
    table.cell(toughness, 42, 0, ' ', bgcolor=toughtnesscolor42, height=hi, width=why)
    table.cell(toughness, 43, 0, ' ', bgcolor=toughtnesscolor43, height=hi, width=why)
    table.cell(toughness, 44, 0, ' ', bgcolor=toughtnesscolor44, height=hi, width=why)
    table.cell(toughness, 45, 0, ' ', bgcolor=toughtnesscolor45, height=hi, width=why)
    table.cell(toughness, 46, 0, ' ', bgcolor=toughtnesscolor46, height=hi, width=why)
    table.cell(toughness, 47, 0, ' ', bgcolor=toughtnesscolor47, height=hi, width=why)
    table.cell(toughness, 48, 0, ' ', bgcolor=toughtnesscolor48, height=hi, width=why)
    table.cell(toughness, 49, 0, ' ', bgcolor=toughtnesscolor49, height=hi, width=why)
    table.cell(toughness, 50, 0, ' ', bgcolor=toughtnesscolor50, height=hi, width=why)
    table.cell(toughness, 51, 0, ' ', bgcolor=toughtnesscolor51, height=hi, width=why)
    table.cell(toughness, 52, 0, ' ', bgcolor=toughtnesscolor52, height=hi, width=why)
    table.cell(toughness, 53, 0, ' ', bgcolor=toughtnesscolor53, height=hi, width=why)
    table.cell(toughness, 54, 0, ' ', bgcolor=toughtnesscolor54, height=hi, width=why)
    table.cell(toughness, 55, 0, ' ', bgcolor=toughtnesscolor55, height=hi, width=why)
    table.cell(toughness, 56, 0, ' ', bgcolor=toughtnesscolor56, height=hi, width=why)
    table.cell(toughness, 57, 0, ' ', bgcolor=toughtnesscolor57, height=hi, width=why)
    table.cell(toughness, 58, 0, ' ', bgcolor=toughtnesscolor58, height=hi, width=why)
    table.cell(toughness, 59, 0, str.tostring(yellowthing, '#') + ' %', bgcolor=toughtnesscolor59, height=hi, width=why, text_color = color.white)
    table.cell(toughness, 60, 0, ' ', bgcolor=toughtnesscolor60, height=hi, width=why)
    table.cell(toughness, 61, 0, ' ', bgcolor=toughtnesscolor61, height=hi, width=why)
    table.cell(toughness, 62, 0, ' ', bgcolor=toughtnesscolor62, height=hi, width=why)
    table.cell(toughness, 63, 0, ' ', bgcolor=toughtnesscolor63, height=hi, width=why)
    table.cell(toughness, 64, 0, ' ', bgcolor=toughtnesscolor64, height=hi, width=why)
    table.cell(toughness, 65, 0, ' ', bgcolor=toughtnesscolor65, height=hi, width=why)
    table.cell(toughness, 66, 0, ' ', bgcolor=toughtnesscolor66, height=hi, width=why)
    table.cell(toughness, 67, 0, ' ', bgcolor=toughtnesscolor67, height=hi, width=why)
    table.cell(toughness, 68, 0, ' ', bgcolor=toughtnesscolor68, height=hi, width=why)
    table.cell(toughness, 69, 0, ' ', bgcolor=toughtnesscolor69, height=hi, width=why)
    table.cell(toughness, 70, 0, ' ', bgcolor=toughtnesscolor70, height=hi, width=why)
    table.cell(toughness, 71, 0, ' ', bgcolor=toughtnesscolor71, height=hi, width=why)
    table.cell(toughness, 72, 0, ' ', bgcolor=toughtnesscolor72, height=hi, width=why)
    table.cell(toughness, 73, 0, ' ', bgcolor=toughtnesscolor73, height=hi, width=why)
    table.cell(toughness, 74, 0, ' ', bgcolor=toughtnesscolor74, height=hi, width=why)
    table.cell(toughness, 75, 0, ' ', bgcolor=toughtnesscolor75, height=hi, width=why)
    table.cell(toughness, 76, 0, ' ', bgcolor=toughtnesscolor76, height=hi, width=why)
    table.cell(toughness, 77, 0, ' ', bgcolor=toughtnesscolor77, height=hi, width=why)
    table.cell(toughness, 78, 0, ' ', bgcolor=toughtnesscolor78, height=hi, width=why)
    table.cell(toughness, 79, 0, ' ', bgcolor=toughtnesscolor79, height=hi, width=why)
    table.cell(toughness, 80, 0, ' ', bgcolor=toughtnesscolor80, height=hi, width=why)
    table.cell(toughness, 81, 0, ' ', bgcolor=toughtnesscolor81, height=hi, width=why)
    table.cell(toughness, 82, 0, ' ', bgcolor=toughtnesscolor82, height=hi, width=why)
    table.cell(toughness, 83, 0, ' ', bgcolor=toughtnesscolor83, height=hi, width=why)
    table.cell(toughness, 84, 0, ' ', bgcolor=toughtnesscolor84, height=hi, width=why)
    table.cell(toughness, 85, 0, ' ', bgcolor=toughtnesscolor85, height=hi, width=why)
    table.cell(toughness, 86, 0, ' ', bgcolor=toughtnesscolor86, height=hi, width=why)
    table.cell(toughness, 87, 0, ' ', bgcolor=toughtnesscolor87, height=hi, width=why)
    table.cell(toughness, 89, 0, ' ', bgcolor=toughtnesscolor89, height=hi, width=why)
    table.cell(toughness, 90, 0, ' ', bgcolor=toughtnesscolor90, height=hi, width=why)
    table.cell(toughness, 91, 0, ' ', bgcolor=toughtnesscolor91, height=hi, width=why)
    table.cell(toughness, 92, 0, ' ', bgcolor=toughtnesscolor92, height=hi, width=why)
    table.cell(toughness, 93, 0, ' ', bgcolor=toughtnesscolor93, height=hi, width=why)
    table.cell(toughness, 94, 0, ' ', bgcolor=toughtnesscolor94, height=hi, width=why)
    table.cell(toughness, 95, 0, ' ', bgcolor=toughtnesscolor95, height=hi, width=why)
    table.cell(toughness, 96, 0, ' ', bgcolor=toughtnesscolor96, height=hi, width=why)
    table.cell(toughness, 97, 0, ' ', bgcolor=toughtnesscolor97, height=hi, width=why)
    table.cell(toughness, 98, 0, ' ', bgcolor=toughtnesscolor98, height=hi, width=why)
    table.cell(toughness, 99, 0, ' ', bgcolor=toughtnesscolor99, height=hi, width=why) 
table.merge_cells(toughness, 59, 0, 61, 0)
table.set_frame_color(toughness, baroutercolor)

patterntrendcolor1 = defaultcolor
patterntrendcolor2 = defaultcolor
patterntrendcolor3 = defaultcolor
patterntrendcolor4 = defaultcolor
patterntrendcolor5 = defaultcolor
patterntrendcolor6 = defaultcolor
patterntrendcolor7 = defaultcolor
patterntrendcolor8 = defaultcolor
patterntrendcolor9 = defaultcolor
patterntrendcolor10 = defaultcolor

patterntrendcolor11 = defaultcolor
patterntrendcolor12 = defaultcolor
patterntrendcolor13 = defaultcolor
patterntrendcolor14 = defaultcolor
patterntrendcolor15 = defaultcolor
patterntrendcolor16 = defaultcolor
patterntrendcolor17 = defaultcolor
patterntrendcolor18 = defaultcolor
patterntrendcolor19 = defaultcolor
patterntrendcolor20 = defaultcolor

patterntrendcolor21 = defaultcolor
patterntrendcolor22 = defaultcolor
patterntrendcolor23 = defaultcolor
patterntrendcolor24 = defaultcolor
patterntrendcolor25 = defaultcolor
patterntrendcolor26 = defaultcolor
patterntrendcolor27 = defaultcolor
patterntrendcolor28 = defaultcolor
patterntrendcolor29 = defaultcolor
patterntrendcolor30 = defaultcolor

patterntrendcolor31 = defaultcolor
patterntrendcolor32 = defaultcolor
patterntrendcolor33 = defaultcolor
patterntrendcolor34 = defaultcolor
patterntrendcolor35 = defaultcolor
patterntrendcolor36 = defaultcolor
patterntrendcolor37 = defaultcolor
patterntrendcolor38 = defaultcolor
patterntrendcolor39 = defaultcolor
patterntrendcolor40 = defaultcolor

patterntrendcolor41 = defaultcolor
patterntrendcolor42 = defaultcolor
patterntrendcolor43 = defaultcolor
patterntrendcolor44 = defaultcolor
patterntrendcolor45 = defaultcolor
patterntrendcolor46 = defaultcolor
patterntrendcolor47 = defaultcolor
patterntrendcolor48 = defaultcolor
patterntrendcolor49 = defaultcolor
patterntrendcolor50 = defaultcolor

patterntrendcolor51 = defaultcolor
patterntrendcolor52 = defaultcolor
patterntrendcolor53 = defaultcolor
patterntrendcolor54 = defaultcolor
patterntrendcolor55 = defaultcolor
patterntrendcolor56 = defaultcolor
patterntrendcolor57 = defaultcolor
patterntrendcolor58 = defaultcolor
patterntrendcolor59 = defaultcolor
patterntrendcolor60 = defaultcolor

patterntrendcolor61 = defaultcolor
patterntrendcolor62 = defaultcolor
patterntrendcolor63 = defaultcolor
patterntrendcolor64 = defaultcolor
patterntrendcolor65 = defaultcolor
patterntrendcolor66 = defaultcolor
patterntrendcolor67 = defaultcolor
patterntrendcolor68 = defaultcolor
patterntrendcolor69 = defaultcolor
patterntrendcolor70 = defaultcolor

patterntrendcolor71 = defaultcolor
patterntrendcolor72 = defaultcolor
patterntrendcolor73 = defaultcolor
patterntrendcolor74 = defaultcolor
patterntrendcolor75 = defaultcolor
patterntrendcolor76 = defaultcolor
patterntrendcolor77 = defaultcolor
patterntrendcolor78 = defaultcolor
patterntrendcolor79 = defaultcolor
patterntrendcolor80 = defaultcolor

patterntrendcolor81 = defaultcolor
patterntrendcolor82 = defaultcolor
patterntrendcolor83 = defaultcolor
patterntrendcolor84 = defaultcolor
patterntrendcolor85 = defaultcolor
patterntrendcolor86 = defaultcolor
patterntrendcolor87 = defaultcolor
patterntrendcolor88 = defaultcolor
patterntrendcolor89 = defaultcolor
patterntrendcolor90 = defaultcolor

patterntrendcolor91 = defaultcolor
patterntrendcolor92 = defaultcolor
patterntrendcolor93 = defaultcolor
patterntrendcolor94 = defaultcolor
patterntrendcolor95 = defaultcolor
patterntrendcolor96 = defaultcolor
patterntrendcolor97 = defaultcolor
patterntrendcolor98 = defaultcolor
patterntrendcolor99 = defaultcolor

barinndercolor2 = color.gray
baroutercolor2 = color.gray

if buyarea >= sellarea
    barinndercolor2 := color.rgb(76, 175, 79, 50)
    baroutercolor2:= color.green
if buyarea < sellarea
    barinndercolor2 := color.rgb(255, 82, 82, 50)
    baroutercolor2:= color.red

if patternpercentage > 0
    patterntrendcolor1 := barinndercolor2
if patternpercentage > 1
    patterntrendcolor2 := barinndercolor2
if patternpercentage > 2
    patterntrendcolor3 := barinndercolor2
if patternpercentage > 3
    patterntrendcolor4 := barinndercolor2
if patternpercentage > 4
    patterntrendcolor5 := barinndercolor2
if patternpercentage > 5
    patterntrendcolor6 := barinndercolor2
if patternpercentage > 6
    patterntrendcolor7 := barinndercolor2
if patternpercentage > 7
    patterntrendcolor8 := barinndercolor2
if patternpercentage > 8
    patterntrendcolor9 := barinndercolor2
if patternpercentage > 9
    patterntrendcolor10 := barinndercolor2
if patternpercentage > 10
    patterntrendcolor11 := barinndercolor2
if patternpercentage > 11
    patterntrendcolor12 := barinndercolor2
if patternpercentage > 12
    patterntrendcolor13 := barinndercolor2
if patternpercentage > 13
    patterntrendcolor14 := barinndercolor2
if patternpercentage > 14
    patterntrendcolor15 := barinndercolor2
if patternpercentage > 15
    patterntrendcolor16 := barinndercolor2
if patternpercentage > 16
    patterntrendcolor17 := barinndercolor2
if patternpercentage > 17
    patterntrendcolor18 := barinndercolor2
if patternpercentage > 18
    patterntrendcolor19 := barinndercolor2
if patternpercentage > 19
    patterntrendcolor20 := barinndercolor2
if patternpercentage > 20
    patterntrendcolor21 := barinndercolor2
if patternpercentage > 21
    patterntrendcolor22 := barinndercolor2
if patternpercentage > 22
    patterntrendcolor23 := barinndercolor2
if patternpercentage > 23
    patterntrendcolor24 := barinndercolor2
if patternpercentage > 24
    patterntrendcolor25 := barinndercolor2
if patternpercentage > 25
    patterntrendcolor26 := barinndercolor2
if patternpercentage > 26
    patterntrendcolor27 := barinndercolor2
if patternpercentage > 27
    patterntrendcolor28 := barinndercolor2
if patternpercentage > 28
    patterntrendcolor29 := barinndercolor2
if patternpercentage > 29
    patterntrendcolor30 := barinndercolor2
if patternpercentage > 30
    patterntrendcolor31 := barinndercolor2
if patternpercentage > 31
    patterntrendcolor32 := barinndercolor2
if patternpercentage > 32
    patterntrendcolor33 := barinndercolor2
if patternpercentage > 33
    patterntrendcolor34 := barinndercolor2
if patternpercentage > 34
    patterntrendcolor35 := barinndercolor2
if patternpercentage > 35
    patterntrendcolor36 := barinndercolor2
if patternpercentage > 36
    patterntrendcolor37 := barinndercolor2
if patternpercentage > 37
    patterntrendcolor38 := barinndercolor2
if patternpercentage > 38
    patterntrendcolor39 := barinndercolor2
if patternpercentage > 39
    patterntrendcolor40 := barinndercolor2
if patternpercentage > 40
    patterntrendcolor41 := barinndercolor2
if patternpercentage > 41
    patterntrendcolor42 := barinndercolor2
if patternpercentage > 42
    patterntrendcolor43 := barinndercolor2
if patternpercentage > 43
    patterntrendcolor44 := barinndercolor2
if patternpercentage > 44
    patterntrendcolor45 := barinndercolor2
if patternpercentage > 45
    patterntrendcolor46 := barinndercolor2
if patternpercentage > 46
    patterntrendcolor47 := barinndercolor2
if patternpercentage > 47
    patterntrendcolor48 := barinndercolor2
if patternpercentage > 48
    patterntrendcolor49 := barinndercolor2
if patternpercentage > 49
    patterntrendcolor50 := barinndercolor2
if patternpercentage > 50
    patterntrendcolor51 := barinndercolor2
if patternpercentage > 51
    patterntrendcolor52 := barinndercolor2
if patternpercentage > 52
    patterntrendcolor53 := barinndercolor2
if patternpercentage > 53
    patterntrendcolor54 := barinndercolor2
if patternpercentage > 54
    patterntrendcolor55 := barinndercolor2
if patternpercentage > 55
    patterntrendcolor56 := barinndercolor2
if patternpercentage > 56
    patterntrendcolor57 := barinndercolor2
if patternpercentage > 57
    patterntrendcolor58 := barinndercolor2
if patternpercentage > 58
    patterntrendcolor59 := barinndercolor2
if patternpercentage > 59
    patterntrendcolor60 := barinndercolor2
if patternpercentage > 60
    patterntrendcolor61 := barinndercolor2
if patternpercentage > 61
    patterntrendcolor62 := barinndercolor2
if patternpercentage > 62
    patterntrendcolor63 := barinndercolor2
if patternpercentage > 63
    patterntrendcolor64 := barinndercolor2
if patternpercentage > 64
    patterntrendcolor65 := barinndercolor2
if patternpercentage > 65
    patterntrendcolor66 := barinndercolor2
if patternpercentage > 66
    patterntrendcolor67 := barinndercolor2
if patternpercentage > 67
    patterntrendcolor68 := barinndercolor2
if patternpercentage > 68
    patterntrendcolor69 := barinndercolor2
if patternpercentage > 69
    patterntrendcolor70 := barinndercolor2
if patternpercentage > 70
    patterntrendcolor71 := barinndercolor2
if patternpercentage > 71
    patterntrendcolor72 := barinndercolor2
if patternpercentage > 72
    patterntrendcolor73 := barinndercolor2
if patternpercentage > 73
    patterntrendcolor74 := barinndercolor2
if patternpercentage > 74
    patterntrendcolor75 := barinndercolor2
if patternpercentage > 75
    patterntrendcolor76 := barinndercolor2
if patternpercentage > 76
    patterntrendcolor77 := barinndercolor2
if patternpercentage > 77
    patterntrendcolor78 := barinndercolor2
if patternpercentage > 78
    patterntrendcolor79 := barinndercolor2
if patternpercentage > 79
    patterntrendcolor80 := barinndercolor2
if patternpercentage > 80
    patterntrendcolor81 := barinndercolor2
if patternpercentage > 81
    patterntrendcolor82 := barinndercolor2
if patternpercentage > 82
    patterntrendcolor83 := barinndercolor2
if patternpercentage > 83
    patterntrendcolor84 := barinndercolor2
if patternpercentage > 84
    patterntrendcolor85 := barinndercolor2
if patternpercentage > 85
    patterntrendcolor86 := barinndercolor2
if patternpercentage > 86
    patterntrendcolor87 := barinndercolor2
if patternpercentage > 87
    patterntrendcolor88 := barinndercolor2
if patternpercentage > 88
    patterntrendcolor89 := barinndercolor2
if patternpercentage > 89
    patterntrendcolor90 := barinndercolor2
if patternpercentage > 90
    patterntrendcolor91 := barinndercolor2
if patternpercentage > 91
    patterntrendcolor92 := barinndercolor2
if patternpercentage > 92
    patterntrendcolor93 := barinndercolor2
if patternpercentage > 93
    patterntrendcolor94 := barinndercolor2
if patternpercentage > 94
    patterntrendcolor95 := barinndercolor2
if patternpercentage > 95
    patterntrendcolor96 := barinndercolor2
if patternpercentage > 96
    patterntrendcolor97 := barinndercolor2
if patternpercentage > 97
    patterntrendcolor98 := barinndercolor2
if patternpercentage > 98
    patterntrendcolor99 := barinndercolor2



var patterntrend = table.new(position.middle_center, 101, 21, border_width = 0, border_color = baroutercolor, frame_color = color.white, frame_width = 2)



if tradabilitybar == "On"
    for i = 0 to 7
        table.cell(patterntrend, 20, i, ' ', bgcolor=color.rgb(255, 255, 255, 100), height=hi, width=why)
    table.cell(patterntrend, 20, 14, ' ', bgcolor=patterntrendcolor20, height=hi, width=why)
    table.cell(patterntrend, 21, 14, ' ', bgcolor=patterntrendcolor21, height=hi, width=why)
    table.cell(patterntrend, 22, 14, ' ', bgcolor=patterntrendcolor22, height=hi, width=why)
    table.cell(patterntrend, 23, 14, ' ', bgcolor=patterntrendcolor23, height=hi, width=why)
    table.cell(patterntrend, 24, 14, ' ', bgcolor=patterntrendcolor24, height=hi, width=why)
    table.cell(patterntrend, 25, 14, ' ', bgcolor=patterntrendcolor25, height=hi, width=why)
    table.cell(patterntrend, 26, 14, ' ', bgcolor=patterntrendcolor26, height=hi, width=why)
    table.cell(patterntrend, 27, 14, ' ', bgcolor=patterntrendcolor27, height=hi, width=why)
    table.cell(patterntrend, 28, 14, ' ', bgcolor=patterntrendcolor28, height=hi, width=why)
    table.cell(patterntrend, 29, 14, ' ', bgcolor=patterntrendcolor29, height=hi, width=why)
    table.cell(patterntrend, 30, 14, ' ', bgcolor=patterntrendcolor40, height=hi, width=why)
    table.cell(patterntrend, 30, 14, ' ', bgcolor=patterntrendcolor30, height=hi, width=why)
    table.cell(patterntrend, 31, 14, ' ', bgcolor=patterntrendcolor31, height=hi, width=why)
    table.cell(patterntrend, 32, 14, ' ', bgcolor=patterntrendcolor32, height=hi, width=why)
    table.cell(patterntrend, 33, 14, ' ', bgcolor=patterntrendcolor33, height=hi, width=why)
    table.cell(patterntrend, 34, 14, ' ', bgcolor=patterntrendcolor34, height=hi, width=why)
    table.cell(patterntrend, 35, 14, ' ', bgcolor=patterntrendcolor35, height=hi, width=why)
    table.cell(patterntrend, 36, 14, ' ', bgcolor=patterntrendcolor36, height=hi, width=why)
    table.cell(patterntrend, 37, 14, ' ', bgcolor=patterntrendcolor37, height=hi, width=why)
    table.cell(patterntrend, 38, 14, ' ', bgcolor=patterntrendcolor38, height=hi, width=why)
    table.cell(patterntrend, 39, 14, ' ', bgcolor=patterntrendcolor39, height=hi, width=why)
    table.cell(patterntrend, 40, 14, ' ', bgcolor=patterntrendcolor40, height=hi, width=why)
    table.cell(patterntrend, 41, 14, ' ', bgcolor=patterntrendcolor41, height=hi, width=why)
    table.cell(patterntrend, 42, 14, ' ', bgcolor=patterntrendcolor42, height=hi, width=why)
    table.cell(patterntrend, 43, 14, ' ', bgcolor=patterntrendcolor43, height=hi, width=why)
    table.cell(patterntrend, 44, 14, ' ', bgcolor=patterntrendcolor44, height=hi, width=why)
    table.cell(patterntrend, 45, 14, ' ', bgcolor=patterntrendcolor45, height=hi, width=why)
    table.cell(patterntrend, 46, 14, ' ', bgcolor=patterntrendcolor46, height=hi, width=why)
    table.cell(patterntrend, 47, 14, ' ', bgcolor=patterntrendcolor47, height=hi, width=why)
    table.cell(patterntrend, 48, 14, ' ', bgcolor=patterntrendcolor48, height=hi, width=why)
    table.cell(patterntrend, 49, 14, ' ', bgcolor=patterntrendcolor49, height=hi, width=why)
    table.cell(patterntrend, 50, 14, ' ', bgcolor=patterntrendcolor50, height=hi, width=why)
    table.cell(patterntrend, 51, 14, ' ', bgcolor=patterntrendcolor51, height=hi, width=why)
    table.cell(patterntrend, 52, 14, ' ', bgcolor=patterntrendcolor52, height=hi, width=why)
    table.cell(patterntrend, 53, 14, ' ', bgcolor=patterntrendcolor53, height=hi, width=why)
    table.cell(patterntrend, 54, 14, ' ', bgcolor=patterntrendcolor54, height=hi, width=why)
    table.cell(patterntrend, 55, 14, ' ', bgcolor=patterntrendcolor55, height=hi, width=why)
    table.cell(patterntrend, 56, 14, ' ', bgcolor=patterntrendcolor56, height=hi, width=why)
    table.cell(patterntrend, 57, 14, ' ', bgcolor=patterntrendcolor57, height=hi, width=why)
    table.cell(patterntrend, 58, 14, ' ', bgcolor=patterntrendcolor58, height=hi, width=why)
    table.cell(patterntrend, 59, 14, str.tostring(patternpercentage, '#') + ' %', bgcolor=patterntrendcolor59, height=hi, width=why, text_color = color.white)
    table.cell(patterntrend, 60, 14, ' ', bgcolor=patterntrendcolor60, height=hi, width=why)
    table.cell(patterntrend, 61, 14, ' ', bgcolor=patterntrendcolor61, height=hi, width=why)
    table.cell(patterntrend, 62, 14, ' ', bgcolor=patterntrendcolor62, height=hi, width=why)
    table.cell(patterntrend, 63, 14, ' ', bgcolor=patterntrendcolor63, height=hi, width=why)
    table.cell(patterntrend, 64, 14, ' ', bgcolor=patterntrendcolor64, height=hi, width=why)
    table.cell(patterntrend, 65, 14, ' ', bgcolor=patterntrendcolor65, height=hi, width=why)
    table.cell(patterntrend, 66, 14, ' ', bgcolor=patterntrendcolor66, height=hi, width=why)
    table.cell(patterntrend, 67, 14, ' ', bgcolor=patterntrendcolor67, height=hi, width=why)
    table.cell(patterntrend, 68, 14, ' ', bgcolor=patterntrendcolor68, height=hi, width=why)
    table.cell(patterntrend, 69, 14, ' ', bgcolor=patterntrendcolor69, height=hi, width=why)
    table.cell(patterntrend, 70, 14, ' ', bgcolor=patterntrendcolor70, height=hi, width=why)
    table.cell(patterntrend, 71, 14, ' ', bgcolor=patterntrendcolor71, height=hi, width=why)
    table.cell(patterntrend, 72, 14, ' ', bgcolor=patterntrendcolor72, height=hi, width=why)
    table.cell(patterntrend, 73, 14, ' ', bgcolor=patterntrendcolor73, height=hi, width=why)
    table.cell(patterntrend, 74, 14, ' ', bgcolor=patterntrendcolor74, height=hi, width=why)
    table.cell(patterntrend, 75, 14, ' ', bgcolor=patterntrendcolor75, height=hi, width=why)
    table.cell(patterntrend, 76, 14, ' ', bgcolor=patterntrendcolor76, height=hi, width=why)
    table.cell(patterntrend, 77, 14, ' ', bgcolor=patterntrendcolor77, height=hi, width=why)
    table.cell(patterntrend, 78, 14, ' ', bgcolor=patterntrendcolor78, height=hi, width=why)
    table.cell(patterntrend, 79, 14, ' ', bgcolor=patterntrendcolor79, height=hi, width=why)
    table.cell(patterntrend, 80, 14, ' ', bgcolor=patterntrendcolor80, height=hi, width=why)
    table.cell(patterntrend, 81, 14, ' ', bgcolor=patterntrendcolor81, height=hi, width=why)
    table.cell(patterntrend, 82, 14, ' ', bgcolor=patterntrendcolor82, height=hi, width=why)
    table.cell(patterntrend, 83, 14, ' ', bgcolor=patterntrendcolor83, height=hi, width=why)
    table.cell(patterntrend, 84, 14, ' ', bgcolor=patterntrendcolor84, height=hi, width=why)
    table.cell(patterntrend, 85, 14, ' ', bgcolor=patterntrendcolor85, height=hi, width=why)
    table.cell(patterntrend, 86, 14, ' ', bgcolor=patterntrendcolor86, height=hi, width=why)
    table.cell(patterntrend, 87, 14, ' ', bgcolor=patterntrendcolor87, height=hi, width=why)
    table.cell(patterntrend, 89, 14, ' ', bgcolor=patterntrendcolor89, height=hi, width=why)
    table.cell(patterntrend, 90, 14, ' ', bgcolor=patterntrendcolor90, height=hi, width=why)
    table.cell(patterntrend, 91, 14, ' ', bgcolor=patterntrendcolor91, height=hi, width=why)
    table.cell(patterntrend, 92, 14, ' ', bgcolor=patterntrendcolor92, height=hi, width=why)
    table.cell(patterntrend, 93, 14, ' ', bgcolor=patterntrendcolor93, height=hi, width=why)
    table.cell(patterntrend, 94, 14, ' ', bgcolor=patterntrendcolor94, height=hi, width=why)
    table.cell(patterntrend, 95, 14, ' ', bgcolor=patterntrendcolor95, height=hi, width=why)
    table.cell(patterntrend, 96, 14, ' ', bgcolor=patterntrendcolor96, height=hi, width=why)
    table.cell(patterntrend, 97, 14, ' ', bgcolor=patterntrendcolor97, height=hi, width=why)
    table.cell(patterntrend, 98, 14, ' ', bgcolor=patterntrendcolor98, height=hi, width=why)
    table.cell(patterntrend, 99, 14, ' ', bgcolor=patterntrendcolor99, height=hi, width=why) 
table.merge_cells(patterntrend, 59, 14, 61, 14)
table.set_frame_color(patterntrend, color.rgb(255, 255, 255, 100))

barinndercolor1 = color.new(barinndercolor, 80)


if useV4 and overlay == false
    for i = 0 to 8
        table.cell(stop, i, 30, '', bgcolor=barinndercolor1, text_color = color.white, height=hii, width=whyy, text_size = size.tiny)
    for i = 16 to 26
        table.cell(stop, i, 30, '', bgcolor=barinndercolor1, text_color = color.white, height=hii, width=whyy, text_size = size.tiny)
    table.cell(stop, 11, 30, 'Micro Quality: '+ str.tostring(percentilemicro, '#') + '  % ' + microqualitydirection, bgcolor=barinndercolor1, text_color = color.from_gradient(percentilemicro, 0, 100, color.blue, color.yellow), height=hii, width=whyy, text_size = size.tiny)
    table.cell(stop, 12, 30, 'Quality: '+ str.tostring(percentile, '#') + '  % ' + qualitydirection, bgcolor=barinndercolor1, text_color = color.from_gradient(percentile, 0, 100, color.blue, color.yellow), height=hii, width=whyy, text_size = size.tiny)
    table.cell(stop, 13, 30, 'Macro Quality: '+ str.tostring(percentilemacro, '#') + '  % ' + macroqualitydirection, bgcolor=barinndercolor1, text_color = color.from_gradient(percentilemacro, 0, 100, color.blue, color.yellow), height=hii, width=whyy, text_size = size.tiny)
    table.cell(stop, 14, 30, 'Micro Trend Area: '+ str.tostring(shortrendarea, '#') + ' %', bgcolor=barinndercolor1, text_color = shortrendareacolor, height=hii, width=whyy, text_size = size.tiny)
    table.cell(stop, 15, 30, 'Trend Area: '+ str.tostring(normalrendarea, '#') + ' %', bgcolor=barinndercolor1, text_color = normalrendareacolor, height=hii, width=whyy, text_size = size.tiny)
    table.cell(stop, 16, 30, 'Macro Trend Area: '+ str.tostring(longrendarea, '#') + ' %', bgcolor=barinndercolor1, text_color = longrendareacolor, height=hii, width=whyy, text_size = size.tiny)
    table.cell(stop, 17, 30, 'Phase: ' + activetrend, bgcolor=barinndercolor1, text_color = activetrendcolor, height=hii, width=whyy, text_size = size.tiny)
    table.cell(stop, 18, 30, 'Current Pattern Stats', bgcolor=barinndercolor1, text_color = color.white, height=hii, width=whyy, text_size = size.tiny)
    table.cell(stop, 18, 32, 'Micro Stats: ' + str.tostring(FrequencyMicro) + ' / ' + str.tostring(FrequencyOfPattern) + ' = ' + str.tostring(percentageMicro, '#') + '%', bgcolor=color.rgb(120, 123, 134, 95), text_color = textcolorMicro, height=hii, width=9, text_size = size.tiny)
    table.cell(stop, 18, 33, 'Normal Stats: ' + str.tostring(FrequencyNormal) + ' / ' + str.tostring(FrequencyOfPattern) + ' = ' + str.tostring(percentageNormal, '#') + '%', bgcolor=color.rgb(120, 123, 134, 95), text_color = textcolorNormal, height=hii, width=9, text_size = size.tiny)
    table.cell(stop, 18, 34, 'Macro Stats: ' + str.tostring(FrequencyMacro) + ' / ' + str.tostring(FrequencyOfPattern) + ' = ' + str.tostring(percentageMacro, '#') + '%', bgcolor=color.rgb(120, 123, 134, 95), text_color = textcolorMacro, height=hii, width=9, text_size = size.tiny)
    table.cell(stop, 18, 36, 'Consistancy Scores', bgcolor=barinndercolor1, text_color = color.white, height=hii, width=whyy, text_size = size.tiny)
    table.cell(stop, 18, 37, 'Micro Score: ' + str.tostring(consistencymicroScore, '#') + '%', bgcolor=color.rgb(120, 123, 134, 95), text_color = textcolorMicro, height=hii, width=9, text_size = size.tiny)
    table.cell(stop, 18, 38, 'Normal Score: ' + str.tostring(consistencynormalScore, '#') + '%', bgcolor=color.rgb(120, 123, 134, 95), text_color = textcolorNormal, height=hii, width=9, text_size = size.tiny)
    table.cell(stop, 18, 39, 'Macro Score: ' + str.tostring(consistencymacroScore, '#') + '%', bgcolor=color.rgb(120, 123, 134, 95), text_color = textcolorMacro, height=hii, width=9, text_size = size.tiny)



PHASEALERT = -1 > 1

if abovebelow == "Above"
    PHASEALERT := (alertmewhen == "Pre-Active" and (activetrend == "Bullish Pre-Active" or activetrend == "Bearish Pre-Active")) or (alertmewhen == "Active" and (activetrend == "Bullish Active" or activetrend == "Bearish Active")) or (alertmewhen == "Mean Reversion" and (activetrend == "Mean Reversion")) or alertmewhen == "None" and (patternpercentage >= trendarea) and (yellowthing >= tradabilityvalue) and (percentilemicro >= microqualityalert) and (percentile >= qualityalert) and (percentilemacro >= macroqualityalert) and (shortrendarea >= microtrendalert) and (normalrendarea >= trendalert) and (longrendarea >= macrotrendalert)

if abovebelow == "Below"
    PHASEALERT := (alertmewhen == "Pre-Active" and (activetrend == "Bullish Pre-Active" or activetrend == "Bearish Pre-Active")) or (alertmewhen == "Active" and (activetrend == "Bullish Active" or activetrend == "Bearish Active")) or (alertmewhen == "Mean Reversion" and (activetrend == "Mean Reversion")) or alertmewhen == "None" and (patternpercentage <= trendarea) and (yellowthing <= tradabilityvalue) and (percentilemicro <= microqualityalert) and (percentile <= qualityalert) and (percentilemacro <= macroqualityalert) and (shortrendarea <= microtrendalert) and (normalrendarea <= trendalert) and (longrendarea <= macrotrendalert)


PATTERNALERT = -1 > 1
if andor == "AND"
    PATTERNALERT := (consistencymicroScoreAlert >= consistencymicroScore) and (consistencynormalScoreAlert >= consistencynormalScore) and (consistencymacroScoreAlert >= consistencymacroScore)
if andor == "OR"
    PATTERNALERT := (consistencymicroScoreAlert >= consistencymicroScore) or (consistencynormalScoreAlert >= consistencynormalScore) or (consistencymacroScoreAlert >= consistencymacroScore)

alertcondition(PHASEALERT, "Alert Me When", "Alert for {{ticker}}")


plot(0, color = activetrendcolor, linewidth = 10, style = plot.style_stepline_diamond) 



