# Too Complex

#### C_Wilson

##### New member
I have the indicator I am trying to use for a scan. Its a divergence indicator and just want to see on a scan list when its true on the last bar .
Im getting too complex error.
Could someone help with this?

This is the code if it matter.
Ruby:
``````input Left_Bars = 5;
input Right_Bars = 5;
input Show_Hidden_Divergences = no;
input Check_Cut_Through_in_indicators = no;
input Show_Divergence_Number = no;
input Show_Indicator_Names = no;
input Show_Pivot_Points = yes;
input Change_Width_by_Number_of_Divergence = yes;
input Minimum_Number_of_Divergence = 1;
input calcmacd = yes;
input calcmacda = yes;
input calcrsi = yes;
input calcstoc = yes;
input calccci = yes;
input calcmom = yes;
input calcobv = yes;
input calcdi = yes;
input calcvwmacd = yes;
input calccmf = yes;
input calcmfi = yes;

script rma
{
input src = close;
input length = 14;

def alpha = 1.0 / length;
def sum = if IsNaN(sum[1]) then Average(src, length) else alpha * src + (1 - alpha) * sum[1];
plot rma = sum;
}
script vwma
{
input x = close;
input y = 12;
plot vwma = Average(x * volume, y) / Average(volume, y);
}
# RSI
def rsi = RSI(14);
# MACD
def macd = MACD(12, 26, 9).Value;
def signal = MACD(12, 26, 9).Avg;
def deltamacd = MACD(12, 26, 9).Diff;
# Momentum
def moment = Momentum(10);
# CCI
def cci = CCI(10);
# OBV
def Obv;
if (close - close[1] > 0)
{
Obv = Obv[1] + volume;
}
else if (close - close[1] < 0)
{
Obv = Obv[1] - volume;
}
else
{
Obv = Obv[1];
}
# Stoch
def stoch = 100 * (close - Lowest(low, 14)) / (Highest(high, 14) - Lowest(low, 14));
def stk = Average(stoch, 3);
# DIOSC
def DI = high - high[1] - -(low - low[1]);
#rma
def tr = Max(high - low, Max(AbsValue(high - close[1]), AbsValue(low - close[1])));
def trur = rma(tr, 14);
def diosc = 100 * rma(DI, 14) / trur;

# volume weighted macd
def maFast = vwma(close, 12);
def maSlow = vwma(close, 26);
def vwmacd = maFast - maSlow;
# Chaikin money flow
def Cmfm = ((close - low) - (high - close)) / (high - low);
def Cmfv = Cmfm * volume;
def cmf = Average(Cmfv, 21) / Average(volume, 21);
# Moneyt Flow Index
def Mfi = MoneyFlowIndex(20, 80, 14).MoneyFlowIndex;

def bn = BarNumber();
def totalBars = HighestAll(bn);

def hh = fold i = 1 to Right_Bars + 1 with p = 1 while p do high > GetValue(high, -i);
def top = if (bn > Left_Bars and high == Highest(high, Left_Bars+1) and hh) then high else Double.NaN;
def ll = fold j = 1 to Right_Bars + 1 with q = 1 while q do low < GetValue(low, -j);
def bot = if (bn > Left_Bars and low == Lowest(low, Left_Bars+1) and ll) then low else Double.NaN;

# Negative Divergence or Hidden Positive Divergence
def newtop = if high == Highest(high, Left_Bars+1) then high else Double.NaN; # check only left side
def newbot = if low == Lowest(low, Left_Bars+1) then low else Double.NaN; # check only left side

AddChartBubble (top and Show_Pivot_Points, high, "[PH]", Color.WHITE, yes);
AddChartBubble (bot and Show_Pivot_Points, low, "[PL]", Color.WHITE, no);

def cnt01 = if !IsNaN(top) then cnt01[1] + 1 else cnt01[1];
def cnt02 = if !IsNaN(bot) then cnt02[1] + 1 else cnt02[1];

def top01 = if cnt01%2==0 then top else double.nan;
def top02 = if cnt01%2!=0 then top else double.nan;
def bot01 = if cnt02%2==0 then bot else double.nan;
def bot02 = if cnt02%2!=0 then bot else double.nan;

def topc = if !IsNaN(top01) then 0 else topc[1] + 1;
def topc1 = if !IsNaN(top02) then 0 else topc1[1] + 1;

def botc = if !IsNaN(bot01) then 0 else botc[1] + 1;
def botc1 = if !IsNaN(bot02) then 0 else botc1[1] + 1;

script nocut1
{
input indi = close;
input len = close;

def temp = if IsNaN(GetValue(indi, len)) then 0.0 else GetValue(indi, len);
def diff = (indi - temp) / len;
def ln = indi;
plot nocut1 = fold x = 1 to len with p2 = yes while p2 do if GetValue(indi, x) > ln - diff * x then no else p2;
}

script nocut2
{
input indi = close;
input len = close;

def temp = if IsNaN(GetValue(indi, len)) then 0.0 else GetValue(indi, len);
def diff = (indi - temp) / len;
def ln = indi;
plot nocut1 = fold x = 1 to len with p2 = yes while p2 do if GetValue(indi, x) < ln - diff * x then no else p2;
}

def emptyh;
def diff = (newtop - GetValue(high, topc)) / topc;
def hline = newtop; # virtual line to check there is no close price higher than it
if (!IsNaN(newtop) and ((newtop > GetValue(high, topc) and !Show_Hidden_Divergences) or (newtop < GetValue(high, topc) and Show_Hidden_Divergences))) # there must not close price higher than the line between last PH and current high
{
emptyh = fold x = 1 to topc with p2 = yes while p2 do if GetValue(close, x) > hline - diff * x then no else p2;
}
else
{
emptyh = no;
}

def rsiok = nocut1(rsi, topc);
def macdok = nocut1(macd, topc);
def deltamacdok = nocut1(deltamacd, topc);
def momentok = nocut1(moment, topc);
def cciok = nocut1(cci, topc);
def obvok = nocut1(Obv, topc);
def stkok = nocut1(stk, topc);
def dioscok = nocut1(diosc, topc);
def vwmacdok = nocut1(vwmacd, topc);
def cmfok = nocut1(cmf, topc);
def mfiok = nocut1(Mfi, topc);

def v1;
def v2;
def v3;
def v4;
def v5;
def v6;
def v7;
def v8;
def v9;
def v10;
def v11;
def t1;
def t2;
def t3;
def t4;
def t5;
def t6;
def t7;
def t8;
def t9;
def t10;
def t11;
if emptyh and !IsNaN(newtop) and !Show_Hidden_Divergences
{
if (calcrsi and GetValue(rsi, topc) > rsi and (!Check_Cut_Through_in_indicators or rsiok))
{
v1 = 1;
t1 = 1;
}
else
{
v1 = 0;
t1 = 0;
}
if (calcmacd and GetValue(macd, topc) > macd and (!Check_Cut_Through_in_indicators or macdok))
{
v2 = 1;
t2 = 2;
}
else
{
v2 = 0;
t2 = 0;
}
if (calcmacda and GetValue(deltamacd, topc) > deltamacd and (!Check_Cut_Through_in_indicators or deltamacdok))
{
v3 = 1;
t3 = 3;
}
else
{
v3 = 0;
t3 = 0;
}
if (calcmom and GetValue(moment, topc) > moment and (!Check_Cut_Through_in_indicators or momentok))
{
v4 = 1;
t4 = 4;
}
else
{
v4 = 0;
t4 = 0;
}
if (calccci and GetValue(cci, topc) > cci and (!Check_Cut_Through_in_indicators or cciok))
{
v5 = 1;
t5 = 5;
}
else
{
v5 = 0;
t5 = 0;

}
if (calcobv and GetValue(Obv, topc) > Obv and (!Check_Cut_Through_in_indicators or obvok))
{
v6 = 1;
t6 = 6;
}
else
{
v6 = 0;
t6 = 0;
}
if (calcstoc and GetValue(stk, topc) > stk and (!Check_Cut_Through_in_indicators or stkok))
{
v7 = 1;
t7 = 7;
}
else
{
v7 = 0;
t7 = 0;
}
if (calcdi and GetValue(diosc, topc) > diosc and (!Check_Cut_Through_in_indicators or dioscok))
{
v8 = 1;
t8 = 8;
}
else
{
v8 = 0;
t8 = 0;
}
if (calcvwmacd and GetValue(vwmacd, topc) > vwmacd and (!Check_Cut_Through_in_indicators or vwmacdok))
{
v9 = 1;
t9 = 9;
}
else
{
v9 = 0;
t9 = 0;
}
if (calccmf and GetValue(cmf, topc) > cmf and (!Check_Cut_Through_in_indicators or cmfok))
{
v10 = 1;
t10 = 10;
}
else
{
v10 = 0;
t10 = 0;
}
if (calcmfi and GetValue(Mfi, topc) > Mfi and (!Check_Cut_Through_in_indicators or mfiok))
{
v11 = 1;
t11 = 11;
}
else
{
v11 = 0;
t11 = 0;
}
}
else
{
v1 = 0;
v2 = 0;
v3 = 0;
v4 = 0;
v5 = 0;
v6 = 0;
v7 = 0;
v8 = 0;
v9 = 0;
v10 = 0;
v11 = 0;
t1 = 0;
t2 = 0;
t3 = 0;
t4 = 0;
t5 = 0;
t6 = 0;
t7 = 0;
t8 = 0;
t9 = 0;
t10 = 0;
t11 = 0;
}
def negdivergence = v1 + v2 + v3 + v4 + v5 + v6 + v7 + v8 + v9 + v10 + v11;

