RSI Overbought Oversold Divergence Strategy w/ Buy/Sell Signals
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// made by Imal_Max 
// thanks to The1IamNeo's and brandons idea
//
// thanks to JayTradingCharts RSI Divergence /w Alerts indicator for the base code. 

//@version=5
//
// 🔥 uncomment the line below to enable the alerts and disable the backtester
//indicator(title="Ultimate Bullish Divergence for RSI MFI RVSI OBV TRIXRSI w/ Buy Alerts", shorttitle="Ultimate Bull Divergence", format=format.price)//, timeframe="")

// 🔥 comment the line below to disable the backtester + uncomment the lines slightly below and at the bottom of the script
strategy(title="Ultimate Bullish Divergence for RSI MFI RVSI OBV TRIXRSI w/ Buy Signals", shorttitle="Ultimate Bull Divergence Backtester", overlay=false, pyramiding=1, process_orders_on_close=true, calc_on_every_tick=true, initial_capital=1000, currency = currency.USD, default_qty_value=100, default_qty_type=strategy.percent_of_equity, commission_type=strategy.commission.percent, commission_value=0.05, slippage=2)
selectOsc = input.string("RSI", title="Select Oscillator", group='═════════ Oscillator settings ══════════', options=["RSI","OBV","RVSI","MFI","TRIX RSI"], tooltip="RSI = Relative Strengh Index; OBV = On Balance Volume; RVSI = Relative Volume Strengh Index; MFI Money Flow Index; TRIX RSI = Tripple Exponetial Relative Strength")
src = input.source(title='Oscillator Source', defval=low, group='═════════ Oscillator settings ══════════', tooltip="RSI, RVSI, OBV and TRIX RSI default Source: Close; MFI default Source: hlc3")
len = input.int(title='Oscillator Length', minval=1, defval=14, group='═════════ Oscillator settings ══════════', tooltip="RSI/MFI default Period = 14; RVSI = 10; TRIX RSI = 9; OBV has no period.")
seclen = input.int(9, minval=1, title="Oscillator secondary length", group="═════════ Oscillator settings ══════════", tooltip="affects only RVSI and Trix RSI. For the TRIX RSI this defines the length of the TRIX. default value = 9 and for the RVSI it defines the Volume moving average length, default value = 5")
emalen = input(9, 'Oscillator Moving Average Length', group='═════════ Oscillator settings ══════════', tooltip="defines the length of the of the Oscillator moving average")

rangeLower = input.int(title='Min Lookback Range', defval=0, group='═════ Oscillator Divergence Settings ══════', inline='Input 0')
rangeUpper = input.int(title='Max Lookback', defval=200, group='═════ Oscillator Divergence Settings ══════', inline='Input 0', tooltip="reduces the Lookback range that you define with Pivot Lookback Left 1-30 ")


enableonlyOneLB = input.bool(title='Only use the first look back period', defval=false, group='═════ Oscillator Divergence Settings ══════')



                                                         
lbL1 = input.int(title='Pivot Lookback Left-1', defval=2, group='═════ Oscillator Divergence Settings ══════', inline='Input 1', tooltip="Amount of bars to look back to find pivot lows on the Oscillator and Price")
lbL2 = input.int(title='Lookback Left-2', defval=3, group='═════ Oscillator Divergence Settings ══════', inline='Input 1')
lbL3 = input.int(title='Pivot Lookback Left-3', defval=4, group='═════ Oscillator Divergence Settings ══════', inline='Input 2')
lbL4 = input.int(title='Lookback Left-4', defval=5, group='═════ Oscillator Divergence Settings ══════', inline='Input 2')
lbL5 = input.int(title='Pivot Lookback Left-5', defval=6, group='═════ Oscillator Divergence Settings ══════', inline='Input 2')
lbL6 = input.int(title='Lookback Left-6', defval=7, group='═════ Oscillator Divergence Settings ══════', inline='Input 2')
lbL7 = input.int(title='Pivot Lookback Left-7', defval=9, group='═════ Oscillator Divergence Settings ══════', inline='Input 2')
lbL8 = input.int(title='Lookback Left-8', defval=10, group='═════ Oscillator Divergence Settings ══════', inline='Input 2')
lbL9 = input.int(title='Pivot Lookback Left-9', defval=11, group='═════ Oscillator Divergence Settings ══════', inline='Input 2')
lbL10 = input.int(title='Lookback Left-10', defval=12, group='═════ Oscillator Divergence Settings ══════', inline='Input 2')
lbL11 = input.int(title='Pivot Lookback Left-11', defval=13, group='═════ Oscillator Divergence Settings ══════', inline='Input 2')
lbL12 = input.int(title='Lookback Left-12', defval=14, group='═════ Oscillator Divergence Settings ══════', inline='Input 2')
lbL13 = input.int(title='Pivot Lookback Left-13', defval=15, group='═════ Oscillator Divergence Settings ══════', inline='Input 2')
lbL14 = input.int(title='Lookback Left-14', defval=16, group='═════ Oscillator Divergence Settings ══════', inline='Input 2')
lbL15 = input.int(title='Pivot Lookback Left-15', defval=17, group='═════ Oscillator Divergence Settings ══════', inline='Input 2')
lbL16 = input.int(title='Lookback Left-16', defval=18, group='═════ Oscillator Divergence Settings ══════', inline='Input 2')
lbL17 = input.int(title='Pivot Lookback Left-17', defval=19, group='═════ Oscillator Divergence Settings ══════', inline='Input 2')
lbL18 = input.int(title='Lookback Left-18', defval=20, group='═════ Oscillator Divergence Settings ══════', inline='Input 2')
lbL19 = input.int(title='Pivot Lookback Left-19', defval=22, group='═════ Oscillator Divergence Settings ══════', inline='Input 2')
lbL20 = input.int(title='Lookback Left-20', defval=25, group='═════ Oscillator Divergence Settings ══════', inline='Input 2')
lbL21 = input.int(title='Pivot Lookback Left-21', defval=28, group='═════ Oscillator Divergence Settings ══════', inline='Input 2')
lbL22 = input.int(title='Lookback Left-22', defval=31, group='═════ Oscillator Divergence Settings ══════', inline='Input 2')
lbL23 = input.int(title='Pivot Lookback Left-23', defval=34, group='═════ Oscillator Divergence Settings ══════', inline='Input 2')
lbL24 = input.int(title='Lookback Left-24', defval=37, group='═════ Oscillator Divergence Settings ══════', inline='Input 2')
lbL25 = input.int(title='Pivot Lookback Left-25', defval=39, group='═════ Oscillator Divergence Settings ══════', inline='Input 2')
lbL26 = input.int(title='Lookback Left-26', defval=42, group='═════ Oscillator Divergence Settings ══════', inline='Input 2')
lbL27 = input.int(title='Pivot Lookback Left-27', defval=45, group='═════ Oscillator Divergence Settings ══════', inline='Input 2')
lbL28 = input.int(title='Lookback Left-28', defval=48, group='═════ Oscillator Divergence Settings ══════', inline='Input 2')
lbL29 = input.int(title='Pivot Lookback Left-29', defval=51, group='═════ Oscillator Divergence Settings ══════', inline='Input 2')
lbL30 = input.int(title='Lookback Left-30', defval=54, group='═════ Oscillator Divergence Settings ══════', inline='Input 2')


plotBull = input.bool(title='Plot Bullish', defval=true, group='═════ Oscillator Divergence Settings ══════', inline='Input 4', tooltip="Price makes a lower low while the Oscillator is making a higher low")
plotHiddenBull = input.bool(title='Plot Hidden Bullish', defval=false, group='═════ Oscillator Divergence Settings ══════', inline='Input 4', tooltip="Price makes a higher low while Oscillator is making a lower low")

///////////// Delay inputs


// Price bounce delay   
enablepriceBounceNeeded = input.bool(title='enable $ bounce delay', defval=false, group='═══════════ Signal Delays  ═══════════')
priceBounceNeeded = input.int(title='min $ bounce needed to flash buy', defval=35, group='═══════════ Signal Delays  ═══════════')
priceBounceNeededLbL = input.int(title='Amount of Candles until giving up', defval=3, minval=0, maxval=10, group='═══════════ Signal Delays  ═══════════')

// bars delay
lbR = input.int(title='Confirmation Time (bars) needed', minval=0, defval=0, group='Time Delay', tooltip='0 means the first candle - no candle close needed to flash a signal. Wait for a candle close to be sure (for alerts on the first cadle close set it to 0 and set the to "once per bar close")')

// EMA break delay
enableWaitForEmaBreak =  input.bool(title='enable wait for moving average break', defval=false, group='EMA break Delay', tooltip="waits for a break of the moving average to flash a signal")
WaitForEmaBreakLen = input(5, 'EMA Length', group='EMA break Delay', tooltip="defines the length of the moving average which it needs to break before a signal flashes")

// wait Oscillator and Oscillator EMA cross
enableOscCross = input.bool(title='enable wait for Oscillator and Oscillator EMA Cross', defval=false, group='Oscillator EMA Cross Delay', tooltip="")
oscCrosslookback = input.int(title='Amount of Candles until giving up', defval=3, minval=0, maxval=10, group='Oscillator EMA Cross Delay')




// ranges and filter

// only long on rising EMA inputs
enableOnlyLongUptrend =  input.bool(title='only long on in an uptrend (rising moving average)', defval=false, group='═══════════ Ranges & Filter ═══════════', tooltip="")
OnlyLongUptrendEmaLen = input(200, 'EMA Length to determine an uptrend', group='═══════════ Ranges & Filter ═══════════', tooltip="defines the length of the of the moving average")
EmaUptrend = ta.ema(src, OnlyLongUptrendEmaLen)
isInEMAUptrend = ta.change(EmaUptrend) > 0 or not enableOnlyLongUptrend

// ob/os divergence settings
enableRsiLimit = input.bool(title='enable Oversold limits', defval=false, group='divergence below OverSold', tooltip="only works with RSI, MFI and RVSI")
osvalue = input.int(title='only show Signals if the Oscillator was below', defval=30, group='divergence below OverSold', inline='Input 1')
oslookback = input.int(title='within the last (x candles)', defval=2, group='divergence below OverSold', inline='Input 1')
maxBullRSI = input.int(title='only show signals if the Oscillator is', defval=35, group='divergence below OverSold')



// manual ranges for divergence                                                           
enableManualbull1 = input.bool(title='enable manual Bullish Range-1', defval=false, group='══════ manual Range for divergence signal')
manualbullHigh1 = input.int(title='Range 1 - Bullish high', defval=10000000, group='══════ manual Range for divergence signal', inline='Input 0')
manualbullLow1 = input.int(title='Range 1 - Bullish low', defval=0, group='══════ manual Range for divergence signal', inline='Input 0')

enableManualbull2 = input.bool(title='enable manual Bullish Range-2', defval=false, group='══════ manual Range for divergence signal')
manualbullHigh2 = input.int(title='Range 2 - Bullish high', defval=10000000, group='══════ manual Range for divergence signal', inline='Input 1')
manualbullLow2 = input.int(title='Range 2 - Bullish low', defval=0, group='══════ manual Range for divergence signal', inline='Input 1')

enableManualbull3 = input.bool(title='enable manual Bullish Range-1', defval=false, group='══════ manual Range for divergence signal')
manualbullHigh3 = input.int(title='Range 3 - Bullish high', defval=10000000, group='══════ manual Range for divergence signal', inline='Input 2')
manualbullLow3 = input.int(title='Range 3 - Bullish low', defval=0, group='══════ manual Range for divergence signal', inline='Input 2')

enableManualbull4 = input.bool(title='enable manual Bullish Range-2', defval=false, group='══════ manual Range for divergence signal')
manualbullHigh4 = input.int(title='Range 4 - Bullish high', defval=10000000, group='══════ manual Range for divergence signal', inline='Input 3')
manualbullLow4 = input.int(title='Range 4 - Bullish low', defval=0, group='══════ manual Range for divergence signal', inline='Input 3')

