// Downloaded From https://www.WiseStockTrader.com

_SECTION_BEGIN("Modified SuperTrend Code @ Fazal");
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
SetChartOptions(1,chartShowArrows|chartShowDates|chartWrapTitle);
function GetSecondNum()
{
    Time = Now( 4 );
    Seconds = int( Time % 100 );
    Minutes = int( Time / 100 % 100 );
    Hours = int( Time / 10000 % 100 );
    SecondNum = int( Hours * 60 * 60 + Minutes * 60 + Seconds );
    return SecondNum;
}
 
function PopupWindowEx( popupID, bodytext, captiontext, timeout, left, top )
{
    displayText = bodytext + captiontext;
    if ( ( StaticVarGetText( "prevPopup" + popupID ) != displayText) OR ( StaticVarGet( "prevPopupTime" + popupID ) < GetSecondNum() ) )
    {
        StaticVarSetText( "prevPopup" + popupID, displayText);
        StaticVarSet( "prevPopupTime" + popupID, GetSecondNum() + timeout );
        PopupWindow( bodytext, Captiontext + popupID, timeout, Left, top );
        Say("crossover.. action");
    }
}


GraphXSpace = 15;

SetBarFillColor(IIf(C>O,ParamColor("Candle UP Color", colorGreen),IIf(C<=O,ParamColor("Candle Down Color", colorRed),colorLightGrey)));
Plot(C,"",IIf(C>O,ParamColor("Wick UP Color", colorDarkGreen),IIf(C<=O,ParamColor("Wick Down Color", colorDarkRed),colorLightGrey)),64,0,0,0,0);
SetChartBkGradientFill( ParamColor( "TopColor", ColorRGB(0,0, 0 ) ), ParamColor( "BottomColor", ColorRGB(0,0, 0)));

//SetTradeDelays(1,1,1,1);
//SetPositionSize(75,spsShares);

Period   = Param("Period", 150, 1, 2400, 1);
mult   = Param("Multiplier", 4, 1.1, 20.0, 0.1);     

f=ATR(period);

VS[0] = Close[0]; 
trend[0] = 0;
HighC[0]=0;
Lowc[0]=0;

for( i = period+1; i < BarCount; i++ ) 
{ 

  vs[i]   = vs[i-1];
  trend[i]   = trend[i-1];
  highC[i]   = HighC[i-1];
  lowc[i]   = lowc[i-1];

   if ((trend[i]>=0) && ( C[i] <VS[i] ))
   {
         trend[i] =-1;
         HighC[i] = C[i];
         lowc[i] = C[i];
   }

   if ((trend[i]<=0) && (C[i] >VS[i]))
   {
         trend[i]=1;
         HighC[i] = C[i];
         lowc[i] = C[i];
   }  

   if (trend[i]==-1) 
   {
      if (C[i]<lowc[i]) lowc[i] = C[i];
    VS[i]= lowc[i]+ (mult*f[i]);
   }


   if (trend[i]==1)  
   {
      if (C[i]>HighC[i]) HighC[i] = C[i];
    VS[i]= HighC[i]-(mult*f[i]);
   }

} 

Plot(VS, "Vol Stop",IIf(trend==1,colorGreen,colorRed ),styleThick);

Buy=Cross(Trend,0);
Short=Cross(0, Trend);
Buy = Ref(Buy, -1);
Short = Ref(Short, -1);

Hp = HHV( H, 40 );
Lp = LLV( L, 40 );

BarsSincebuy = BarsSince( Buy );
BarsSinceshort = BarsSince( Short );
LastSignal = IIf( BarsSincebuy < BarsSinceshort, 1, -1 );
Sig = WriteIf( BarsSincebuy < BarsSinceshort, "BUY", "SELL" );

slPrice = IIf( LastSignal == 1, HighestSince( Buy, Lp ), LowestSince( Short, Hp ) );
initialrisk = IIf( LastSignal == 1, BuyPrice - SLPrice, SLPrice - ShortPrice );
CurrentPL = IIf( LastSignal == 1, C - BuyPrice, SellPrice - C );

BuyPrice=ValueWhen(Buy,O);
ShortPrice=ValueWhen(Short,O);

entry = IIf( LastSignal == 1, BuyPrice, ShortPrice );

