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 );
}