def hv1;
def hv2;
def hv3;
def hv4;
def hv5;
def hv6;
def hv7;
def hv8;
def hv9;
def hv10;
def hv11;
def ht1;
def ht2;
def ht3;
def ht4;
def ht5;
def ht6;
def ht7;
def ht8;
def ht9;
def ht10;
def ht11;
if emptyh and !IsNaN(newtop) and Show_Hidden_Divergences
{
if (calcrsi and GetValue(rsi, topc) < rsi and (!Check_Cut_Through_in_indicators or rsiok))
{
hv1 = 1;
ht1 = 1;
}
else
{
hv1 = 0;
ht1 = 0;
}
if (calcmacd and GetValue(macd, topc) < macd and (!Check_Cut_Through_in_indicators or macdok))
{
hv2 = 1;
ht2 = 2;
}
else
{
hv2 = 0;
ht2 = 0;
}
if (calcmacda and GetValue(deltamacd, topc) < deltamacd and (!Check_Cut_Through_in_indicators or deltamacdok))
{
hv3 = 1;
ht3 = 3;
}
else
{
hv3 = 0;
ht3 = 0;
}
if (calcmom and GetValue(moment, topc) < moment and (!Check_Cut_Through_in_indicators or momentok))
{
hv4 = 1;
ht4 = 4;
}
else
{
hv4 = 0;
ht4 = 0;
}
if (calccci and GetValue(cci, topc) < cci and (!Check_Cut_Through_in_indicators or cciok))
{
hv5 = 1;
ht5 = 5;
}
else
{
hv5 = 0;
ht5 = 0;

}
if (calcobv and GetValue(Obv, topc) < Obv and (!Check_Cut_Through_in_indicators or obvok))
{
hv6 = 1;
ht6 = 6;
}
else
{
hv6 = 0;
ht6 = 0;
}
if (calcstoc and GetValue(stk, topc) < stk and (!Check_Cut_Through_in_indicators or stkok))
{
hv7 = 1;
ht7 = 7;
}
else
{
hv7 = 0;
ht7 = 0;
}
if (calcdi and GetValue(diosc, topc) < diosc and (!Check_Cut_Through_in_indicators or dioscok))
{
hv8 = 1;
ht8 = 8;
}
else
{
hv8 = 0;
ht8 = 0;
}
if (calcvwmacd and GetValue(vwmacd, topc) < vwmacd and (!Check_Cut_Through_in_indicators or vwmacdok))
{
hv9 = 1;
ht9 = 9;
}
else
{
hv9 = 0;
ht9 = 0;
}
if (calccmf and GetValue(cmf, topc) < cmf and (!Check_Cut_Through_in_indicators or cmfok))
{
hv10 = 1;
ht10 = 10;
}
else
{
hv10 = 0;
ht10 = 0;
}
if (calcmfi and GetValue(Mfi, topc) < Mfi and (!Check_Cut_Through_in_indicators or mfiok))
{
hv11 = 1;
ht11 = 11;
}
else
{
hv11 = 0;
ht11 = 0;
}
}
else
{
hv1 = 0;
hv2 = 0;
hv3 = 0;
hv4 = 0;
hv5 = 0;
hv6 = 0;
hv7 = 0;
hv8 = 0;
hv9 = 0;
hv10 = 0;
hv11 = 0;
ht1 = 0;
ht2 = 0;
ht3 = 0;
ht4 = 0;
ht5 = 0;
ht6 = 0;
ht7 = 0;
ht8 = 0;
ht9 = 0;
ht10 = 0;
ht11 = 0;
}
def hposdivergence = hv1 + hv2 + hv3 + hv4 + hv5 + hv6 + hv7 + hv8 + hv9 + hv10 + hv11;

# Negative Divergence or Hidden Positive Divergence

def emptyh2;
def diff2 = (newtop - GetValue(high, topc1)) / topc1;
def hline2 = newtop; # virtual line to check there is no close price higher than it
if (!IsNaN(newtop) and ((newtop > GetValue(high, topc1) and !Show_Hidden_Divergences) or (newtop < GetValue(high, topc1) and Show_Hidden_Divergences))) # there must not close price higher than the line between last PH and current high
{
emptyh2 = fold x2 = 1 to topc1 with p3 = yes while p3 do if GetValue(close, x2) > hline2 - diff2 * x2 then no else p3;
}
else
{
emptyh2 = no;
}

def rsiok2 = nocut1(rsi, topc1);
def macdok2 = nocut1(macd, topc1);
def deltamacdok2 = nocut1(deltamacd, topc1);
def momentok2 = nocut1(moment, topc1);
def cciok2 = nocut1(cci, topc1);
def obvok2 = nocut1(Obv, topc1);
def stkok2 = nocut1(stk, topc1);
def dioscok2 = nocut1(diosc, topc1);
def vwmacdok2 = nocut1(vwmacd, topc1);
def cmfok2 = nocut1(cmf, topc1);
def mfiok2 = nocut1(Mfi, topc1);

def v1_2;
def v2_2;
def v3_2;
def v4_2;
def v5_2;
def v6_2;
def v7_2;
def v8_2;
def v9_2;
def v10_2;
def v11_2;
def t1_2;
def t2_2;
def t3_2;
def t4_2;
def t5_2;
def t6_2;
def t7_2;
def t8_2;
def t9_2;
def t10_2;
def t11_2;
if emptyh2 and !IsNaN(newtop) and !Show_Hidden_Divergences
{
if (calcrsi and GetValue(rsi, topc1) > rsi and (!Check_Cut_Through_in_indicators or rsiok2))
{
v1_2 = 1;
t1_2 = 1;
}
else
{
v1_2 = 0;
t1_2 = 0;
}
if (calcmacd and GetValue(macd, topc1) > macd and (!Check_Cut_Through_in_indicators or macdok2))
{
v2_2 = 1;
t2_2 = 2;
}
else
{
v2_2 = 0;
t2_2 = 0;
}
if (calcmacda and GetValue(deltamacd, topc1) > deltamacd and (!Check_Cut_Through_in_indicators or deltamacdok2))
{
v3_2 = 1;
t3_2 = 3;
}
else
{
v3_2 = 0;
t3_2 = 0;
}
if (calcmom and GetValue(moment, topc1) > moment and (!Check_Cut_Through_in_indicators or momentok2))
{
v4_2 = 1;
t4_2 = 4;
}
else
{
v4_2 = 0;
t4_2 = 0;
}
if (calccci and GetValue(cci, topc1) > cci and (!Check_Cut_Through_in_indicators or cciok2))
{
v5_2 = 1;
t5_2 = 5;
}
else
{
v5_2 = 0;
t5_2 = 0;

}
if (calcobv and GetValue(Obv, topc1) > Obv and (!Check_Cut_Through_in_indicators or obvok2))
{
v6_2 = 1;
t6_2 = 6;
}
else
{
v6_2 = 0;
t6_2 = 0;
}
if (calcstoc and GetValue(stk, topc1) > stk and (!Check_Cut_Through_in_indicators or stkok2))
{
v7_2 = 1;
t7_2 = 7;
}
else
{
v7_2 = 0;
t7_2 = 0;
}
if (calcdi and GetValue(diosc, topc1) > diosc and (!Check_Cut_Through_in_indicators or dioscok2))
{
v8_2 = 1;
t8_2 = 8;
}
else
{
v8_2 = 0;
t8_2 = 0;
}
if (calcvwmacd and GetValue(vwmacd, topc1) > vwmacd and (!Check_Cut_Through_in_indicators or vwmacdok2))
{
v9_2 = 1;
t9_2 = 9;
}
else
{
v9_2 = 0;
t9_2 = 0;
}
if (calccmf and GetValue(cmf, topc1) > cmf and (!Check_Cut_Through_in_indicators or cmfok2))
{
v10_2 = 1;
t10_2 = 10;
}
else
{
v10_2 = 0;
t10_2 = 0;
}
if (calcmfi and GetValue(Mfi, topc1) > Mfi and (!Check_Cut_Through_in_indicators or mfiok2))
{
v11_2 = 1;
t11_2 = 11;
}
else
{
v11_2 = 0;
t11_2 = 0;
}
}
else
{
v1_2 = 0;
v2_2 = 0;
v3_2 = 0;
v4_2 = 0;
v5_2 = 0;
v6_2 = 0;
v7_2 = 0;
v8_2 = 0;
v9_2 = 0;
v10_2 = 0;
v11_2 = 0;
t1_2 = 0;
t2_2 = 0;
t3_2 = 0;
t4_2 = 0;
t5_2 = 0;
t6_2 = 0;
t7_2 = 0;
t8_2 = 0;
t9_2 = 0;
t10_2 = 0;
t11_2 = 0;
}
def negdivergence2 = v1_2 + v2_2 + v3_2 + v4_2 + v5_2 + v6_2 + v7_2 + v8_2 + v9_2 + v10_2 + v11_2;