// check if divergence happend within manual levels
isWithinLongRange1 = (close < manualbullHigh1 and close > manualbullLow1)
isWithinLongRange2 = (close < manualbullHigh2 and close > manualbullLow2)
isWithinLongRange3 = (close < manualbullHigh3 and close > manualbullLow3)
isWithinLongRange4 = (close < manualbullHigh4 and close > manualbullLow4)

isWithinLongRange = true
if enableManualbull1 or enableManualbull2 or enableManualbull3 or enableManualbull4
    isWithinLongRange := (isWithinLongRange1 and enableManualbull1) or (isWithinLongRange2 and enableManualbull2) or (isWithinLongRange3 and enableManualbull3) or (isWithinLongRange4 and enableManualbull4)


/////////////////////////// Get ranges to look for divergences from indicators

// EMA 

// EMA Bull range high
enableEmaBullHigh = input.bool(title='enable EMA as High of the Long Range', defval=false, group='══════ EMA Range for divergence signal')
emaBullHighLen = input.int(9, minval=1, title='Length', group='══════ EMA Range for divergence signal', inline='Input 4')
emaBullHighSrc = input(close, title='Source', group='══════ EMA Range for divergence signal', inline='Input 4')
emaBullHighDevi = input.int(title='deviation', defval=0, minval=-500, maxval=500, group='══════ EMA Range for divergence signal', inline='Input 4')
emaBullHighOut = ta.sma(emaBullHighSrc, emaBullHighLen)
isBelowEmaBullHigh = close < emaBullHighOut or not enableEmaBullHigh
// EMA Bull range low
enableEmaBullLow = input.bool(title='enable EMA as Low of the Long Range', defval=false, group='══════ EMA Range for divergence signal')
emaBullLowLen = input.int(55, minval=1, title='Length', group='══════ EMA Range for divergence signal', inline='Input 5')
emaBullLowSrc = input(close, title='Source', group='══════ EMA Range for divergence signal', inline='Input 5')
emaBullLowOffset = input.int(title='deviation', defval=0, minval=-500, maxval=500, group='══════ EMA Range for divergence signal', inline='Input 5')
emaBullLowOut = ta.sma(emaBullLowSrc, emaBullLowLen)
isBelowEmaBullLow = close > emaBullLowOut or not enableEmaBullLow

iswithinEmaRange = isBelowEmaBullHigh and isBelowEmaBullLow

// EMA END

// VWAP
enableVwapBullHigh = input.bool(title='enable VWAP as High of the Long Range', defval=false, group='════ VWAP Range for divergence signal')
enableVwapBullLow = input.bool(title='enable VWAP as Low of the Long Range', defval=false, group='════ VWAP Range for divergence signal')
enableVwapDiviBullHigh = input.bool(title='enable VWAP Upper Diviation as High of the Long Range', defval=false, group='════ VWAP Range for divergence signal')
enableVwapDiviBullLow = input.bool(title='enable VWAP Lower Diviation as Low of the Long Range', defval=false, group='════ VWAP Range for divergence signal')

computeVWAP(src, isNewPeriod, stDevMultiplier) =>
    var float sumSrcVol = na
    var float sumVol = na
    var float sumSrcSrcVol = na

    sumSrcVol := isNewPeriod ? src * volume : src * volume + sumSrcVol[1]
    sumVol := isNewPeriod ? volume : volume + sumVol[1]
    // sumSrcSrcVol calculates the dividend of the equation that is later used to calculate the standard deviation
    sumSrcSrcVol := isNewPeriod ? volume * math.pow(src, 2) : volume * math.pow(src, 2) + sumSrcSrcVol[1]

    _vwap = sumSrcVol / sumVol
    variance = sumSrcSrcVol / sumVol - math.pow(_vwap, 2)
    variance := variance < 0 ? 0 : variance
    stDev = math.sqrt(variance)

    lowerBand = _vwap - stDev * stDevMultiplier
    upperBand = _vwap + stDev * stDevMultiplier

    [_vwap, lowerBand, upperBand]

hideonDWM = false
var anchor = input.string(defval='Session', title='Anchor Period', options=['Session', 'Week', 'Month', 'Quarter', 'Year', 'Decade', 'Century', 'Earnings', 'Dividends', 'Splits'], group='VWAP Settings')
vsrc = input.source(title='Source', defval=hlc3, group='VWAP Settings')
offset = input.int(0, title='Offset', group='VWAP Settings')

stdevMult = input.float(1.0, title='Bands Standard Diviation Multiplier', group='VWAP Settings')

timeChange(period) =>
    ta.change(time(period))

new_earnings = request.earnings(syminfo.tickerid, earnings.actual, barmerge.gaps_on, barmerge.lookahead_on)
new_dividends = request.dividends(syminfo.tickerid, dividends.gross, barmerge.gaps_on, barmerge.lookahead_on)
new_split = request.splits(syminfo.tickerid, splits.denominator, barmerge.gaps_on, barmerge.lookahead_on)

isNewPeriod = anchor == 'Earnings' ? new_earnings : anchor == 'Dividends' ? new_dividends : anchor == 'Splits' ? new_split : na(vsrc[1]) ? true : anchor == 'Session' ? timeChange('D') : anchor == 'Week' ? timeChange('W') : anchor == 'Month' ? timeChange('M') : anchor == 'Quarter' ? timeChange('3M') : anchor == 'Year' ? timeChange('12M') : anchor == 'Decade' ? timeChange('12M') and year % 10 == 0 : anchor == 'Century' ? timeChange('12M') and year % 100 == 0 : false

float vwapValue = na
float std = na
float upperBandValue = na
float lowerBandValue = na
showBands = true
if not(hideonDWM and timeframe.isdwm)
    [_vwap, bottom, top] = computeVWAP(vsrc, isNewPeriod, stdevMult)
    vwapValue := _vwap
    upperBandValue := showBands ? top : na
    lowerBandValue := showBands ? bottom : na
    

// check if inside VWAP Range
isBelowVwap = vwapValue < close or not enableVwapBullHigh
isAboveVwap = vwapValue > close or not enableVwapBullLow
isAboveVwapLowerDivi = lowerBandValue < close or not enableVwapDiviBullLow
isBelowVwapUpperDivi = upperBandValue > close or not enableVwapDiviBullHigh

isWithinVwapRange = isBelowVwap and isAboveVwap and isAboveVwapLowerDivi and isBelowVwapUpperDivi


// VWAP END

// auto fib ranges

// fib fibbull range high                                                                                                                                    
enablefibbullHigh = input.bool(title='enable Auto fib as High of the Long Range', defval=false, group='════ auto fib range for divergence signal', inline='Input 0')
selectfibbullHigh = input.float(0.5, 'fib Level', options=[0, 0.236, 0.382, 0.5, 0.618, 0.702, 0.71, 0.786, 0.83, 0.886, 1], group='══ auto fib range for divergence signal')
fibbullHighLookback = input.int(title='Auto Fib Lookback', minval=1, defval=200, group='════ auto fib range for divergence signal', inline='Input 1')
//fibbullHighDivi = input.int(title='diviation in %', defval=5, group='══ auto fib range for divergence signal', inline='Input 1')
// fib fibbull range low
enablefibbullLow = input.bool(title='enable Auto fib as Low of the Long Range', defval=false, group='════ auto fib range for divergence signal', inline='Input 2')
selectfibbullLow = input.float(0.886, 'fib Level', options=[0, 0.236, 0.382, 0.5, 0.618, 0.702, 0.71, 0.786, 0.83, 0.886, 1], group='════ auto fib range for divergence signal')
fibbullLowhLookback = input.int(title='Auto Fib Lookback', minval=1, defval=200, group='════ auto fib range for divergence signal', inline='Input 3')
//fibbullLowhDivi = input.int(title='diviation in %', defval=5, group='══ auto fib range for divergence signal', inline='Input 3')


isBelowfibbullHigh = true
isBelowfibbullLow = true
fibLowPrice = 0.1
fibHighPrice = 0.1
fibHighLow = 0.1
fibHighHigh = 0.1
fibLowHigh = 0.1
fibLowLow = 0.1

if enablefibbullHigh
    fibHighHigh := ta.highest(high, fibbullHighLookback)
    fibHighLow := ta.lowest(low, fibbullHighLookback)
    fibHighPrice := (fibHighHigh - fibHighLow) * (1 - selectfibbullHigh) + fibHighLow //+ fibbullHighDivi
    isBelowfibbullHigh := fibHighPrice > ta.lowest(low, 2) or not enablefibbullHigh

if enablefibbullLow
    fibLowHigh := ta.highest(high, fibbullLowhLookback)
    fibLowLow := ta.lowest(low, fibbullLowhLookback)
    fibLowPrice := (fibLowHigh - fibLowLow) * (1 - selectfibbullLow) + fibLowLow// + fibbullLowhDivi
    isBelowfibbullLow := fibLowPrice < ta.highest(low, 2) or not enablefibbullLow
    

//plot(fibHighHigh)
//plot(fibHighLow)
//plot(fibHighPrice, color=color.orange)
//plot(fibLowPrice, color=color.orange)

isWithinFibRange =  isBelowfibbullHigh and isBelowfibbullLow
///////  auto fib END


/// Ranges checks
isWithinLongRanges = isWithinLongRange and iswithinEmaRange and isWithinVwapRange and isWithinFibRange and isInEMAUptrend



// divergence label colors
bullColor = color.purple
bullColor1 = color.aqua
bullColor2 = color.black
bullColor3 = color.blue
bullColor4 = color.fuchsia
bullColor5 = color.gray
bullColor6 = color.green
bullColor7 = color.lime
bullColor8 = color.maroon
bullColor9 = color.navy
bullColor10 = color.olive
bullColor11 = color.orange
bullColor12 = color.purple
bullColor13 = color.red
bullColor14 = color.silver
bullColor15 = color.teal
bullColor16 = color.blue
bullColor17 = color.navy
bullColor18 = color.purple
bullColor19 = color.purple
bullColor20 = color.red
bullColor21 = color.orange
bullColor22 = color.purple
bullColor23 = color.red
bullColor24 = color.silver
bullColor25 = color.teal
bullColor26 = color.lime
bullColor27 = color.fuchsia
bullColor28 = color.lime
bullColor29 = color.purple
bullColor30 = color.red
bullColor31 = color.aqua
bullColor32 = color.black
bullColor33 = color.blue
bullColor34 = color.fuchsia
bullColor35 = color.gray

hiddenBullColor = color.new(color.purple, 80)
textColor = color.white
noneColor = color.new(color.white, 100)


//// Oscillators

// set the var to RSI to make it work
osc = ta.rsi(src, len)

// calculate RSI
if selectOsc == "RSI"
    osc := ta.rsi(src, len)

// calculate RVSI
if selectOsc == "RVSI"
    nv = ta.cum(ta.change(src) * volume)
    av = ta.ema(nv, seclen)
    RVSI = ta.rsi(av, len)
    osc := RVSI
    
// getting OBV 
if selectOsc == "OBV"
    osc := ta.obv
    
// calculate MFI
if selectOsc == "MFI"
    upper = math.sum(volume * (ta.change(src) <= 0 ? 0 : src), len)
    lower = math.sum(volume * (ta.change(src) >= 0 ? 0 : src), len)
    osc := 100.0 - 100.0 / (1.0 + upper / lower)
    
if selectOsc == "TRIX RSI"
    trix = 10000 * ta.change(ta.ema(ta.ema(ta.ema(math.log(src), seclen), seclen), seclen))
    osc := ta.rsi(trix, len)


oscema = ta.ema(osc, emalen)

//// Oscillators


//////////////// Oscillator Conditions

