Rsi divergence plot on chart

ok sir , thank u so much.

i was struggling a bit with the extension colors. Since they sometimes overlapped.

so code below is better i think

``````SetChartOptions( 0, chartShowArrows | chartShowDates );

periods = Param( "Periods", 20, 1, 200, 1 );
n = Param( "% Reverse ", 20, 0, 100, 1 );
displayMode1 = ParamToggle( "Display Price or RSI", "Price|RSI", 1 );
displayMode2 = ParamToggle( "Display All Divergences or Just the Couples", "Show All|Show Couple Only", 1 );

ZigZag = line1 = line2 = Null;
func1 = C;
func2 = RSIa( C, periods );

bi = BarIndex();
fvb = FirstVisibleValue( bi );
lvb = LastVisibleValue( bi );
Var = Zig( func2, n );
tr = Trough( func2, n, 1 );
pk = Peak( func2, n, 1 );
tr1 = Trough( func2, n, 1 ) == Var;
pk1 = Peak( func2, n, 1 ) == Var;

// zigzag func1
prch = H;
prcl = L;
zigup = Flip( tr1, pk1 );
zigupLow = ValueWhen( tr1, prcl, 1 );
zigupHigh = ValueWhen( pk1, prch, 0 );
zigupLowIndex = ValueWhen( tr1, bi, 1 );
zigupHighIndex = ValueWhen( pk1, bi, 0 );
slopeup = IIf( zigup, ( zigupHigh - zigupLow ) / ( zigupHighIndex - zigupLowIndex ) , Null );
zigupLine = IIf( zigup, zigupLow + slopeup * BarsSince( tr1 ), Null );
zigdn = Flip( pk1, tr1 );
zigdnLow = ValueWhen( tr1, prcl, 0 );
zigdnHigh = ValueWhen( pk1, prch, 1 );
zigdnLowIndex = ValueWhen( tr1, bi, 0 );
zigdnHighIndex = ValueWhen( pk1, bi, 1 );
slopedn = IIf( zigdn, ( zigdnLow - zigdnHigh ) / ( zigdnLowIndex - zigdnHighIndex ) , Null );
zigdnLine = IIf( zigdn, zigdnHigh + slopedn * BarsSince( pk1 ), Null );
ZigZag = IIf( zigup, zigupLine, IIf( zigdn, zigdnLine, Null ) );
ZigZag = IIf( bi > Max( LastValue( ValueWhen( tr1, bi ) ), LastValue( ValueWhen( pk1, bi ) ) ), Null, ZigZag );
ZigZag = IIf( !IsEmpty( line1 ), line1, ZigZag );

for( i = 0; i < 3; i++ )
{
VarSet( "px" + i, ValueWhen( pk1, bi, i ) );
VarSet( "tx" + i, ValueWhen( tr1, bi, i ) );
VarSet( "ph" + i, ValueWhen( pk1, H, i ) );
VarSet( "tl" + i, ValueWhen( tr1, L, i ) );
}

// No divergence
onlywhen = pk == Var;
pkPrice = SparseCompress( onlywhen, func1 );
pkRsi = SparseCompress( onlywhen, pk );
pkBiN = SparseCompress( onlywhen, bi );
SellN = pkPrice < Ref( pkPrice, -1 ) AND pkRsi < Ref( pkRsi, -1 );
SellNo = SparseExpand( onlywhen, SellN );
slopeSN1 = SafeDivide( pkPrice - Ref( pkPrice, -1 ), pkBiN - Ref( pkBiN, -1 ) );
slopeSN2 = SafeDivide( pkRsi - Ref( pkRsi, -1 ), pkBiN - Ref( pkBiN, -1 ) );
slopeSN1 = SparseExpand( onlywhen, slopeSN1 );
slopeSN2 = SparseExpand( onlywhen, slopeSN2 );
f2Sy0N = SparseExpand( onlywhen, Ref( pkRsi, -1 ) );
f2Sy1N = SparseExpand( onlywhen, pkRsi );
f2Sx0N = SparseExpand( onlywhen, Ref( pkBiN, -1 ) );
f1Sy0N = SparseExpand( onlywhen, Ref( pkPrice, -1 ) );
f1Sy1N = SparseExpand( onlywhen, pkPrice );
f1Sx0N = SparseExpand( onlywhen, Ref( pkBiN, -1 ) );

onlywhen = tr == Var;
thPrice = SparseCompress( onlywhen, func1 );
thRsi = SparseCompress( onlywhen, tr );
thBiN = SparseCompress( onlywhen, bi );
BuyN = thPrice > Ref( thPrice, -1 ) AND thRsi > Ref( thRsi, -1 );
slopeBN1 = SafeDivide( thPrice - Ref( thPrice, -1 ), thBiN - Ref( thBiN, -1 ) );
slopeBN2 = SafeDivide( thRsi - Ref( thRsi, -1 ), thBiN - Ref( thBiN, -1 ) );
slopeBN1 = SparseExpand( onlywhen, slopeBN1 );
slopeBN2 = SparseExpand( onlywhen, slopeBN2 );
f2By0N = SparseExpand( onlywhen, Ref( thRsi, -1 ) );
f2By1N = SparseExpand( onlywhen, thRsi );
f2Bx0N = SparseExpand( onlywhen, Ref( thBiN, -1 ) );
f1By0N = SparseExpand( onlywhen, Ref( thPrice, -1 ) );
f1By1N = SparseExpand( onlywhen, thPrice );
f1Bx0N = SparseExpand( onlywhen, Ref( thBiN, -1 ) );