def hv1_2;
def hv2_2;
def hv3_2;
def hv4_2;
def hv5_2;
def hv6_2;
def hv7_2;
def hv8_2;
def hv9_2;
def hv10_2;
def hv11_2;
def ht1_2;
def ht2_2;
def ht3_2;
def ht4_2;
def ht5_2;
def ht6_2;
def ht7_2;
def ht8_2;
def ht9_2;
def ht10_2;
def ht11_2;
if emptyh2 and !IsNaN(newtop) and Show_Hidden_Divergences
{
if (calcrsi and GetValue(rsi, topc1) < rsi and (!Check_Cut_Through_in_indicators or rsiok2))
{
hv1_2 = 1;
ht1_2 = 1;
}
else
{
hv1_2 = 0;
ht1_2 = 0;
}
if (calcmacd and GetValue(macd, topc1) < macd and (!Check_Cut_Through_in_indicators or macdok2))
{
hv2_2 = 1;
ht2_2 = 2;
}
else
{
hv2_2 = 0;
ht2_2 = 0;
}
if (calcmacda and GetValue(deltamacd, topc1) < deltamacd and (!Check_Cut_Through_in_indicators or deltamacdok2))
{
hv3_2 = 1;
ht3_2 = 3;
}
else
{
hv3_2 = 0;
ht3_2 = 0;
}
if (calcmom and GetValue(moment, topc1) < moment and (!Check_Cut_Through_in_indicators or momentok2))
{
hv4_2 = 1;
ht4_2 = 4;
}
else
{
hv4_2 = 0;
ht4_2 = 0;
}
if (calccci and GetValue(cci, topc1) < cci and (!Check_Cut_Through_in_indicators or cciok2))
{
hv5_2 = 1;
ht5_2 = 5;
}
else
{
hv5_2 = 0;
ht5_2 = 0;

}
if (calcobv and GetValue(Obv, topc1) < Obv and (!Check_Cut_Through_in_indicators or obvok2))
{
hv6_2 = 1;
ht6_2 = 6;
}
else
{
hv6_2 = 0;
ht6_2 = 0;
}
if (calcstoc and GetValue(stk, topc1) < stk and (!Check_Cut_Through_in_indicators or stkok2))
{
hv7_2 = 1;
ht7_2 = 7;
}
else
{
hv7_2 = 0;
ht7_2 = 0;
}
if (calcdi and GetValue(diosc, topc1) < diosc and (!Check_Cut_Through_in_indicators or dioscok2))
{
hv8_2 = 1;
ht8_2 = 8;
}
else
{
hv8_2 = 0;
ht8_2 = 0;
}
if (calcvwmacd and GetValue(vwmacd, topc1) < vwmacd and (!Check_Cut_Through_in_indicators or vwmacdok2))
{
hv9_2 = 1;
ht9_2 = 9;
}
else
{
hv9_2 = 0;
ht9_2 = 0;
}
if (calccmf and GetValue(cmf, topc1) < cmf and (!Check_Cut_Through_in_indicators or cmfok2))
{
hv10_2 = 1;
ht10_2 = 10;
}
else
{
hv10_2 = 0;
ht10_2 = 0;
}
if (calcmfi and GetValue(Mfi, topc1) < Mfi and (!Check_Cut_Through_in_indicators or mfiok2))
{
hv11_2 = 1;
ht11_2 = 11;
}
else
{
hv11_2 = 0;
ht11_2 = 0;
}
}
else
{
hv1_2 = 0;
hv2_2 = 0;
hv3_2 = 0;
hv4_2 = 0;
hv5_2 = 0;
hv6_2 = 0;
hv7_2 = 0;
hv8_2 = 0;
hv9_2 = 0;
hv10_2 = 0;
hv11_2 = 0;
ht1_2 = 0;
ht2_2 = 0;
ht3_2 = 0;
ht4_2 = 0;
ht5_2 = 0;
ht6_2 = 0;
ht7_2 = 0;
ht8_2 = 0;
ht9_2 = 0;
ht10_2 = 0;
ht11_2 = 0;
}
def hposdivergence2 = hv1_2 + hv2_2 + hv3_2 + hv4_2 + hv5_2 + hv6_2 + hv7_2 + hv8_2 + hv9_2 + hv10_2 + hv11_2;

def condLine = negdivergence >= Minimum_Number_of_Divergence or hposdivergence >= Minimum_Number_of_Divergence;
def condLine2 = negdivergence2 >= Minimum_Number_of_Divergence or hposdivergence2 >= Minimum_Number_of_Divergence;

def newwd = if !Show_Hidden_Divergences then (if !Change_Width_by_Number_of_Divergence then 2 else if negdivergence <= 2 then 2 else if negdivergence <= 5 then 3 else if negdivergence <= 8 then 4 else 5) else (if !Change_Width_by_Number_of_Divergence then 2 else if hposdivergence <= 2 then 2 else if hposdivergence <= 5 then 3 else if hposdivergence <= 8 then 4 else 5);

def newwd2 = if !Show_Hidden_Divergences then (if !Change_Width_by_Number_of_Divergence then 2 else if negdivergence2 <= 2 then 2 else if negdivergence2 <= 5 then 3 else if negdivergence2 <= 8 then 4 else 5) else (if !Change_Width_by_Number_of_Divergence then 2 else if hposdivergence2 <= 2 then 2 else if hposdivergence2 <= 5 then 3 else if hposdivergence2 <= 8 then 4 else 5);

def tt0 = fold i1 = 0 to totalBars + 1 - bn with k1 = 0 while GetValue(topc,-i1)<GetValue(topc1,-i1) do k1+1;
def tt = if topc < topc1 or topc1==0 then tt0 else -1;
def tt2 = fold i2 = 0 to tt + 1 with k2 = -1 do if GetValue(condLine, -i2) then i2 else k2;
def newwd3 = fold i3 = 0 to tt + 1 with k3 = 2 do if GetValue(condLine, -i3) then GetValue(newwd,-i3) else k3;
def plot1 = if tt2 >= 0 then GetValue(high, -tt2) - (GetValue(high, -tt2) - GetValue(high, topc)) / (topc + tt2) * tt2 else Double.NaN;

def tt02 = fold i4 = 0 to totalBars + 1 - bn with k4 = 0 while GetValue(topc,-i4)>GetValue(topc1,-i4) do k4+1;
def tt4 = if topc > topc1 or topc==0 then tt02 else -1;
def tt5 = fold i5 = 0 to tt4 + 1 with k5 = -1 do if GetValue(condLine2, -i5) then i5 else k5;
def newwd6 = fold i6 = 0 to tt4 + 1 with k6 = 2 do if GetValue(condLine2, -i6) then GetValue(newwd2,-i6) else k6;
def plot2 = if tt5 >= 0 then GetValue(high, -tt5) - (GetValue(high, -tt5) - GetValue(high, topc1)) / (topc1 + tt5) * tt5 else Double.NaN;

plot test1 = if condLine then close else double.nan;
plot test2 = if condLine2 then close else double.nan;

plot Div1 = if newwd3 == 2 and !isnan(plot1) then plot1 else Double.NaN;
plot Div2 = if newwd3 == 3 and !isnan(plot1) then plot1 else Double.NaN;
plot Div3 = if newwd3 == 4 and !isnan(plot1) then plot1 else Double.NaN;
plot Div4 = if newwd3 == 5 and !isnan(plot1) then plot1 else Double.NaN;
Div1.SetDefaultColor(Color.RED);
Div2.SetDefaultColor(Color.RED);
Div3.SetDefaultColor(Color.RED);
Div4.SetDefaultColor(Color.RED);
Div1.SetLineWeight(2);
Div2.SetLineWeight(3);
Div3.SetLineWeight(4);
Div4.SetLineWeight(5);