// check if RSI was OS or OB recently
osLowestRsi = ta.lowest(osc, oslookback)
RSIwasBelowOS = osLowestRsi < osvalue or not enableRsiLimit

// check if RSI is below maxBullRSI and above minBearRSI
isBelowRsiBull = osc < maxBullRSI or not enableRsiLimit







/////////////////// plot ob os levels

osLevel = hline(osvalue, title='Oversold', linestyle=hline.style_dotted)
maxRSIline = hline(maxBullRSI, title='max RSI for Bull divergence', linestyle=hline.style_dotted)
//fill(osLevel, maxRSIline, title='Bull Zone Background', color=color.new(#4caf50, 90))
//RSI0line = hline(0, title='RSI 0 Line', linestyle=hline.style_dotted)
//RSI100line = hline(100, title='RSI 100 Line', linestyle=hline.style_dotted)
//fill(osLevel, RSI0line, title='Oversold Zone Background', color=color.new(#4caf50, 75))


////////////////////// plot Oscillator, histogram and Oscillator ema

plot(osc, title='Oscillator', linewidth=2, color=color.new(#00bcd4, 0))
plot(oscema, color=color.purple, title='Oscillator EMA', style=plot.style_circles)

//hist = osc - oscema
//plot(hist, title='Oscillator Histogram', style=plot.style_columns, color=hist >= 0 ? hist[1] < hist ? color.lime : color.purple : hist[1] < hist ? color.orange : color.red)

//////////////////// plot Oscillator, histogram and Oscillator ema END
   






//////////////// divergence stuff start

////// divergence Lookback period #1

// get pivots
plFound1 = na(ta.pivotlow(osc, lbL1, lbR)) ? false : true
_inRange1(cond) =>
    bars = ta.barssince(cond == true)
    rangeLower <= bars and bars <= rangeUpper


//------------------------------------------------------------------------------
// Regular Bullish
// Osc: Higher Low
oscHL1 = osc[lbR] > ta.valuewhen(plFound1, osc[lbR], 1) and _inRange1(plFound1[1])

// Price: Lower Low
priceLL1 = low[lbR] < ta.valuewhen(plFound1, low[lbR], 1)

bullCond1 = plotBull and priceLL1 and oscHL1 and plFound1 and RSIwasBelowOS and isWithinLongRanges

//plot(plFound1 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish', linewidth=2, color=bullCond1 ? bullColor : noneColor)
plotshape(bullCond1 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish 1 Label', text=' B1 ', style=shape.labelup, location=location.absolute, color=bullColor1, textcolor=textColor)


//------------------------------------------------------------------------------
// Hidden Bullish
// Osc: Lower Low
oscLL1 = osc[lbR] < ta.valuewhen(plFound1, osc[lbR], 1) and _inRange1(plFound1[1])

// Price: Higher Low
priceHL1 = low[lbR] > ta.valuewhen(plFound1, low[lbR], 1)

hiddenBullCond1 = plotHiddenBull and priceHL1 and oscLL1 and plFound1 and RSIwasBelowOS and isWithinLongRanges

//plot(plFound1 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish', linewidth=2, color=hiddenBullCond1 ? hiddenBullColor : noneColor)
plotshape(hiddenBullCond1 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish 1 Label', text=' H B1 ', style=shape.labelup, location=location.absolute, color=bullColor1, textcolor=textColor)

//------------------------------------------------------------------------------

////// divergence Lookback period #1 END


////// divergence Lookback period #2

// get pivots
plFound2 = na(ta.pivotlow(osc, lbL2, lbR)) ? false : true
_inRange2(cond) =>
    bars = ta.barssince(cond == true)
    rangeLower <= bars and bars <= rangeUpper


//------------------------------------------------------------------------------
// Regular Bullish
// Osc: Higher Low
oscHL2 = osc[lbR] > ta.valuewhen(plFound2, osc[lbR], 1) and _inRange2(plFound2[1])

// Price: Lower Low
priceLL2 = low[lbR] < ta.valuewhen(plFound2, low[lbR], 1)

bullCond2 = plotBull and priceLL2 and oscHL2 and plFound2 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound2 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish', linewidth=2, color=bullCond2 ? bullColor : noneColor)
plotshape(bullCond2 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish 2 Label', text=' B2 ', style=shape.labelup, location=location.absolute, color=bullColor2, textcolor=textColor)

//------------------------------------------------------------------------------
// Hidden Bullish
// Osc: Lower Low
oscLL2 = osc[lbR] < ta.valuewhen(plFound2, osc[lbR], 1) and _inRange2(plFound2[1])

// Price: Higher Low
priceHL2 = low[lbR] > ta.valuewhen(plFound2, low[lbR], 1)

hiddenBullCond2 = plotHiddenBull and priceHL2 and oscLL2 and plFound2 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound2 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish', linewidth=2, color=hiddenBullCond2 ? hiddenBullColor : noneColor)
plotshape(hiddenBullCond2 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish 2 Label', text=' H B2 ', style=shape.labelup, location=location.absolute, color=bullColor2, textcolor=textColor)

//------------------------------------------------------------------------------


////// divergence Lookback period #3

// get pivots
plFound3 = na(ta.pivotlow(osc, lbL3, lbR)) ? false : true
_inRange3(cond) =>
    bars = ta.barssince(cond == true)
    rangeLower <= bars and bars <= rangeUpper


//------------------------------------------------------------------------------
// Regular Bullish
// Osc: Higher Low
oscHL3 = osc[lbR] > ta.valuewhen(plFound3, osc[lbR], 1) and _inRange3(plFound3[1])

// Price: Lower Low
priceLL3 = low[lbR] < ta.valuewhen(plFound3, low[lbR], 1)

bullCond3 = plotBull and priceLL3 and oscHL3 and plFound3 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound3 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish', linewidth=2, color=bullCond3 ? bullColor : noneColor)
plotshape(bullCond3 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish 3 Label', text=' B3 ', style=shape.labelup, location=location.absolute, color=bullColor3, textcolor=textColor)

//------------------------------------------------------------------------------
// Hidden Bullish
// Osc: Lower Low
oscLL3 = osc[lbR] < ta.valuewhen(plFound3, osc[lbR], 1) and _inRange3(plFound3[1])

// Price: Higher Low
priceHL3 = low[lbR] > ta.valuewhen(plFound3, low[lbR], 1)

hiddenBullCond3 = plotHiddenBull and priceHL3 and oscLL3 and plFound3 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound3 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish', linewidth=2, color=hiddenBullCond3 ? hiddenBullColor : noneColor)
plotshape(hiddenBullCond3 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish 3 Label', text=' H B3 ', style=shape.labelup, location=location.absolute, color=bullColor3, textcolor=textColor)

//------------------------------------------------------------------------------

////// divergence Lookback period #3 END


////// divergence Lookback period #4

// get pivots
plFound4 = na(ta.pivotlow(osc, lbL4, lbR)) ? false : true
_inRange4(cond) =>
    bars = ta.barssince(cond == true)
    rangeLower <= bars and bars <= rangeUpper


//------------------------------------------------------------------------------
// Regular Bullish
// Osc: Higher Low
oscHL4 = osc[lbR] > ta.valuewhen(plFound4, osc[lbR], 1) and _inRange4(plFound4[1])

// Price: Lower Low
priceLL4 = low[lbR] < ta.valuewhen(plFound4, low[lbR], 1)

bullCond4 = plotBull and priceLL4 and oscHL4 and plFound4 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound4 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish', linewidth=2, color=bullCond4 ? bullColor : noneColor)
plotshape(bullCond4 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish 4 Label', text=' B4 ', style=shape.labelup, location=location.absolute, color=bullColor4, textcolor=textColor)

//------------------------------------------------------------------------------
// Hidden Bullish
// Osc: Lower Low
oscLL4 = osc[lbR] < ta.valuewhen(plFound4, osc[lbR], 1) and _inRange4(plFound4[1])

// Price: Higher Low
priceHL4 = low[lbR] > ta.valuewhen(plFound4, low[lbR], 1)

hiddenBullCond4 = plotHiddenBull and priceHL4 and oscLL4 and plFound4 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound4 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish', linewidth=2, color=hiddenBullCond4 ? hiddenBullColor : noneColor)
plotshape(hiddenBullCond4 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish 4 Label', text=' H B4 ', style=shape.labelup, location=location.absolute, color=bullColor4, textcolor=textColor)

//------------------------------------------------------------------------------

////// divergence Lookback period #4 END

////// divergence Lookback period #5

// get pivots
plFound5 = na(ta.pivotlow(osc, lbL5, lbR)) ? false : true
_inRange5(cond) =>
    bars = ta.barssince(cond == true)
    rangeLower <= bars and bars <= rangeUpper


//------------------------------------------------------------------------------
// Regular Bullish
// Osc: Higher Low
oscHL5 = osc[lbR] > ta.valuewhen(plFound5, osc[lbR], 1) and _inRange5(plFound5[1])

// Price: Lower Low
priceLL5 = low[lbR] < ta.valuewhen(plFound5, low[lbR], 1)

bullCond5 = plotBull and priceLL5 and oscHL5 and plFound5 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound5 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish', linewidth=2, color=bullCond5 ? bullColor : noneColor)
plotshape(bullCond5 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish 5 Label', text=' B5 ', style=shape.labelup, location=location.absolute, color=bullColor5, textcolor=textColor)

//------------------------------------------------------------------------------
// Hidden Bullish
// Osc: Lower Low
oscLL5 = osc[lbR] < ta.valuewhen(plFound5, osc[lbR], 1) and _inRange5(plFound5[1])

// Price: Higher Low
priceHL5 = low[lbR] > ta.valuewhen(plFound5, low[lbR], 1)

hiddenBullCond5 = plotHiddenBull and priceHL5 and oscLL5 and plFound5 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound5 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish', linewidth=2, color=hiddenBullCond5 ? hiddenBullColor : noneColor)
plotshape(hiddenBullCond5 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish 5 Label', text=' H B5 ', style=shape.labelup, location=location.absolute, color=bullColor5, textcolor=textColor)

//------------------------------------------------------------------------------


////// divergence Lookback period #5 END


////// divergence Lookback period #6

// get pivots
plFound6 = na(ta.pivotlow(osc, lbL6, lbR)) ? false : true
_inRange6(cond) =>
    bars = ta.barssince(cond == true)
    rangeLower <= bars and bars <= rangeUpper


//------------------------------------------------------------------------------
// Regular Bullish
// Osc: Higher Low
oscHL6 = osc[lbR] > ta.valuewhen(plFound6, osc[lbR], 1) and _inRange6(plFound6[1])

// Price: Lower Low
priceLL6 = low[lbR] < ta.valuewhen(plFound6, low[lbR], 1)

bullCond6 = plotBull and priceLL6 and oscHL6 and plFound6 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound6 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish', linewidth=2, color=bullCond6 ? bullColor : noneColor)
plotshape(bullCond6 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish 6 Label', text=' B6 ', style=shape.labelup, location=location.absolute, color=bullColor6, textcolor=textColor)

//------------------------------------------------------------------------------
// Hidden Bullish
// Osc: Lower Low
oscLL6 = osc[lbR] < ta.valuewhen(plFound6, osc[lbR], 1) and _inRange6(plFound6[1])

// Price: Higher Low
priceHL6 = low[lbR] > ta.valuewhen(plFound6, low[lbR], 1)

hiddenBullCond6 = plotHiddenBull and priceHL6 and oscLL6 and plFound6 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound6 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish', linewidth=2, color=hiddenBullCond6 ? hiddenBullColor : noneColor)
plotshape(hiddenBullCond6 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish 6 Label', text=' H B6 ', style=shape.labelup, location=location.absolute, color=bullColor6, textcolor=textColor)

//------------------------------------------------------------------------------

////// divergence Lookback period #6 END


////// divergence Lookback period #7

// get pivots
plFound7 = na(ta.pivotlow(osc, lbL7, lbR)) ? false : true
_inRange7(cond) =>
    bars = ta.barssince(cond == true)
    rangeLower <= bars and bars <= rangeUpper


//------------------------------------------------------------------------------
// Regular Bullish
// Osc: Higher Low
oscHL7 = osc[lbR] > ta.valuewhen(plFound7, osc[lbR], 1) and _inRange7(plFound7[1])

// Price: Lower Low
priceLL7 = low[lbR] < ta.valuewhen(plFound7, low[lbR], 1)

bullCond7 = plotBull and priceLL7 and oscHL7 and plFound7 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound7 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish', linewidth=2, color=bullCond7 ? bullColor : noneColor)
plotshape(bullCond7 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish 7 Label', text=' B7 ', style=shape.labelup, location=location.absolute, color=bullColor7, textcolor=textColor)

//------------------------------------------------------------------------------
// Hidden Bullish
// Osc: Lower Low
oscLL7 = osc[lbR] < ta.valuewhen(plFound7, osc[lbR], 1) and _inRange7(plFound7[1])

// Price: Higher Low
priceHL7 = low[lbR] > ta.valuewhen(plFound7, low[lbR], 1)

hiddenBullCond7 = plotHiddenBull and priceHL7 and oscLL7 and plFound7 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound7 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish', linewidth=2, color=hiddenBullCond7 ? hiddenBullColor : noneColor)
plotshape(hiddenBullCond7 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish 7 Label', text=' H B7 ', style=shape.labelup, location=location.absolute, color=bullColor7, textcolor=textColor)

//------------------------------------------------------------------------------

////// divergence Lookback period #7 END


////// divergence Lookback period #8

// get pivots
plFound8 = na(ta.pivotlow(osc, lbL8, lbR)) ? false : true
_inRange8(cond) =>
    bars = ta.barssince(cond == true)
    rangeLower <= bars and bars <= rangeUpper


//------------------------------------------------------------------------------
// Regular Bullish
// Osc: Higher Low
oscHL8 = osc[lbR] > ta.valuewhen(plFound8, osc[lbR], 1) and _inRange8(plFound8[1])

// Price: Lower Low
priceLL8 = low[lbR] < ta.valuewhen(plFound8, low[lbR], 1)

bullCond8 = plotBull and priceLL8 and oscHL8 and plFound8 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound8 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish', linewidth=2, color=bullCond8 ? bullColor : noneColor)
plotshape(bullCond8 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish 8 Label', text=' B8 ', style=shape.labelup, location=location.absolute, color=bullColor8, textcolor=textColor)

//------------------------------------------------------------------------------
// Hidden Bullish
// Osc: Lower Low
oscLL8 = osc[lbR] < ta.valuewhen(plFound8, osc[lbR], 1) and _inRange8(plFound8[1])

// Price: Higher Low
priceHL8 = low[lbR] > ta.valuewhen(plFound8, low[lbR], 1)

hiddenBullCond8 = plotHiddenBull and priceHL8 and oscLL8 and plFound8 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound8 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish', linewidth=2, color=hiddenBullCond8 ? hiddenBullColor : noneColor)
plotshape(hiddenBullCond8 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish 8 Label', text=' H B8 ', style=shape.labelup, location=location.absolute, color=bullColor8, textcolor=textColor)

//------------------------------------------------------------------------------

////// divergence Lookback period #8 END


////// divergence Lookback period #9

// get pivots
plFound9 = na(ta.pivotlow(osc, lbL9, lbR)) ? false : true
_inRange9(cond) =>
    bars = ta.barssince(cond == true)
    rangeLower <= bars and bars <= rangeUpper


//------------------------------------------------------------------------------
// Regular Bullish
// Osc: Higher Low
oscHL9 = osc[lbR] > ta.valuewhen(plFound9, osc[lbR], 1) and _inRange9(plFound9[1])

// Price: Lower Low
priceLL9 = low[lbR] < ta.valuewhen(plFound9, low[lbR], 1)

bullCond9 = plotBull and priceLL9 and oscHL9 and plFound9 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound9 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish', linewidth=2, color=bullCond9 ? bullColor : noneColor)
plotshape(bullCond9 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish 9 Label', text=' B9 ', style=shape.labelup, location=location.absolute, color=bullColor9, textcolor=textColor)

//------------------------------------------------------------------------------
// Hidden Bullish
// Osc: Lower Low
oscLL9 = osc[lbR] < ta.valuewhen(plFound9, osc[lbR], 1) and _inRange9(plFound9[1])

// Price: Higher Low
priceHL9 = low[lbR] > ta.valuewhen(plFound9, low[lbR], 1)

hiddenBullCond9 = plotHiddenBull and priceHL9 and oscLL9 and plFound9 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound9 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish', linewidth=2, color=hiddenBullCond9 ? hiddenBullColor : noneColor)
plotshape(hiddenBullCond9 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish 9 Label', text=' H B9 ', style=shape.labelup, location=location.absolute, color=bullColor9, textcolor=textColor)

//------------------------------------------------------------------------------


////// divergence Lookback period #9 END


////// divergence Lookback period #10

// get pivots
plFound10 = na(ta.pivotlow(osc, lbL10, lbR)) ? false : true
_inRange10(cond) =>
    bars = ta.barssince(cond == true)
    rangeLower <= bars and bars <= rangeUpper


//------------------------------------------------------------------------------
// Regular Bullish
// Osc: Higher Low
oscHL10 = osc[lbR] > ta.valuewhen(plFound10, osc[lbR], 1) and _inRange10(plFound10[1])

// Price: Lower Low
priceLL10 = low[lbR] < ta.valuewhen(plFound10, low[lbR], 1)

bullCond10 = plotBull and priceLL10 and oscHL10 and plFound10 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound10 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish', linewidth=2, color=bullCond10 ? bullColor : noneColor)
plotshape(bullCond10 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish 10 Label', text=' B10 ', style=shape.labelup, location=location.absolute, color=bullColor10, textcolor=textColor)

//------------------------------------------------------------------------------
// Hidden Bullish
// Osc: Lower Low
oscLL10 = osc[lbR] < ta.valuewhen(plFound10, osc[lbR], 1) and _inRange10(plFound10[1])

// Price: Higher Low
priceHL10 = low[lbR] > ta.valuewhen(plFound10, low[lbR], 1)

hiddenBullCond10 = plotHiddenBull and priceHL10 and oscLL10 and plFound10 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound10 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish', linewidth=2, color=hiddenBullCond10 ? hiddenBullColor : noneColor)
plotshape(hiddenBullCond10 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish 10 Label', text=' H B10 ', style=shape.labelup, location=location.absolute, color=bullColor10, textcolor=textColor)

//------------------------------------------------------------------------------


////// divergence Lookback period #10 END


////// divergence Lookback period #11

// get pivots
plFound11 = na(ta.pivotlow(osc, lbL11, lbR)) ? false : true
_inRange11(cond) =>
    bars = ta.barssince(cond == true)
    rangeLower <= bars and bars <= rangeUpper


//------------------------------------------------------------------------------
// Regular Bullish
// Osc: Higher Low
oscHL11 = osc[lbR] > ta.valuewhen(plFound11, osc[lbR], 1) and _inRange11(plFound11[1])

// Price: Lower Low
priceLL11 = low[lbR] < ta.valuewhen(plFound11, low[lbR], 1)

bullCond11 = plotBull and priceLL11 and oscHL11 and plFound11 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound11 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish', linewidth=2, color=bullCond11 ? bullColor : noneColor)
plotshape(bullCond11 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish 11 Label', text=' B11 ', style=shape.labelup, location=location.absolute, color=bullColor11, textcolor=textColor)

//------------------------------------------------------------------------------
// Hidden Bullish
// Osc: Lower Low
oscLL11 = osc[lbR] < ta.valuewhen(plFound11, osc[lbR], 1) and _inRange11(plFound11[1])

// Price: Higher Low
priceHL11 = low[lbR] > ta.valuewhen(plFound11, low[lbR], 1)

hiddenBullCond11 = plotHiddenBull and priceHL11 and oscLL11 and plFound11 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound11 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish', linewidth=2, color=hiddenBullCond11 ? hiddenBullColor : noneColor)
plotshape(hiddenBullCond11 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish 11 Label', text=' H B11 ', style=shape.labelup, location=location.absolute, color=bullColor11, textcolor=textColor)

//------------------------------------------------------------------------------

////// divergence Lookback period #11 END

////// divergence Lookback period #12

// get pivots
plFound12 = na(ta.pivotlow(osc, lbL12, lbR)) ? false : true
_inRange12(cond) =>
    bars = ta.barssince(cond == true)
    rangeLower <= bars and bars <= rangeUpper


//------------------------------------------------------------------------------
// Regular Bullish
// Osc: Higher Low
oscHL12 = osc[lbR] > ta.valuewhen(plFound12, osc[lbR], 1) and _inRange12(plFound12[1])

// Price: Lower Low
priceLL12 = low[lbR] < ta.valuewhen(plFound12, low[lbR], 1)

bullCond12 = plotBull and priceLL12 and oscHL12 and plFound12 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound12 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish', linewidth=2, color=bullCond12 ? bullColor : noneColor)
plotshape(bullCond12 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish 12 Label', text=' B12 ', style=shape.labelup, location=location.absolute, color=bullColor12, textcolor=textColor)

//------------------------------------------------------------------------------
// Hidden Bullish
// Osc: Lower Low
oscLL12 = osc[lbR] < ta.valuewhen(plFound12, osc[lbR], 1) and _inRange12(plFound12[1])

// Price: Higher Low
priceHL12 = low[lbR] > ta.valuewhen(plFound12, low[lbR], 1)

hiddenBullCond12 = plotHiddenBull and priceHL12 and oscLL12 and plFound12 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound12 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish', linewidth=2, color=hiddenBullCond12 ? hiddenBullColor : noneColor)
plotshape(hiddenBullCond12 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish 12 Label', text=' H B12 ', style=shape.labelup, location=location.absolute, color=bullColor12, textcolor=textColor)

//------------------------------------------------------------------------------

////// divergence Lookback period #12 END

////// divergence Lookback period #13

// get pivots
plFound13 = na(ta.pivotlow(osc, lbL13, lbR)) ? false : true
_inRange13(cond) =>
    bars = ta.barssince(cond == true)
    rangeLower <= bars and bars <= rangeUpper


//------------------------------------------------------------------------------
// Regular Bullish
// Osc: Higher Low
oscHL13 = osc[lbR] > ta.valuewhen(plFound13, osc[lbR], 1) and _inRange13(plFound13[1])

// Price: Lower Low
priceLL13 = low[lbR] < ta.valuewhen(plFound13, low[lbR], 1)

bullCond13 = plotBull and priceLL13 and oscHL13 and plFound13 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound13 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish', linewidth=2, color=bullCond13 ? bullColor : noneColor)
plotshape(bullCond13 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish 13 Label', text=' B13 ', style=shape.labelup, location=location.absolute, color=bullColor13, textcolor=textColor)

//------------------------------------------------------------------------------
// Hidden Bullish
// Osc: Lower Low
oscLL13 = osc[lbR] < ta.valuewhen(plFound13, osc[lbR], 1) and _inRange13(plFound13[1])

// Price: Higher Low
priceHL13 = low[lbR] > ta.valuewhen(plFound13, low[lbR], 1)

hiddenBullCond13 = plotHiddenBull and priceHL13 and oscLL13 and plFound13 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound13 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish', linewidth=2, color=hiddenBullCond13 ? hiddenBullColor : noneColor)
plotshape(hiddenBullCond13 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish 13 Label', text=' H B13 ', style=shape.labelup, location=location.absolute, color=bullColor13, textcolor=textColor)

//------------------------------------------------------------------------------

////// divergence Lookback period #13 END


////// divergence Lookback period #14

// get pivots
plFound14 = na(ta.pivotlow(osc, lbL14, lbR)) ? false : true
_inRange14(cond) =>
    bars = ta.barssince(cond == true)
    rangeLower <= bars and bars <= rangeUpper


//------------------------------------------------------------------------------
// Regular Bullish
// Osc: Higher Low
oscHL14 = osc[lbR] > ta.valuewhen(plFound14, osc[lbR], 1) and _inRange14(plFound14[1])

// Price: Lower Low
priceLL14 = low[lbR] < ta.valuewhen(plFound14, low[lbR], 1)

bullCond14 = plotBull and priceLL14 and oscHL14 and plFound14 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound14 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish', linewidth=2, color=bullCond14 ? bullColor : noneColor)
plotshape(bullCond14 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish 14 Label', text=' B14 ', style=shape.labelup, location=location.absolute, color=bullColor14, textcolor=textColor)

//------------------------------------------------------------------------------
// Hidden Bullish
// Osc: Lower Low
oscLL14 = osc[lbR] < ta.valuewhen(plFound14, osc[lbR], 1) and _inRange14(plFound14[1])

// Price: Higher Low
priceHL14 = low[lbR] > ta.valuewhen(plFound14, low[lbR], 1)

hiddenBullCond14 = plotHiddenBull and priceHL14 and oscLL14 and plFound14 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound14 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish', linewidth=2, color=hiddenBullCond14 ? hiddenBullColor : noneColor)
plotshape(hiddenBullCond14 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish 14 Label', text=' H B14 ', style=shape.labelup, location=location.absolute, color=bullColor14, textcolor=textColor)

//------------------------------------------------------------------------------

////// divergence Lookback period #14 END


////// divergence Lookback period #15

// get pivots
plFound15 = na(ta.pivotlow(osc, lbL15, lbR)) ? false : true
_inRange15(cond) =>
    bars = ta.barssince(cond == true)
    rangeLower <= bars and bars <= rangeUpper


//------------------------------------------------------------------------------
// Regular Bullish
// Osc: Higher Low
oscHL15 = osc[lbR] > ta.valuewhen(plFound15, osc[lbR], 1) and _inRange15(plFound15[1])

// Price: Lower Low
priceLL15 = low[lbR] < ta.valuewhen(plFound15, low[lbR], 1)

bullCond15 = plotBull and priceLL15 and oscHL15 and plFound15 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound15 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish', linewidth=2, color=bullCond15 ? bullColor : noneColor)
plotshape(bullCond15 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish 15 Label', text=' B15 ', style=shape.labelup, location=location.absolute, color=bullColor15, textcolor=textColor)

//------------------------------------------------------------------------------
// Hidden Bullish
// Osc: Lower Low
oscLL15 = osc[lbR] < ta.valuewhen(plFound15, osc[lbR], 1) and _inRange15(plFound15[1])

// Price: Higher Low
priceHL15 = low[lbR] > ta.valuewhen(plFound15, low[lbR], 1)

hiddenBullCond15 = plotHiddenBull and priceHL15 and oscLL15 and plFound15 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound15 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish', linewidth=2, color=hiddenBullCond15 ? hiddenBullColor : noneColor)
plotshape(hiddenBullCond15 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish 15 Label', text=' H B15 ', style=shape.labelup, location=location.absolute, color=bullColor15, textcolor=textColor)

//------------------------------------------------------------------------------

////// divergence Lookback period #15 END


////// divergence Lookback period #16

// get pivots
plFound16 = na(ta.pivotlow(osc, lbL16, lbR)) ? false : true
_inRange16(cond) =>
    bars = ta.barssince(cond == true)
    rangeLower <= bars and bars <= rangeUpper


//------------------------------------------------------------------------------
// Regular Bullish
// Osc: Higher Low
oscHL16 = osc[lbR] > ta.valuewhen(plFound16, osc[lbR], 1) and _inRange16(plFound16[1])

// Price: Lower Low
priceLL16 = low[lbR] < ta.valuewhen(plFound16, low[lbR], 1)

bullCond16 = plotBull and priceLL16 and oscHL16 and plFound16 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound16 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish', linewidth=2, color=bullCond16 ? bullColor : noneColor)
plotshape(bullCond16 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish 16 Label', text=' B16 ', style=shape.labelup, location=location.absolute, color=bullColor16, textcolor=textColor)

//------------------------------------------------------------------------------
// Hidden Bullish
// Osc: Lower Low
oscLL16 = osc[lbR] < ta.valuewhen(plFound16, osc[lbR], 1) and _inRange16(plFound16[1])

// Price: Higher Low
priceHL16 = low[lbR] > ta.valuewhen(plFound16, low[lbR], 1)

hiddenBullCond16 = plotHiddenBull and priceHL16 and oscLL16 and plFound16 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound16 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish', linewidth=2, color=hiddenBullCond16 ? hiddenBullColor : noneColor)
plotshape(hiddenBullCond16 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish 16 Label', text=' H B16 ', style=shape.labelup, location=location.absolute, color=bullColor16, textcolor=textColor)

//------------------------------------------------------------------------------

////// divergence Lookback period #16 END

////// divergence Lookback period #17

// get pivots
plFound17 = na(ta.pivotlow(osc, lbL17, lbR)) ? false : true
_inRange17(cond) =>
    bars = ta.barssince(cond == true)
    rangeLower <= bars and bars <= rangeUpper


//------------------------------------------------------------------------------
// Regular Bullish
// Osc: Higher Low
oscHL17 = osc[lbR] > ta.valuewhen(plFound17, osc[lbR], 1) and _inRange17(plFound17[1])

// Price: Lower Low
priceLL17 = low[lbR] < ta.valuewhen(plFound17, low[lbR], 1)

bullCond17 = plotBull and priceLL17 and oscHL17 and plFound17 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound17 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish', linewidth=2, color=bullCond17 ? bullColor : noneColor)
plotshape(bullCond17 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish 17 Label', text=' B17 ', style=shape.labelup, location=location.absolute, color=bullColor17, textcolor=textColor)

//------------------------------------------------------------------------------
// Hidden Bullish
// Osc: Lower Low
oscLL17 = osc[lbR] < ta.valuewhen(plFound17, osc[lbR], 1) and _inRange17(plFound17[1])

// Price: Higher Low
priceHL17 = low[lbR] > ta.valuewhen(plFound17, low[lbR], 1)

hiddenBullCond17 = plotHiddenBull and priceHL17 and oscLL17 and plFound17 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound17 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish', linewidth=2, color=hiddenBullCond17 ? hiddenBullColor : noneColor)
plotshape(hiddenBullCond17 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish 17 Label', text=' H B17 ', style=shape.labelup, location=location.absolute, color=bullColor17, textcolor=textColor)

//------------------------------------------------------------------------------

////// divergence Lookback period #17 END

////// divergence Lookback period #18

// get pivots
plFound18 = na(ta.pivotlow(osc, lbL18, lbR)) ? false : true
_inRange18(cond) =>
    bars = ta.barssince(cond == true)
    rangeLower <= bars and bars <= rangeUpper


//------------------------------------------------------------------------------
// Regular Bullish
// Osc: Higher Low
oscHL18 = osc[lbR] > ta.valuewhen(plFound18, osc[lbR], 1) and _inRange18(plFound18[1])

// Price: Lower Low
priceLL18 = low[lbR] < ta.valuewhen(plFound18, low[lbR], 1)

bullCond18 = plotBull and priceLL18 and oscHL18 and plFound18 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound18 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish', linewidth=2, color=bullCond18 ? bullColor : noneColor)
plotshape(bullCond18 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish 18 Label', text=' B18 ', style=shape.labelup, location=location.absolute, color=bullColor18, textcolor=textColor)

//------------------------------------------------------------------------------
// Hidden Bullish
// Osc: Lower Low
oscLL18 = osc[lbR] < ta.valuewhen(plFound18, osc[lbR], 1) and _inRange18(plFound18[1])

// Price: Higher Low
priceHL18 = low[lbR] > ta.valuewhen(plFound18, low[lbR], 1)

hiddenBullCond18 = plotHiddenBull and priceHL18 and oscLL18 and plFound18 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound18 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish', linewidth=2, color=hiddenBullCond18 ? hiddenBullColor : noneColor)
plotshape(hiddenBullCond18 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish 18 Label', text=' H B18 ', style=shape.labelup, location=location.absolute, color=bullColor18, textcolor=textColor)

//------------------------------------------------------------------------------

////// divergence Lookback period #18 END

////// divergence Lookback period #19

// get pivots
plFound19 = na(ta.pivotlow(osc, lbL19, lbR)) ? false : true
_inRange19(cond) =>
    bars = ta.barssince(cond == true)
    rangeLower <= bars and bars <= rangeUpper


//------------------------------------------------------------------------------
// Regular Bullish
// Osc: Higher Low
oscHL19 = osc[lbR] > ta.valuewhen(plFound19, osc[lbR], 1) and _inRange19(plFound19[1])

// Price: Lower Low
priceLL19 = low[lbR] < ta.valuewhen(plFound19, low[lbR], 1)

bullCond19 = plotBull and priceLL19 and oscHL19 and plFound19 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound19 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish', linewidth=2, color=bullCond19 ? bullColor : noneColor)
plotshape(bullCond19 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish 19 Label', text=' B19 ', style=shape.labelup, location=location.absolute, color=bullColor19, textcolor=textColor)

//------------------------------------------------------------------------------
// Hidden Bullish
// Osc: Lower Low
oscLL19 = osc[lbR] < ta.valuewhen(plFound19, osc[lbR], 1) and _inRange19(plFound19[1])

// Price: Higher Low
priceHL19 = low[lbR] > ta.valuewhen(plFound19, low[lbR], 1)

hiddenBullCond19 = plotHiddenBull and priceHL19 and oscLL19 and plFound19 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound19 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish', linewidth=2, color=hiddenBullCond19 ? hiddenBullColor : noneColor)
plotshape(hiddenBullCond19 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish 19 Label', text=' H B19 ', style=shape.labelup, location=location.absolute, color=bullColor19, textcolor=textColor)

//------------------------------------------------------------------------------

////// divergence Lookback period #19 END


////// divergence Lookback period #20

// get pivots
plFound20 = na(ta.pivotlow(osc, lbL20, lbR)) ? false : true
_inRange20(cond) =>
    bars = ta.barssince(cond == true)
    rangeLower <= bars and bars <= rangeUpper


//------------------------------------------------------------------------------
// Regular Bullish
// Osc: Higher Low
oscHL20 = osc[lbR] > ta.valuewhen(plFound20, osc[lbR], 1) and _inRange20(plFound20[1])

// Price: Lower Low
priceLL20 = low[lbR] < ta.valuewhen(plFound20, low[lbR], 1)

bullCond20 = plotBull and priceLL20 and oscHL20 and plFound20 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound20 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish', linewidth=2, color=bullCond20 ? bullColor : noneColor)
plotshape(bullCond20 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish 20 Label', text=' B20 ', style=shape.labelup, location=location.absolute, color=bullColor20, textcolor=textColor)

//------------------------------------------------------------------------------
// Hidden Bullish
// Osc: Lower Low
oscLL20 = osc[lbR] < ta.valuewhen(plFound20, osc[lbR], 1) and _inRange20(plFound20[1])

// Price: Higher Low
priceHL20 = low[lbR] > ta.valuewhen(plFound20, low[lbR], 1)

hiddenBullCond20 = plotHiddenBull and priceHL20 and oscLL20 and plFound20 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound20 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish', linewidth=2, color=hiddenBullCond20 ? hiddenBullColor : noneColor)
plotshape(hiddenBullCond20 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish 20 Label', text=' H B20 ', style=shape.labelup, location=location.absolute, color=bullColor20, textcolor=textColor)

//------------------------------------------------------------------------------

////// divergence Lookback period #20 END

////// divergence Lookback period #21

// get pivots
plFound21 = na(ta.pivotlow(osc, lbL21, lbR)) ? false : true
_inRange21(cond) =>
    bars = ta.barssince(cond == true)
    rangeLower <= bars and bars <= rangeUpper


//------------------------------------------------------------------------------
// Regular Bullish
// Osc: Higher Low
oscHL21 = osc[lbR] > ta.valuewhen(plFound21, osc[lbR], 1) and _inRange21(plFound21[1])

// Price: Lower Low
priceLL21 = low[lbR] < ta.valuewhen(plFound21, low[lbR], 1)

bullCond21 = plotBull and priceLL21 and oscHL21 and plFound21 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound21 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish', linewidth=2, color=bullCond21 ? bullColor : noneColor)
plotshape(bullCond21 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish 21 Label', text=' B21 ', style=shape.labelup, location=location.absolute, color=bullColor21, textcolor=textColor)

//------------------------------------------------------------------------------
// Hidden Bullish
// Osc: Lower Low
oscLL21 = osc[lbR] < ta.valuewhen(plFound21, osc[lbR], 1) and _inRange21(plFound21[1])

// Price: Higher Low
priceHL21 = low[lbR] > ta.valuewhen(plFound21, low[lbR], 1)

hiddenBullCond21 = plotHiddenBull and priceHL21 and oscLL21 and plFound21 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound21 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish', linewidth=2, color=hiddenBullCond21 ? hiddenBullColor : noneColor)
plotshape(hiddenBullCond21 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish 21 Label', text=' H B21 ', style=shape.labelup, location=location.absolute, color=bullColor21, textcolor=textColor)

//------------------------------------------------------------------------------

////// divergence Lookback period #21 END

////// divergence Lookback period #22

// get pivots
plFound22 = na(ta.pivotlow(osc, lbL22, lbR)) ? false : true
_inRange22(cond) =>
    bars = ta.barssince(cond == true)
    rangeLower <= bars and bars <= rangeUpper


//------------------------------------------------------------------------------
// Regular Bullish
// Osc: Higher Low
oscHL22 = osc[lbR] > ta.valuewhen(plFound22, osc[lbR], 1) and _inRange22(plFound22[1])

// Price: Lower Low
priceLL22 = low[lbR] < ta.valuewhen(plFound22, low[lbR], 1)

bullCond22 = plotBull and priceLL22 and oscHL22 and plFound22 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound22 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish', linewidth=2, color=bullCond22 ? bullColor : noneColor)
plotshape(bullCond22 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish 22 Label', text=' B22 ', style=shape.labelup, location=location.absolute, color=bullColor22, textcolor=textColor)

//------------------------------------------------------------------------------
// Hidden Bullish
// Osc: Lower Low
oscLL22 = osc[lbR] < ta.valuewhen(plFound22, osc[lbR], 1) and _inRange22(plFound22[1])

// Price: Higher Low
priceHL22 = low[lbR] > ta.valuewhen(plFound22, low[lbR], 1)

hiddenBullCond22 = plotHiddenBull and priceHL22 and oscLL22 and plFound22 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound22 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish', linewidth=2, color=hiddenBullCond22 ? hiddenBullColor : noneColor)
plotshape(hiddenBullCond22 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish 22 Label', text=' H B22 ', style=shape.labelup, location=location.absolute, color=bullColor22, textcolor=textColor)

//------------------------------------------------------------------------------

////// divergence Lookback period #22 END

////// divergence Lookback period #23

// get pivots
plFound23 = na(ta.pivotlow(osc, lbL23, lbR)) ? false : true
_inRange23(cond) =>
    bars = ta.barssince(cond == true)
    rangeLower <= bars and bars <= rangeUpper


//------------------------------------------------------------------------------
// Regular Bullish
// Osc: Higher Low
oscHL23 = osc[lbR] > ta.valuewhen(plFound23, osc[lbR], 1) and _inRange23(plFound23[1])

// Price: Lower Low
priceLL23 = low[lbR] < ta.valuewhen(plFound23, low[lbR], 1)

bullCond23 = plotBull and priceLL23 and oscHL23 and plFound23 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound23 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish', linewidth=2, color=bullCond23 ? bullColor : noneColor)
plotshape(bullCond23 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish 23 Label', text=' B23 ', style=shape.labelup, location=location.absolute, color=bullColor23, textcolor=textColor)

//------------------------------------------------------------------------------
// Hidden Bullish
// Osc: Lower Low
oscLL23 = osc[lbR] < ta.valuewhen(plFound23, osc[lbR], 1) and _inRange23(plFound23[1])

// Price: Higher Low
priceHL23 = low[lbR] > ta.valuewhen(plFound23, low[lbR], 1)

hiddenBullCond23 = plotHiddenBull and priceHL23 and oscLL23 and plFound23 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound23 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish', linewidth=2, color=hiddenBullCond23 ? hiddenBullColor : noneColor)
plotshape(hiddenBullCond23 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish 23 Label', text=' H B23 ', style=shape.labelup, location=location.absolute, color=bullColor23, textcolor=textColor)

//------------------------------------------------------------------------------

////// divergence Lookback period #23 END

////// divergence Lookback period #24

// get pivots
plFound24 = na(ta.pivotlow(osc, lbL24, lbR)) ? false : true
_inRange24(cond) =>
    bars = ta.barssince(cond == true)
    rangeLower <= bars and bars <= rangeUpper


//------------------------------------------------------------------------------
// Regular Bullish
// Osc: Higher Low
oscHL24 = osc[lbR] > ta.valuewhen(plFound24, osc[lbR], 1) and _inRange24(plFound24[1])

// Price: Lower Low
priceLL24 = low[lbR] < ta.valuewhen(plFound24, low[lbR], 1)

bullCond24 = plotBull and priceLL24 and oscHL24 and plFound24 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound24 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish', linewidth=2, color=bullCond24 ? bullColor : noneColor)
plotshape(bullCond24 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish 24 Label', text=' B24 ', style=shape.labelup, location=location.absolute, color=bullColor24, textcolor=textColor)

//------------------------------------------------------------------------------
// Hidden Bullish
// Osc: Lower Low
oscLL24 = osc[lbR] < ta.valuewhen(plFound24, osc[lbR], 1) and _inRange24(plFound24[1])

// Price: Higher Low
priceHL24 = low[lbR] > ta.valuewhen(plFound24, low[lbR], 1)

hiddenBullCond24 = plotHiddenBull and priceHL24 and oscLL24 and plFound24 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound24 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish', linewidth=2, color=hiddenBullCond24 ? hiddenBullColor : noneColor)
plotshape(hiddenBullCond24 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish 24 Label', text=' H B24 ', style=shape.labelup, location=location.absolute, color=bullColor24, textcolor=textColor)

//------------------------------------------------------------------------------

////// divergence Lookback period #24 END

////// divergence Lookback period #25

// get pivots
plFound25 = na(ta.pivotlow(osc, lbL25, lbR)) ? false : true
_inRange25(cond) =>
    bars = ta.barssince(cond == true)
    rangeLower <= bars and bars <= rangeUpper


//------------------------------------------------------------------------------
// Regular Bullish
// Osc: Higher Low
oscHL25 = osc[lbR] > ta.valuewhen(plFound25, osc[lbR], 1) and _inRange25(plFound25[1])

// Price: Lower Low
priceLL25 = low[lbR] < ta.valuewhen(plFound25, low[lbR], 1)

bullCond25 = plotBull and priceLL25 and oscHL25 and plFound25 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound25 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish', linewidth=2, color=bullCond25 ? bullColor : noneColor)
plotshape(bullCond25 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish 25 Label', text=' B25 ', style=shape.labelup, location=location.absolute, color=bullColor25, textcolor=textColor)

//------------------------------------------------------------------------------
// Hidden Bullish
// Osc: Lower Low
oscLL25 = osc[lbR] < ta.valuewhen(plFound25, osc[lbR], 1) and _inRange25(plFound25[1])

// Price: Higher Low
priceHL25 = low[lbR] > ta.valuewhen(plFound25, low[lbR], 1)

hiddenBullCond25 = plotHiddenBull and priceHL25 and oscLL25 and plFound25 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound25 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish', linewidth=2, color=hiddenBullCond25 ? hiddenBullColor : noneColor)
plotshape(hiddenBullCond25 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish 25 Label', text=' H B25 ', style=shape.labelup, location=location.absolute, color=bullColor25, textcolor=textColor)

//------------------------------------------------------------------------------

////// divergence Lookback period #25 END

////// divergence Lookback period #26

// get pivots
plFound26 = na(ta.pivotlow(osc, lbL26, lbR)) ? false : true
_inRange26(cond) =>
    bars = ta.barssince(cond == true)
    rangeLower <= bars and bars <= rangeUpper


//------------------------------------------------------------------------------
// Regular Bullish
// Osc: Higher Low
oscHL26 = osc[lbR] > ta.valuewhen(plFound26, osc[lbR], 1) and _inRange26(plFound26[1])

// Price: Lower Low
priceLL26 = low[lbR] < ta.valuewhen(plFound26, low[lbR], 1)

bullCond26 = plotBull and priceLL26 and oscHL26 and plFound26 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound26 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish 26', linewidth=2, color=bullCond26 ? bullColor : noneColor)
plotshape(bullCond26 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish 26 Label', text=' B26 ', style=shape.labelup, location=location.absolute, color=bullColor26, textcolor=textColor)

//------------------------------------------------------------------------------
// Hidden Bullish
// Osc: Lower Low
oscLL26 = osc[lbR] < ta.valuewhen(plFound26, osc[lbR], 1) and _inRange26(plFound26[1])

// Price: Higher Low
priceHL26 = low[lbR] > ta.valuewhen(plFound26, low[lbR], 1)

hiddenBullCond26 = plotHiddenBull and priceHL26 and oscLL26 and plFound26 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound26 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish 26', linewidth=2, color=hiddenBullCond26 ? hiddenBullColor : noneColor)
plotshape(hiddenBullCond26 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish 26 Label', text=' H B26 ', style=shape.labelup, location=location.absolute, color=bullColor26, textcolor=textColor)

//------------------------------------------------------------------------------

////// divergence Lookback period #26 END

////// divergence Lookback period #27

// get pivots
plFound27 = na(ta.pivotlow(osc, lbL27, lbR)) ? false : true
_inRange27(cond) =>
    bars = ta.barssince(cond == true)
    rangeLower <= bars and bars <= rangeUpper


//------------------------------------------------------------------------------
// Regular Bullish
// Osc: Higher Low
oscHL27 = osc[lbR] > ta.valuewhen(plFound27, osc[lbR], 1) and _inRange27(plFound27[1])

// Price: Lower Low
priceLL27 = low[lbR] < ta.valuewhen(plFound27, low[lbR], 1)

bullCond27 = plotBull and priceLL27 and oscHL27 and plFound27 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound27 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish 27', linewidth=2, color=bullCond27 ? bullColor : noneColor)
plotshape(bullCond27 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish 27 Label ', text=' B27 ', style=shape.labelup, location=location.absolute, color=bullColor27, textcolor=textColor)

//------------------------------------------------------------------------------
// Hidden Bullish
// Osc: Lower Low
oscLL27 = osc[lbR] < ta.valuewhen(plFound27, osc[lbR], 1) and _inRange27(plFound27[1])

// Price: Higher Low
priceHL27 = low[lbR] > ta.valuewhen(plFound27, low[lbR], 1)

hiddenBullCond27 = plotHiddenBull and priceHL27 and oscLL27 and plFound27 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound27 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish 27', linewidth=2, color=hiddenBullCond27 ? hiddenBullColor : noneColor)
plotshape(hiddenBullCond27 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish 27 Label', text=' H B27 ', style=shape.labelup, location=location.absolute, color=bullColor27, textcolor=textColor)

//------------------------------------------------------------------------------

////// divergence Lookback period #27 END

////// divergence Lookback period #28

// get pivots
plFound28 = na(ta.pivotlow(osc, lbL28, lbR)) ? false : true
_inRange28(cond) =>
    bars = ta.barssince(cond == true)
    rangeLower <= bars and bars <= rangeUpper


//------------------------------------------------------------------------------
// Regular Bullish
// Osc: Higher Low
oscHL28 = osc[lbR] > ta.valuewhen(plFound28, osc[lbR], 1) and _inRange28(plFound28[1])

// Price: Lower Low
priceLL28 = low[lbR] < ta.valuewhen(plFound28, low[lbR], 1)

bullCond28 = plotBull and priceLL28 and oscHL28 and plFound28 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound28 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish 28', linewidth=2, color=bullCond28 ? bullColor : noneColor)
plotshape(bullCond28 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish 28 Label ', text=' B28 ', style=shape.labelup, location=location.absolute, color=bullColor28, textcolor=textColor)

//------------------------------------------------------------------------------
// Hidden Bullish
// Osc: Lower Low
oscLL28 = osc[lbR] < ta.valuewhen(plFound28, osc[lbR], 1) and _inRange28(plFound28[1])

// Price: Higher Low
priceHL28 = low[lbR] > ta.valuewhen(plFound28, low[lbR], 1)

hiddenBullCond28 = plotHiddenBull and priceHL28 and oscLL28 and plFound28 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound28 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish 28', linewidth=2, color=hiddenBullCond28 ? hiddenBullColor : noneColor)
plotshape(hiddenBullCond28 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish 28 Label', text=' H B28 ', style=shape.labelup, location=location.absolute, color=bullColor28, textcolor=textColor)

//------------------------------------------------------------------------------

////// divergence Lookback period #28 END

////// divergence Lookback period #29

// get pivots
plFound29 = na(ta.pivotlow(osc, lbL29, lbR)) ? false : true
_inRange29(cond) =>
    bars = ta.barssince(cond == true)
    rangeLower <= bars and bars <= rangeUpper


//------------------------------------------------------------------------------
// Regular Bullish
// Osc: Higher Low
oscHL29 = osc[lbR] > ta.valuewhen(plFound29, osc[lbR], 1) and _inRange29(plFound29[1])

// Price: Lower Low
priceLL29 = low[lbR] < ta.valuewhen(plFound29, low[lbR], 1)

bullCond29 = plotBull and priceLL29 and oscHL29 and plFound29 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound29 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish 29', linewidth=2, color=bullCond29 ? bullColor : noneColor)
plotshape(bullCond29 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish 29 Label ', text=' B29 ', style=shape.labelup, location=location.absolute, color=bullColor29, textcolor=textColor)

//------------------------------------------------------------------------------
// Hidden Bullish
// Osc: Lower Low
oscLL29 = osc[lbR] < ta.valuewhen(plFound29, osc[lbR], 1) and _inRange29(plFound29[1])

// Price: Higher Low
priceHL29 = low[lbR] > ta.valuewhen(plFound29, low[lbR], 1)

hiddenBullCond29 = plotHiddenBull and priceHL29 and oscLL29 and plFound29 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound29 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish 29', linewidth=2, color=hiddenBullCond29 ? hiddenBullColor : noneColor)
plotshape(hiddenBullCond29 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish 29 Label', text=' H B29 ', style=shape.labelup, location=location.absolute, color=bullColor29, textcolor=textColor)

//------------------------------------------------------------------------------

////// divergence Lookback period #29 END

////// divergence Lookback period #30

// get pivots
plFound30 = na(ta.pivotlow(osc, lbL30, lbR)) ? false : true
_inRange30(cond) =>
    bars = ta.barssince(cond == true)
    rangeLower <= bars and bars <= rangeUpper


//------------------------------------------------------------------------------
// Regular Bullish
// Osc: Higher Low
oscHL30 = osc[lbR] > ta.valuewhen(plFound30, osc[lbR], 1) and _inRange30(plFound30[1])

// Price: Lower Low
priceLL30 = low[lbR] < ta.valuewhen(plFound30, low[lbR], 1)

bullCond30 = plotBull and priceLL30 and oscHL30 and plFound30 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound30 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish 30', linewidth=2, color=bullCond30 ? bullColor : noneColor)
plotshape(bullCond30 ? osc[lbR] : na, offset=-lbR, title='Regular Bullish 30 Label ', text=' B30 ', style=shape.labelup, location=location.absolute, color=bullColor30, textcolor=textColor)

//------------------------------------------------------------------------------
// Hidden Bullish
// Osc: Lower Low
oscLL30 = osc[lbR] < ta.valuewhen(plFound30, osc[lbR], 1) and _inRange30(plFound30[1])

// Price: Higher Low
priceHL30 = low[lbR] > ta.valuewhen(plFound30, low[lbR], 1)

hiddenBullCond30 = plotHiddenBull and priceHL30 and oscLL30 and plFound30 and RSIwasBelowOS and isWithinLongRanges and not enableonlyOneLB

//plot(plFound30 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish 30', linewidth=2, color=hiddenBullCond30 ? hiddenBullColor : noneColor)
plotshape(hiddenBullCond30 ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish 30 Label', text=' H B30 ', style=shape.labelup, location=location.absolute, color=bullColor30, textcolor=textColor)

//------------------------------------------------------------------------------

////// divergence Lookback period #30 END


// combine divergence alerts intwo bullish and hidden

combibullCond = (bullCond1 or bullCond2 or bullCond3 or bullCond4 or bullCond5 or bullCond6 or bullCond7 or bullCond8 or bullCond9 or bullCond10 or bullCond11 or bullCond12 or bullCond13 or bullCond14 or bullCond15 or bullCond16 or bullCond17 or bullCond18 or bullCond19 or bullCond20 or bullCond21 or bullCond22 or bullCond23 or bullCond24 or bullCond25 or bullCond26 or bullCond27 or bullCond28 or bullCond29 or bullCond30)
combihiddenBullCond = (hiddenBullCond1 or hiddenBullCond2 or hiddenBullCond3 or hiddenBullCond4 or hiddenBullCond5 or hiddenBullCond6 or hiddenBullCond7 or hiddenBullCond8 or hiddenBullCond9 or hiddenBullCond10 or hiddenBullCond11 or hiddenBullCond12 or hiddenBullCond13 or hiddenBullCond14 or hiddenBullCond15 or hiddenBullCond16 or hiddenBullCond17 or hiddenBullCond18 or hiddenBullCond19 or hiddenBullCond20 or hiddenBullCond21 or hiddenBullCond22 or hiddenBullCond23 or hiddenBullCond24 or hiddenBullCond25 or hiddenBullCond26 or hiddenBullCond27 or hiddenBullCond28 or hiddenBullCond29 or hiddenBullCond30)


///////////////////////// Delays



// wait for a Oscillator and Oscillator EMA cross
oscCross = ((1 < (osc - oscema)) and (ta.barssince(combihiddenBullCond or combibullCond) <= oscCrosslookback) ) or not enableOscCross

// wait for price to go above ema
EmaIsBroken = (ta.ema(src, WaitForEmaBreakLen) < close) or not enableWaitForEmaBreak

///////////// check if price bounced before the alert
priceBounced = priceBounceNeeded < (ta.highest(high, 1) - ta.lowest(low, priceBounceNeededLbL)) or not enablepriceBounceNeeded

delaysEnabled = enablepriceBounceNeeded or enableOscCross or enableWaitForEmaBreak
DelayState = oscCross and EmaIsBroken and priceBounced

// dirty workaround to dont reprint the final hidden bull alert
HiddenBullAndBounce0 = DelayState and (ta.barssince(combihiddenBullCond) == 0) 
HiddenBullAndBounce1 = DelayState and (ta.barssince(combihiddenBullCond) == 1) and not (ta.barssince(HiddenBullAndBounce0) < 1)
HiddenBullAndBounce2 = DelayState and (ta.barssince(combihiddenBullCond) == 2) and not (ta.barssince(HiddenBullAndBounce1 or HiddenBullAndBounce0) < 2)
HiddenBullAndBounce3 = DelayState and (ta.barssince(combihiddenBullCond) == 3) and not (ta.barssince(HiddenBullAndBounce1 or HiddenBullAndBounce0 or HiddenBullAndBounce2) < 3)
HiddenBullAndBounce4 = DelayState and (ta.barssince(combihiddenBullCond) == 4) and not (ta.barssince(HiddenBullAndBounce1 or HiddenBullAndBounce0 or HiddenBullAndBounce2 or HiddenBullAndBounce3) < 5)
HiddenBullAndBounce5 = DelayState and (ta.barssince(combihiddenBullCond) == 5) and not (ta.barssince(HiddenBullAndBounce1 or HiddenBullAndBounce0 or HiddenBullAndBounce2 or HiddenBullAndBounce3 or HiddenBullAndBounce4) < 6)
HiddenBullAndBounce6 = DelayState and (ta.barssince(combihiddenBullCond) == 6) and not (ta.barssince(HiddenBullAndBounce1 or HiddenBullAndBounce0 or HiddenBullAndBounce2 or HiddenBullAndBounce3 or HiddenBullAndBounce4 or HiddenBullAndBounce5) < 7)
HiddenBullAndBounce7 = DelayState and (ta.barssince(combihiddenBullCond) == 7) and not (ta.barssince(HiddenBullAndBounce1 or HiddenBullAndBounce0 or HiddenBullAndBounce2 or HiddenBullAndBounce3 or HiddenBullAndBounce4 or HiddenBullAndBounce5 or HiddenBullAndBounce6) < 8)
HiddenBullAndBounce8 = DelayState and (ta.barssince(combihiddenBullCond) == 8) and not (ta.barssince(HiddenBullAndBounce1 or HiddenBullAndBounce0 or HiddenBullAndBounce2 or HiddenBullAndBounce3 or HiddenBullAndBounce4 or HiddenBullAndBounce5 or HiddenBullAndBounce6 or HiddenBullAndBounce7) < 9)
HiddenBullAndBounce9 = DelayState and (ta.barssince(combihiddenBullCond) == 9) and not (ta.barssince(HiddenBullAndBounce1 or HiddenBullAndBounce0 or HiddenBullAndBounce2 or HiddenBullAndBounce3 or HiddenBullAndBounce4 or HiddenBullAndBounce5 or HiddenBullAndBounce6 or HiddenBullAndBounce7 or HiddenBullAndBounce8) < 10)
HiddenBullAndBounce10 = DelayState and (ta.barssince(combihiddenBullCond) == 10) and not (ta.barssince(HiddenBullAndBounce1 or HiddenBullAndBounce0 or HiddenBullAndBounce2 or HiddenBullAndBounce3 or HiddenBullAndBounce4 or HiddenBullAndBounce5 or HiddenBullAndBounce6 or HiddenBullAndBounce7 or HiddenBullAndBounce8 or HiddenBullAndBounce9) < 11)

FinalHiddenBullDelayAlert = delaysEnabled and (HiddenBullAndBounce1 or HiddenBullAndBounce0 or HiddenBullAndBounce2 or HiddenBullAndBounce3 or HiddenBullAndBounce4 or HiddenBullAndBounce5 or HiddenBullAndBounce6 or HiddenBullAndBounce7 or HiddenBullAndBounce8 or HiddenBullAndBounce9 or HiddenBullAndBounce10)

FinalHiddenBullAlert = false
FinalHiddenBullAlert2 = false
if delaysEnabled
    FinalHiddenBullAlert := FinalHiddenBullDelayAlert
    FinalHiddenBullAlert2 := false
if not delaysEnabled
    FinalHiddenBullAlert := false
    FinalHiddenBullAlert2 := combihiddenBullCond


// dirty workaround to dont reprint the final bull alert
BullAndBounce0 = DelayState and (ta.barssince(combibullCond) == 0) 
BullAndBounce1 = DelayState and (ta.barssince(combibullCond) == 1) and not (ta.barssince(BullAndBounce0) < 2)
BullAndBounce2 = DelayState and (ta.barssince(combibullCond) == 2) and not (ta.barssince(BullAndBounce1 or BullAndBounce0) < 3)
BullAndBounce3 = DelayState and (ta.barssince(combibullCond) == 3) and not (ta.barssince(BullAndBounce1 or BullAndBounce0 or BullAndBounce2) < 4)
BullAndBounce4 = DelayState and (ta.barssince(combibullCond) == 4) and not (ta.barssince(BullAndBounce1 or BullAndBounce0 or BullAndBounce2 or BullAndBounce3) < 5)
BullAndBounce5 = DelayState and (ta.barssince(combibullCond) == 5) and not (ta.barssince(BullAndBounce1 or BullAndBounce0 or BullAndBounce2 or BullAndBounce3 or BullAndBounce4) < 6)
BullAndBounce6 = DelayState and (ta.barssince(combibullCond) == 6) and not (ta.barssince(BullAndBounce1 or BullAndBounce0 or BullAndBounce2 or BullAndBounce3 or BullAndBounce4 or BullAndBounce5) < 7)
BullAndBounce7 = DelayState and (ta.barssince(combibullCond) == 7) and not (ta.barssince(BullAndBounce1 or BullAndBounce0 or BullAndBounce2 or BullAndBounce3 or BullAndBounce4 or BullAndBounce5 or BullAndBounce6) < 8)
BullAndBounce8 = DelayState and (ta.barssince(combibullCond) == 8) and not (ta.barssince(BullAndBounce1 or BullAndBounce0 or BullAndBounce2 or BullAndBounce3 or BullAndBounce4 or BullAndBounce5 or BullAndBounce6 or BullAndBounce7) < 9)
BullAndBounce9 = DelayState and (ta.barssince(combibullCond) == 9) and not (ta.barssince(BullAndBounce1 or BullAndBounce0 or BullAndBounce2 or BullAndBounce3 or BullAndBounce4 or BullAndBounce5 or BullAndBounce6 or BullAndBounce7 or BullAndBounce8) < 10)
BullAndBounce10 = DelayState and (ta.barssince(combibullCond) == 10) and not (ta.barssince(BullAndBounce1 or BullAndBounce0 or BullAndBounce2 or BullAndBounce3 or BullAndBounce4 or BullAndBounce5 or BullAndBounce6 or BullAndBounce7 or BullAndBounce8 or BullAndBounce9) < 11)

FinalBullDelayAlert = delaysEnabled and (BullAndBounce1 or BullAndBounce0 or BullAndBounce2 or BullAndBounce3 or BullAndBounce4 or BullAndBounce5 or BullAndBounce6 or BullAndBounce7 or BullAndBounce8 or BullAndBounce9 or BullAndBounce10)

FinalBullAlert = false
FinalBullAlert2 = false
if delaysEnabled
    FinalBullAlert := FinalBullDelayAlert
    FinalBullAlert2 := false
if not delaysEnabled
    FinalBullAlert := false
    FinalBullAlert2 := combibullCond
///////////// check if price bounced before the alert END

// plot delay buy
plotshape(FinalBullAlert ? osc : na, offset=0, title='Buy ', text=" buy ", style=shape.labelup, location=location.absolute, color=color.lime, textcolor=textColor)




////////////////// Stop Loss

// Stop loss
enableSL =  input.bool(true, title='enable Stop Loss', group='══════════ Stop Loss Settings ══════════')
whichSL =  input.string(defval='use the low as SL', title='SL based on static % or based on the low', options=['use the low as SL', 'use the static % as SL'], group='══════════ Stop Loss Settings ══════════')
whichOffset = input.string(defval='use % as offset', title='choose offset from the low', options=['use $ as offset', 'use % as offset'], group='Stop Loss at the low')
lowPBuffer = input.float(0.35, title='SL Offset from the Low in %', group='Stop Loss at the low') / 100
lowDBuffer = input.float(100, title='SL Offset from the Low in $', group='Stop Loss at the low')
SlLowLookback = input.int(title='SL lookback for Low', defval=5, minval=1, maxval=50, group='Stop Loss at the low')


longSlLow = float(na)
if whichOffset == "use % as offset" and whichSL == "use the low as SL" and enableSL
    longSlLow := ta.lowest(low, SlLowLookback) * (1 - lowPBuffer)

if whichOffset == "use $ as offset" and whichSL == "use the low as SL" and enableSL
    longSlLow := ta.lowest(low, SlLowLookback) - lowDBuffer
//plot(longSlLow, title="stoploss", color=color.new(#00bcd4, 0))



// long settings - 🔥 uncomment the 6 lines below to disable the alerts and enable the backtester 
longStopLoss = input.float(0.5, title='Stop Loss in %', group='static % Stop Loss', inline='Input 1') / 100

/////// Take profit

longTakeProfit1 = input.float(2, title='Take Profit in %', group='Take Profit', inline='Input 1') / 100
//longTakeProfit2 = input.float(2, title='25% Take Profit in %', group='Take Profit', inline='Input 1') / 100
//longTakeProfit3 = input.float(2, title='25% Take Profit in %', group='Take Profit', inline='Input 1') / 100
//longTakeProfit4 = input.float(2, title='25% Take Profit in %', group='Take Profit', inline='Input 1') / 100




////////////////// SL TP END


/////////////////// alerts 

///////////////ver final alert variable
veryFinalAlert = FinalHiddenBullAlert2 or FinalHiddenBullAlert or FinalBullAlert or FinalBullAlert2

/////////////////// alerts 


selectalertFreq = input.string(defval='once per bar close', title='Alert Options', options=['once per bar', 'once per bar close', 'all'], group='═══════════ alert settings ═══════════')
BuyAlertMessage = input.string(defval="Bullish Divergence detected, put your SL @", title='Buy Alert Message',  group='═══════════ alert settings ═══════════')
enableSlMessage =  input.bool(true, title='enable Stop Loss Value at the end of "buy Alert message"', group='═══════════ alert settings ═══════════')
AfterSLMessage = input.string(defval="", title='Buy Alert message after SL Value',  group='═══════════ alert settings ═══════════')


alertFreq = selectalertFreq == "once per bar" ? alert.freq_once_per_bar : selectalertFreq == "once per bar close" ? alert.freq_once_per_bar_close : selectalertFreq == "all" ? alert.freq_all : na

if not enableSlMessage and veryFinalAlert
    alert(str.tostring(BuyAlertMessage), alertFreq)
if enableSlMessage and veryFinalAlert
    alert(str.tostring(BuyAlertMessage) + str.tostring(longSlLow) + str.tostring(AfterSLMessage), alertFreq)

if enableSlMessage 
    alert(str.tostring(BuyAlertMessage) + str.tostring(longSlLow) + str.tostring(AfterSLMessage), alertFreq)


alertcondition(veryFinalAlert, title='Bullish divergence', message='Bull Div {{ticker}}')



////////////////// Backtester 



////////////////// Backtester input stuff

// Backtesting Range settings - 🔥 uncomment the 6 lines below to disable the alerts and enable the backtester 
//startDate = input.int(title='Start Date', defval=1, minval=1, maxval=31, group='Backtesting range')
//startMonth = input.int(title='Start Month', defval=1, minval=1, maxval=12, group='Backtesting range')
//startYear = input.int(title='Start Year', defval=2016, minval=1800, maxval=2100, group='Backtesting range')
//endDate = input.int(title='End Date', defval=1, minval=1, maxval=31, group='Backtesting range')
//endMonth = input.int(title='End Month', defval=1, minval=1, maxval=12, group='Backtesting range')
//endYear = input.int(title='End Year', defval=2040, minval=1800, maxval=2100, group='Backtesting range')
 
////////////////// Backtester 


// 🔥 uncomment the all lines below for the backtester and revert for alerts
shortTrading = false
longTrading = plotBull or plotHiddenBull
longTP1 = strategy.position_size > 0 ? strategy.position_avg_price * (1 + longTakeProfit1) : strategy.position_size < 0 ? strategy.position_avg_price * (1 - longTakeProfit1) : na


//longTP2 = strategy.position_size > 0 ? strategy.position_avg_price * (1 + longTakeProfit2) : strategy.position_size < 0 ? strategy.position_avg_price * (1 - longTakeProfit2) : na
//longTP3 = strategy.position_size > 0 ? strategy.position_avg_price * (1 + longTakeProfit3) : strategy.position_size < 0 ? strategy.position_avg_price * (1 - longTakeProfit3) : na
//longTP4 = strategy.position_size > 0 ? strategy.position_avg_price * (1 + longTakeProfit4) : strategy.position_size < 0 ? strategy.position_avg_price * (1 - longTakeProfit4) : na

longSL = strategy.position_size > 0 ? strategy.position_avg_price * (1 - longStopLoss) : strategy.position_size < 0 ? strategy.position_avg_price * (1 + longStopLoss) : na
strategy.risk.allow_entry_in(longTrading == true and shortTrading == true ? strategy.direction.all : longTrading == true ? strategy.direction.long : shortTrading == true ? strategy.direction.short : na)
strategy.entry('Bull', strategy.long, comment='B Long', when=FinalBullAlert or FinalBullAlert2)
strategy.entry('Bull', strategy.long, comment='HB Long', when=FinalHiddenBullAlert2 or FinalHiddenBullAlert)

// check which SL to use
if enableSL and whichSL == 'use the static % as SL'
    strategy.exit(id='longTP-SL', from_entry='Bull', limit=longTP1, stop=longSL)

//    strategy.exit(id='longTP-SL', qty_percent=25, from_entry='Bull', limit=longTP2, stop=longSlLow)
//    strategy.exit(id='longTP-SL', qty_percent=25, from_entry='Bull', limit=longTP3, stop=longSlLow)
//    strategy.exit(id='longTP-SL', qty_percent=25, from_entry='Bull', limit=longTP4, stop=longSlLow)

// get bars since last entry for the SL at low to work
barsSinceLastEntry()=>
    strategy.opentrades > 0 ? (bar_index - strategy.opentrades.entry_bar_index(strategy.opentrades-1)) : na
    
if enableSL and whichSL == 'use the low as SL' and ta.barssince(veryFinalAlert) < 2 and barsSinceLastEntry() < 2
    strategy.exit(id='longTP-SL', from_entry='Bull', limit=longTP1, stop=longSlLow)


//    strategy.exit(id='longTP-SL', qty_percent=25, from_entry='Bull', limit=longTP2, stop=longSlLow)
//    strategy.exit(id='longTP-SL', qty_percent=25, from_entry='Bull', limit=longTP3, stop=longSlLow)
//    strategy.exit(id='longTP-SL', qty_percent=25, from_entry='Bull', limit=longTP4, stop=longSlLow)
if not enableSL
    strategy.exit(id='longTP-SL', from_entry='Bull', limit=longTP1)

//    strategy.exit(id='longTP-SL', qty_percent=25, from_entry='Bull', limit=longTP2)
//    strategy.exit(id='longTP-SL', qty_percent=25, from_entry='Bull', limit=longTP3)
//    strategy.exit(id='longTP-SL', qty_percent=25, from_entry='Bull', limit=longTP4)