// Hidden divergence
onlywhen = pk == Var;
pkPrice = SparseCompress( onlywhen, func1 );
pkRsi = SparseCompress( onlywhen, pk );
pkBiH = SparseCompress( onlywhen, bi );
SellH = pkPrice < Ref( pkPrice, -1 ) AND pkRsi > Ref( pkRsi, -1 );
SellHo = SparseExpand( onlywhen, SellH );
slopeSH1 = SafeDivide( pkPrice - Ref( pkPrice, -1 ), pkBiH - Ref( pkBiH, -1 ) );
slopeSH2 = SafeDivide( pkRsi - Ref( pkRsi, -1 ), pkBiH - Ref( pkBiH, -1 ) );
slopeSH1 = SparseExpand( onlywhen, slopeSH1 );
slopeSH2 = SparseExpand( onlywhen, slopeSH2 );
f2Sy0H = SparseExpand( onlywhen, Ref( pkRsi, -1 ) );
f2Sy1H = SparseExpand( onlywhen, pkRsi );
f2Sx0H = SparseExpand( onlywhen, Ref( pkBiH, -1 ) );
f1Sy0H = SparseExpand( onlywhen, Ref( pkPrice, -1 ) );
f1Sy1H = SparseExpand( onlywhen, pkPrice );
f1Sx0H = SparseExpand( onlywhen, Ref( pkBiH, -1 ) );

onlywhen = tr == Var;
thPrice = SparseCompress( onlywhen, func1 );
thRsi = SparseCompress( onlywhen, tr );
thBiH = SparseCompress( onlywhen, bi );
BuyH = thPrice > Ref( thPrice, -1 ) AND thRsi < Ref( thRsi, -1 );
slopeBH1 = SafeDivide( thPrice - Ref( thPrice, -1 ), thBiH - Ref( thBiH, -1 ) );
slopeBH2 = SafeDivide( thRsi - Ref( thRsi, -1 ), thBiH - Ref( thBiH, -1 ) );
slopeBH1 = SparseExpand( onlywhen, slopeBH1 );
slopeBH2 = SparseExpand( onlywhen, slopeBH2 );
f2By0H = SparseExpand( onlywhen, Ref( thRsi, -1 ) );
f2By1H = SparseExpand( onlywhen, thRsi );
f2Bx0H = SparseExpand( onlywhen, Ref( thBiH, -1 ) );
f1By0H = SparseExpand( onlywhen, Ref( thPrice, -1 ) );
f1By1H = SparseExpand( onlywhen, thPrice );
f1Bx0H = SparseExpand( onlywhen, Ref( thBiH, -1 ) );

// Regular divergence
onlywhen = pk == Var;
pkPrice = SparseCompress( onlywhen, func1 );
pkRsi = SparseCompress( onlywhen, pk );
pkBiR = SparseCompress( onlywhen, bi );
SellR = pkPrice > Ref( pkPrice, -1 ) AND pkRsi < Ref( pkRsi, -1 );
SellRo = SparseExpand( onlywhen, SellR );
SellRH = SparseExpand( onlywhen, SellR ) AND ValueWhen( BuyHo, bi ) > px2;
SellRN = SparseExpand( onlywhen, SellR ) AND ValueWhen( BuyNo, bi ) > px2;
SellR = SellRH OR SellRN;
slopeSR1 = SafeDivide( pkPrice - Ref( pkPrice, -1 ), pkBiR - Ref( pkBiR, -1 ) );
slopeSR2 = SafeDivide( pkRsi - Ref( pkRsi, -1 ), pkBiR - Ref( pkBiR, -1 ) );
slopeSR1 = SparseExpand( onlywhen, slopeSR1 );
slopeSR2 = SparseExpand( onlywhen, slopeSR2 );
slopeSR1 = ValueWhen( SellR, slopeSR1 );
slopeSR2 = ValueWhen( SellR, slopeSR2 );
f2Sy0R = SparseExpand( onlywhen, Ref( pkRsi, -1 ) );
f2Sy1R = SparseExpand( onlywhen, pkRsi );
f2Sx0R = SparseExpand( onlywhen, Ref( pkBiR, -1 ) );
f1Sy0R = SparseExpand( onlywhen, Ref( pkPrice, -1 ) );
f1Sy1R = SparseExpand( onlywhen, pkPrice );
f1Sx0R = SparseExpand( onlywhen, Ref( pkBiR, -1 ) );
// blue extension
SRExt1 = IIf( bi > SellR,  ValueWhen( SellR, f1Sy1R ) + slopeSR1 * BarsSince( SellR ), Null );
SRExt2 = IIf( bi > SellR,  ValueWhen( SellR, var ) + slopeSR2 * BarsSince( SellR ), Null );
SRExt1 = IIf( SellR, Null, SRExt1 );
SRExt2 = IIf( SellR, Null, SRExt2 );