plot Div12 = if newwd6 == 2 and !isnan(plot2) then plot2 else Double.NaN;
plot Div22 = if newwd6 == 3 and !isnan(plot2) then plot2 else Double.NaN;
plot Div32 = if newwd6 == 4 and !isnan(plot2) then plot2 else Double.NaN;
plot Div42 = if newwd6 == 5 and !isnan(plot2) then plot2 else Double.NaN;
Div12.SetDefaultColor(Color.RED);
Div22.SetDefaultColor(Color.RED);
Div32.SetDefaultColor(Color.RED);
Div42.SetDefaultColor(Color.RED);
Div12.SetLineWeight(2);
Div22.SetLineWeight(3);
Div32.SetLineWeight(4);
Div42.SetLineWeight(5);

def emptyl;
def ldiff = (newbot - GetValue(low, botc)) / botc;
def lline = newbot; # virtual line to check there is no close price higher than it
if (!IsNaN(newbot) and ((newbot < GetValue(low, botc) and !Show_Hidden_Divergences) or (newbot > GetValue(low, botc) and Show_Hidden_Divergences))) # there must not close price higher than the line between last PH and current low
{
emptyl = fold x3 = 1 to botc with p4 = yes while p4 do if GetValue(close, x3) < lline - ldiff * x3 then no else p4;
}
else
{
emptyl = no;
}

def rsiok3 = nocut2(rsi, botc);
def macdok3 = nocut2(macd, botc);
def deltamacdok3 = nocut2(deltamacd, botc);
def momentok3 = nocut2(moment, botc);
def cciok3 = nocut2(cci, botc);
def obvok3 = nocut2(Obv, botc);
def stkok3 = nocut2(stk, botc);
def dioscok3 = nocut2(diosc, botc);
def vwmacdok3 = nocut2(vwmacd, botc);
def cmfok3 = nocut2(cmf, botc);
def mfiok3 = nocut2(Mfi, botc);

def lv1;
def lv2;
def lv3;
def lv4;
def lv5;
def lv6;
def lv7;
def lv8;
def lv9;
def lv10;
def lv11;
def lt1;
def lt2;
def lt3;
def lt4;
def lt5;
def lt6;
def lt7;
def lt8;
def lt9;
def lt10;
def lt11;
if emptyl and !IsNaN(newbot) and !Show_Hidden_Divergences
{
if (calcrsi and GetValue(rsi, botc) < rsi and (!Check_Cut_Through_in_indicators or rsiok3))
{
lv1 = 1;
lt1 = 1;
}
else
{
lv1 = 0;
lt1 = 0;
}
if (calcmacd and GetValue(macd, botc) < macd and (!Check_Cut_Through_in_indicators or macdok3))
{
lv2 = 1;
lt2 = 2;
}
else
{
lv2 = 0;
lt2 = 0;
}
if (calcmacda and GetValue(deltamacd, botc) < deltamacd and (!Check_Cut_Through_in_indicators or deltamacdok3))
{
lv3 = 1;
lt3 = 3;
}
else
{
lv3 = 0;
lt3 = 0;
}
if (calcmom and GetValue(moment, botc) < moment and (!Check_Cut_Through_in_indicators or momentok3))
{
lv4 = 1;
lt4 = 4;
}
else
{
lv4 = 0;
lt4 = 0;
}
if (calccci and GetValue(cci, botc) < cci and (!Check_Cut_Through_in_indicators or cciok3))
{
lv5 = 1;
lt5 = 5;
}
else
{
lv5 = 0;
lt5 = 0;

}
if (calcobv and GetValue(Obv, botc) < Obv and (!Check_Cut_Through_in_indicators or obvok3))
{
lv6 = 1;
lt6 = 6;
}
else
{
lv6 = 0;
lt6 = 0;
}
if (calcstoc and GetValue(stk, botc) < stk and (!Check_Cut_Through_in_indicators or stkok3))
{
lv7 = 1;
lt7 = 7;
}
else
{
lv7 = 0;
lt7 = 0;
}
if (calcdi and GetValue(diosc, botc) < diosc and (!Check_Cut_Through_in_indicators or dioscok3))
{
lv8 = 1;
lt8 = 8;
}
else
{
lv8 = 0;
lt8 = 0;
}
if (calcvwmacd and GetValue(vwmacd, botc) < vwmacd and (!Check_Cut_Through_in_indicators or vwmacdok3))
{
lv9 = 1;
lt9 = 9;
}
else
{
lv9 = 0;
lt9 = 0;
}
if (calccmf and GetValue(cmf, botc) < cmf and (!Check_Cut_Through_in_indicators or cmfok3))
{
lv10 = 1;
lt10 = 10;
}
else
{
lv10 = 0;
lt10 = 0;
}
if (calcmfi and GetValue(Mfi, botc) < Mfi and (!Check_Cut_Through_in_indicators or mfiok3))
{
lv11 = 1;
lt11 = 11;
}
else
{
lv11 = 0;
lt11 = 0;
}
}
else
{
lv1 = 0;
lv2 = 0;
lv3 = 0;
lv4 = 0;
lv5 = 0;
lv6 = 0;
lv7 = 0;
lv8 = 0;
lv9 = 0;
lv10 = 0;
lv11 = 0;
lt1 = 0;
lt2 = 0;
lt3 = 0;
lt4 = 0;
lt5 = 0;
lt6 = 0;
lt7 = 0;
lt8 = 0;
lt9 = 0;
lt10 = 0;
lt11 = 0;
}
def lnegdivergence = lv1 + lv2 + lv3 + lv4 + lv5 + lv6 + lv7 + lv8 + lv9 + lv10 + lv11;

def lhv1;
def lhv2;
def lhv3;
def lhv4;
def lhv5;
def lhv6;
def lhv7;
def lhv8;
def lhv9;
def lhv10;
def lhv11;
def lht1;
def lht2;
def lht3;
def lht4;
def lht5;
def lht6;
def lht7;
def lht8;
def lht9;
def lht10;
def lht11;
if emptyl and !IsNaN(newbot) and Show_Hidden_Divergences
{
if (calcrsi and GetValue(rsi, botc) > rsi and (!Check_Cut_Through_in_indicators or rsiok3))
{
lhv1 = 1;
lht1 = 1;
}
else
{
lhv1 = 0;
lht1 = 0;
}
if (calcmacd and GetValue(macd, botc) > macd and (!Check_Cut_Through_in_indicators or macdok3))
{
lhv2 = 1;
lht2 = 2;
}
else
{
lhv2 = 0;
lht2 = 0;
}
if (calcmacda and GetValue(deltamacd, botc) > deltamacd and (!Check_Cut_Through_in_indicators or deltamacdok3))
{
lhv3 = 1;
lht3 = 3;
}
else
{
lhv3 = 0;
lht3 = 0;
}
if (calcmom and GetValue(moment, botc) > moment and (!Check_Cut_Through_in_indicators or momentok3))
{
lhv4 = 1;
lht4 = 4;
}
else
{
lhv4 = 0;
lht4 = 0;
}
if (calccci and GetValue(cci, botc) > cci and (!Check_Cut_Through_in_indicators or cciok3))
{
lhv5 = 1;
lht5 = 5;
}
else
{
lhv5 = 0;
lht5 = 0;

}
if (calcobv and GetValue(Obv, botc) > Obv and (!Check_Cut_Through_in_indicators or obvok3))
{
lhv6 = 1;
lht6 = 6;
}
else
{
lhv6 = 0;
lht6 = 0;
}
if (calcstoc and GetValue(stk, botc) > stk and (!Check_Cut_Through_in_indicators or stkok3))
{
lhv7 = 1;
lht7 = 7;
}
else
{
lhv7 = 0;
lht7 = 0;
}
if (calcdi and GetValue(diosc, botc) > diosc and (!Check_Cut_Through_in_indicators or dioscok3))
{
lhv8 = 1;
lht8 = 8;
}
else
{
lhv8 = 0;
lht8 = 0;
}
if (calcvwmacd and GetValue(vwmacd, botc) > vwmacd and (!Check_Cut_Through_in_indicators or vwmacdok3))
{
lhv9 = 1;
lht9 = 9;
}
else
{
lhv9 = 0;
lht9 = 0;
}
if (calccmf and GetValue(cmf, botc) > cmf and (!Check_Cut_Through_in_indicators or cmfok3))
{
lhv10 = 1;
lht10 = 10;
}
else
{
lhv10 = 0;
lht10 = 0;
}
if (calcmfi and GetValue(Mfi, botc) > Mfi and (!Check_Cut_Through_in_indicators or mfiok3))
{
lhv11 = 1;
lht11 = 11;
}
else
{
lhv11 = 0;
lht11 = 0;
}
}
else
{
lhv1 = 0;
lhv2 = 0;
lhv3 = 0;
lhv4 = 0;
lhv5 = 0;
lhv6 = 0;
lhv7 = 0;
lhv8 = 0;
lhv9 = 0;
lhv10 = 0;
lhv11 = 0;
lht1 = 0;
lht2 = 0;
lht3 = 0;
lht4 = 0;
lht5 = 0;
lht6 = 0;
lht7 = 0;
lht8 = 0;
lht9 = 0;
lht10 = 0;
lht11 = 0;
}
def lhposdivergence = lhv1 + lhv2 + lhv3 + lhv4 + lhv5 + lhv6 + lhv7 + lhv8 + lhv9 + lhv10 + lhv11;

