// Downloaded From https://www.WiseStockTrader.com
_SECTION_BEGIN("PARABOLIC SAR");

/////////////////////////////////

// Parabolic SAR re-implemented in

// native AFL.

//

// Example of for/if/else control statements

//

// Requires:

// AmiBroker 4.31.1

//

// Written by: Tomasz Janeczko

 

IAF = 0.01;       // acceleration factor

MaxAF = 0.02;     // max acceleration

 

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( Close, "Price", colorBlack, styleCandle );

Plot( psar, "SAR", colorRed, styleDots | styleNoLine | styleThick );

range = 30;//Param("Periods", 14, 2, 200, 1 );

ADXi=ADX(range);

PDIi=PDI(range);

MDIi=MDI(range);

//Plot( ADXi=ADX(range), _DEFAULT_NAME(), ParamColor( "ADX color", colorBlue ), ParamStyle("ADX style", styleThick ) );

//Plot( PDIi=PDI(range), "", ParamColor( "+DI color", colorGreen ), ParamStyle("+DI style") );

//Plot( MDIi=MDI(range), "", ParamColor( "-DI color", colorRed ), ParamStyle("-DI style") );

uptrend=PDIi>MDIi;//+di greater than -di

downtrend=MDIi>PDIi;//-di greater than +di

Ribboncol=IIf(upTrend,colorGreen, IIf(downtrend,colorRed, colorBlack));

Plot(6, "", Ribboncol, styleOwnScale|styleArea|styleNoLabel, -7.5,100);

 

Buy=(Cross(L,psar) OR Cross(Ref(L,-1),Ref(psar,-1)))  AND uptrend;

Sell=(Cross(psar,H) OR Cross(Ref(psar,-1),Ref(H,-1)))  AND downtrend;

PlotShapes(Sell*shapeDownArrow,colorRed);

PlotShapes(Buy*shapeUpArrow,colorGreen);

AlertIf (Buy, "SOUND C: \ \ Program Files \ \ amibroker \ \ RekamSuara \ \ Beli.wav"

 ,"Audio alert", 2);

 AlertIf (Sell, "SOUND C: \ \ Program Files \ \ amibroker \ \ RekamSuara \ \ Jual.wav"

 ,"Audio alert", 2);

 

_SECTION_END();