// Downloaded From https://www.WiseStockTrader.com
SetBacktestMode(backtestregularrawmulti);
Title = Name() + ", " + Interval(2) + ", " + Date() + " " +C;

_SECTION_BEGIN("RSI Divergence");
//------------------------------------------------------------------------------
//
//  Formula Name:    RSI divergence
//  Author:          Rajandran R
//  Url:             www.marketcalls.in
//  Formula URL:     
//  Details URL:     
//
//------------------------------------------------------------------------------
//
//  + scanner
//
//------------------------------------------------------------------------------

/*---------------------------------------------------
	OBV Divergence
--------------------------------------------------------*/
//OldAlert = ParamToggle("Old alert active", "No|Yes");
AudioAlert = ParamToggle("Human Voice Alert", "No|Yes");
EmailAlert = ParamToggle("E-mail Alert", "No|Yes");
OrderType = ParamList("Short Name of Security for alerts", "Gold|Dollar|Bank Nifty|RSI|Nifty|CNXIT" );
NDays=Param("Number of RSI Days",28,1,50,1);// new addition by sph
NzigDays=Param("Zig Number",9,5,50,1);// new addition by sph
GraphXSpace=7;
//n=Param("% Reverse ",12,0,100,1);
n=Optimize("ZIG",NzigDays,5,50,1);//default is 9
per=Optimize("rsi",NDays,5,50,1);//default is 28
Buy=Sell=0;
Var = Zig(RSI(per), n); 
t= Trough(RSI(per), n, 1); 
p= Peak(RSI(per), n, 1); 
x[0] =Var[0];
price[0] = C[0];
j=0;

// bearish divergence
for ( i=0; i<BarCount; i++) 
{
if(Var[i] == p[i])
{

j++;
x[j] =Var[i];
price[j] =C[i];
if(x[j] <x[j-1] && price[j-1]< price[j]) 
Sell[i] =1;
}
}

// bullish divergence
for ( i=0; i<BarCount; i++) 
{
if(Var[i] == t[i])
{
j++;
x[j] =Var[i];
price[j] =C[i];
if(x[j] >x[j-1] && price[j]<price[j-1]) 
Buy[i] =1;
}
}
Short=Sell;
Cover=Buy;

Plot(Var, "", 39); 
PlotShapes ( IIf(Sell, shapeCircle, shapeNone), colorRed, 0 , Var,0);
PlotShapes( IIf(Buy, shapeCircle, shapeNone),  colorBrightGreen, 0, Var,0);

Title ="RSI Divergence" ;
_SECTION_END();

_SECTION_BEGIN("TEMA");
P = ParamField("Price field",-1);
Periods = Param("Periods", 15, 2, 200, 1, 10 );
Plot( TEMA( P, Periods ), _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle("Style") ); 
_SECTION_END();

//if (OldAlert==True)
//{
if (AudioAlert==True)
{
AlertIf( Buy, "SOUND C:\\Windows\\Media\\Afternoon\\Windows Error.wav", "Buy alert A7", 2,2,5 );

AlertIf( Sell, "SOUND C:\\Windows\\Media\\Afternoon\\Windows Error.wav", "Sell alert A7", 2,2,5 );



}

if (EmailAlert==True) 
{

//barcomplete = BarIndex() < LastValue(BarIndex());
//AlertIf( barcomplete AND Buy, "EMAIL", "RSI Buy" + BarIndex() +FullName()+" "+ C, 1, 1+2+4+8, 5 );
//AlertIf( barcomplete AND Sell, "EMAIL", "RSI Sell"+ BarIndex() +FullName()+" "+ C, 2,1+2+4+8,5 );
AlertIf( Sell, "EMAIL", "RSI Sell"+ BarIndex() +FullName()+" "+ C, 2,10,5 );
AlertIf( Buy, "EMAIL", "RSI Buy" + BarIndex() +FullName()+" "+ C, 1, 10, 5 );
}


//}

//if (AudioAlert==True)
//{

//AlertCondition = LastValue( Sum( Buy, 5) );
//if (alertCondition)
//{
//LastAlert = StaticVarGet("AlertsTrack");
//if ( LastValue(Buy) AND LastAlert != 1 )
//{
//    AlertIf( Buy, "SOUND C:\\Windows\\Media\\Notify.WAV", "Buy", 1, 1 +2 );
   // AlertIf( Buy, "EMAIL", "Buy EMAIL", 1, 1 + 2 );
   // StaticVarSet( "AlertsTrack", 1 );
//}

//if ( LastValue(Sell) AND LastAlert != -1 )
//{
//    AlertIf( Sell, "SOUND C:\\Windows\\Media\\Notify.WAV", "Sell", 2,1+2 );
 //   AlertIf( Sell, "EMAIL", "Sell EMAIL", 1, 1 + 2 );
 //   StaticVarSet( "AlertsTrack", -1 );
//}

//if( ParamTrigger("RESET ALERTS", "RESET ALERTS") )
    //StaticVarSet( "AlertsTrack", 0 );
//}
//}

GfxSelectFont( "Tahoma", 35, 100 );
GfxSetBkMode( 1 );

GfxSetTextColor
( colorBlack );
pxChartTop = Status( "pxcharttop" ) ;
pxChartRight = Status( "pxchartright" ) ;
x=pxChartRight;
y = pxChartTop;
//GfxTextOut
//( "P " + C,X-210,y+20);
GfxSelectFont( "Tahoma", 18, 100 );
GfxTextOut
( "V " + Volume ,X-300,y+80);
//barcompletex =  LastValue(BarIndex());
barcompletex = BarIndex();
GfxTextOut
( "Bar " + " " + barcompletex, X-300,y+110);
GfxTextOut
( Date(),X-300,y+140);
SellPrice=ValueWhen(Short,C,1);
BuyPrice=ValueWhen(Buy,C,1);
Long=Flip(Buy,Sell);
Shrt=Flip(Short,Cover);

GfxSelectFont( "Tahoma", 35, 100 );
GfxSetTextColor
( colorGreen );
GfxTextOut
( ("" + WriteIf (Long AND NOT Buy, "P "+C+" ("+(C-BuyPrice)+")","")), x-410, y+20);
//( "P " + C,X-310,y+20);
GfxSetTextColor
( colorRed );
GfxTextOut
( ("" + WriteIf (shrt AND NOT Short, "P "+C+" ("+(SellPrice-C)+")","")), x-410, y+20);
//( "P " + C,X-210,y+20);
GfxSetTextColor
( colorBlue );
GfxTextOut
( ("" + WriteIf (Buy, "P "+C,"")), x-310, y+20);
GfxSetTextColor
( colorOrange );
GfxTextOut
( ("" + WriteIf (Sell, "P "+C,"")), x-310, y+20);