PlotShapes(Buy * shapeUpArrow,colorGreen, 0,L, Offset=-45); 
PlotShapes(Short * shapeDownArrow,colorRed, 0,H, Offset=-45);


bars = LastValue( IIf(BarsSincebuy < BarsSinceshort, BarsSincebuy, BarsSinceshort));
Offset = 15;
Clr = IIf(LastValue(LastSignal) == 1, colorRed, colorGreen);


if ( ParamToggle( "Message Board ", "Show|Hide", 1 ) )
{
    GfxSelectFont( "Tahoma", 9, 700 );
    GfxSetBkMode( 1 );
    GfxSetTextColor( colorWhite );

    if ( SelectedValue( LastSignal ) == 1 )
    {
        GfxSelectSolidBrush( colorDarkGreen );
        Datetim = "" + ValueWhen( Buy, Day(), 1 ) + "/" + ValueWhen( Buy, Month(), 1 ) + "/" + ValueWhen( Buy, Year(), 1 ) + " " + ValueWhen( Buy, Hour(), 1 ) + ":" + ValueWhen( Buy, Minute(), 1 );
    }
    else
    {
        GfxSelectSolidBrush( colorBlue );
        Datetim = "" + ValueWhen( Short, Day(), 1 ) + "/" + ValueWhen( Short, Month(), 1 ) + "/" + ValueWhen( Short, Year(), 1 ) + " " + ValueWhen( Short, Hour(), 1 ) + ":" + ValueWhen( Short, Minute(), 1 );
    }
    
LastClose= Ref(C,-1);  
 
if (Buy[BarCount-2]==True)      
{
PopupWindowEx( "ID:1", "Get Ready to BUY  \n"+Name() + "  "+ Interval(2)+" :  "+ " Last ="+LastClose  , "Buy Alert -", 1000, 100, 1 ) ;   
}
if (Short[BarCount-2]==True)  
{
PopupWindowEx( "ID:2", "Get Ready to SHORT  \n"+Name() + "  "+ Interval(2) + "  :  "+ " Last ="+LastClose , "Short   Alert ", 1000, 1, 150 ) ;
}

}


    pxHeight = Status( "pxchartheight" ) ;

    xx = Status( "pxchartwidth" );
    Left = 1100;
    width = 310;
    x = 1.5;
    x2 = 236;

    y = pxHeight / 1;

    GfxSelectPen( colorLightBlue, 1 );
    GfxRoundRect( x, y - 105, x2, y , 7, 7 ) ;
    GfxTextOut( ( "Fazal,s   SuperTrend  " ), 25, y - 100 );
    GfxTextOut( ( " ........................................." ), 18, y - 90 );
    GfxTextOut( ( "Last Signal"), 10, y - 65 );
    GfxTextOut( ( ": " + Datetim ), 110, y - 65 );
    GfxTextOut( ( "" + sig + " Entry @" ), 10, y - 45 );
    GfxTextOut( ( ": " + entry ), 110, y - 45 );
    GfxTextOut( ( "Current P/L"), 10, y - 25 );
    GfxTextOut( ( ": " + WriteVal( IIf( sig == "BUY", (  C - entry ), ( entry - C ) ), 2.2 ) ), 110, y - 25);;
    x = 290;
    x2 = 500;
 

_SECTION_END();

_SECTION_BEGIN("Pivot Points");


//---------------------------------------------------------------------------
// This section gets whether they want pivot level for intraday or thier eod 
//---------------------------------------------------------------------------

_N(ioreod =ParamList("Pivot Levels for ",  "Intraday|EOD"));

if (ioreod=="Intraday") 
	{
		yh = TimeFrameGetPrice( "H", inDaily, -1 );
		yl = TimeFrameGetPrice( "L", inDaily, -1 );
		yc = TimeFrameGetPrice( "C", inDaily, -1 );
	}
else
	{
		yh = TimeFrameGetPrice( "H", inDaily, 0 );
		yl = TimeFrameGetPrice( "L", inDaily, 0 );
		yc = TimeFrameGetPrice( "C", inDaily, 0 );
	}

//---------------------------------------------------------------------------
//						 To calculate the Pivot Levels 
//---------------------------------------------------------------------------