# Negative Divergence or Hidden Positive Divergence

def emptyl2;
def ldiff2 = (newbot - GetValue(low, botc1)) / botc1;
def lline2 = newbot; # virtual line to check there is no close price higher than it
if (!IsNaN(newbot) and ((newbot < GetValue(low, botc1) and !Show_Hidden_Divergences) or (newbot > GetValue(low, botc1) and Show_Hidden_Divergences))) # there must not close price higher than the line between last PH and current low
{
emptyl2 = fold x4 = 1 to botc1 with p5 = yes while p5 do if GetValue(close, x4) < lline2 - ldiff2 * x4 then no else p5;
}
else
{
emptyl2 = no;
}

def rsiok4 = nocut2(rsi, botc1);
def macdok4 = nocut2(macd, botc1);
def deltamacdok4 = nocut2(deltamacd, botc1);
def momentok4 = nocut2(moment, botc1);
def cciok4 = nocut2(cci, botc1);
def obvok4 = nocut2(Obv, botc1);
def stkok4 = nocut2(stk, botc1);
def dioscok4 = nocut2(diosc, botc1);
def vwmacdok4 = nocut2(vwmacd, botc1);
def cmfok4 = nocut2(cmf, botc1);
def mfiok4 = nocut2(Mfi, botc1);

def lv1_2;
def lv2_2;
def lv3_2;
def lv4_2;
def lv5_2;
def lv6_2;
def lv7_2;
def lv8_2;
def lv9_2;
def lv10_2;
def lv11_2;
def lt1_2;
def lt2_2;
def lt3_2;
def lt4_2;
def lt5_2;
def lt6_2;
def lt7_2;
def lt8_2;
def lt9_2;
def lt10_2;
def lt11_2;
if emptyl2 and !IsNaN(newbot) and !Show_Hidden_Divergences
{
if (calcrsi and GetValue(rsi, botc1) < rsi and (!Check_Cut_Through_in_indicators or rsiok4))
{
lv1_2 = 1;
lt1_2 = 1;
}
else
{
lv1_2 = 0;
lt1_2 = 0;
}
if (calcmacd and GetValue(macd, botc1) < macd and (!Check_Cut_Through_in_indicators or macdok4))
{
lv2_2 = 1;
lt2_2 = 2;
}
else
{
lv2_2 = 0;
lt2_2 = 0;
}
if (calcmacda and GetValue(deltamacd, botc1) < deltamacd and (!Check_Cut_Through_in_indicators or deltamacdok4))
{
lv3_2 = 1;
lt3_2 = 3;
}
else
{
lv3_2 = 0;
lt3_2 = 0;
}
if (calcmom and GetValue(moment, botc1) < moment and (!Check_Cut_Through_in_indicators or momentok4))
{
lv4_2 = 1;
lt4_2 = 4;
}
else
{
lv4_2 = 0;
lt4_2 = 0;
}
if (calccci and GetValue(cci, botc1) < cci and (!Check_Cut_Through_in_indicators or cciok4))
{
lv5_2 = 1;
lt5_2 = 5;
}
else
{
lv5_2 = 0;
lt5_2 = 0;

}
if (calcobv and GetValue(Obv, botc1) < Obv and (!Check_Cut_Through_in_indicators or obvok4))
{
lv6_2 = 1;
lt6_2 = 6;
}
else
{
lv6_2 = 0;
lt6_2 = 0;
}
if (calcstoc and GetValue(stk, botc1) < stk and (!Check_Cut_Through_in_indicators or stkok4))
{
lv7_2 = 1;
lt7_2 = 7;
}
else
{
lv7_2 = 0;
lt7_2 = 0;
}
if (calcdi and GetValue(diosc, botc1) < diosc and (!Check_Cut_Through_in_indicators or dioscok4))
{
lv8_2 = 1;
lt8_2 = 8;
}
else
{
lv8_2 = 0;
lt8_2 = 0;
}
if (calcvwmacd and GetValue(vwmacd, botc1) < vwmacd and (!Check_Cut_Through_in_indicators or vwmacdok4))
{
lv9_2 = 1;
lt9_2 = 9;
}
else
{
lv9_2 = 0;
lt9_2 = 0;
}
if (calccmf and GetValue(cmf, botc1) < cmf and (!Check_Cut_Through_in_indicators or cmfok4))
{
lv10_2 = 1;
lt10_2 = 10;
}
else
{
lv10_2 = 0;
lt10_2 = 0;
}
if (calcmfi and GetValue(Mfi, botc1) < Mfi and (!Check_Cut_Through_in_indicators or mfiok4))
{
lv11_2 = 1;
lt11_2 = 11;
}
else
{
lv11_2 = 0;
lt11_2 = 0;
}
}
else
{
lv1_2 = 0;
lv2_2 = 0;
lv3_2 = 0;
lv4_2 = 0;
lv5_2 = 0;
lv6_2 = 0;
lv7_2 = 0;
lv8_2 = 0;
lv9_2 = 0;
lv10_2 = 0;
lv11_2 = 0;
lt1_2 = 0;
lt2_2 = 0;
lt3_2 = 0;
lt4_2 = 0;
lt5_2 = 0;
lt6_2 = 0;
lt7_2 = 0;
lt8_2 = 0;
lt9_2 = 0;
lt10_2 = 0;
lt11_2 = 0;
}
def lnegdivergence2 = lv1_2 + lv2_2 + lv3_2 + lv4_2 + lv5_2 + lv6_2 + lv7_2 + lv8_2 + lv9_2 + lv10_2 + lv11_2;

def lhv1_2;
def lhv2_2;
def lhv3_2;
def lhv4_2;
def lhv5_2;
def lhv6_2;
def lhv7_2;
def lhv8_2;
def lhv9_2;
def lhv10_2;
def lhv11_2;
def lht1_2;
def lht2_2;
def lht3_2;
def lht4_2;
def lht5_2;
def lht6_2;
def lht7_2;
def lht8_2;
def lht9_2;
def lht10_2;
def lht11_2;
if emptyl2 and !IsNaN(newbot) and Show_Hidden_Divergences
{
if (calcrsi and GetValue(rsi, botc1) > rsi and (!Check_Cut_Through_in_indicators or rsiok4))
{
lhv1_2 = 1;
lht1_2 = 1;
}
else
{
lhv1_2 = 0;
lht1_2 = 0;
}
if (calcmacd and GetValue(macd, botc1) > macd and (!Check_Cut_Through_in_indicators or macdok4))
{
lhv2_2 = 1;
lht2_2 = 2;
}
else
{
lhv2_2 = 0;
lht2_2 = 0;
}
if (calcmacda and GetValue(deltamacd, botc1) > deltamacd and (!Check_Cut_Through_in_indicators or deltamacdok4))
{
lhv3_2 = 1;
lht3_2 = 3;
}
else
{
lhv3_2 = 0;
lht3_2 = 0;
}
if (calcmom and GetValue(moment, botc1) > moment and (!Check_Cut_Through_in_indicators or momentok4))
{
lhv4_2 = 1;
lht4_2 = 4;
}
else
{
lhv4_2 = 0;
lht4_2 = 0;
}
if (calccci and GetValue(cci, botc1) > cci and (!Check_Cut_Through_in_indicators or cciok4))
{
lhv5_2 = 1;
lht5_2 = 5;
}
else
{
lhv5_2 = 0;
lht5_2 = 0;

}
if (calcobv and GetValue(Obv, botc1) > Obv and (!Check_Cut_Through_in_indicators or obvok4))
{
lhv6_2 = 1;
lht6_2 = 6;
}
else
{
lhv6_2 = 0;
lht6_2 = 0;
}
if (calcstoc and GetValue(stk, botc1) > stk and (!Check_Cut_Through_in_indicators or stkok4))
{
lhv7_2 = 1;
lht7_2 = 7;
}
else
{
lhv7_2 = 0;
lht7_2 = 0;
}
if (calcdi and GetValue(diosc, botc1) > diosc and (!Check_Cut_Through_in_indicators or dioscok4))
{
lhv8_2 = 1;
lht8_2 = 8;
}
else
{
lhv8_2 = 0;
lht8_2 = 0;
}
if (calcvwmacd and GetValue(vwmacd, botc1) > vwmacd and (!Check_Cut_Through_in_indicators or vwmacdok4))
{
lhv9_2 = 1;
lht9_2 = 9;
}
else
{
lhv9_2 = 0;
lht9_2 = 0;
}
if (calccmf and GetValue(cmf, botc1) > cmf and (!Check_Cut_Through_in_indicators or cmfok4))
{
lhv10_2 = 1;
lht10_2 = 10;
}
else
{
lhv10_2 = 0;
lht10_2 = 0;
}
if (calcmfi and GetValue(Mfi, botc1) > Mfi and (!Check_Cut_Through_in_indicators or mfiok4))
{
lhv11_2 = 1;
lht11_2 = 11;
}
else
{
lhv11_2 = 0;
lht11_2 = 0;
}
}
else
{
lhv1_2 = 0;
lhv2_2 = 0;
lhv3_2 = 0;
lhv4_2 = 0;
lhv5_2 = 0;
lhv6_2 = 0;
lhv7_2 = 0;
lhv8_2 = 0;
lhv9_2 = 0;
lhv10_2 = 0;
lhv11_2 = 0;
lht1_2 = 0;
lht2_2 = 0;
lht3_2 = 0;
lht4_2 = 0;
lht5_2 = 0;
lht6_2 = 0;
lht7_2 = 0;
lht8_2 = 0;
lht9_2 = 0;
lht10_2 = 0;
lht11_2 = 0;
}
def lhposdivergence2 = lhv1_2 + lhv2_2 + lhv3_2 + lhv4_2 + lhv5_2 + lhv6_2 + lhv7_2 + lhv8_2 + lhv9_2 + lhv10_2 + lhv11_2;