onlywhen = tr == Var;
thPrice = SparseCompress( onlywhen, func1 );
thRsi = SparseCompress( onlywhen, tr );
thBiR = SparseCompress( onlywhen, bi );
BuyR = thPrice < Ref( thPrice, -1 ) AND thRsi > Ref( thRsi, -1 );
BuyRH = SparseExpand( onlywhen, BuyR ) AND ValueWhen( SellHo, bi ) > tx2;
BuyRN = SparseExpand( onlywhen, BuyR ) AND ValueWhen( SellNo, bi ) > tx2;
slopeBR1 = SafeDivide( thPrice - Ref( thPrice, -1 ), thBiR - Ref( thBiR, -1 ) );
slopeBR2 = SafeDivide( thRsi - Ref( thRsi, -1 ), thBiR - Ref( thBiR, -1 ) );
slopeBR1 = SparseExpand( onlywhen, slopeBR1 );
slopeBR2 = SparseExpand( onlywhen, slopeBR2 );
slopeBR1 = ValueWhen( BuyR, slopeBR1 );
slopeBR2 = ValueWhen( BuyR, slopeBR2 );
f2By0R = SparseExpand( onlywhen, Ref( thRsi, -1 ) );
f2By1R = SparseExpand( onlywhen, thRsi );
f2Bx0R = SparseExpand( onlywhen, Ref( thBiR, -1 ) );
f2Bx1R = SparseExpand( onlywhen, thBiR );
f1By0R = SparseExpand( onlywhen, Ref( thPrice, -1 ) );
f1By1R = SparseExpand( onlywhen, thPrice );
f1Bx0R = SparseExpand( onlywhen, Ref( thBiR, -1 ) );
f1Bx1R = SparseExpand( onlywhen, thBiR );
// colorRed extension
BRExt1 = IIf( BuyR, Null, BRExt1 );
BRExt2 = IIf( BuyR, Null, BRExt2 );

// colorOrange extension
slopeBH1 = ValueWhen( BuyHo1, slopeBH1 );
slopeBH2 = ValueWhen( BuyHo1, slopeBH2 );
BHExt1 = IIf( BuyHo1, Null, BHExt1 );
BHExt2 = IIf( BuyHo1, Null, BHExt2 );

// colorGold extension
slopeBN1 = ValueWhen( BuyNo1, slopeBN1 );
slopeBN2 = ValueWhen( BuyNo1, slopeBN2 );
BNExt1 = IIf( BuyNo1, Null, BNExt1 );
BNExt2 = IIf( BuyNo1, Null, BNExt2 );

// colorAqua extension
SellHo1 = SellHo AND ValueWhen( BuyR, bi, 0 ) == tx0;
slopeSH1 = ValueWhen( SellHo1, slopeSH1 );
slopeSH2 = ValueWhen( SellHo1, slopeSH2 );
SHExt1 = IIf( bi > SellH,  ValueWhen( SellHo1, f1Sy1H ) + slopeSH1 * BarsSince( SellHo1 ), Null );
SHExt2 = IIf( bi > SellH,  ValueWhen( SellHo1, var ) + slopeSH2 * BarsSince( SellHo1 ), Null );
SHExt1 = IIf( SellHo1, Null, SHExt1 );
SHExt2 = IIf( SellHo1, Null, SHExt2 );

// colorPink extension
SellNo1 = SellNo AND ValueWhen( BuyR, bi, 0 ) == tx0;
slopeSN1 = ValueWhen( SellNo1, slopeSN1 );
slopeSN2 = ValueWhen( SellNo1, slopeSN2 );
SNExt1 = IIf( bi > SellN,  ValueWhen( SellNo1, f1Sy1N ) + slopeSN1 * BarsSince( SellNo1 ), Null );
SNExt2 = IIf( bi > SellN,  ValueWhen( SellNo1, var ) + slopeSN2 * BarsSince( SellNo1 ), Null );
SNExt1 = IIf( SellNo1, Null, SNExt1 );
SNExt2 = IIf( SellNo1, Null, SNExt2 );

buyLevel = ValueWhen( BuySig, func2 + func2 * ( n / 100 ) );
crx = Ref( Cross( func2, buyLevel ), -1 );
crx = IIf( BuySig, 0, crx );

SellSig = SellR;
sellLevel = ValueWhen( SellSig, func2 - func2 * ( n / 100 ) );
crx = Ref( Cross( sellLevel, func2 ), -1 );
crx = IIf( SellSig, 0, crx );
sellLevel = IIf( Flip( SellSig, crx ), sellLevel, Null );
dotSell = IIf( sellLevel AND Cross( sellLevel, func2 ), 1, 0 );

lineThickness1 = 5;
lineThickness2 = 1;

