Location: PHPKode > projects > Finasystem > fs_ta/sar.php
<?php
/**
* Finasystem version 0.4 Lin Hai - Technical Analysis Library in PHP
 *Release: 14/10/2006
 *Copyright (C) 2006 Rudy Zuck
 *mailto:rudy[-at-]zuck.fr
 *web:http://sourceforge.net/projects/finasystem
 *
 *This program is free software; you can redistribute it and/or
 *modify it under the terms of the GNU General Public License
 *as published by the Free Software Foundation; either version 2
 *of the License, or any later version.
 *
 *This program is distributed in the hope that it will be useful,
 *but WITHOUT ANY WARRANTY; without even the implied warranty of
 *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *GNU General Public License for more details.
 *
 *You should have received a copy of the GNU General Public License
 *along with this program; if not, write to the Free Software
 *Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA  
 *
 * Parabolic (sar)
 * 
 *<p>The Parabolic SAR calculates a trailing stop. Simply exit when the price crosses the SAR. The SAR assumes that you are always  
 *in the market, and calculates the Stop And Reverse point when you would close a long position and open a short position or vice 
 *versa.</p>
 *
 *<p>The Parabolic SAR was developed by J. Welles Wilder and is described in his 1978 book, New Concepts In Technical Trading 
 *Systems.</p> 
 *
 *
 * @author Rudy Zuck <rudy[-at-]zuck.fr>
 * @copyright Copyright, 2006, Rudy Zuck
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 * @version 0.4
 * @package fs_ta
 */

class sar{
	
	/**
	 * @var double[]
	 */
	private $sar;
	
	/**
     * @param double[] $high
	 * @param double[] $low
	 * @param double $acceleration
	 * @param double $step
	 * @param double $max_acceleration
	 * @return double[]
     */
	function get($high,$low,$acceleration,$step,$max_acceleration){
		
		//Initialization :
		if($high[1]>$high[0]){
			$trend=1;
			$this->sar[0] = $low[0];
			$xp = $high[0];
			$a = $acceleration;
		}
		else{
			$trend=0;
			$this->sar[0] = $high[0];
			$xp = $low[0];
			$a = $acceleration;
		}
		
		$size_data=count($high);
		$i=1;
		while(true){
			if($i>=$size_data){break;}
			if($trend==1){
				while(true){
					if($i>=$size_data){break;}
					
					if($high[$i]>$xp){$xp=$high[$i];}
					
					if($high[$i]<=$this->sar[$i-1]){
						$trend=0;
						$this->sar[$i] = $xp;
						$a = $acceleration;
						$i++;
						break;
					}
					$this->sar[$i] = $this->sar[$i-1] + ( $a * ( $xp - $this->sar[$i-1] ) );
					if ($a<=$max_acceleration){$a += $step;} 
					$i++;
				}
			}
			
			if($trend==0){
				while(true){
				if($i>=$size_data){break;}
				
					if($low[$i]<$xp){$xp=$low[$i];}
					
					if($low[$i]>$this->sar[$i-1]){
						$trend=1;
						$this->sar[$i] = $xp;
						$a = $acceleration;
						$i++;
						break;
					}
					
					$this->sar[$i] = $this->sar[$i-1] + ( $a * ( $xp - $this->sar[$i-1] ) );
					if ($a<=$max_acceleration){$a += $step;} 				
					$i++;
				}
			}
		}
		return $this->sar;	
	}
}
?>
Return current item: Finasystem