def lcondLine = lnegdivergence >= Minimum_Number_of_Divergence or lhposdivergence >= Minimum_Number_of_Divergence;
def lcondLine2 = lnegdivergence2 >= Minimum_Number_of_Divergence or lhposdivergence2 >= Minimum_Number_of_Divergence;

def lnewwd = if !Show_Hidden_Divergences then (if !Change_Width_by_Number_of_Divergence then 2 else if lnegdivergence <= 2 then 2 else if lnegdivergence <= 5 then 3 else if lnegdivergence <= 8 then 4 else 5) else (if !Change_Width_by_Number_of_Divergence then 2 else if lhposdivergence <= 2 then 2 else if lhposdivergence <= 5 then 3 else if lhposdivergence <= 8 then 4 else 5);

def lnewwd2 = if !Show_Hidden_Divergences then (if !Change_Width_by_Number_of_Divergence then 2 else if lnegdivergence2 <= 2 then 2 else if lnegdivergence2 <= 5 then 3 else if lnegdivergence2 <= 8 then 4 else 5) else (if !Change_Width_by_Number_of_Divergence then 2 else if lhposdivergence2 <= 2 then 2 else if lhposdivergence2 <= 5 then 3 else if lhposdivergence2 <= 8 then 4 else 5);

def ltt0 = fold li1 = 0 to totalBars + 1 - bn with lk1 = 0 while GetValue(botc,-li1)<GetValue(botc1,-li1) do lk1+1;
def ltt = if botc < botc1 or botc1==0 then ltt0 else -1;
def ltt2 = fold li2 = 0 to ltt + 1 with lk2 = -1 do if GetValue(lcondLine, -li2) then li2 else lk2;
def lnewwd3 = fold li3 = 0 to ltt + 1 with lk3 = 2 do if GetValue(lcondLine, -li3) then GetValue(lnewwd,-li3) else lk3;
def lplot1 = if ltt2 >= 0 then GetValue(low, -ltt2) - (GetValue(low, -ltt2) - GetValue(low, botc)) / (botc + ltt2) * ltt2 else Double.NaN;

def ltt02 = fold li4 = 0 to totalBars + 1 - bn with lk4 = 0 while GetValue(botc,-li4)>GetValue(botc1,-li4) do lk4+1;
def ltt4 = if botc > botc1 or botc==0 then ltt02 else -1;
def ltt5 = fold li5 = 0 to ltt4 + 1 with lk5 = -1 do if GetValue(lcondLine2, -li5) then li5 else lk5;
def lnewwd6 = fold li6 = 0 to ltt4 + 1 with lk6 = 2 do if GetValue(lcondLine2, -li6) then GetValue(lnewwd2,-li6) else lk6;
def lplot2 = if ltt5 >= 0 then GetValue(low, -ltt5) - (GetValue(low, -ltt5) - GetValue(low, botc1)) / (botc1 + ltt5) * ltt5 else Double.NaN;

plot Div5 = if lnewwd3 == 2 and !isnan(lplot1) then lplot1 else Double.NaN;
plot Div6 = if lnewwd3 == 3 and !isnan(lplot1) then lplot1 else Double.NaN;
plot Div7 = if lnewwd3 == 4 and !isnan(lplot1) then lplot1 else Double.NaN;
plot Div8 = if lnewwd3 == 5 and !isnan(lplot1) then lplot1 else Double.NaN;
Div5.SetDefaultColor(Color.GREEN);
Div6.SetDefaultColor(Color.GREEN);
Div7.SetDefaultColor(Color.GREEN);
Div8.SetDefaultColor(Color.GREEN);
Div5.SetLineWeight(2);
Div6.SetLineWeight(3);
Div7.SetLineWeight(4);
Div8.SetLineWeight(5);

plot Div52 = if lnewwd6 == 2 and !isnan(lplot2) then lplot2 else Double.NaN;
plot Div62 = if lnewwd6 == 3 and !isnan(lplot2) then lplot2 else Double.NaN;
plot Div72 = if lnewwd6 == 4 and !isnan(lplot2) then lplot2 else Double.NaN;
plot Div82 = if lnewwd6 == 5 and !isnan(lplot2) then lplot2 else Double.NaN;
Div52.SetDefaultColor(Color.GREEN);
Div62.SetDefaultColor(Color.GREEN);
Div72.SetDefaultColor(Color.GREEN);
Div82.SetDefaultColor(Color.GREEN);
Div52.SetLineWeight(2);
Div62.SetLineWeight(3);
Div72.SetLineWeight(4);
Div82.SetLineWeight(5);

AddChartBubble (Show_Divergence_Number and !isnan(plot1) and isnan(Getvalue(plot1,-1)), high,if Show_Hidden_Divergences then hposdivergence else negdivergence, Color.RED, yes);
AddChartBubble (Show_Divergence_Number and !isnan(plot2) and isnan(Getvalue(plot2,-1)), high,if Show_Hidden_Divergences then hposdivergence2 else negdivergence2, Color.RED, yes);

AddChartBubble (Show_Divergence_Number and !isnan(lplot1) and isnan(Getvalue(lplot1,-1)), low,if Show_Hidden_Divergences then lhposdivergence else lnegdivergence, Color.GREEN, no);
AddChartBubble (Show_Divergence_Number and !isnan(lplot2) and isnan(Getvalue(lplot2,-1)), low,if Show_Hidden_Divergences then lhposdivergence2 else lnegdivergence2, Color.GREEN, no);
#----------
AddChartBubble (Show_Indicator_Names and !isnan(plot1) and isnan(Getvalue(plot1,-1)) and ((!Show_Hidden_Divergences && t1==1) or (Show_Hidden_Divergences and ht1==1)), high,"RSI", Color.RED, yes);
AddChartBubble (Show_Indicator_Names and !isnan(plot2) and isnan(Getvalue(plot2,-1)) and ((!Show_Hidden_Divergences && t1_2==1) or (Show_Hidden_Divergences and ht1_2==1)), high,"RSI", Color.RED, yes);

AddChartBubble (Show_Indicator_Names and !isnan(plot1) and isnan(Getvalue(plot1,-1)) and ((!Show_Hidden_Divergences && t2==2) or (Show_Hidden_Divergences and ht2==2)), high,"MACD", Color.RED, yes);
AddChartBubble (Show_Indicator_Names and !isnan(plot2) and isnan(Getvalue(plot2,-1)) and ((!Show_Hidden_Divergences && t2_2==2) or (Show_Hidden_Divergences and ht2_2==2)), high,"MACD", Color.RED, yes);

