# Dilbert_RsiDivergence_with_HiddenDivergence
# V1.4 - 091615 - Dilbert - Add hidden divergence
# V1.3 - 082715 - Dilbert - Add LastHighAtRsiPivotHigh and LastLowAtRsiPivotLow
# V1.2 - 082615 - zzz - Limit plots to == count
# V1.1 - 082615 - Original author unknown, posted to chat by Jr2146
input pricecolor = yes;
input count = 6;#Hint count: number of plots to display
#hint: recognizes and signals divergences between rsi and price.
input n=3;
#hint n: controls how the slow pivot level is recognized. n=3 means that a slow pivot level is recognized when a high/low is preceded by 3 equal or lesser/greater (resp.) high's/low's and followed by 3 lesser/greater high's/low's.
input length = 14;
input ob = 70;
input ob2 = 78;
input os = 29;
input os2 = 10;
def highPrice = StDev(high, 10);
def lowPrice = StDev(low, 10);
def highAvgUp = ExpAverage(if high > high[1] then highPrice else 0, length);
def highAvgDown = ExpAverage(if high < high[1] then highPrice else 0, length);
def lowAvgUp = ExpAverage(if low > low[1] then lowPrice else 0, length);
def lowAvgDown = ExpAverage(if low < low[1] then lowPrice else 0, length);
def highRVI = 100 - 100 / (1 + highAvgUp / highAvgDown);
def lowRVI = 100 - 100 / (1 + lowAvgUp / lowAvgDown);
def r = (highRVI + lowRVI) / 2;
def isRsiHigh = CompoundValue(n,
r[n] == Highest(r, n * 2 + 1) && r[n] > r && fold i = 1 to n with x = 1 while x == 1 do
r[n] > GetValue(r, i)
, 0
);
def isRsiLow = CompoundValue(n,
r[n] == Lowest(r, n * 2 + 1) && r[n] < r && fold j = 1 to n with y=1 while y == 1 do
r[n] < GetValue(r, j)
, 0
);
def rsiPivotHigh = CompoundValue(n,
if isRsiHigh then r[n] else rsiPivotHigh[1],
0
);
def highAtRsiPivotHigh = CompoundValue(n,
if isRsiHigh then Max(high[n], high[n - 1]) else highAtRsiPivotHigh[1],
0
);
def rsiPivotLow = CompoundValue(n,
if isRsiLow then r[n] else rsiPivotLow[1],
0
);
def lowAtRsiPivotLow = CompoundValue(n,
if isRsiLow then Min(low[n], low[n - 1]) else lowAtRsiPivotLow[1],
0
);
def TheBar = BarNumber();
plot divergenceColors = Double.NaN;
divergenceColors.DefineColor( "high divergence", Color.Yellow);
divergenceColors.DefineColor( "low divergence", Color.Yellow);
divergenceColors.HideTitle();
def isFastRsiHigh = r[1] > r && r[2] <= r[1] && r[3] <= r[1];
def isFastRsiLow = r[1] < r && r[2] >= r[1] && r[3] >= r[1];
def fastPriceHigh = Max(high, high[1]);
def fastPriceLow = Min(low, low[1]);
def isHigherFastRsiHigh = isFastRsiHigh && r[1] < rsiPivotHigh[1];
def isLowerFastRsiLow = isFastRsiLow && r[1] > rsiPivotLow[1];
def highDivergence = (isHigherFastRsiHigh && fastPriceHigh >= highAtRsiPivotHigh[1]);
def lowDivergence = (isLowerFastRsiLow && fastPriceLow <= lowAtRsiPivotLow[1]);
############################################################################################
#####
input crosscount = 2;
def cond2 = highDivergence;
rec dataCount2 = CompoundValue(1, if (cond2) == 1 then dataCount2[1] + 1 else dataCount2[1], 0);
plot CrossDn = if HighestAll(dataCount2) - dataCount2 <= crosscount - 1 and highDivergence then 1 else Double.NaN;
def cond3 = lowDivergence;
rec dataCount3 = CompoundValue(1, if (cond3) == 1 then dataCount3[1] + 1 else dataCount3[1], 0);
plot CrossUp = if HighestAll(dataCount3) - dataCount3 <= crosscount - 1 and lowDivergence then 1 else Double.NaN;
CrossUp.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
CrossDn.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
CrossUp.SetDefaultColor(Color.MAGENTA);
CrossDn.SetDefaultColor(Color.CYAN);
AssignPriceColor(
if HighestAll(dataCount2) - dataCount2 <= crosscount - 1 and highDivergence then divergenceColors.Color("high divergence")
else if HighestAll(dataCount3) - dataCount3 <= crosscount - 1 and lowDivergence then divergenceColors.Color("low divergence")
else Color.CURRENT);
alert(highDivergence,"HighDivergence", sound = Sound.Ring, "alert type" = Alert.BAR);
################################
input displace = 0;
input MA1_length = 8;
input MA2_length = 21;
input price = close;
input showverticals = yes;
input number_verticals_displayed = 1;
DefineGlobalColor("RisingMA", Color.BLUE);
DefineGlobalColor("FallingMA", Color.RED);
input movingAverageType1 = {default Simple, Exponential, Weighted, Hull, Variable};
input movingAverageType2 = {default Variable, Simple, Exponential, Weighted, Hull};
def data1;
switch (movingAverageType1) {
case Simple:
data1 = CompoundValue(1, Average(price[-displace], MA1_length), price);
case Exponential:
data1 = CompoundValue(1, ExpAverage(price[-displace], MA1_length), price);
case Weighted:
data1 = CompoundValue(1, WMA(price[-displace], MA1_length), price);
case Hull:
data1 = CompoundValue(1, HullMovingAvg(price[-displace], MA1_length), price);
case Variable:
data1 = CompoundValue(1, VariableMA(price = price, length = MA1_length), price);
}
plot DoubleMA;
switch (movingAverageType2) {
case Simple:
DoubleMA = CompoundValue(1, Average(data1[-displace], MA2_length), data1);
case Exponential:
DoubleMA = CompoundValue(1, ExpAverage(data1[-displace], MA2_length), data1);
case Weighted:
DoubleMA = CompoundValue(1, WMA(data1[-displace], MA2_length), data1);
case Hull:
DoubleMA = CompoundValue(1, HullMovingAvg(data1[-displace], MA2_length), data1);
case Variable:
DoubleMA = CompoundValue(1, VariableMA( data1, MA2_length), data1);
}
DoubleMA.SetLineWeight(2);
DoubleMA.AssignValueColor(if DoubleMA > DoubleMA[1] then GlobalColor("RisingMA") else GlobalColor("FallingMA"));
DoubleMA.HideBubble();
DoubleMA.SetLineWeight(2);
DoubleMA.AssignValueColor(if DoubleMA > DoubleMA[1] then GlobalColor("RisingMA") else GlobalColor("FallingMA"));
# Verticals
def condgreen = if DoubleMA > DoubleMA[1] then 1 else 0;
def count1 = number_verticals_displayed;
def cond1 = if condgreen[1] == 0 and condgreen or condgreen[1] == 1 and condgreen == 0 then 1 else Double.NaN;
rec dataCount1 = CompoundValue(1, if !IsNaN(cond1) then dataCount1[1] + 1 else dataCount1[1], 0);
AddVerticalLine(HighestAll(dataCount1) - dataCount1 <= count1 - 1 and showverticals and condgreen[1] == 0 and condgreen , " Trend Rising " + Round(DoubleMA, 2), Color.BLUE, Curve.FIRM);
AddVerticalLine(HighestAll(dataCount1) - dataCount1 <= count1 - 1 and showverticals and condgreen[1] == 1 and condgreen == 0 , " Trend Falling " + Round(DoubleMA, 2), Color.RED, Curve.FIRM);
input showcloud = yes;
input pct_above_below = 1.0;
plot UpperBand = DoubleMA * (1 + pct_above_below / 100);
UpperBand.AssignValueColor(if DoubleMA > DoubleMA[1] then GlobalColor("RisingMA") else GlobalColor("FallingMA"));
plot LowerBand = DoubleMA * (1 - pct_above_below / 100);
LowerBand.AssignValueColor(if DoubleMA > DoubleMA[1] then GlobalColor("RisingMA") else GlobalColor("FallingMA"));
input number_clouds_displayed = 3;
rec dataCount4 = CompoundValue(1, if !IsNaN(cond1) then dataCount4[1] + 1 else dataCount4[1], 0);
def dcount4 = dataCount4;
AddCloud(if HighestAll(dCount4) - dCount4 <= number_clouds_displayed - 1 and showcloud and DoubleMA > DoubleMA[1] then UpperBand else Double.NaN, LowerBand, GlobalColor("RisingMA"), GlobalColor("RisingMA"));
AddCloud(if HighestAll(dCount4) - dCount4 <= number_clouds_displayed - 1 and showcloud and DoubleMA < DoubleMA[1] then UpperBand else Double.NaN, LowerBand, GlobalColor("FallingMA"), GlobalColor("FallingMA"));
#Arrow Option
input showarrows = yes;
input arrow_mover_updown = 1;
plot greenarrow = if HighestAll(dataCount1) - dataCount1 <= count1 - 1 and showarrows and condgreen[1] == 0 and condgreen then doublema - TickSize() * arrow_mover_updown else Double.NaN;
greenarrow.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
greenarrow.SetDefaultColor(Color.BLUE);
greenarrow.SetLineWeight(3);
greenarrow.HideBubble();
plot redarrow = if HighestAll(dataCount1) - dataCount1 <= count1 - 1 and showarrows and condgreen[1] == 1 and condgreen == 0 then doublema + TickSize() * arrow_mover_updown else Double.NaN;
redarrow.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
redarrow.SetDefaultColor(Color.RED);
redarrow.SetLineWeight(3);
redarrow.HideBubble();
#Bar Color
def green = if condgreen[1] == 0 and condgreen then 1 else if green[1]==1 and !(condgreen[1] == 1 and condgreen == 0) then 1 else 0;
AssignPriceColor(if pricecolor then if Between(r, 87, 95) or between(r,5,13) then color.GREEN else if r > 95 or r < 5 then color.WHITE else if green==0 then color.red else color.BLUE else Color.CURRENT);