Modifying SAR code to work in 2nd time frame

Hello Members,

I am a long time user of AB, first time poster. Appreciate any feedback if I can improve my posting skills.

Situation:

I've modified the PSAR code from the AB library to use a custom series instead of close. I'm using EMA(C,10) and it works well in the native time frame. I can't seem to make it work across multiple time frame. Appreciate any suggestions.

In the screen shot below, the built in SAR series is plotted in red in both time frames. My custom SAR series on native timeframe is plotted in yellow, but the hourly time frame custom SAR series is empty.

Please try this code on 15min bar (or any TF less than hour).

// original code is http://www.amibroker.com/members/library/formula.php?id=268
// Modified code to calcular PSAR on custom series, instead of close.
// In this code serA is simply the 10 period EMA of close.

global custompsar;

procedure custom_psar()
{
	lb = 10;
	nstart = lb + 2;
	customA = EMA(C, lb); 


	IAF = 0.02;       // acceleration factor
	MaxAF = 0.2;     // max acceleration 

	psar = customA; 		// *** Using abritrary series A instead of close.

	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 = nstart; 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 ] );
		}

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

				psar [ i ] =  lp;
				hp = customA [ i ];
				af = IAF;
			}
		}

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

			}
		}
	}
custompsar = psar;
}

plotnative = ParamToggle("Plot native SAR intraday (< hourly)  ?", "No|Yes",1); // This is AB's built in SAR
plotnativeTF2 = ParamToggle("Plot native SAR  hourly?", "No|Yes",1); // This is AB's built in SAR
plotcustomsar = ParamToggle("Plot custom series SAR intraday (< hourly) ?", "No|Yes",1); // This is AFL SAR, modified to use EMA10 of close
plotcustomsarTF2 = ParamToggle("Plot custom series SAR hourly?", "No|Yes",1);



if (plotnative)
{
nativepsar = SAR(0.02,.2);
Plot( Close, "Price", colorBlack, styleCandle );
Plot(nativepsar, "SAR", colorRed, styleDots | styleNoLine | styleThick );
}

if (plotnativeTF2)
{
TimeFrameSet(inHourly);
psar2 = SAR(0.02,.2);
TimeFrameRestore();
psar2= TimeFrameExpand(psar2, inHourly);
Plot( Close, "Price", colorBlack, styleCandle );
Plot( psar2, "SAR hourly", colorRed, styleDots | styleNoLine | styleThick );
}

if (plotcustomsar)
{
custom_psar();
Plot( Close, "Price", colorBlack, styleCandle );
Plot( custompsar, "SAR on custom", colorYellow, styleDots | styleNoLine | styleThick );

}
if (plotcustomsarTF2)
{
TimeFrameSet(inHourly);
custom_psar();
TimeFrameRestore();
custompsar2= TimeFrameExpand(custompsar, inHourly);
Plot( Close, "Price", colorBlack, styleCandle );
Plot( custompsar2, "SAR on custom hourly", colorYellow, styleDots | styleNoLine | styleThick );

}

As for loop and multi time frames...
You have to add NullCount(array) to nstart.
array being e.g. your EMA array.

First don't use black color on black chart. It is nonsensical.

Plot( Close, "Price", colorBlack, styleCandle ); // don't use colorBlack, use colorDefault

Secondly, don't repeat the same Plot() over and over again (you are drawing candlesticks over and over in your code) again - nonsense.

Thirdly - use built-in SAR() function instead of looping code. There is no point in doing so. Custom arrays can be used together with SAR (simply assign C, H, and L arrays before calling SAR).

Fourthly - SAR requires C and L and H arrays, not only Close as in your code

C = customC;
H = customH;
L = customL;
customSAR = SAR(); // will use custom values

Don't reinvent the wheel, use SAR().

Lastly, TimeFrame functions would give NULLS at the beginning of the array and you have to SKIP nulls, if you are writing loops.

1 Like

I did not realize I could use built in SAR. That solves it. Thank you Tomasz.

Sorry about posting tardy coding. I was copy-pasting to create a standalone formula file.

Excellent, thank you @fxshrat!

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.