Packagexp.utils
Classpublic class TechnicalAnalysis

This class contains functions for the Technical Analysis of market quotes. Primarily to support charting



Public Methods
 MethodDefined by
  
avg(source:Array, period:int):Number
[static] Find the moving average of the given quotes.
TechnicalAnalysis
  
bestFit(source:Array, period:int):Number
[static] Calculate the line of best fit of the data given by source.
TechnicalAnalysis
  
bestFitFunction(source:Array, start:int, period:int):Array
[static] Return the equation of the line of best fit of the data given by source.
TechnicalAnalysis
  
bollingerLower(source:Array, period:int):Number
[static] Calculate the lower band of the bollinger graph.
TechnicalAnalysis
  
bollingerUpper(source:Array, period:int):Number
[static] Calculate the upper band of the bollinger graph.
TechnicalAnalysis
  
corr(x:Array, y:Array, period:int):Number
[static] Calculate the Pearson product-moment correlation between the two variables.
TechnicalAnalysis
  
ema(source:Array, period:int, smoothingConstant:Number):Number
[static] Calculate the Exponential Moving Average (EMA) value.
TechnicalAnalysis
  
macd(sourceSlow:Array, sourceFast:Array):Number
[static] Calculate the Moving Average Convergence Divergence (MACD) value.
TechnicalAnalysis
  
momentum(source:Array, period:int):Number
[static] Calculate the Momentum value.
TechnicalAnalysis
  
obv(sourceOpen:Array, sourceClose:Array, sourceVolume:Array, range:int, initialValue:int):int
[static] Calculate the On Balance Volume (OBV) value.
TechnicalAnalysis
  
roundDouble(d:Number, places:int):Number
[static]
TechnicalAnalysis
  
rsi(source:Array, period:int):Number
[static] Calculate the Relative Strength Indicator (RSI) value.
TechnicalAnalysis
  
sd(source:Array, period:int):Number
[static] Find the standard deviation of the given values.
TechnicalAnalysis
Public Constants
 ConstantDefined by
  DEFAULT_RSI_PERIOD : int = 45
[static] This is the default period for the RSI.
TechnicalAnalysis
Method detail
avg()method
public static function avg(source:Array, period:int):Number

Find the moving average of the given quotes. This function will calculate the average of the first period days. If a quote is missing on any of the days, then that day will be skipped and the function will find the average of the shorter period.

Parameters
source:Array — source of quotes to average
 
period:int — the number of days to average

Returns
Number — the average
bestFit()method 
public static function bestFit(source:Array, period:int):Number

Calculate the line of best fit of the data given by source. using the formula: slope = period Sum(xy) - Sum(x)Sum(y) / period Sum(x^2) - (Sum(x))^2 intercept = ( Sum(y) - slope Sum(x) ) / period

Parameters
source:Array — the source of quotes
 
period:int — the number of days to analyse

Returns
Number — the value of the trend at the end of period
bestFitFunction()method 
public static function bestFitFunction(source:Array, start:int, period:int):Array

Return the equation of the line of best fit of the data given by source. Uses the same formula as bestFit, but returns slope and intercept so that it can be used on charts.

Parameters
source:Array — the source of quotes
 
start:int — the number of days to analyse
 
period:int

Returns
Array — the value of the trend at the end of period
bollingerLower()method 
public static function bollingerLower(source:Array, period:int):Number

Calculate the lower band of the bollinger graph. The lower band can be calculated by: BollingerLower = Average - 2 SD Where SD is the standard deviation.

Parameters
source:Array — the source of quotes
 
period:int — the number of days to analyse

Returns
Number — the lower bollinger band
bollingerUpper()method 
public static function bollingerUpper(source:Array, period:int):Number

Calculate the upper band of the bollinger graph. The upper band can be calculated by: BollingerUpper = Average + 2 SD Where SD is the standard deviation.

Parameters
source:Array — the source of quotes
 
period:int — the number of days to analyse

Returns
Number — the upper bollinger band
corr()method 
public static function corr(x:Array, y:Array, period:int):Number

