WebCab Technical Analysis
(J2SE Edition)

webcab.lib.finance.trading.indicators
Class MovingAverage

java.lang.Object
  |
  +--webcab.lib.finance.trading.indicators.MovingAverage
All Implemented Interfaces:
Serializable

public class MovingAverage
extends Object
implements Serializable

Moving Averages in there various forms are used to smooth data so that the underlying trend is more discernible. Since a moving average's aim is to recognize a trending market from historical prices the sensitivity of the measures will depend on the number of a historical values used. For relatively few values used the moving average may itself oscillate rapidly and give many force signals to the start of trending markets. If many values are used fewer force signals will be generated but a trend may in well into its cycle before it is detected and conversely when the trend finishes or changes direction the indicator will take correspondingly longer the reflect this.

With the construction of the moving averages themselves the main significant difference between them is the weight assigned to each price point. Simple moving averages apply equal weight to all historical prices. Exponential and weighted averages apply more weight to recent prices. Triangular averages apply more weight to prices in the middle of the time period and variable moving averages change the weighting based on the volatility of prices.

Interpretation
When the moving average us higher than the asset price then the asset is in a bearish trend (i.e. trending down) and when the asset is above the moving average then the asset is in a bullish trend (i.e. trending up-wards).

Moving Averages are also used in pairs and even triples. When using a pair of moving averages of the same type where one moving average uses fewer days and hence is more sensitive, the underlying asset is said to be in bullish mode if the moving average using the fewer number of days crosses above the less sensitive moving average which is using more days data. Conversely, if the less sensitive moving average cross above the more sensitive moving average then asset is said to be in a bearish mode.

All moving averages are lagging indicators and hence will miss the first part of a trend and over-run the same trend. There is also a play-off is the number of historical days use which determines the sensitivity of the indicator.

See Also:
Serialized Form

Constructor Summary
MovingAverage()
          Creates a new instance.
 
Method Summary
 double exponentiallyWeightedMovingAverage(double[] timeSeries, double smoothingFactor)
          Evaluates the (x-day) Exponentially Weighted Moving Average (EWMA) of a time series from the 0-th period until the (x-1)-th period.
 double geometricMovingAverage(double[] historicalValue)
          Calculates the x-day Geometric Moving Average (GMA) which is the geometric average of the values given over the past x days.
 double kairi(double movingAverage, double price)
          Calculates the Kairi Indicator measures as a percentage of the price the divergence between the a moving average (generally the simple moving average) of the price and the price itself.
 double linearlyWeightedMovingAverage(double[] priceSeries)
          Returns the value of the Linearly Weighted Moving Average (LWMA) of a (finite) price series.
 double medianMovingAverage(double[] historicalHigh, double[] historicalLow)
          Returns the x-day Median Moving Average of the market price of a traded asset over the past x-days.
 int simpleCrossingSignal(double lastShortMA, double previousShortMA, double lastLongMA, double previousLongMA)
          Generates of trading signal in accordance with the Simple crossing two moving average trading system.
 double simpleMovingAverage(double[] historicalPrice)
          Calculates the x-day arithmetic moving average of the market price of a traded asset over the past x-days.
 double weightedxDayMovingAverage(double[] historicalPrices, double[] weights)
          Here we evaluate the Weighted Moving Average (WMA) which allows you to assign more significance to resent price dynamics.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MovingAverage

public MovingAverage()
Creates a new instance.

Method Detail

simpleMovingAverage

public double simpleMovingAverage(double[] historicalPrice)
Calculates the x-day arithmetic moving average of the market price of a traded asset over the past x-days. Care should be taken when evaluating the moving average that the price on each of these days is sampled in a consistent manor. For example, the moving average could take the average closing prices on the last x-days in order to evaluate the average.

Parameters:
historicalPrice - an array of length x, where the first element historicalPrice[0], corresponds to the market price on the first of the x-day period. The term historicalPrice[1], corresponds to the market price of the second of the x-day period, and so on...
Throws:
IllegalArgumentException - thrown if the historicalPrices array is empty.

medianMovingAverage

public double medianMovingAverage(double[] historicalHigh,
                                  double[] historicalLow)
Returns the x-day Median Moving Average of the market price of a traded asset over the past x-days.

Parameters:
historicalHigh - an array where the first element historicalHigh[0], corresponds to the highest market price during the last trading period, the historicalHigh[1], corresponds to the highest market price in the previous period, and so on..
historicalLow - an array where the first element historicalLow[0], corresponds to the lowest market price during the last trading period, the historicalLow[1], corresponds to the lowest market price in the previous period, and so on...
Throws:
IllegalArgumentException - thrown if the arrays historicalHigh and historicalLow are of different lengths or if one of these arrays is empty.

geometricMovingAverage

public double geometricMovingAverage(double[] historicalValue)
Calculates the x-day Geometric Moving Average (GMA) which is the geometric average of the values given over the past x days. This indicator is particularly appropriate in the study of time series of values which obey the logarithm addition law. One such example is a series of the returns of an asset over a number of periods.