AddChartBubble (Show_Indicator_Names and !isnan(plot1) and isnan(Getvalue(plot1,-1)) and ((!Show_Hidden_Divergences && t3==3) or (Show_Hidden_Divergences and ht3==3)), high,"MACD Hist", Color.RED, yes);
AddChartBubble (Show_Indicator_Names and !isnan(plot2) and isnan(Getvalue(plot2,-1)) and ((!Show_Hidden_Divergences && t3_2==3) or (Show_Hidden_Divergences and ht3_2==3)), high,"MACD Hist", Color.RED, yes);

AddChartBubble (Show_Indicator_Names and !isnan(plot1) and isnan(Getvalue(plot1,-1)) and ((!Show_Hidden_Divergences && t4==4) or (Show_Hidden_Divergences and ht4==4)), high,"Momentum", Color.RED, yes);
AddChartBubble (Show_Indicator_Names and !isnan(plot2) and isnan(Getvalue(plot2,-1)) and ((!Show_Hidden_Divergences && t4_2==4) or (Show_Hidden_Divergences and ht4_2==4)), high,"Momentum", Color.RED, yes);

AddChartBubble (Show_Indicator_Names and !isnan(plot1) and isnan(Getvalue(plot1,-1)) and ((!Show_Hidden_Divergences && t5==5) or (Show_Hidden_Divergences and ht5==5)), high,"CCI", Color.RED, yes);
AddChartBubble (Show_Indicator_Names and !isnan(plot2) and isnan(Getvalue(plot2,-1)) and ((!Show_Hidden_Divergences && t5_2==5) or (Show_Hidden_Divergences and ht5_2==5)), high,"CCI", Color.RED, yes);

AddChartBubble (Show_Indicator_Names and !isnan(plot1) and isnan(Getvalue(plot1,-1)) and ((!Show_Hidden_Divergences && t6==6) or (Show_Hidden_Divergences and ht6==6)), high,"OBV", Color.RED, yes);
AddChartBubble (Show_Indicator_Names and !isnan(plot2) and isnan(Getvalue(plot2,-1)) and ((!Show_Hidden_Divergences && t6_2==6) or (Show_Hidden_Divergences and ht6_2==6)), high,"OBV", Color.RED, yes);

AddChartBubble (Show_Indicator_Names and !isnan(plot1) and isnan(Getvalue(plot1,-1)) and ((!Show_Hidden_Divergences && t7==7) or (Show_Hidden_Divergences and ht7==7)), high,"Stoch", Color.RED, yes);
AddChartBubble (Show_Indicator_Names and !isnan(plot2) and isnan(Getvalue(plot2,-1)) and ((!Show_Hidden_Divergences && t7_2==7) or (Show_Hidden_Divergences and ht7_2==7)), high,"Stoch", Color.RED, yes);

AddChartBubble (Show_Indicator_Names and !isnan(plot1) and isnan(Getvalue(plot1,-1)) and ((!Show_Hidden_Divergences && t8==8) or (Show_Hidden_Divergences and ht8==8)), high,"Diosc", Color.RED, yes);
AddChartBubble (Show_Indicator_Names and !isnan(plot2) and isnan(Getvalue(plot2,-1)) and ((!Show_Hidden_Divergences && t8_2==8) or (Show_Hidden_Divergences and ht8_2==8)), high,"Diosc", Color.RED, yes);

AddChartBubble (Show_Indicator_Names and !isnan(plot1) and isnan(Getvalue(plot1,-1)) and ((!Show_Hidden_Divergences && t9==9) or (Show_Hidden_Divergences and ht9==9)), high,"VWMacd", Color.RED, yes);
AddChartBubble (Show_Indicator_Names and !isnan(plot2) and isnan(Getvalue(plot2,-1)) and ((!Show_Hidden_Divergences && t9_2==9) or (Show_Hidden_Divergences and ht9_2==9)), high,"VWMacd", Color.RED, yes);

AddChartBubble (Show_Indicator_Names and !isnan(plot1) and isnan(Getvalue(plot1,-1)) and ((!Show_Hidden_Divergences && t10==10) or (Show_Hidden_Divergences and ht10==10)), high,"CMF", Color.RED, yes);
AddChartBubble (Show_Indicator_Names and !isnan(plot2) and isnan(Getvalue(plot2,-1)) and ((!Show_Hidden_Divergences && t10_2==10) or (Show_Hidden_Divergences and ht10_2==10)), high,"CMF", Color.RED, yes);

AddChartBubble (Show_Indicator_Names and !isnan(plot1) and isnan(Getvalue(plot1,-1)) and ((!Show_Hidden_Divergences && t11==11) or (Show_Hidden_Divergences and ht11==11)), high,"MFI", Color.RED, yes);
AddChartBubble (Show_Indicator_Names and !isnan(plot2) and isnan(Getvalue(plot2,-1)) and ((!Show_Hidden_Divergences && t11_2==11) or (Show_Hidden_Divergences and ht11_2==11)), high,"MFI", Color.RED, yes);

#----------
AddChartBubble (Show_Indicator_Names and !isnan(lplot1) and isnan(Getvalue(lplot1,-1)) and ((!Show_Hidden_Divergences && lt1==1) or (Show_Hidden_Divergences and lht1==1)), low,"RSI", Color.GREEN, no);
AddChartBubble (Show_Indicator_Names and !isnan(lplot2) and isnan(Getvalue(lplot2,-1)) and ((!Show_Hidden_Divergences && lt1_2==1) or (Show_Hidden_Divergences and lht1_2==1)), low,"RSI", Color.GREEN, no);

AddChartBubble (Show_Indicator_Names and !isnan(lplot1) and isnan(Getvalue(lplot1,-1)) and ((!Show_Hidden_Divergences && lt2==2) or (Show_Hidden_Divergences and lht2==2)), low,"MACD", Color.GREEN, no);
AddChartBubble (Show_Indicator_Names and !isnan(lplot2) and isnan(Getvalue(lplot2,-1)) and ((!Show_Hidden_Divergences && lt2_2==2) or (Show_Hidden_Divergences and lht2_2==2)), low,"MACD", Color.GREEN, no);

AddChartBubble (Show_Indicator_Names and !isnan(lplot1) and isnan(Getvalue(lplot1,-1)) and ((!Show_Hidden_Divergences && lt3==3) or (Show_Hidden_Divergences and lht3==3)), low,"MACD Hist", Color.GREEN, no);
AddChartBubble (Show_Indicator_Names and !isnan(lplot2) and isnan(Getvalue(lplot2,-1)) and ((!Show_Hidden_Divergences && lt3_2==3) or (Show_Hidden_Divergences and lht3_2==3)), low,"MACD Hist", Color.GREEN, no);

AddChartBubble (Show_Indicator_Names and !isnan(lplot1) and isnan(Getvalue(lplot1,-1)) and ((!Show_Hidden_Divergences && lt4==4) or (Show_Hidden_Divergences and lht4==4)), low,"Momentum", Color.GREEN, no);
AddChartBubble (Show_Indicator_Names and !isnan(lplot2) and isnan(Getvalue(lplot2,-1)) and ((!Show_Hidden_Divergences && lt4_2==4) or (Show_Hidden_Divergences and lht4_2==4)), low,"Momentum", Color.GREEN, no);

AddChartBubble (Show_Indicator_Names and !isnan(lplot1) and isnan(Getvalue(lplot1,-1)) and ((!Show_Hidden_Divergences && lt5==5) or (Show_Hidden_Divergences and lht5==5)), low,"CCI", Color.GREEN, no);
AddChartBubble (Show_Indicator_Names and !isnan(lplot2) and isnan(Getvalue(lplot2,-1)) and ((!Show_Hidden_Divergences && lt5_2==5) or (Show_Hidden_Divergences and lht5_2==5)), low,"CCI", Color.GREEN, no);

AddChartBubble (Show_Indicator_Names and !isnan(lplot1) and isnan(Getvalue(lplot1,-1)) and ((!Show_Hidden_Divergences && lt6==6) or (Show_Hidden_Divergences and lht6==6)), low,"OBV", Color.GREEN, no);
AddChartBubble (Show_Indicator_Names and !isnan(lplot2) and isnan(Getvalue(lplot2,-1)) and ((!Show_Hidden_Divergences && lt6_2==6) or (Show_Hidden_Divergences and lht6_2==6)), low,"OBV", Color.GREEN, no);

AddChartBubble (Show_Indicator_Names and !isnan(lplot1) and isnan(Getvalue(lplot1,-1)) and ((!Show_Hidden_Divergences && lt7==7) or (Show_Hidden_Divergences and lht7==7)), low,"Stoch", Color.GREEN, no);
AddChartBubble (Show_Indicator_Names and !isnan(lplot2) and isnan(Getvalue(lplot2,-1)) and ((!Show_Hidden_Divergences && lt7_2==7) or (Show_Hidden_Divergences and lht7_2==7)), low,"Stoch", Color.GREEN, no);