Calculate the Pearson product-moment correlation between the two variables. This will return a correlation co-efficient which is in the range of -1 (negative correlation) through to (no correlation) through to 1 (perfect correlation. The correlation co-efficient is calculated as follows:

     r = sum(Zx Zy)
         ------------
             N - 1
          Where Zx = X - E(X)
                --------
                  Sx
     
Where E(X) is the mean of X and Sx is the standard deviation of X. Simillarly for Zy.

Parameters
x:Array — values to test against
 
y:Array — values to detect correlation against x
 
period:int — number of days to analyse

Returns
Number — the correlation co-efficient
ema()method 
public static function ema(source:Array, period:int, smoothingConstant:Number):Number

Calculate the Exponential Moving Average (EMA) value. The Exponential Moving Average is a weighted moving average where the most recent values are weighted higher than the previous values. The formula for the EMA is as follows: EMA(current) = EMA(previous) + k (day close - EMA(previous)) Where EMA(current) is the current EMA value you are calculating, EMA(previous) is the previous value and k is a smoothing constant.

Parameters
source:Array — the source of quotes to average
 
period:int — the number of days to analyse
 
smoothingConstant:Number — a smoothing constant

Returns
Number — the exponential moving average
macd()method 
public static function macd(sourceSlow:Array, sourceFast:Array):Number

Calculate the Moving Average Convergence Divergence (MACD) value. The Moving Average Convergence Divergence is the remainder of the 26 days EMA and the 12 days EMA. The smoothing constant for the EMA functions is set to 0.1. The formula for the MACD is as follows: MACD = EMA(26) - EMA(12) Where EMA(26) is the 26 days EMA and EMA(12) is the 12 days EMA.

Parameters
sourceSlow:Array — the source of quotes used by EMA to average (slow average)
 
sourceFast:Array — the source of quotes used by EMA to average (fast average)

Returns
Number — the moving average convergence divergence
momentum()method 
public static function momentum(source:Array, period:int):Number

Calculate the Momentum value. The Moving Average Convergence Divergence is the remainder of the today value and the period delayed value. The formula for the Momentum is as follows: Momentum = Quote(Today) - Quote(Today+1-period) Where Quote is got from the input parameter: source. Today is assumed to be the first item in source.

Parameters
source:Array — the source of quotes
 
period:int

Returns
Number — the momentum
obv()method 
public static function obv(sourceOpen:Array, sourceClose:Array, sourceVolume:Array, range:int, initialValue:int):int

Calculate the On Balance Volume (OBV) value. The On Balance Volume is counted adding or subtracting the day volume from range until today, starting from an initial value. The formula for the OBV is as follows: if close(current)>open(current): OBV(current) = OBV(previous) + Volume(current) if close(current)Parameters

sourceOpen:Array — the source of open quotes
 
sourceClose:Array — the source of close quotes
 
sourceVolume:Array — the source of volumes
 
range:int — the range which we calculate over
 
initialValue:int — the starting value of OBV

Returns
int — the on balance volume value
roundDouble()method 
public static function roundDouble(d:Number, places:int):NumberParameters
d:Number
 
places:int

Returns
Number
rsi()method 
public static function rsi(source:Array, period:int):Number

Calculate the Relative Strength Indicator (RSI) value. The formula for the RSI is as follows:

                        100
     RSI = 100 - ------
                 1 + RS
                average of x days' up closes
     RS = ------------------------------
          average of x days' down closes
          
To calculate an X day RSI you need X + 1 quote values. So make the period argument one more day that the period of the RSI.

Parameters
source:Array — source of quotes to average
 
period:int — one plus the period of the RSI

Returns
Number — RSI
sd()method 
public static function sd(source:Array, period:int):Number

Find the standard deviation of the given values. This algorthim will calculate the standard deviation of the first period days. If a quote is missing on any of the days, then that day will be skipped and the function will find the average of the shorter period.

Parameters
source:Array — the source quotes
 
period:int — the number of days to average

Returns
Number — the standard deviation
Constant detail
DEFAULT_RSI_PERIODconstant
public static const DEFAULT_RSI_PERIOD:int = 45

This is the default period for the RSI.