if( displayMode1 )
{
Overbought = 70;
Oversold = 30;
Centre = 50;

Plot( Overbought, "", colorRed );
Plot( Oversold, "", colorGreen );
Plot( Centre, "", colorWhite, styleDashed );
Plot( func2, "RSI mode", colorViolet, styleLine, Null, Null, 0, 0, 2 );
Plot( var, "", ColorRGB( 80, 80, 80 ), styleDashed | styleNoRescale, Null, Null, 0, -1, 1 );
PlotShapes( shapeSmallCircle * tr1, ColorRGB( 0, 0, 255 ), 0, var, -10 );
PlotShapes( shapeSmallCircle * pk1, ColorRGB( 255, 0, 0 ), 0, var, 10 );

if( !displaymode2 )
{
for( b = fvb; b <= lvb; b ++ )
{
{
Plot( LineArray( f2Bx0R[b], f2By0R[b], b, f2By1R[b] ), "", colorRed, styleLine, Null, Null, 0, 0, lineThickness1 );
Plot( BRExt2, "", colorRed, styleDashed | styleNoRescale, Null, Null, 0, -1, lineThickness2 );
}
else
{
Plot( LineArray( f2Bx0H[b], f2By0H[b], b, f2By1H[b] ), "", colorOrange, styleLine, Null, Null, 0, 0, lineThickness1 );
Plot( BHExt2, "", colorOrange, styleDashed | styleNoRescale, Null, Null, 0, -1, lineThickness2 );
}
else
{
Plot( LineArray( f2Bx0N[b], f2By0N[b], b, f2By1N[b] ), "", colorGold, styleLine, Null, Null, 0, 0, lineThickness1 );
Plot( BNExt2, "", colorGold, styleDashed | styleNoRescale, Null, Null, 0, -1, lineThickness2 );
}
else
if( SellRo[b] )
{
Plot( LineArray( f2Sx0R[b], f2Sy0R[b], b, f2Sy1R[b] ), "", colorBlue, styleLine, Null, Null, 0, 0, lineThickness1 );
Plot( SRExt2, "", colorBlue, styleDashed | styleNoRescale, Null, Null, 0, -1, lineThickness2 );
}
else
if( SellHo[b] )
{
Plot( LineArray( f2Sx0H[b], f2Sy0H[b], b, f2Sy1H[b] ), "", colorAqua, styleLine, Null, Null, 0, 0, lineThickness1 );
Plot( SHExt2, "", colorAqua, styleDashed | styleNoRescale, Null, Null, 0, -1, lineThickness2 );
}
else
if( SellNo[b] )
{
Plot( LineArray( f2Sx0N[b], f2Sy0N[b], b, f2Sy1N[b] ), "", colorPink, styleLine, Null, Null, 0, 0, lineThickness1 );
Plot( SNExt2, "", colorPink, styleDashed | styleNoRescale, Null, Null, 0, -1, lineThickness2 );
}
}
}
else
{
for( b = fvb; b <= lvb; b ++ )
{
{
Plot( LineArray( f2Bx0R[b], f2By0R[b], b, f2By1R[b] ), "", colorRed, styleLine, Null, Null, 0, 0, lineThickness1 );
Plot( LineArray( f2Sx0H[px1[b]], f2Sy0H[px1[b]], px1[b], f2Sy1H[px1[b]] ), "", colorAqua, styleLine, Null, Null, 0, 0, lineThickness1 );
Plot( BRExt2, "", colorRed, styleDashed | styleNoRescale | styleNoLabel, Null, Null, 0, -1, lineThickness2 );
Plot( SHExt2, "", colorAqua, styleDashed | styleNoRescale | styleNoLabel, Null, Null, 0, -1, lineThickness2 );
}
else
{
Plot( LineArray( f2Bx0R[b], f2By0R[b], b, f2By1R[b] ), "", colorRed, styleLine, Null, Null, 0, 0, lineThickness1 );
Plot( LineArray( f2Sx0N[px1[b]], f2Sy0N[px1[b]], px1[b], f2Sy1N[px1[b]] ), "", colorPink, styleLine, Null, Null, 0, 0, lineThickness1 );
Plot( BRExt2, "", colorRed, styleDashed | styleNoRescale | styleNoLabel, Null, Null, 0, -1, lineThickness2 );
Plot( SNExt2, "", colorPink, styleDashed | styleNoRescale | styleNoLabel, Null, Null, 0, -1, lineThickness2 );
}
else
if( SellR[b] AND SellRH[b] )
{
Plot( LineArray( f2Sx0R[b], f2Sy0R[b], b, f2Sy1R[b] ), "", colorBlue, styleLine, Null, Null, 0, 0, lineThickness1 );
Plot( LineArray( f2Bx0H[tx1[b]], f2By0H[tx1[b]], tx1[b], f2By1H[tx1[b]] ), "", colorOrange, styleLine, Null, Null, 0, 0, lineThickness1 );
Plot( SRExt2, "", colorBlue, styleDashed | styleNoRescale | styleNoLabel, Null, Null, 0, -1, lineThickness2 );
Plot( BHExt2, "", colorOrange, styleDashed | styleNoRescale | styleNoLabel, Null, Null, 0, -1, lineThickness2 );
}
else
if( SellR[b] AND SellRN[b] )
{
Plot( LineArray( f2Sx0R[b], f2Sy0R[b], b, f2Sy1R[b] ), "", colorBlue, styleLine, Null, Null, 0, 0, lineThickness1 );
Plot( LineArray( f2Bx0N[tx1[b]], f2By0N[tx1[b]], tx1[b], f2By1N[tx1[b]] ), "", colorGold, styleLine, Null, Null, 0, 0, lineThickness1 );
Plot( SRExt2, "", colorBlue, styleDashed | styleNoRescale | styleNoLabel, Null, Null, 0, -1, lineThickness2 );
Plot( BNExt2, "", colorGold, styleDashed | styleNoRescale | styleNoLabel, Null, Null, 0, -1, lineThickness2 );
}
}
}

Plot( buyLevel, "", colorYellow, styleDashed | styleNoRescale, Null, Null, 0, 0, 1 );
PlotShapes( IIf( dotBuy, shapeSmallSquare, shapeNone ), colorAqua, 0, func2, 0, 0 );
Plot( sellLevel, "", colorYellow, styleDashed | styleNoRescale, Null, Null, 0, 0, 1 );
PlotShapes( IIf( dotSell, shapeSmallSquare, shapeNone ), colorOrange, 0, func2, 0, 0 );
PlotShapes( IIf( dotSell, shapeDownArrow, shapeNone ), colorRed, 0 , func2, Offset = -20 );
PlotShapes( IIf( dotBuy, shapeUpArrow, shapeNone ),  colorBrightGreen, 0, func2, Offset = -20 );
}
else
{
Plot( func1, "Price mode", colorViolet, styleCandle, Null, Null, 0, 0, 1 );
Plot( zigzag, "", ColorRGB( 80, 80, 80 ), styleDashed | styleNoRescale, Null, Null, 0, -1, 1 );
PlotShapes( shapeSmallCircle * tr1, ColorRGB( 0, 0, 255 ), 0, L, -10 );
PlotShapes( shapeSmallCircle * pk1, ColorRGB( 255, 0, 0 ), 0, H, 10 );

if( !displaymode2 )
{
for( b = fvb; b <= lvb; b ++ )
{
{
Plot( LineArray( f1Bx0R[b], f1By0R[b], b, f1By1R[b] ), "", colorRed, styleLine, Null, Null, 0, 0, lineThickness1 );
Plot( BRExt1, "", colorRed, styleDashed | styleNoRescale, Null, Null, 0, -1, lineThickness2 );
}
else
{
Plot( LineArray( f1Bx0H[b], f1By0H[b], b, f1By1H[b] ), "", colorOrange, styleLine, Null, Null, 0, 0, lineThickness1 );
Plot( BHExt1, "", colorOrange, styleDashed | styleNoRescale, Null, Null, 0, -1, lineThickness2 );
}
else
{
Plot( LineArray( f1Bx0N[b], f1By0N[b], b, f1By1N[b] ), "", colorGold, styleLine, Null, Null, 0, 0, lineThickness1 );
Plot( BNExt1, "", colorGold, styleDashed | styleNoRescale, Null, Null, 0, -1, lineThickness2 );
}
else
if( SellRo[b] )
{
Plot( LineArray( f1Sx0R[b], f1Sy0R[b], b, f1Sy1R[b] ), "", colorBlue, styleLine, Null, Null, 0, 0, lineThickness1 );
Plot( SRExt1, "", colorBlue, styleDashed | styleNoRescale, Null, Null, 0, -1, lineThickness2 );
}
else
if( SellHo[b] )
{
Plot( LineArray( f1Sx0H[b], f1Sy0H[b], b, f1Sy1H[b] ), "", colorAqua, styleLine, Null, Null, 0, 0, lineThickness1 );
Plot( SHExt1, "", colorAqua, styleDashed | styleNoRescale, Null, Null, 0, -1, lineThickness2 );
}
else
if( SellNo[b] )
{
Plot( LineArray( f1Sx0N[b], f1Sy0N[b], b, f1Sy1N[b] ), "", colorPink, styleLine, Null, Null, 0, 0, lineThickness1 );
Plot( SNExt1, "", colorPink, styleDashed | styleNoRescale, Null, Null, 0, -1, lineThickness2 );
}
}
}
else
{
for( b = fvb; b <= lvb; b ++ )
{
{
Plot( LineArray( f1Bx0R[b], f1By0R[b], b, f1By1R[b] ), "", colorRed, styleLine, Null, Null, 0, 0, lineThickness1 );
Plot( LineArray( f1Sx0H[px1[b]], f1Sy0H[px1[b]], px1[b], f1Sy1H[px1[b]] ), "", colorAqua, styleLine, Null, Null, 0, 0, lineThickness1 );
Plot( BRExt1, "", colorRed, styleDashed | styleNoRescale, Null, Null, 0, -1, lineThickness2 );
Plot( SHExt1, "", colorAqua, styleDashed | styleNoRescale, Null, Null, 0, -1, lineThickness2 );
}
else
{
Plot( LineArray( f1Bx0R[b], f1By0R[b], b, f1By1R[b] ), "", colorRed, styleLine, Null, Null, 0, 0, lineThickness1 );
Plot( LineArray( f1Sx0N[px1[b]], f1Sy0N[px1[b]], px1[b], f1Sy1N[px1[b]] ), "", colorPink, styleLine, Null, Null, 0, 0, lineThickness1 );
Plot( BRExt1, "", colorRed, styleDashed | styleNoRescale, Null, Null, 0, -1, lineThickness2 );
Plot( SNExt1, "", colorPink, styleDashed | styleNoRescale, Null, Null, 0, -1, lineThickness2 );
}
else
if( SellR[b] AND SellRH[b] )
{
Plot( LineArray( f1Sx0R[b], f1Sy0R[b], b, f1Sy1R[b] ), "", colorBlue, styleLine, Null, Null, 0, 0, lineThickness1 );
Plot( LineArray( f1Bx0H[tx1[b]], f1By0H[tx1[b]], tx1[b], f1By1H[tx1[b]] ), "", colorOrange, styleLine, Null, Null, 0, 0, lineThickness1 );
Plot( SRExt1, "", colorBlue, styleDashed | styleNoRescale, Null, Null, 0, -1, lineThickness2 );
Plot( BHExt1, "", colorOrange, styleDashed | styleNoRescale, Null, Null, 0, -1, lineThickness2 );
}
else
if( SellR[b] AND SellRN[b] )
{
Plot( LineArray( f1Sx0R[b], f1Sy0R[b], b, f1Sy1R[b] ), "", colorBlue, styleLine, Null, Null, 0, 0, lineThickness1 );
Plot( LineArray( f1Bx0N[tx1[b]], f1By0N[tx1[b]], tx1[b], f1By1N[tx1[b]] ), "", colorGold, styleLine, Null, Null, 0, 0, lineThickness1 );
Plot( SRExt1, "", colorBlue, styleDashed | styleNoRescale, Null, Null, 0, -1, lineThickness2 );
Plot( BNExt1, "", colorGold, styleDashed | styleNoRescale, Null, Null, 0, -1, lineThickness2 );
}
}
}

PlotShapes( IIf( dotSell, shapeDownArrow, shapeNone ), colorRed, 0 , H, -20, 0 );
PlotShapes( IIf( dotBuy, shapeUpArrow, shapeNone ),  colorBrightGreen, 0, L, -20, 0 );
PlotShapes( IIf( dotSell, shapeSmallCircle, shapeNone ), colorWhite, 0 , C, 0, 0 );
PlotShapes( IIf( dotBuy, shapeSmallCircle, shapeNone ),  colorWhite, 0, C, 0, 0 );
}
``````
6 Likes

