Location: PHPKode > projects > Finasystem > fs_ta/bbands.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  
 */

/**
 * Bollinger Bands (bbands)
 *
 *<p>Bollinger Bands are a kind of trading envelope. They are lines plotted at 
 *an interval around a moving average. Bollinger Bands consist of a moving average 
 *and two standard deviations charted as one line above and one line below the moving average. 
 *The line above is two standard deviations added to the moving average. The line below is 
 *two standard deviations subtracted from the moving average. Traders generally use them to 
 *determine overbought and oversold zones, to confirm divergences between prices and indicators, 
 *and to project price targets. The wider the bands are, the greater the volatility is. 
 *The narrower the bands are, the lesser the volatility is. The moving average is calculated 
 *on the close.</p>
 *
 *
 *<p>Formula:</p>
 *
 * - Initialization : Computing the STANDARD DEVIATION (SD)
 * - Initialization : Computing a Simple Moving Average (SMA)
 * - Upper Band = SMA + 2*(SD)
 * - Lower Band = SMA - 2*(SD)
 *
 *<p>Additionnal Indicators:</p>
 *
 * - % Upper Band = (Input - Upper Band)/Upper Band
 * - % Lower Band = (Input - Lower Band)/Lower Band
 *
 * @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 bbands{

	/**
	 * @var double[]
	 */
	private $bbands_up;
	/**
	 * @var double[]
	 */	
	private $bbands_down;
	/**
	 * @var double[]
	 */	
	private $pr_bbands_up;
	/**
	 * @var double[]
	 */	
	private $pr_bbands_down;
	
	/**
    * @param double[] $data 
    * @param int $range
	* @return void
    */
	function set($data,$range){
		
		//Initialization : Computing a Simple Moving Average (SMA)
		$my_sma = new sma();
		$sma = $my_sma->get($data,$range);

		//Initialization : Computing the STANDARD DEVIATION (SD)
		$my_stddev = new stddev();
		$stddev = $my_stddev->get($data,$range);
		
		$size_data = count($data);
		for($i=$range-1; $i<$size_data; $i++){
			$this->bbands_up[$i] = $sma[$i] + 2*$stddev[$i];
			$this->bbands_down[$i] = $sma[$i] - 2*$stddev[$i];
			$this->pr_bbands_up[$i] = ($data[$i] - $this->bbands_up[$i]) / $this->bbands_up[$i];
			$this->pr_bbands_down[$i] = ($data[$i] - $this->bbands_down[$i]) / $this->bbands_down[$i];
		}
	}
	
	/** 
    * @return double[]
    */
	
	function get_bbands_up(){
		return $this->bbands_up;
	}
	
	/** 
    * @return double[]
    */
	
	function get_bbands_down(){
		return $this->bbands_down;
	}
	
	/** 
    * @return double[]
    */
	
	function get_pr_bbands_up(){
		return $this->pr_bbands_up;
	}
	
	/** 
    * @return double[]
    */
	
	function get_pr_bbands_down(){
		return $this->pr_bbands_up;
	}
}
?>
Return current item: Finasystem