to = TimeFrameGetPrice( "O", inDaily, 0 );
pivot = (yh + yl + yc) / 3;
range = yh - yl;
_N(pist =ParamList("Select Pivot Type ", "Classical Pivot|Woodie Pivot|Caramilla Pivot|Fibonacci Pivot"));

if (pist =="Classical Pivot" )
	{
		r1 = (2 * pivot) - yl ;
		s1 = (2 * pivot) - yh ;
		r2 = pivot - s1 + r1;
		s2 = pivot - (r1 - s1) ;
		r3 = 2 * (pivot - yl) + yh ;
		s3 = yl - (2 * (yh - pivot));
	}

else if(pist =="Woodie Pivot" )
	{
		pivot = (yh + yl + yc + to) / 4;
		r1 = (2 * pivot) - yl;
		r2 = pivot + range;
		r3 = yh + 2 * (pivot - yl);
		r4 = r3 + range;
		s1 = (2 * pivot) - yh;
		s2 = pivot - range;
		s3 = yl - 2 * (yh - pivot);
		s4 = S3 - range;
	}

else if(pist =="Caramilla Pivot" )
	{
		r4 = yc + range * 1.1/2;
		r3 = yc + range * 1.1/4;
		r2 = yc + range * 1.1/6;
		r1 = yc + range * 1.1/12;
		s1 = yc - range * 1.1/12;
		s2 = yc - range * 1.1/6;
		s3 = yc - range * 1.1/4;
		s4 = yc - range * 1.1/2;
	}

else 
	{
		r3 = pivot + 1.000 * (yh - yl);
		r2 = pivot + 0.618 * (yh - yl);
		r1 = pivot + 0.382 * (yh - yl); 
		s1 = pivot - 0.382 * (yh - yl); 
		s2 = pivot - 0.618 * (yh - yl); 
		s3 = pivot - 1.000 * (yh - yl);
	}

//---------------------------------------------------------------------------
//			 			To Plot Pivot Levels in the screen 
//---------------------------------------------------------------------------

_N(dsr =ParamList("Draw Intraday Pivot Levels ",  "None|Both|Support|Resistance"));

if (dsr =="Support" OR  dsr == "Both")
{
	Plot(pivot, "\n Pivot - ",colorWhite,1);
	Plot(r1, "Resistance 1 - ",colorYellow,1);
	Plot(r2, "Resistance 2 - ",colorYellow,1);
	Plot(r3, "Resistance 3 - ",colorYellow,1);
	Plot((pivot+r1)/2, "Mid Value of R1 & Pivot  ",colorYellow,1);
	Plot((r3+r2)/2, "Mid Value of R2 & R3 -  ",colorYellow,1);
	Plot((r1+r2)/2, "Mid Value of R1 & R2 - ",colorYellow,1);
}

if( dsr == "Resistance" OR  dsr == "Both")
{
	Plot(pivot, "\n Pivot - ",colorWhite,1);
	Plot(s3, "Support 2 - ",colorYellow,1);
	Plot(s2, "Support 2 - ",colorYellow,1);
	Plot(s1, "Support 1 - ",colorYellow,1);
	Plot((s3+s2)/2, "Mid Value of S2 & S3 ",colorYellow,1);
	Plot((s1+s2)/2, "Mid Value of S1 & S2 -  ",colorYellow,1);
	Plot((pivot+s1)/2, "Mid Value of S1 & Pivot  ",colorYellow,1);
}

//---------------------------------------------------------------------------
//				 Printing the pivot level in interpretation window
//---------------------------------------------------------------------------


printf(Name()+ "\n\nResistance - 3  |  %g\nResistance - 2  |  %g\nResistance - 1  |  %g\n" + 
"Pivot                |  %g\nSupport - 1      |  %g\nSupport - 2      |  %g\nSupport - 3      |  %g",
r3,r2,r1,pivot,s1,s2,s3);


//---------------------------------------------------------------------------
//					 	This section is for Exploration 
//---------------------------------------------------------------------------

Filter = 1;
AddColumn(r3,"Resistance 3");
AddColumn(r2,"Resistance 2");
AddColumn(r1,"Resistance 1");
AddColumn(Pivot,"Pivot");
AddColumn(s1,"Support 1");
AddColumn(s2,"Support 2");
AddColumn(s3,"Support 3");