yes, most recent extension was missing .now it is there . thank u so much.

1 Like
``````func1 =rsi() ;
Plot(func1, _DEFAULT_NAME(),colorRED,
ParamStyle( "Style", styleline )
);
bi=BarIndex();
_SECTION_BEGIN(" ");
H= O;
L=ValueWhen(HHV(H,14),H,2);
SetBarsRequired(sbrAll,sbrAll);
xx = Cum(1);
nbar = PARAM("nbar",30,1,50,2);
pk=phigh=H>Ref(HHV(H,nbar),-1) AND Ref(HHV(H,nbar),nbar)<=H;
tr=plow=L<Ref(LLV(L,nbar),-1) AND Ref(LLV(L,nbar),nbar)>=L;

px0=ValueWhen(pk,bi,0); tx0=ValueWhen(tr,bi,0);
px1=ValueWhen(pk,bi,1); tx1=ValueWhen(tr,bi,1);
px2=ValueWhen(pk,bi,2); tx2=ValueWhen(tr,bi,2);
ph0=ValueWhen(pk,H,0); tl0=ValueWhen(tr,L,0);
ph1=ValueWhen(pk,H,1); tl1=ValueWhen(tr,L,1);
ph2=ValueWhen(pk,H,2); tl2=ValueWhen(tr,L,2);

