nice coding @awilson. I never used SparseCompress/SparseExpand before, pretty handy.
I made a version that uses the price as the main array and RSI as confirmation. Then you can drag it on top of a price chart. Also I added the "hidden" divergence. Also I added lines at the price where the divergence line is confirmed. I have set the percentage n to 0.5% as default which gives a bunch of signals in 5min charts of silver, gold and platinum which I follow.
_SECTION_BEGIN( "awilson1a Divergence using RSI" );
//SetBarsRequired( sbrAll, 0 );
periods = Param( "Periods", 14, 1, 200, 1 );
n = Param( "% Reverse ", 0.5, 0.01, 10, 0.01 );
func1 = C;
func2 = RSIa( C, periods );
Buy = Sell = 0;
bi = BarIndex();
Var = Zig( func1, n );
t = Trough( func1, n, 1 );
p = Peak( func1, n, 1 );
onlywhen = Var == p;
pkPrice = SparseCompress( onlywhen, func2 );
pkRsi = SparseCompress( onlywhen, p );
Sell1 = pkPrice > Ref( pkPrice, -1 ) AND pkRsi < Ref( pkRsi, -1 ); // regular
Sell2 = pkPrice < Ref( pkPrice, -1 ) AND pkRsi > Ref( pkRsi, -1 ); // hidden
Sell = Sell1 OR Sell2;
Sell = SparseExpand( onlywhen, Sell );
Sell1 = SparseExpand( onlywhen, Sell1 );
Sell2 = SparseExpand( onlywhen, Sell2 );
Sy0 = SparseExpand( onlywhen, Ref( pkRsi, -1 ) );
Sy1 = SparseExpand( onlywhen, pkRsi );
pkBi = SparseCompress( onlywhen, bi );
Sx0 = SparseExpand( onlywhen, Ref( pkBi, -1 ) );
onlywhen = Var == t;
thPrice = SparseCompress( onlywhen, func2 );
thRsi = SparseCompress( onlywhen, t );
Buy1 = thPrice < Ref( thPrice, -1 ) AND thRsi > Ref( thRsi, -1 ); // regular
Buy2 = thPrice > Ref( thPrice, -1 ) AND thRsi < Ref( thRsi, -1 ); // hidden
Buy = Buy1 OR Buy2;
Buy = SparseExpand( onlywhen, Buy );
Buy1 = SparseExpand( onlywhen, Buy1 );
Buy2 = SparseExpand( onlywhen, Buy2 );
By0 = SparseExpand( onlywhen, Ref( thRsi, -1 ) );
By1 = SparseExpand( onlywhen, thRsi );
thBi = SparseCompress( onlywhen, bi );
Bx0 = SparseExpand( onlywhen, Ref( thBi, -1 ) );
PlotShapes( IIf( Sell1, shapeDownArrow, shapeNone ), colorRed, 0 , Var, Offset = -20 );
PlotShapes( IIf( Sell2, shapeDownArrow, shapeNone ), colorOrange, 0 , Var, Offset = -20 );
PlotShapes( IIf( Buy1, shapeUpArrow, shapeNone ), colorBlue, 0, Var, Offset = -20 );
PlotShapes( IIf( Buy2, shapeUpArrow, shapeNone ), colorAqua, 0, Var, Offset = -20 );
Plot( Var, "", colorLightGrey, styleLine | styleNoRescale, Null, Null, 0, 0, 1 );
buyLevel = ValueWhen( Buy, func1 + func1 * ( n / 100 ) );
buyLevel = IIf( Flip( Buy, Ref( Cross( func1, buyLevel ), -1 ) ), buyLevel, Null );
dotBuy = IIf( buyLevel AND Cross( func1, buyLevel ), 1, 0 );
Plot( buyLevel, "", colorYellow, styleDashed | styleNoRescale, Null, Null, 0, 0, 1 );
PlotShapes( IIf( dotBuy, shapeSmallSquare, shapeNone ), colorYellow, 0, func1, 0, 0 );
sellLevel = ValueWhen( Sell, func1 - func1 * ( n / 100 ) );
sellLevel = IIf( Flip( Sell, Ref( Cross( sellLevel, func1 ), -1 ) ), sellLevel, Null );
dotSell = IIf( sellLevel AND Cross( sellLevel, func1 ), 1, 0 );
Plot( sellLevel, "", colorYellow, styleDashed | styleNoRescale, Null, Null, 0, 0, 1 );
PlotShapes( IIf( dotSell, shapeSmallSquare, shapeNone ), colorYellow, 0, func1, 0, 0 );
// Plot the Lines
FirstVisibleBar = Status( "FirstVisibleBar" );
Lastvisiblebar = Status( "LastVisibleBar" );
for( b = Firstvisiblebar; b <= Lastvisiblebar AND b < BarCount; b++ )
{
// regular divergence
if( Buy[b] AND Buy1[b] )
{
Plot( LineArray( Bx0[b], By0[b], b, By1[b] ), "", colorBlue, styleLine | styleNoRescale, Null, Null, 0, 15, 3 );
}
// hidden divergence
if( Buy[b] AND Buy2[b] )
{
Plot( LineArray( Bx0[b], By0[b], b, By1[b] ), "", colorAqua, styleLine | styleNoRescale, Null, Null, 0, 15, 3 );
}
// regular divergence
if( Sell[b] AND Sell1[b] )
{
Plot( LineArray( Sx0[b], Sy0[b], b, Sy1[b] ), "", colorRed, styleLine | styleNoRescale, Null, Null, 0, 15, 3 );
}
// hidden divergence
if( Sell[b] AND Sell2[b] )
{
Plot( LineArray( Sx0[b], Sy0[b], b, Sy1[b] ), "", colorOrange, styleLine | styleNoRescale, Null, Null, 0, 15, 3 );
}
}
_SECTION_END();