Parameters:
historicalValue - an array of length x, where the first element historicalPrice[0], corresponds to the market on the first of the x-day period. The term historicalPrice[1], corresponds to the market price of the second of the x-day period, and so on...
Throws:
IllegalArgumentException - thrown if the historicalValue array is empty.

weightedxDayMovingAverage

public double weightedxDayMovingAverage(double[] historicalPrices,
                                        double[] weights)
Here we evaluate the Weighted Moving Average (WMA) which allows you to assign more significance to resent price dynamics.

Parameters:
weights - this is an array of length x, which assigns to each of the historicalPrices a weighting. The ith element weights[i], assigns to the element historicalPrice[i], a weighting.
historicalPrices - an array of length x, where the first element historicalPrice[0], corresponds to the market on the first of the x-day period. The term historicalPrice[1], corresponds to the market price of the second of the x-day period, and so on.
Throws:
IllegalArgumentException - thrown if the length of the weights and historicalPrices arrays differ or if either array is empty.
See Also:
xDayMovingAverage

linearlyWeightedMovingAverage

public double linearlyWeightedMovingAverage(double[] priceSeries)
Returns the value of the Linearly Weighted Moving Average (LWMA) of a (finite) price series. The Linearly Weighted Moving Average (LWMA) weights the time series by assigning a weight of 1, to the oldest price and a weight of 2 to the second oldest price on so on... Until the weight of the most recent value is assigned to be the number of days in the time series. Then the LWMA is given by the sum of the weighted prices divided by the sum of the weights.

Parameters:
priceSeries - an array where the first element is the price on the earliest day, the second element is the price on the next earliest day and on so.
Throws:
IllegalArgumentException - thrown if the array priceSeries is empty.

exponentiallyWeightedMovingAverage

public double exponentiallyWeightedMovingAverage(double[] timeSeries,
                                                 double smoothingFactor)
Evaluates the (x-day) Exponentially Weighted Moving Average (EWMA) of a time series from the 0-th period until the (x-1)-th period.

Parameters:
timeSeries - an array where the first value corresponds to the value of the asset in the $t$th period, and the second value corresponds to the value of the asset in the $t-1$th period and so on
smoothingFactor - the number between 0 and 1 which is known as a smoothing factor. The closer the value is to zero the more influence more resent measurements will have on the EWMA.
Throws:
IllegalArgumentException - thrown if the timeSeries is empty or if the value given for the smoothing factor lies outside the closed range [0,1].

kairi

public double kairi(double movingAverage,
                    double price)
Calculates the Kairi Indicator measures as a percentage of the price the divergence between the a moving average (generally the simple moving average) of the price and the price itself. The Kairi Indicator is often used with conjunction with other moving averages within trading systems.

The formulae for the Kairi Indicator is as follows:


Kairi Indicator = (MA - price)/ price

where MA is the moving average being considered and price is the present price of the underlying asset.

Application

The Kairi Indicator can be used in order to take advantage of an over extended trending market. For example, in an upwardly trending market when the price gets say more than 10% above the simple moving average, the asset could be sold and repurchased when the next hits the simple moving average again.

The Kairi Indicator could also be used in order to detect market tops and bottom. The idea being that market tops and bottoms often occur when the price is at an extreme value in relation to its moving average. That is, the Kairi Indicator should take an extreme value at market tops and bottoms.

Parameters:
movingAverage - the value of the moving average (generally the simple moving average) of the underlying asset
price - the present price of the underlying asset
Returns:
The percentage expressed as decimal format (i.e. 0.01 = 1%) between the present price and a moving average of the price.

simpleCrossingSignal

public int simpleCrossingSignal(double lastShortMA,
                                double previousShortMA,
                                double lastLongMA,
                                double previousLongMA)
Generates of trading signal in accordance with the Simple crossing two moving average trading system. This system uses the classical approach of using the crossing of moving averages to generate trading signals.

Selecting the Moving Averages

You will need to select the type of moving average used and the different periods over which these moving averages are evaluated. In most, instances the simple moving average is used but in principle any type of moving average could be used. The periods of moving averages must be different. Typical choices of period used correspond roughly to convenient time periods, such as: 5 (1 week), 20 (1 month), 50 (2 months) (i.e. 50), 200 (1 year).

We will refer to the moving average with the shorter period as the Short MA, and the moving average with the longer period as the Long MA. Corresponding to the fact that they measure the trending behavior on shorter and long time spans.

Generation of Trading Signals

Trading signals are generated when:

Parameters:
lastShortMA - the last value of the Short moving average (i.e. the moving average with the shorter period)
previousShortMA - the previous value of the short moving average (i.e. the moving average with the shorter period)
lastLongMA - the last value of the Long moving average (i.e. the moving average with the longer period)
previousLongMA - the previous value of the long moving average (i.e. the moving average with the long period)
Returns:
this method returns a -1, 0, 1; depending on whether a sell, no signal or buy signal was generate by the system.

WebCab Technical Analysis
(J2SE Edition)