ll=tr AND tl1<tl2;
hl=tr AND tl1>tl2;
hh=pk AND ph1>ph2;
lh=pk AND ph1<ph2;
dt=pk AND ph1==ph2;
db=tr AND tl1==tl2;

// lower trendline, segment 1: between fractal points
startval_L = ValueWhen(PLow,L,1);
endval_L = ValueWhen(PLow,L,0);
startbar_L = ValueWhen(PLow,xx,1);
endbar_L = ValueWhen(PLow,xx,0);
aa_L = (endval_L-startval_L)/(endbar_L-startbar_L);
bb_L = startval_L;
trendline_L = aa_L * (xx - startbar_L) + bb_L;
// slope calculations for display purpose only
dtrendline_L = trendline_L - Ref(trendline_L,-1);

// upper trendline, segment 1: between fractal points
startval_H = ValueWhen(PHigh,H,1);
endval_H = ValueWhen(PHigh,H,0);
startbar_H = ValueWhen(PHigh,xx,1);
endbar_H = ValueWhen(PHigh,xx,0);
aa_H = (endval_H-startval_H)/(endbar_H-startbar_H);
bb_H = startval_H;
trendline_H = aa_H * (xx - startbar_H) + bb_H;
// slope calculations for display purpose only
dtrendline_H = trendline_H - Ref(trendline_H,-1);

_SECTION_END();

_SECTION_BEGIN("rsidiv");
H= rsi();
L=ValueWhen(HHV(H,14),H,2);
SetBarsRequired(sbrAll,sbrAll);
xx = Cum(1);

