// Downloaded From https://www.WiseStockTrader.com
_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
_SECTION_END();

_SECTION_BEGIN("Multi frame sar");
Offset=10;
Filter=1;
NumColumns=4;

function MySAR()
{

IAF = Param("sarIAF",0.1,0.01,1,0.01);    // acceleration factor
MaxAF = Param("sarMaxAF",0.25,0.01,1.5,0.01); // max acceleration
ASAR = Param( "ASAR", -1, -50, 1000, 1 );

psar = Close;		// initialize
long = 1;        // assume long for initial conditions
af = IAF;         // init acelleration factor
ep = Low[ 0 ];   // init extreme point
hp = High [ 0 ];
lp = Low [ 0 ];

for( i = 2; i < BarCount; i++ )
{
	if ( long )
	{
		psar [ i ] = psar [ i-1 ] + af * ( hp - psar [ i-1 ] );
	}
	else
	{
		psar [ i ] = psar [ i-1 ] + af * ( lp - psar [ i-1 ] );
	}

	reverse =  0;
	//check for reversal
	if ( long )
	{
		if ( Low [ i ] < psar [ i ]  )
		{
			long = 0; reverse = 1; // reverse position to Short
			psar [ i ] =  hp;       // SAR is High point in prev trade
			lp = Low [ i ];
			af = IAF;
		}
	}
	else
	{
		if ( High [ i ] > psar [ i ]  )
		{
			long = 1; reverse = 1;        //reverse position to long
			psar [ i ] =  lp;
			hp = High [ i ];
			af = IAF;
		}
	}

	if ( reverse == 0 )
	{
		if ( long )
		{
			if ( High [ i ] > hp ) 
			{
				hp = High [ i ]; 
				af = af + IAF; 
				if( af > MaxAF ) af = MaxAF; 
			}
             
			if( Low[ i - 1 ] < psar[ i ] ) psar[ i ] = Low[ i - 1 ];
			if( Low[ i - 2 ] < psar[ i ] ) psar[ i ] = Low[ i - 2 ];
		}
       else
		{
			if ( Low [ i ] < lp )  
			{ 
				lp = Low [ i ]; 
				af = af + IAF; 
				if( af > MaxAF ) af = MaxAF; 
			}	
				
			if( High[ i - 1 ] > psar[ i ] ) psar[ i ] = High[ i - 1 ];
			if( High[ i - 2 ] > psar[ i ] ) psar[ i ] = High[ i - 2 ];

		}
	}
}



  Plot(0,"Null",colorYellow,styleLine|styleNoLabel);

xxxx=SAR(IAF,MaxAF)>Ref(C,-ASAR);//trabajando con close de candela anterior
yyyy=Ref(C,-ASAR)>SAR(IAF,MaxAF) ;

SAREVENTO2=(IIf(yyyy,1,IIf(xxxx,0,1)));

   return(SAREVENTO2);
}

function DrawSAR(style)
{
Offset=Offset+10;

if(style == 0)
  {
  style=in1Minute;
  }

  TimeFrameSet(style);
  SAREVENTO2=TimeFrameExpand(MySAR(),style);
  PlotShapes( IIf(SAREVENTO2<=1,14,0) ,IIf(SAREVENTO2==0,colorWhite,colorGreen)
 ,  0, 1, Offset);

  PlotShapes( IIf(SAREVENTO2==0,13,0) ,IIf(SAREVENTO2==1,colorWhite,colorRed),
0, 1, Offset*-1);
  TimeFrameRestore();  
}

Offset=0;

//DrawSAR(in1Minute);
DrawSAR(2*in5Minute);
DrawSAR(2*in15Minute);
DrawSAR(inHourly);
DrawSAR(inDaily);
//DrawSAR(inWeekly);
//DrawSAR(inMonthly);


GraphXSpace = 40;
_SECTION_END();