AddChartBubble (Show_Indicator_Names and !isnan(lplot1) and isnan(Getvalue(lplot1,-1)) and ((!Show_Hidden_Divergences && lt8==8) or (Show_Hidden_Divergences and lht8==8)), low,"Diosc", Color.GREEN, no);
AddChartBubble (Show_Indicator_Names and !isnan(lplot2) and isnan(Getvalue(lplot2,-1)) and ((!Show_Hidden_Divergences && lt8_2==8) or (Show_Hidden_Divergences and lht8_2==8)), low,"Diosc", Color.GREEN, no);

AddChartBubble (Show_Indicator_Names and !isnan(lplot1) and isnan(Getvalue(lplot1,-1)) and ((!Show_Hidden_Divergences && lt9==9) or (Show_Hidden_Divergences and lht9==9)), low,"VWMacd", Color.GREEN, no);
AddChartBubble (Show_Indicator_Names and !isnan(lplot2) and isnan(Getvalue(lplot2,-1)) and ((!Show_Hidden_Divergences && lt9_2==9) or (Show_Hidden_Divergences and lht9_2==9)), low,"VWMacd", Color.GREEN, no);

AddChartBubble (Show_Indicator_Names and !isnan(lplot1) and isnan(Getvalue(lplot1,-1)) and ((!Show_Hidden_Divergences && lt10==10) or (Show_Hidden_Divergences and lht10==10)), low,"CMF", Color.GREEN, no);
AddChartBubble (Show_Indicator_Names and !isnan(lplot2) and isnan(Getvalue(lplot2,-1)) and ((!Show_Hidden_Divergences && lt10_2==10) or (Show_Hidden_Divergences and lht10_2==10)), low,"CMF", Color.GREEN, no);

AddChartBubble (Show_Indicator_Names and !isnan(lplot1) and isnan(Getvalue(lplot1,-1)) and ((!Show_Hidden_Divergences && lt11==11) or (Show_Hidden_Divergences and lht11==11)), low,"MFI", Color.GREEN, no);
AddChartBubble (Show_Indicator_Names and !isnan(lplot2) and isnan(Getvalue(lplot2,-1)) and ((!Show_Hidden_Divergences && lt11_2==11) or (Show_Hidden_Divergences and lht11_2==11)), low,"MFI", Color.GREEN, no);``````

Solution
@C_Wilson When you receive that "too complex" error from the scanner it means just what it says... The only fix is to use less complex and/or fewer calculations... Your code is, in fact, too complex... I have actually never seen such a complex method of coding in Thinkscript... Thinkscript uses a somewhat rudimentary parser compared to other well developed and robust scripting/programming languages... It doesn't take much to get it to gag and throw errors... Unfortunately, we have to live within the constraints of the trading platform
I had the too complex error issue for code I wrote. It contained several "reference" calls to a standard TOS study with different input parameters for three different calls to the same...
@C_Wilson When you receive that "too complex" error from the scanner it means just what it says... The only fix is to use less complex and/or fewer calculations... Your code is, in fact, too complex... I have actually never seen such a complex method of coding in Thinkscript... Thinkscript uses a somewhat rudimentary parser compared to other well developed and robust scripting/programming languages... It doesn't take much to get it to gag and throw errors... Unfortunately, we have to live within the constraints of the trading platform

Last edited by a moderator:
Thanks. I kinda thought the same thing when I saw it and I don't know anything about coding. I had hired a guy from freelancers to convert that code over from Trading view and it seemed way longer than the original.

Maybe you can help me with something less complicated.
I have this scan in TOS but would like it to show if 3 or more of these are true.

TOS will only allow me to pick if any or all are true.

StochasticFast("k period" = 9, "show breakout signals" = "On FastD")."UpSignal" is true within 2 bars or StochasticFast("k period" = 14, "show breakout signals" = "On FastD")."UpSignal" is true within 2 bars or StochasticFast("k period" = 40, "d period" = 4, "show breakout signals" = "On FastD")."UpSignal" is true within 2 bars or StochasticFull("k period" = 60, "show breakout signals" = "On FullD")."UpSignal" is true within 2 bars

@C_Wilson Here you go...

Ruby:
``````# Displays scan results if at least  three of the following are true

def s1 = if StochasticFast("k period" = 9, "show breakout signals" = "On FastD")."UpSignal" is true within 2 bars then 1 else 0;

def s2 = if StochasticFast("k period" = 14, "show breakout signals" = "On FastD")."UpSignal" is true within 2 bars then 1 else 0;

def s3 = if StochasticFast("k period" = 40, "d period" = 4, "show breakout signals" = "On FastD")."UpSignal" is true within 2 bars then 1 else 0;

def s4 = if StochasticFull("k period" = 60, "show breakout signals" = "On FullD")."UpSignal" is true within 2 bars then 1 else 0;

def counter = s1 + s2 + s3 + s4;

plot data = if counter >= 3 then 1 else Double.NaN;``````

@C_Wilson Here you go...

Ruby:
``````# Displays scan results if at least  three of the following are true

def s1 = if StochasticFast("k period" = 9, "show breakout signals" = "On FastD")."UpSignal" is true within 2 bars then 1 else 0;

def s2 = if StochasticFast("k period" = 14, "show breakout signals" = "On FastD")."UpSignal" is true within 2 bars then 1 else 0;

def s3 = if StochasticFast("k period" = 40, "d period" = 4, "show breakout signals" = "On FastD")."UpSignal" is true within 2 bars then 1 else 0;

def s4 = if StochasticFull("k period" = 60, "show breakout signals" = "On FullD")."UpSignal" is true within 2 bars then 1 else 0;

def counter = s1 + s2 + s3 + s4;

plot data = if counter >= 3 then 1 else Double.NaN;``````
Hi, when I enter the above, nothing will be added to the chart, have you tried this codes?

Did someone bundle a lot of divergence indicators into one be careful this repaints.

Hi, when I enter the above, nothing will be added to the chart, have you tried this codes?

@majidg The request by @C_Wilson was for Scan code, not for use on a Chart... The code I posted works perfectly for Scans...

Did someone bundle a lot of divergence indicators into one be careful this repaints.
Yes . I like to use this towards the end of the day. I know it repaints, however this has made some good trades. I dont take them all. I do use Stoch with it also.

@C_Wilson When you receive that "too complex" error from the scanner it means just what it says... The only fix is to use less complex and/or fewer calculations... Your code is, in fact, too complex... I have actually never seen such a complex method of coding in Thinkscript... Thinkscript uses a somewhat rudimentary parser compared to other well developed and robust scripting/programming languages... It doesn't take much to get it to gag and throw errors... Unfortunately, we have to live within the constraints of the trading platform
I had the too complex error issue for code I wrote. It contained several "reference" calls to a standard TOS study with different input parameters for three different calls to the same study. The standard study contained "fold" operations and was itself somewhat complex.

The fix for me was to copy the standard TOS study itself into my code as a "script" and then just call the script with the different input parameters. This avoided several calls as "reference" to the standard TOS study. This removed the too complex indication when using it to display my study as a lower graph in charts. Also, when run with the needed conversions to define possible entry as a study in the scanner, no more too complex error in the scanner either.

If one's code uses several calls by "reference" to standard TOS study, consider trying what worked for me as outlined above.

does this indicator repaint also how do you use it?

@VJET Yes, it repaints. Best to do a google search on Divergence to familiarize yourself with this type of indicator.

does this indIicator repaint also how do you use it?
Yes it repaints.
In the settings you can select "Show divergence number". It will show how many divergences are triggered. The greater the number, the greater the chances of it making a move in the appropriate direction. Researching divergences is a good idea, but in a nutshell its when an indicator is moving in one direction and the price is moving in the other direction, the price will then correct it self to match the indicator. This is the idea behind it, however , this does not always happen. I have mine set up to show up on at least 3 divergences. I have made good money and have lost money

I had the too complex error issue for code I wrote. It contained several "reference" calls to a standard TOS study with different input parameters for three different calls to the same study. The standard study contained "fold" operations and was itself somewhat complex.

The fix for me was to copy the standard TOS study itself into my code as a "script" and then just call the script with the different input parameters. This avoided several calls as "reference" to the standard TOS study. This removed the too complex indication when using it to display my study as a lower graph in charts. Also, when run with the needed conversions to define possible entry as a study in the scanner, no more too complex error in the scanner either.

If one's code uses several calls by "reference" to standard TOS study, consider trying what worked for me as outlined above.
script {
}

is awesome for building solutions, although wonky. I think I couldn't call AddLabel() in it if i remember correctly. So there are caveats.

@Lexureyesed script calls are only for calculations. It isn't a caveat. It is the definition of its function.
AddLabel is not a mathematical function. Embedding any chart formatting is not appropriate.

87k+ Posts
382 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?