// define fractals
pkm=PHighM = H > Ref(HHV(H,nbar),-1) AND Ref(HHV(H,nbar),nbar) <= H;
PHighPrice = ValueWhen(PHighM,H);
trm=PLowM = L < Ref(LLV(L,nbar),-1) AND Ref(LLV(L,nbar),nbar) >= L;
PLowPrice = ValueWhen(PLowM,L);
px0=ValueWhen(pkm,bi,0); txm0=ValueWhen(trm,bi,0);
pxm1=ValueWhen(pkm,bi,1); txm1=ValueWhen(trm,bi,1);
pxm2=ValueWhen(pkm,bi,2); txm2=ValueWhen(trm,bi,2);
phm0=ValueWhen(pkm,H,0); tlm0=ValueWhen(trm,L,0);
phm1=ValueWhen(pkm,H,1); tlm1=ValueWhen(trm,L,1);
phm2=ValueWhen(pkm,H,2); tlm2=ValueWhen(trm,L,2);
// lower trendline, segment 1: between fractal points
startval_L1 = ValueWhen(PLowM,L,1);
endval_L1 = ValueWhen(PLowM,L,0);
startbar_L1 = ValueWhen(PLowM,xx,1);
endbar_L1 = ValueWhen(PLowM,xx,0);
aa_L1 = (endval_L1-startval_L1)/(endbar_L1-startbar_L1);
bb_L1 = startval_L1;
trendline_L1 = aa_L1 * (xx - startbar_L1) + bb_L1;
// slope calculations for display purpose only
dtrendline_L1 = trendline_L1 - Ref(trendline_L1,-1);
// lower extended trendline, segment 2: extend segment 1 to next fractals pivot
startval_L_extend = ValueWhen(PLowm,L,2);
endval_L_extend = ValueWhen(PLowm,L,1);
startbar_L_extend = ValueWhen(PLowm,xx,2);
endbar_L_extend = ValueWhen(PLowm,xx,1);
aa_L_extend = (endval_L_extend-startval_L_extend)/(endbar_L_extend-startbar_L_extend);
bb_L_extend = startval_L1;
trendline_L_extend = aa_L_extend * (xx - startbar_L1) + endval_L_extend;
// slope calculations for display purpose only
dtrendline_L_extend = trendline_L_extend - Ref(trendline_L_extend,-1);
dtrendline_L_extend = IIf(PLowm,Ref(dtrendline_L1,-1),dtrendline_L_extend);

// lower extended trendline, segment 3: extend segment 2 nbars past the fractal pivot
startval_L_extend2 = ValueWhen(PLowm,L,3);
endval_L_extend2 = ValueWhen(PLowm,L,2);
startbar_L_extend2 = ValueWhen(PLowm,xx,3);
endbar_L_extend2 = ValueWhen(PLowm,xx,2);
aa_L_extend2 = (endval_L_extend2-startval_L_extend2)/(endbar_L_extend2-startbar_L_extend2);
bb_L_extend2 = endval_L_extend2;
trendline_L_extend2 = aa_L_extend2 * (xx - endbar_L_extend2) + endval_L_extend2;
// slope calculations for display purpose only
dtrendline_L_extend2 = trendline_L_extend2 - Ref(trendline_L_extend2,-1);
dtrendline_L_extend2 = IIf(PLowm,Ref(dtrendline_L_extend,-1),dtrendline_L_extend2);
// upper trendline, segment 1: between fractal points
startval_H1 = ValueWhen(PHighM,H,1);
endval_H1 = ValueWhen(PHighM,H,0);
startbar_H1 = ValueWhen(PHighM,xx,1);
endbar_H1 = ValueWhen(PHighM,xx,0);
aa_H1 = (endval_H1-startval_H1)/(endbar_H1-startbar_H1);
bb_H1 = startval_H1;
trendline_H1 = aa_H1 * (xx - startbar_H1) + bb_H1;
// slope calculations for display purpose only
dtrendline_H1 = trendline_H1 - Ref(trendline_H1,-1);
// upper extended trendline, segment 2: extend segment 1 to next fractals pivot
startval_H_extend = ValueWhen(PHighm,H,2);
endval_H_extend = ValueWhen(PHighm,H,1);
startbar_H_extend = ValueWhen(PHighm,xx,2);
endbar_H_extend = ValueWhen(PHighm,xx,1);
aa_H_extend = (endval_H_extend-startval_H_extend)/(endbar_H_extend-startbar_H_extend);
bb_H_extend = startval_H1;
trendline_H_extend = aa_H_extend * (xx - startbar_H1) + endval_H_extend;
// slope calculations for display purpose only
dtrendline_H_extend = trendline_H_extend - Ref(trendline_H_extend,-1);
dtrendline_H_extend = IIf(PHighm,Ref(dtrendline_H1,-1),dtrendline_H_extend);

