// Downloaded From https://www.WiseStockTrader.com
_SECTION_BEGIN("Stress");
function PJKStress( High2, Low2, Close2, period )
{
  range1 = HHV( High, period ) - LLV( Low, period );
  range2 = HHV( High2, period ) - LLV( Low2, period );
  stoch1 = ( Close - LLV( Low, period ) )/range1;
  stoch2 = ( Close2 - LLV( Low2, period ) )/range2;

  VarSet("sstoch1", 100 * stoch1 );
  VarSet("sstoch2", 100 * stoch2 );

  diff = stoch1 - stoch2;

  range1 = HHV( diff, period ) - LLV( diff, period );
  
  return 100 * ( diff - LLV( diff, period ) )/range1;
}

Per = Param("Period", 60, 5, 100, 5); 
momper = Per;
Hedgeper = Per;

SetForeign("NIFTY"); 
// store index data in High2/Low2/Close2 variables
High2 = High;
Low2 = Low;
Close2 = Close;
RestorePriceArrays();

stress = PJKStress( High2, Low2, Close2, momper );

Stress_Buy_Level = Param("Buy Level", 10, 1, 49, 1) ;
Stress_Sell_Level = Param("Sell Level", 50, 50, 90, 1) ;
Stress_BuyWatch_Level = Stress_Buy_Level + 10;

Stress_Short_Level = Param("Short Level", 90, 50, 100, 1) ;
Stress_Cover_Level = Param("Cover Level", 50, 50, 89, 1) ;
Stress_ShortWatch_Level = Stress_Short_Level - 10;

Stress_Buy = Cross(Stress_Buy_Level, stress) ; //AND sstoch1 < 20 ;
Stress_Sell = Cross(stress, Stress_Sell_Level) ; //AND sstoch1 > 80 ;
Stress_buy = ExRem( Stress_buy, Stress_sell );
Stress_sell = ExRem( Stress_sell, Stress_buy ); 

Stress_Short = Cross(Stress, Stress_Short_Level) ;
Stress_Cover = Cross(Stress_Cover_Level, Stress) ; 
Stress_Cover = ExRem( Stress_Cover, Stress_Short );
Stress_Short = ExRem( Stress_Short, Stress_Cover ); 

shape_Buy = Stress_Buy * shapeUpArrow + Stress_Sell * shapesmallsquare; 
//PlotShapes( shape_Buy, IIf( Stress_Buy, colorTurquoise, colorTurquoise ), 0, 
//	IIf( Stress_Buy, Stress_Buy_Level, Stress_Sell_Level ) );
	
shape_Short = Stress_Short * shapeDownArrow + Stress_Cover * shapesmallsquare; 
//PlotShapes( shape_Short, IIf( Stress_Short, colorPink, colorPink ), 0, 
//	IIf( Stress_Cover, Stress_Cover_Level, Stress_Short_Level ) );

//SetGradientFill( colorIndigo /*top*/, colorDarkGrey /*bottom*/, 0 /*baseline level*/, GetChartBkColor() /*baseline color */ );
//PlotOHLC( stress, stress, 0, stress , "Stress Nifty("+Per+")", colorBluegrey, styleLine | styleGradient, Null, Null, 0, 0  );
Stress_Color = IIf(Cross(stress,Stress_Short_Level), colorRed, 
					IIf(Cross(Stress_Buy_Level,stress), colorRed,colorBlueGrey )); 
SetGradientFill( colorIndigo /*top*/, colorDarkGrey /*bottom*/, 0 /*baseline level*/, GetChartBkColor() /*baseline color */ );
PlotOHLC( stress, stress, 0, stress , "Stress Nifty("+Per+")", Stress_color, styleLine | styleGradient, Null, Null, 0, 0  );
//Plot( stress, "Stress Nifty("+Per+")", colorTan, ParamStyle("Style", styleGradient, maskHistogram), Null, Null, 0, 0, 1) ; //stylethick );

if (ParamToggle("Show Stoch","NO|YES",0))
{	
//Plot( stress, "Stress Nifty("+Per+")", colorTan, ParamStyle("Style", styleGradient, maskHistogram), Null, Null, 0, 0, 1) ; //stylethick );
Plot( sstoch1, "Stoch-"+Name(), colorGreen, Null, Null, 0, 0, 1) ;
Plot( sstoch2, "Stoch-Nifty", colorGold, Null, Null, 0, 0, 1) ;
}

//Plot( Stress_Buy_Level, "", colorViolet, stylenolabel) ;
//Plot( Stress_Sell_Level, "", colorGrey50, styleDots | stylenolabel) ;
//Plot( Stress_BuyWatch_Level, "", colorViolet, styleDashed | styleNoLabel) ;

//Plot( Stress_Short_Level, "", colorViolet, stylenolabel) ;
//Plot( Stress_Cover_Level, "", colorgrey50, styleDots | stylenolabel) ;
//Plot( Stress_ShortWatch_Level, "", colorViolet, styleDashed | styleNoLabel) ;
_SECTION_END();