A description of the divergence im trying to code is the following:
(for bullish)
Divergence is scanned for when the stochastic crosses up
Divergence is true if the lowest the price went from the last cross down to this current bar (cross up) is higher than the lowest price of the last pivot low ( 10,10 pivot) while the lowest RSI value since the last crossdown is lower than the lowest RSI value in the last pivot low.
I used the TradingView divergence indicator as reference but its very lagging
TLDR how to code hidden divergence without the offset but rather use a stochastic cross up.
My current code is below, the reason it is longer is because I want to scan multiple pivot sizes so the lest pivot 5,5 if divergence is false scan the last pivot 10,10 and 20,20 and lastly 40,40.
rangeHigh = 300
rangeLow = 5
tinyLow = ta.pivotlow(5,5)
midLow = ta.pivotlow(10,10)
bigLow = ta.pivotlow(20,20)
massiveLow = ta.pivotlow(40,40)
tinyHigh = ta.pivothigh(5,5)
midHigh = ta.pivothigh(10,10)
bigHigh = ta.pivothigh(20,20)
massiveHigh = ta.pivothigh(40,40)
RSItinyLow = ta.pivotlow(RSI,5,5)
RSImidLow = ta.pivotlow(RSI,10,10)
RSIbigLow = ta.pivotlow(RSI,20,20)
RSImassiveLow = ta.pivotlow(RSI,40,40)
RSItinyHigh = ta.pivothigh(RSI,5,5)
RSImidHigh = ta.pivothigh(RSI,10,10)
RSIbigHigh = ta.pivothigh(RSI,20,20)
RSImassiveHigh = ta.pivothigh(RSI,40,40)
//Finding Pivots RSI////////////////////////////////////////////////////////////////////////////////////////////////////////////////
tplF = na(RSItinyLow) ? false : true
mplF = na(RSImidLow) ? false : true
bplF = na(RSIbigLow) ? false : true
maplF = na(RSImassiveLow) ? false : true
tphF = na(RSItinyHigh) ? false : true
mphF = na(RSImidHigh) ? false : true
bphF = na(RSIbigHigh) ? false : true
maphF = na(RSImassiveHigh) ? false : true
//Finding Pivots Price/////////////////////////////////////////////////////////////////////////////////////////////////////////////
tplFP = na(tinyLow) ? false : true
mplFP = na(midLow) ? false : true
bplFP = na(bigLow) ? false : true
maplFP = na(massiveLow) ? false : true
tphFP = na(tinyHigh) ? false : true
mphFP = na(midHigh) ? false : true
bphFP = na(bigHigh) ? false : true
maphFP = na(massiveHigh) ? false : true
_inRange(cond) =>
bars = ta.barssince(cond == true)
rangeLow <= bars and bars <= rangeHigh
//Bullish Divergence///////////////////////////////////////////////////////////////////////////////////////////////////////////////
//RSI lower low
rsiTLL = RSI < ta.valuewhen(tplF,RSI,1) and _inRange(tplF[1])
rsiMLL = RSI < ta.valuewhen(mplF,RSI,1) and _inRange(mplF[1])
rsiMLL2 = RSI < ta.valuewhen(mplF,RSI,2) and _inRange(mplF[2])
rsiBLL = RSI < ta.valuewhen(bplF,RSI,1) and _inRange(bplF[1])
rsiBLL2 = RSI < ta.valuewhen(bplF,RSI,2) and _inRange(bplF[2])
rsiMALL = RSI < ta.valuewhen(maplF,RSI,1) and _inRange(maplF[1])
rsiMALL2 = RSI < ta.valuewhen(maplF,RSI,2) and _inRange(maplF[2])
//Price higher low
priceTHL = low > ta.valuewhen(tplF,low,1) and _inRange(tplF[1])
priceMHL = low > ta.valuewhen(mplF,low,1) and _inRange(tplF[1])
priceMHL2 = low > ta.valuewhen(mplF,low,2) and _inRange(mplF[2])
priceBHL = low > ta.valuewhen(bplF,low,1) and _inRange(bplF[1])
priceBHL2 = low > ta.valuewhen(bplF,low,2) and _inRange(bplF[2])
priceMAHL = low > ta.valuewhen(maplF,low,1) and _inRange(maplF[1])
//Defining Bull Divergence
hiddenBulltiny1 = rsiTLL and priceTHL and tplF
hiddenBullmid1 = rsiMLL and priceMHL
hiddenBullmid2 = rsiMLL2 and priceMHL2
hiddenBullbig1 = rsiBLL and priceBHL
hiddenBullbig2 = rsiBLL2 and priceBHL2
hiddenBullmassive1 = rsiMALL and priceMAHL
BullishHiddenDivergence = hiddenBulltiny1 //or hiddenBullmid1 or hiddenBullmid2 or hiddenBullbig1 or hiddenBullbig2 or hiddenBullmassive1
rsiColor = color.white
if BullishHiddenDivergence
rsiColor := color.red
plot(RSI,color=rsiColor)
The current code does not do what I'm trying to accomplish as the RSI becomes red at times where it shouldn't according to my parameters (as discribed not coded)