finance
Class Stats

java.lang.Object
  extended by finance.Stats

public class Stats
extends java.lang.Object

This class contains functions that manipulate stock quotes. By placing them together in a single class, they can be used by both the Gondola language and charting functions.

Author:
Andrew Leppard

Field Summary
static int DEFAULT_RSI_PERIOD
          This is the default/recommended period for the RSI.
 
Method Summary
static double avg(double[] source, int period)
          Find the average of the given quotes.
static double avg(double[] source, int period, int startPoint)
           
static double bestFit(double[] source, int period)
          Calculate the line of best fit of the data given by source.
static double[] bestFitFunction(double[] source, int period)
          Return the equation of the line of best fit of the data given by source.
static double bollingerLower(double[] source, int period)
          Calculate the lower band of the bollinger graph.
static double bollingerUpper(double[] source, int period)
          Calculate the upper band of the bollinger graph.
static double corr(double[] x, double[] y, int period)
          Calculate the Pearson product-moment correlation between the two variables.
static double corr(double[] x, double[] y, int period, int startpoint)
          Calculate the Pearson product-moment correlation between the two variables.
static double ema(double[] source, int period, double smoothingConstant)
          Calculate the Exponential Moving Average (EMA) value.
static double ema(double[] source, int period, int startPoint, double smoothingConstant)
           
static double[] getAvg(double[] source, int period)
          Find the average of the given quotes.
static double[] getEma(double[] source, int period, double smoothingConstant)
          Calculate the Exponential Moving Average (EMA) value.
static double macd(double[] sourceSlow, double[] sourceFast)
          Calculate the Moving Average Convergence Divergence (MACD) value.
static void main(java.lang.String[] args)
           
static double momentum(double[] source, int period)
          Calculate the Momentum value.
static int obv(double[] sourceOpen, double[] sourceClose, double[] sourceVolume, int range, int initialValue)
          Calculate the On Balance Volume (OBV) value.
static double roundDouble(double d, int places)
           
static double rsi(double[] source, int period)
          Calculate the Relative Strength Indicator (RSI) value.
static double sd(double[] source, int period)
          Find the standard deviation of the given values.
static double sd(double[] source, int period, int startpoint)
          Find the standard deviation of the given values.
static double sma(double[] in, int startPoint, int period)
           
static void testSma()
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_RSI_PERIOD

public static final int DEFAULT_RSI_PERIOD
This is the default/recommended period for the RSI.

See Also:
Constant Field Values
Method Detail

sd

public static double sd(double[] source,
                        int period)
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 - the source quottes
period - the number of days to average
Returns:
the standard deviation

sd

public static double sd(double[] source,
                        int period,
                        int startpoint)
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 - the source quottes
period - the number of days to average
Returns:
the standard deviation

getAvg

public static double[] getAvg(double[] source,
                              int period)
Find the 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 - source of quotes to average
period - the number of days to average
Returns:
the average

avg

public static double avg(double[] source,
                         int period,
                         int startPoint)

corr

public static double corr(double[] x,
                          double[] y,
                          int period,
                          int startpoint)
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 - values to test against
y - values to detect correlation against x
period - number of days to analyse
Returns:
the correlation co-efficient

getEma

public static double[] getEma(double[] source,
                              int period,
                              double smoothingConstant)
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 - the source of quotes to average
period - the number of days to analyse
smoothingConstant -
Returns:
the exponential moving average

ema

public static double ema(double[] source,
                         int period,
                         int startPoint,
                         double smoothingConstant)

avg

public static double avg(double[] source,
                         int period)
Find the average of the given quotes. This function will calculate the average of the first period days.

Parameters:
source - source of quotes to average
period - the number of days to average
Returns:
the average

corr

public static double corr(double[] x,
                          double[] y,
                          int period)
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 - values to test against
y - values to detect correlation against x
period - number of days to analyse
Returns:
the correlation co-efficient

rsi

public static double rsi(double[] source,
                         int period)
Calculate the Relative Strength Indicator (RSI) value. Technical Analysis by Martin J. Pring describes the RSI as:

"It is a momentum indicator, or oscillator, that measures the relative internal strength of a security against itself....".

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.

Returns:
RSI

ema

public static double ema(double[] source,
                         int period,
                         double smoothingConstant)
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 - the source of quotes to average
period - the number of days to analyse
smoothingConstant - a smoothing constant
Returns:
the exponential moving average

macd

public static double macd(double[] sourceSlow,
                          double[] sourceFast)
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 - the source of quotes used by EMA to average (slow average)
sourceFast - the source of quotes used by EMA to average (fast average)
Returns:
the moving average convergence divergence

momentum

public static double momentum(double[] source,
                              int period)
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.

Parameters:
source - the source of quotes
Returns:
the momentum

obv

public static int obv(double[] sourceOpen,
                      double[] sourceClose,
                      double[] sourceVolume,
                      int range,
                      int initialValue)
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 - the source of open quotes
sourceClose - the source of close quotes
sourceVolume - the source of volumes
range - the range which we calculate over
initialValue - the starting value of OBV
Returns:
the on balance volume value

bollingerUpper

public static double bollingerUpper(double[] source,
                                    int period)
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 - the source of quotes
period - the number of days to analyse
Returns:
the upper bollinger band

bollingerLower

public static double bollingerLower(double[] source,
                                    int period)
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 - the source of quotes
period - the number of days to analyse
Returns:
the lower bollinger band

roundDouble

public static double roundDouble(double d,
                                 int places)

bestFit

public static double bestFit(double[] source,
                             int period)
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 - the source of quotes
period - the number of days to analyse
Returns:
the value of the trend at the end of period

bestFitFunction

public static double[] bestFitFunction(double[] source,
                                       int period)
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 - the source of quotes
period - the number of days to analyse
Returns:
the value of the trend at the end of period

main

public static void main(java.lang.String[] args)

testSma

public static void testSma()

sma

public static double sma(double[] in,
                         int startPoint,
                         int period)