Parabolic SAR 2

Posted on 13, February 2014

in Category SID


Description for ParabolicSAR SID 280

The Parabolic SAR indicator developed by Welles Wilder. Also known as the Parabolic Stop and Reverse indicator.

Parabolic SAR/sid The logic for this technical analysis indicator:

#The Parabolic SAR indicator developed by Welles Wilder.

my $IAF = 0.02; #Acceleration factor.
my $MaxAF = 0.2; #Max acceleration.

my $psar = NewColumn("PSAR"); #Initialize.
my $long = 1; #Assume long for initial conditions.
my $af = $IAF; #Init acelleration factor.
my $ep = Ref(Low,0); #Init extreme point.
my $hp = Ref(High,0);
my $lp = Ref(Low,0);

for my $i (2..NumberOfBars-1){
	if($long){
		SetValue($psar,$i,Value($psar,$i-1)+$af*($hp-Value($psar,$i-1)));
	}
	else
	{
		SetValue($psar,$i,Value($psar,$i-1)+$af*($lp-Value($psar,$i-1)));
	}

	my $reverse =  0; #Check for reversal.

	if($long)
	{
		if(Value(Low,$i) < Value($psar,$i))
		{
			$long = 0;
			$reverse = 1; #Reverse position to short.
			SetValue($psar,$i,$hp); #SAR is high point in previous trade.
			$lp = Value(Low,$i);
			$af = $IAF;
		}
	}
	else
	{
		if(Value(High,$i) > Value($psar,$i))
		{
			$long = 1;
			$reverse = 1; #Reverse position to long.
			SetValue($psar,$i,$lp);
			$hp = Value(High,$i);
			$af = $IAF;
		}
	}

	if($reverse == 0)
	{
		if($long)
		{
			if(Value(High,$i) > $hp) 
			{
				$hp = Value(High,$i); 
				$af = $af + $IAF; 
				$af = $MaxAF if $af > $MaxAF; 
			}
             
			if(Value(Low,$i-1)<Value($psar,$i)){
				SetValue($psar,$i,Value(Low,$i-1)); 
			}
			
			if(Value(Low,$i-2)<Value($psar,$i)){
				SetValue($psar,$i,Value(Low,$i-2));
			}
		}
       else
		{
			if (Value(Low,$i) < $lp)  
			{ 
				$lp = Value(Low,$i);
				$af = $af + $IAF; 
				$af = $MaxAF if $af > $MaxAF; 
			}	
			
			if(Value(High,$i-1)>Value($psar,$i)){
				SetValue($psar,$i,Value(High,$i-1));
			}
			
			if(Value(High,$i-2)>Value($psar,$i)){
				SetValue($psar,$i,Value(High,$i-2));
			}
		}
	}
}

return $psar;

2 Comments

  1. samtrinston samtrinston says:

    Please could you change this code so that it takes parameters and then post it as a SID so that we can download the indicator.
    Also, could you build a chart object with parameters around this indicator so that I can plot it.

  2. blinker says:

    samtrinston,

    To add parameters to the indicator, simply comment out the original declarations for $IAF and $MaxAF and in their place add:

    my ($IAF,$MaxAF)=@_;

    You will also need to set the Parameters editbox at the top to:
    0.02,0.2
    and the Prototype editbox to:
    2

    For the chart, copy and paste this code in a new Chart
    object:

    AF = ChartInputSlider('Max Acceleration', 0.2,0.1, 1);
    my $psar = ParabolicSAR (0.02, 0.2);
    PlotPrice('Candle');
    PlotColumn($psar, Red);

    The only problem with this code is that it seems the ChartInputSlider will only increment in whole numbers – namely 1. Parabolic SAR uses values less than 1, so the sliders do not want to work.

    I hope this helps!
    blinker

Leave a Message