Parabolic SAR

The Parabolic SAR (stop and reverse) was authored by J. Welles Wilder. It sets trailing price stops for long or short positions. Note: AF below stands for Acceleration Factor. The user may change the acceleration factors and value. This indicator’s definition is further expressed in the raw code given in the calculation below.
Click here for more information.

Parabolic Sar1

How To Trade Using Parabolic SAR

The Parabolic SAR may be used for an exit strategy. If a low is less than or equal to the pSAR a sell to exit (exit long) signal will be generated. Conversely, if the high is more than or equal to the pSAR a buy to cover (exit short) signal will be given.

How To Access in MotiveWave

Go to the top menu, choose Study>Welles Wilder>Parabolic SAR

or go to the top menu, choose Add Study, start typing in this study name until you see it appear in the list, click on the study name, click OK.

Important Disclaimer: The information provided on this page is strictly for informational purposes and is not to be construed as advice or solicitation to buy or sell any security. Please see our Risk Disclosure and Performance Disclaimer Statement.

Calculation

//Initial Accelerator Factor = AF = user defined, default is .02
//Max Accelerator Factor = AFmax = user defined, default is .2
//Step = AFstep = user defined, default is .02
//index = current bar number

  protected void calculate(int index, DataContext ctx)
  
    DataSeries series=ctx.getDataSeries();
    if (index lessThan 1) return;
 
    double SAR=0; // Stop And Reverse
    double AF=getSettings().getDouble(INITIAL_AF, 0.02);
    double AFmax=getSettings().getDouble(MAX_AF, 0.2);
    double AFstep=getSettings().getDouble(STEP, 0.02);
    double EP=0; // Extreme Point 
    boolean isLong=false;
    double diff=0;
    double pSAR=0;
    Instrument instr=ctx.getInstrument();
    boolean latest=index == series.size() - 1;

    if (series.getDouble(index - 1, Values.PSAR) == null) 
      // first entry point, calculate the first SAR 
      isLong=series.getClose(index) moreThan series.getClose(index - 1);
      SAR=isLong ? series.getLow(index - 1) : series.getHigh(index - 1);
      if (isLong) EP=Util.max(series.getHigh(index), series.getHigh(index - 1));
      else EP=Util.min(series.getLow(index), series.getLow(index - 1));
      pSAR=SAR;
    else 
      pSAR=series.getDouble(index - 1, Values.PSAR);
      AF=series.getDouble(index - 1, Values.AF);
      EP=series.getDouble(index - 1, Values.EP);
      isLong=series.getBoolean(index - 1, Values.LONG);

      // Check for stop and reversals 
      // Have we penetrated the pSAR? 
      double low=series.getLow(index);
      double high=series.getHigh(index);
      double oldSAR=pSAR;

      if (isLong AND low lessOrEqual instr.round(pSAR)) 
        // Stop and Reverse 
        pSAR=EP;
        EP=series.getLow(index);
        isLong=false;
        AF=getSettings().getDouble(INITIAL_AF, 0.02);
        series.setBoolean(index, Signals.SAR_LONG, true);
        if (!latest) 
          ctx.signal(index, Signals.SAR_LONG, get("SIGNAL_SAR_LONG", instr.format(low), instr.format(oldSAR)), low);
        end
      end
      else if (!isLong AND high moreOrEqual instr.round(pSAR)) 
        pSAR=EP;
        EP=series.getHigh(index);
        isLong=true;
        AF=getSettings().getDouble(INITIAL_AF, 0.02);
        series.setBoolean(index, Signals.SAR_SHORT, true);
        if (!latest) 
          ctx.signal(index, Signals.SAR_SHORT, get("SIGNAL_SAR_SHORT", instr.format(high), instr.format(oldSAR)), high);
        end
      end

      // Compute the next SAR 
      if (isLong) 
        if (series.getHigh(index) moreThan EP) 
          if (AF lessThan AFmax) AF+=AFstep;
          EP=series.getHigh(index);
        end
        diff=Math.abs(EP - pSAR);
        SAR=pSAR + AF * (diff); 
        if (SAR moreThan series.getLow(index) OR SAR moreThan series.getLow(index - 1)) 
          SAR=Math.min(series.getLow(index), series.getLow(index - 1));
        end
      end
      else 
        if (series.getLow(index) lessThan EP) 
          if (AF lessThan AFmax) AF+=AFstep;
          EP=series.getLow(index);
        end
        diff=Math.abs(EP - pSAR);
        SAR=pSAR - AF * (diff); 
        if (SAR lessThan series.getHigh(index) OR SAR lessThan series.getHigh(index - 1)) 
          SAR=Math.max(series.getHigh(index), series.getHigh(index - 1));
        end
      end
    end

    series.setDouble(index, Values.PSAR, SAR);
    series.setDouble(index, Values.AF, AF);
    series.setDouble(index, Values.EP, EP);
    series.setBoolean(index, Values.LONG, isLong);
    series.setComplete(index);
  endMethod