// Cor verde se MACD acima do sinal, vermelho caso contrário bbTrendColor = outMacD > outSignal ? color.green : color.red fill(uBB, lBB, color=color.new(bbTrendColor, 80))
// ───── SINAIS DE COMPRA/VENDA ───── proxCompra = input.float(1.1, "Fator de Proximidade BB (Compra)") proxVenda = input.float(0.9, "Fator de Proximidade BB (Venda)")
buySignal = ta.cross(outMacD, outSignal) and outMacD >= outSignal and close <= lowerBB * proxCompra sellSignal = ta.cross(outMacD, outSignal) and outMacD < outSignal and close >= upperBB * proxVenda
levelMethod = input.string( title = "Detection Method", defval = "Wick", options = ["Wick", "Body"], group = MAIN_GROUP)
leftBars = input.int( title = "Left Bars", defval = 20, group = MAIN_GROUP, inline = "pivot")
rightBars = input.int( title = "Right Bars", defval = 20, group = MAIN_GROUP, inline = "pivot")
retestLogic = input.bool( title = "Retest Weaker", defval = false, tooltip = "Retest makes level weaker vs retest makes level stronger", group = MAIN_GROUP)
flipsUntilDeletion = input.int( title = "Breakouts Until Level Deletes", defval = 1, group = DELETE_GROUP)
ageUntilDeletion = input.int( title = "Bars Until Level Deletes", defval = 300, group = DELETE_GROUP)
supportColor = input.color( title = "Initial Support Re-Test Color", defval = color.rgb(76, 175, 79, 50), group = COSMETIC_GROUP)
resistanceColor = input.color( title = "Initial Resistance Re-Test Color", defval = color.rgb(255, 82, 82, 50), group = COSMETIC_GROUP)
uniqueDeleteColor = input.color( title = "Unique Deletion Color", defval = color.rgb(120, 123, 134, 50), group = COSMETIC_GROUP)
changeColorMethod = input.string( title = "Change Color Method", defval = "Price Above/Below", options = ["Price Above/Below", "Fade Out Based On Age"], group = COSMETIC_GROUP)
lineWidth = input.int( title = "Line Width", defval = 10, group = COSMETIC_GROUP)
var firstPhaseHigh = array.new_float() var firstPhaseHighI = array.new_int() var firstPhaseLow = array.new_float() var firstPhaseLowI = array.new_int()
var secondPhaseHigh = array.new_float() var secondPhaseHighI = array.new_int() var secondPhaseLow = array.new_float() var secondPhaseLowI = array.new_int()
var flipManager = flipLevelManager.new( array.new_string(), array.new_line(), array.new_int(), array.new_int(), array.new_int())
if barstate.isconfirmed if newHigh array.push(firstPhaseHigh, pivotHigh) array.push(firstPhaseHighI, bar_index[rightBars])
if newLow array.push(firstPhaseLow, pivotLow) array.push(firstPhaseLowI, bar_index[rightBars])
for i = array.size(secondPhaseHigh) > 0 ? array.size(secondPhaseHigh) - 1 : na to 0 price = array.get(secondPhaseHigh, i) index = array.get(secondPhaseHighI, i)
if low <= price and close > price and close > open newLine = line.new( x1 = index, y1 = price, x2 = bar_index, y2 = price, xloc = xloc.bar_index, extend = extend.none, color = supportColor, width = lineWidth)
else if bar_index - index > 1000 or close < price array.remove(secondPhaseHigh, i) array.remove(secondPhaseHighI, i)
for i = array.size(secondPhaseLow) > 0 ? array.size(secondPhaseLow) - 1 : na to 0 price = array.get(secondPhaseLow, i) index = array.get(secondPhaseLowI, i)
if high >= price and close < price and close < open newLine = line.new( x1 = index, y1 = price, x2 = bar_index, y2 = price, xloc = xloc.bar_index, extend = extend.none, color = resistanceColor, width = lineWidth)
else if bar_index - index > 1000 or close > price array.remove(secondPhaseLow, i) array.remove(secondPhaseLowI, i)
for i = array.size(firstPhaseHigh) > 0 ? array.size(firstPhaseHigh) - 1 : na to 0 price = array.get(firstPhaseHigh, i) index = array.get(firstPhaseHighI, i)
if low > price array.push(secondPhaseHigh, price) array.push(secondPhaseHighI, index) array.remove(firstPhaseHigh, i) array.remove(firstPhaseHighI, i) else if bar_index - index > 1000 array.remove(firstPhaseHigh, i) array.remove(firstPhaseHighI, i)
for i = array.size(firstPhaseLow) > 0 ? array.size(firstPhaseLow) - 1 : na to 0 price = array.get(firstPhaseLow, i) index = array.get(firstPhaseLowI, i)
if high < price array.push(secondPhaseLow, price) array.push(secondPhaseLowI, index) array.remove(firstPhaseLow, i) array.remove(firstPhaseLowI, i) else if bar_index - index > 1000 array.remove(firstPhaseLow, i) array.remove(firstPhaseLowI, i)
for i = array.size(flipManager.lineArray) > 0 ? array.size(flipManager.lineArray) - 1 : na to 0 lineObject = array.get(flipManager.lineArray, i) lineAge = array.get(flipManager.lineAge, i) lineType = array.get(flipManager.variation, i) lineFlips = array.get(flipManager.lineFlips, i) linePhase = array.get(flipManager.linePhase, i)
lineColor = lineType == "Support" ? supportColor : resistanceColor gradientLineColor = color.from_gradient(lineAge, 0, ageUntilDeletion, lineColor, color.new(lineColor, 100)) newLineColor = changeColorMethod == "Fade Out Based On Age" ? gradientLineColor : close > linePrice ? supportColor : resistanceColor line.set_color(lineObject, newLineColor)
array.set(flipManager.lineAge, i, lineAge + 1)
if lineType == "Support"
if linePhase == 1 and close < linePrice array.set(flipManager.lineFlips, i, lineFlips + 1) array.set(flipManager.linePhase, i, -1) supportBrokeDownside := true else if linePhase == -1 and close > linePrice array.set(flipManager.lineFlips, i, lineFlips + 1) array.set(flipManager.linePhase, i, 1) supportBrokeUpside := true
if lineType == "Resistance"
if linePhase == -1 and close > linePrice array.set(flipManager.lineFlips, i, lineFlips + 1) array.set(flipManager.linePhase, i, 1) resistanceBrokeUpside := true else if linePhase == 1 and close < linePrice array.set(flipManager.lineFlips, i, lineFlips + 1) array.set(flipManager.linePhase, i, -1) resistanceBrokeDownside := true