//---------------------------------------------------------------------------
//					 	Add Pivot levels along with the title
//---------------------------------------------------------------------------

_N(Title = EncodeColor(colorOrange)+ StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g(%.1f%%)\n"+
	EncodeColor(colorWhite)+"Resistance 3 -=- %g ::::: Resistance 2 -=- %g ::::: Resistance 1 -=- %g :::::"+
	EncodeColor(colorYellow)+" Pivot -=- %g"+
	EncodeColor(colorYellow)+"\nSupport 1 -=- %g ::::: Support 2 -=- %g ::::: Support 3 -=- %g\n ",
 O, H, L, C,SelectedValue( ROC( C, 1 ) ),r3,r2,r1,pivot,s1,s2,s3));

//---------------------------------------------------------------------------
//					 			End of Pivot Point 
//---------------------------------------------------------------------------

_SECTION_END();

_SECTION_BEGIN( "L T Price" );
GfxSetOverlayMode( 0 );
GfxSelectPen( colorRed, 3 );
GfxSelectSolidBrush( colorLightYellow );
GfxRoundRect( 500, 38, 600, 70, 15, 15 );
GfxSetBkMode( 1 );
GfxSelectFont( "Arial", 14.5, 400, False );
GfxSetTextColor( colorBrown );
GfxSetTextAlign( 0 );
GfxSetTextColor( colorBlack );
GfxTextOut( "  " + C , 500, 40 );
_SECTION_END();


_SECTION_BEGIN("Linear Reg Channel"); 
//Plot(Close, "", 55, GetPriceStyle());
P        = ParamField("Price field", -1);
Daysback = Param("Period for Linear Regression Channel", 50, 1, 300, 1);
SDP1     = Param("Standard Deviation 1", 1.0, 0, 6, 0.05);
SDP2     = Param("Standard Deviation 2", 2.0, 0, 6, 0.05);
SDP3     = Param("Standard Deviation 3", 3.0, 0, 6, 0.05);
ext      = Param("extend Linear Regression Channel", 15, 0, 50, 1);
shift    = Param("Look back period", 0, 0, 240, 1); 
Collg    = ParamColor("Color Linear Reg Line", colorRed); 
Colsd1   = ParamColor("Color StDev 1", colorBlue);
Colsd2   = ParamColor("Color StDev 2", colorLime);
Colsd3   = ParamColor("Color StDev 3", colorGold);
SDP1     = SDP1/2;
SDP2     = SDP2/2;
SDP3     = SDP3/2;
pds      = Daysback;
x        = BarIndex() + 1;
sx       = SelectedValue(x)-shift;
aa       = SelectedValue(Ref(LinRegIntercept(P, pds), -shift));
bb       = SelectedValue(Ref(LinRegSlope(P, pds), -shift));
StDev0   = StDev(P, pds);
fd1      = SelectedValue(Ref(SDP1 * StDev0, -shift) );
fd2      = SelectedValue(Ref(SDP2 * StDev0, -shift) );
fd3      = SelectedValue(Ref(SDP3 * StDev0, -shift) );
ys       = SelectedValue(ValueWhen(x, aa, 1));
yi       = SelectedValue(ValueWhen(x, bb, 1));
xs       = sx - pds;
d        = ext;//Shift Line Right
xe       = xs + pds + d;
ye       = ys + yi * (xe - xs);
sty      = 1|32|2048;
Plot(LineArray(xs-d,ys,xe-d,ye,1),"",Collg,sty,0,0,d);
Plot(LineArray(xs-d,ys-fd1,xe-d,ye-fd1,1),"",Colsd1,sty,0,0,d);
Plot(LineArray(xs-d,ys+fd1,xe-d,ye+fd1,1),"",Colsd1,sty,0,0,d);
Plot(LineArray(xs-d,ys-fd2,xe-d,ye-fd2,1),"",Colsd2,sty,0,0,d);
Plot(LineArray(xs-d,ys+fd2,xe-d,ye+fd2,1),"",Colsd2,sty,0,0,d);
Plot(LineArray(xs-d,ys-fd3,xe-d,ye-fd3,1),"",Colsd3,sty,0,0,d);
Plot(LineArray(xs-d,ys+fd3,xe-d,ye+fd3,1),"",Colsd3,sty,0,0,d);
_SECTION_END();