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

/**
 * Random Walk Index (rwi)
 *
 *<p>The Random Walk Index (rwi) is used to determine if an issue is trending or in 
 *a random trading range by comparing it to a straight line. The more random the 
 *price movement, the more the rwi fluctuates.</p>

 *<p>The short-term (2 to 7 periods) rwi is an overbought/oversold indicator, while the 
 *long-term (8 to 64 periods) rwi is a trend indicator. An issue is trending higher if 
 *the rwi of the highs is greater than 1, while a downtrend is indicated if the rwi of 
 *the lows is greater than 1. A buy signal is generated when the long-term rwi of the 
 *highs is greater than 1 and the short-term rwi of the lows rises above 1. A sell 
 *signal is generated when the long-term rwi of the lows is greater than 1 and the 
 *short-term rwi of the highs rises above 1.</p>
 *
 *<p>The Random Walk Index was developed by Michael Poulos and is described in his article 
 *in the February, 1991 issue of Technical Analysis of Stocks & Commodities magazine.</p>
 *
 *
 *<p>Formula:</p>
 *
 * - rwiminus = (high(n) - Current Low) / (ATR(n) * SQRT(n))
 * - rwihigh = (Current high - low(n)) / (ATR(n) * SQRT(n))
 * - rwiADX is the equivelent formula of the ADX of Wilder except that DI+ and DI- are remplaced by rwi- and rwi+
 *
 * @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 rwi{
	
	/**
	 * @var double[]
	 */
	private $rwi_up;
	/**
	 * @var double[]
	 */
	private $rwi_down;
	/**
	 * @var double[]
	 */
	private $rwi_adx;
	/**
	 * @var double[]
	 */
	private $rwi_dx;

	/**
     * @param double[] $high
	 * @param double[] $low
	 * @param double[] $close
	 * @param int $range
	 * @return void
     */
	function set($high,$low,$close,$range){
		
		//Computation of the Average True Range (ATR)
		$my_dmi = &new dmi;
		$my_dmi->set($high,$low,$close,$range);
		$my_atr = $my_dmi->get_atr();
		
		//Computation of the rwi_low and rwi_high
		$size_data = count($close);
		
		for($i=2*$range-2;$i<$size_data;$i++){
		
		$max_rwi_down=0;
		$max_rwi_up=0;
		$temp_rwi_down=0;
		$temp_rwi_up=0;
		
			for($j=0;$j<$range;$j++){
				$temp_rwi_up=($high[$i] - $low[$i-($range-1)+$j]) / ($my_atr[$i-($range-1)+$j] * sqrt($j+1));
				$temp_rwi_down=($high[$i-($range-1)+$j] - $low[$i]) / ($my_atr[$i-($range-1)+$j] * sqrt($j+1)); 
				if($temp_rwi_up > $max_rwi_up){
					$max_rwi_up = $temp_rwi_up; 
				}
				
				if($temp_rwi_down > $max_rwi_down){
					$max_rwi_down = $temp_rwi_down;
				}			
			}
			
			$this->rwi_down[$i] = $max_rwi_down;
			$this->rwi_up[$i] = $max_rwi_up; 
			
		}
		
		//Calculus of the rwi DX (Normalisation, the same than the calculus of the DX Wilder
		for($i=2*$range-2;$i<$size_data;$i++){
			$this->rwi_dx[$i] = floor(((abs($this->rwi_up[$i] - $this->rwi_down[$i])) / ($this->rwi_up[$i] + $this->rwi_down[$i]))*100);
		}
		//Calculus of the rwi ADX (Normalisation, the same than the calculus of the DX Wilder
		
		//initialization of the serie
		$adx_temp = 0;
		for ($i=2*$range-2; $i<3*$range-3; $i++){
			$adx_temp += $this->rwi_dx[$i]; 
		}
		$this->rwi_adx[3*$range-3] = $adx_temp/$range;

		for ($i=3*$range-2; $i<$size_data; $i++){
			$this->rwi_adx[$i] = (($range-1) * $this->rwi_adx[$i-1] + $this->rwi_dx[$i]) / $range; 
		}
		
	
	}
	
	/**
	 * @return double[]
         */
	function get_rwi_down(){
			return $this->rwi_down;
	}
	/**
	 * @return double[]
         */
	function get_rwi_up(){
			return $this->rwi_up;
	}
	
	/**
	 * @return double[]
         */
	function get_rwi_adx(){
			return $this->rwi_adx;
	}
	
	/**
	 * @return double[]
         */
	function get_rwi_dx(){
			return $this->rwi_dx;
	}
}
?>
Return current item: Finasystem