Learn R Programming

TTR (version 0.21-1)

stochastics: Stochastic Oscillator / Stochastic Momentum Index

Description

The stochastic oscillator is a momentum indicator that relates the location of each day's close relative to the high/low range over the past n periods. Developed by George C. Lane in the late 1950s. The SMI relates the close to the midpoint of the high/low range. Developed by William Blau in 1993.

Usage

stoch(HLC, nFastK=14, nFastD=3, nSlowD=3, maType, bounded=TRUE, smooth=1, ...)

SMI(HLC, n=13, nFast=2, nSlow=25, nSig=9, maType, bounded=TRUE, ...)

Arguments

HLC
Object that is coercible to xts or matrix and contains High-Low-Close prices. If only a univariate series is given, it will be used. See details.
n
Number of periods to use.
nFastK
Number of periods for fast %K (i.e. the number of past periods to use).
nFastD
Number of periods for fast %D (i.e. the number smoothing periods to apply to fast %K).
nSlowD
Number of periods for slow %D (i.e. the number smoothing periods to apply to fast %D).
smooth
Number of internal smoothing periods to be applied before calculating FastK. See Details.
nFast
Number of periods for initial smoothing.
nSlow
Number of periods for double smoothing.
nSig
Number of periods for signal line.
maType
Either: (1) A function or a string naming the function to be called, or (2) a list with the first component like (1) above, and additional parameters specified as named components. See Examples.
bounded
Logical, should current period's values be used in the calculation?
...
Other arguments to be passed to the maType function in case (1) above.

Value

  • A object of the same class as HLC or a matrix (if try.xts fails) containing the columns:
  • fastKStochastic Fast %K
  • fastDStochastic Fast %D
  • slowDStochastic Slow %D
  • SMIStochastic Momentum Index
  • signalStochastic Momentum Index signal line

Details

If a High-Low-Close series is provided, the indicator is calculated using the high/low values. If a vector is provided, the calculation only uses that series. This allows stochastics to be calculated for: (1) series that have no HLC definition (e.g. foreign exchange), and (2) stochastic indicators (e.g. stochastic RSI - see examples).

The smooth argument is the number of periods of internal smoothing to apply to the differences in the high-low-close range before calculating Fast K. Thanks to Stanley Neo for the suggestion.

References

The following site(s) were used to code/document these indicators: Stochastic Oscillator: http://www.fmlabs.com/reference/StochasticOscillator.htm http://www.equis.com/Customer/Resources/TAAZ?c=3&p=106 http://linnsoft.com/tour/techind/stoc.htm http://stockcharts.com/education/IndicatorAnalysis/indic_stochasticOscillator.html SMI: http://www.fmlabs.com/reference/default.htm?url=SMI.htm

See Also

See EMA, SMA, etc. for moving average options; and note Warning section. See WPR to compare it's results to fast %K.

Examples

Run this code
data(ttrc)
  stochOSC <- stoch(ttrc[,c("High","Low","Close")])
  stochWPR <- WPR(ttrc[,c("High","Low","Close")])

  plot(tail(stochOSC[,"fastK"], 100), type="l",
      main="Fast %K and Williams %R", ylab="",
      ylim=range(cbind(stochOSC, stochWPR), na.rm=TRUE) )
  lines(tail(stochWPR, 100), col="blue")
  lines(tail(1-stochWPR, 100), col="red", lty="dashed")

  stoch2MA <- stoch( ttrc[,c("High","Low","Close")],
      maType=list(list(SMA), list(EMA, wilder=TRUE), list(SMA)) )

  SMI3MA <- SMI(ttrc[,c("High","Low","Close")],
      maType=list(list(SMA), list(EMA, wilder=TRUE), list(SMA)) )

  stochRSI <- stoch( RSI(ttrc[,"Close"]) )

Run the code above in your browser using DataLab