// upper extended trendline, segment 3: extend segment 2 nbars past the fractal pivot
startval_H_extend2 = ValueWhen(PHighm,H,3);
endval_H_extend2 = ValueWhen(PHighm,H,2);
startbar_H_extend2 = ValueWhen(PHighm,xx,3);
endbar_H_extend2 = ValueWhen(PHighm,xx,2);
aa_H_extend2 = (endval_H_extend2-startval_H_extend2)/(endbar_H_extend2-startbar_H_extend2);
bb_H_extend2 = endval_H_extend2;
trendline_H_extend2 = aa_H_extend2 * (xx - endbar_H_extend2) + endval_H_extend2;
// slope calculations for display purpose only
dtrendline_H_extend2 = trendline_H_extend2 - Ref(trendline_H_extend2,-1);
dtrendline_H_extend2 = IIf(PHighm,Ref(dtrendline_H_extend,-1),dtrendline_H_extend2);
//Plot(trendline_L, "\nLower Trendline", colorBrightGreen,styleLine);
// segment 2, grey dots unconfirmed trend, green confirmed trend
l_extend=trendline_L_extend0+trendline_L_extend1+trendline_L_extend2;
Plot(IIf(BarsSince(Plowm) <= nbar,trendline_L_extend,Null), "", colorLightGrey,styleLine | styleDashed);
Plot(IIf(BarsSince(Plowm) > nbar,trendline_L_extend,Null), "",colorDarkGreen, styleLine | styleDASHED );
// segment 3
Plot(IIf(BarsSince(Plowm) <= nbar,trendline_L_extend2,Null), "",colorDarkGreen,styleLine | styledashed| styleThick);
// segment 1
//Plot(trendline_H, "\nUpper Trendline", colorRed,styleLine);
// segment 2, grey dots unconfirmed trend, green confirmed trend
H_extend=trendline_H_extend0+trendline_H_extend1+trendline_H_extend2;
Plot(IIf(BarsSince(PHighm) <= nbar,trendline_H_extend,Null), "", colorLightGrey, styleLine | styleDASHED );
Plot(IIf(BarsSince(PHighm) > nbar,trendline_H_extend,Null), "",colorOrange, styleLine | styleDASHED);
// segment 3
Plot(IIf(BarsSince(PHighm) <= nbar,trendline_H_extend2,Null), "",colorOrange, styleLine | styleDashed | styleThick);

_SECTION_END();

FHPDIV=IIf(dtrendline_L<0 AND dtrendline_L1<0 ,1,0);
HTHB=IIf(dtrendline_L>0 AND dtrendline_L1>0 ,1,0);

HPDIV=IIf(dtrendline_L>0 AND dtrendline_L1<0 ,1,0);
FHNDIV=iif(dtrendline_H>0 AND dtrendline_h1>0 ,1, 0);
LTLB=iif(dtrendline_H<0 AND dtrendline_h1<0 ,1, 0);

HNDIV=iif(dtrendline_H<0 AND dtrendline_h1>0 ,1, 0);
SPDIV=IIf(dtrendline_L<0 AND dtrendline_L1>0 ,1, 0)   ;

SNDIV=iif(dtrendline_H>0 AND dtrendline_h1<0 ,1, 0) ;
NDIV=LTLB OR  sndiv OR  hndiv OR   fhndiv;
PDIV=HTHB  OR  spdiv OR  hpdiv OR  fhpdiv;
color=IIf(SPDIV ,colorBRIGHTGreen,IIf(FHPDIV ,colorBLUE , IIf(HPDIV ,colorYELLOW, IIf(HTHB ,colorWHITE,NULL))));
color1=iif(SNDIV ,colorRED,IIf(FHNDIV ,colorBLUE , IIf(HNDIV ,colorYELLOW,IIf(LTLB ,colorWHITE,NULL))));

// segment 1
Plot(trendline_L1, "\nLower Trendline", color,styleLine|styleThick|styleNoRescale);
// segment 1
Plot(trendline_H1, "\nUpper Trendline", color1,styleLine|styleTHICK|styleNoRescale);
tit="";
``````

it based on trendline afl forrnulated by you , it detect similar type of divergence(green and red line regular divergence and yellow hidden divergence) but again pivot symetry is missing . is it feaseble to get pivot symetry in this afl ?

1 Like

with symmetry I think you mean that the pivots alternate, like: peak - trough - peak - through etc.

This is hard to do with these fractal pivots. I have some code to do that but for trading it is tricky. But looking at your code I am not sure what you like to do. Since "divergence" is to compare the behavior of 2 different functions. In the code of Nov 4 the pivots are calculated for the RSIa function and then transposed to the price function.

The pivots calculated for the RSIa function are calculated using the Zig, Trough and Peak function. You can use another method to calculate the pivots (like fractal pivots or ATR pivots or Gann pivots etc). And you can also calculate the pivots directly on the Price function and then transpose them to the RSIa function.

3 Likes

extension like SHExt2,SNExt2, SRExt2, BRExt2,BHExt2 and BNExt2 etc extend upto next divergence end as shown in figure from point a to b. is it possible to extend it further beyond point b upto point c (ie next cross )?

2 Likes

currently it is programmed in such a way that the extension is calculated until the next extension begins. I guess it is possible but I think I will leave that for someone else

2 Likes

sir please , i wont bother you again. thank you in advance .

1 Like

i do not have time at the moment. Also I do not like the idea. If it was a great idea I would do it but I do not like the idea. Often there is no cross and when there is a cross it is because of some line that started many bars ago. I do not see the point.

3 Likes