Join our facebook group for Amibroker afl code discussion https://www.facebook.com/groups/603980776889348

Double Confirmation Parabolic SAR

Double Confirmation Parabolic SAR
Double Confirmation Parabolic SAR
//www.aflcode.com

StartAF = Param("AccelerationFactor", 0.02, 0.01, 0.10, 0.01);
MaxAF = Param("MaxAcceleration", 0.2, 0.1, 0.5, 0.1);


// Filter = 1;
// AddColumn(curSAR, "curSAR");
// AddColumn(plus1SAR, "plus1SAR");





longPos = 1;        // assume longPos for initial conditions
psar = Close;  // initialize
psarNextBar = Close;

af = StartAF;         // init acelleration factor
ep = Low[ 0 ];   // init extreme point
hp = High [ 0 ];
lp = Low [ 0 ];


//   if ( longPos ) {
//     psar [ i ] = psar [ i-1 ] + af * ( hp - psar [ i-1 ] );
//   }
//   else {
//     psar [ i ] = psar [ i-1 ] + af * ( lp - psar [ i-1 ] );
//   }
function getPsar1(idx) {
  global longPos;
  global psar;
  local retVal;
  if(longPos) {
    retVal = psar [ idx-1 ] + af * ( hp - psar [ idx-1 ] );
  } else {
    retVal = psar [ idx-1 ] + af * ( lp - psar [ idx-1 ] );
  }
  return retVal;
}
    

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

  if ( reverse == 0 ) {
    if ( longPos ) {
      if ( High [ i ] > hp ) {
 hp = High [ i ]; 
 af = af + StartAF; 
 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 + StartAF; 
 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 ];

    }
  }
  //NextBarSar.
  psarNextBar[i] = getPsar1(i + 1);
  if( Low[ i ] < psarNextBar[ i ] ) psarNextBar[ i ] = Low[ i ];
  if( Low[ i - 1 ] < psarNextBar[ i ] ) psarNextBar[ i ] = Low[ i - 1 ];

}


//PLOT
_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() );



Plot( Close, "Price", colorBlack, styleCandle );
Plot( psar, "PSAR", colorRed, styleDots | styleNoLine | styleThick );
Plot( psarNextBar, "PSARNEXTBAR", colorBlue, styleDots | styleNoLine | styleThick );


//EXPLORE
Filter = 1;
AddColumn(psar, "psar");
AddColumn(psarNextBar, "psarNextBar");
Previous
Next Post »