Venice 0.751beta

## nz.org.venice.quote Class QuoteFunctions

```java.lang.Object nz.org.venice.quote.QuoteFunctions
```

public class QuoteFunctions
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
`QuoteFunctionSource`

 Field Summary `static int` `DEFAULT_RSI_PERIOD`           This is the default/recommended period for the RSI.

 Method Summary `static double` ```avg(double[] values, int start, int end)``` `static double` ```avg(QuoteFunctionSource source, int period)```           Find the average of the given quotes. `static double` ```bestFit(QuoteFunctionSource source, int period)```           Calculate the line of best fit of the data given by source. `static double` ```bestFit2(QuoteFunctionSource source, int period)``` `static double[]` ```bestFitFunction(QuoteFunctionSource source, int start, int period)```           Return the equation of the line of best fit of the data given by source. `static double` ```bollingerLower(QuoteFunctionSource source, int period)```           Calculate the lower band of the bollinger graph. `static double` ```bollingerUpper(QuoteFunctionSource source, int period)```           Calculate the upper band of the bollinger graph. `static double` ```corr(QuoteFunctionSource x, QuoteFunctionSource y, int period)```           Calculate the Pearson product-moment correlation between the two variables. `static double` ```ema(QuoteFunctionSource source, int period, double smoothingConstant)```           Calculate the Exponential Moving Average (EMA) value. `static double` ```macd(QuoteFunctionSource sourceSlow, QuoteFunctionSource sourceFast)```           Calculate the Moving Average Convergence Divergence (MACD) value. `static double` ```momentum(QuoteFunctionSource source, int period)```           Calculate the Momentum value. `static int` ```obv(QuoteFunctionSource sourceOpen, QuoteFunctionSource sourceClose, QuoteFunctionSource sourceVolume, int range, int initialValue)```           Calculate the On Balance Volume (OBV) value. `static double` ```roundDouble(double d, int places)``` `static double` ```rsi(QuoteFunctionSource source, int period)```           Calculate the Relative Strength Indicator (RSI) value. `static double` ```sd(double[] values, int start, int end)``` `static double` ```sd(QuoteFunctionSource source, int period)```           Find the standard deviation of the given values.

 Methods inherited from class java.lang.Object `clone, equals, finalize, 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.

Constant Field Values
 Method Detail

### sd

```public static double sd(QuoteFunctionSource source,
int period)
throws EvaluationException```
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
Throws:
`EvaluationException` - if `QuoteBundleFunctionSource` is not allowed access to a quote. See `GPQuoteBundle`.
`StandardDeviationGraph`

### sd

```public static double sd(double[] values,
int start,
int end)```

### avg

```public static double avg(QuoteFunctionSource source,
int period)
throws EvaluationException```
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
Throws:
`EvaluationException` - if `QuoteBundleFunctionSource` is not allowed access to a quote. See `GPQuoteBundle`.
`MovingAverageGraph`, `AvgExpression`

### avg

```public static double avg(double[] values,
int start,
int end)```

### corr

```public static double corr(QuoteFunctionSource x,
QuoteFunctionSource y,
int period)
throws EvaluationException```
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
Throws:
`EvaluationException` - if `QuoteBundleFunctionSource` is not allowed access to a quote. See `GPQuoteBundle`.
`CorrExpression`

### rsi

```public static double rsi(QuoteFunctionSource source,
int period)
throws EvaluationException```
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.

Parameters:
`source` - source of quotes to average
`period` - one plus the period of the RSI
Returns:
RSI
Throws:
`EvaluationException` - if `QuoteBundleFunctionSource` is not allowed access to a quote. See `GPQuoteBundle`.
`RSIGraph`, `RSIExpression`

### ema

```public static double ema(QuoteFunctionSource source,
int period,
double smoothingConstant)
throws EvaluationException```
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
Throws:
`EvaluationException` - if `QuoteBundleFunctionSource` is not allowed access to a quote. See `GPQuoteBundle`.
`ExpMovingAverageGraph`

### macd

```public static double macd(QuoteFunctionSource sourceSlow,
QuoteFunctionSource sourceFast)
throws EvaluationException```
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
Throws:
`EvaluationException` - if `QuoteBundleFunctionSource` is not allowed access to a quote. See `GPQuoteBundle`.
`MACDGraph`

### momentum

```public static double momentum(QuoteFunctionSource source,
int period)
throws EvaluationException```
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
Throws:
`EvaluationException` - if `QuoteBundleFunctionSource` is not allowed access to a quote. See `GPQuoteBundle`.
`MomentumGraph`

### obv

```public static int obv(QuoteFunctionSource sourceOpen,
QuoteFunctionSource sourceClose,
QuoteFunctionSource sourceVolume,
int range,
int initialValue)
throws EvaluationException```
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
Throws:
`EvaluationException` - if `QuoteBundleFunctionSource` is not allowed access to a quote. See `GPQuoteBundle`.
`OBVGraph`

### bollingerUpper

```public static double bollingerUpper(QuoteFunctionSource source,
int period)
throws EvaluationException```
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
Throws:
`EvaluationException` - if `QuoteBundleFunctionSource` is not allowed access to a quote. See `GPQuoteBundle`.
`BollingerBandsGraph`

### bollingerLower

```public static double bollingerLower(QuoteFunctionSource source,
int period)
throws EvaluationException```
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
Throws:
`EvaluationException` - if `QuoteBundleFunctionSource` is not allowed access to a quote. See `GPQuoteBundle`.
`BollingerBandsGraph`

### roundDouble

```public static final double roundDouble(double d,
int places)```

### bestFit

```public static double bestFit(QuoteFunctionSource source,
int period)
throws EvaluationException```
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 (Least squares regression)

Parameters:
`source` - the source of quotes
`period` - the number of days to analyse
Returns:
the value of the trend at the end of period
Throws:
`EvaluationException`

### bestFit2

```public static double bestFit2(QuoteFunctionSource source,
int period)
throws EvaluationException```
Throws:
`EvaluationException`

### bestFitFunction

```public static double[] bestFitFunction(QuoteFunctionSource source,
int start,
int period)
throws EvaluationException```
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
Throws:
`EvaluationException`

Venice 0.751beta