# Stochastic

ICE Data Services -

stochK(), stochD() Built-in Study Functions

The Stochastic is designed to indicate when the market is overbought or oversold. It is based on the premise that when a market's price increases, the closing prices tend to move toward the daily highs and, conversely, when a market's price decreases, the closing prices move toward the daily lows. A Stochastic displays two lines, %K and %D. %K is calculated by finding the highest and lowest point in a trading period and then finding where the current close is in relation to that trading range, %K is then smoothed with a moving average. %D is a moving average of %K.

Syntax
stochK(  nKLength , nKSmoothing , nDLength [, Series | sym() | inv() ][, nBarIndex ] )
stochD( nKLength , nKSmoothing , nDLength [, Series | sym() | inv() ][, nBarIndex ] )

Parameters
Parameter: Description: Default:
nKLength Required. Number of periods to use for the fastK calculation.

n/a nKSmoothing Required. Number of periods to use for the %K smoothing calculation.

n/a nDLength Required. Number of periods to use for the fastD calculation.

n/a Series or
sym() or
inv()
[Optional]  Series Object or function of sym() or inv() to determine symbol/interval source for the study.

Chart's sym/inv nBarIndex [Optional]  Bar index of series to retrieve.

n/a

Return Value(s)

Returns a Series Object when nBarIndex is not specified.

Returns a single value when nBarIndex is specified.

Notes
Only available in versions 7.9 or later.

Code Examples
Single Line Indicator:
 `function main() { return new Array(stochK(14, 1, 3), stochD(14, 1, 3) );}`

Single Line Study-on-Study Indicator:
 `function main() { return new Array(stochK(14, 1, 3, hl2()), stochD(14, 1, 3, hl2()) );}`

Retrieve single values:
 ``function main() { var nValue_0 = stochD(14, 1, 3, 0); // Current Bar Index var nValue_1 = stochD(14, 1, 3, -1); // Prior Bar Index return new Array(nValue_0, nValue_1);}``

Initialize Series Objects:
 ``//global Series Object variablesvar xStudy1 = null;var xStudy2 = null;function main() { if (xStudy1 == null) xStudy1 = stochK(14, 1, 3); if (xStudy2 == null) xStudy2 = stochD(14, 1, 3); // retrieve single values for conditional statements var nValue1_0 = xStudy1.getValue(0); // Current Bar Index value var nValue2_0 = xStudy2.getValue(0); // Current Bar Index value var nValue1_1 = xStudy1.getValue(-1); // Prior Bar Index value // Plot Current Bar Index Value return new Array(nValue1_0, nValue2_0); }``

Initialize a Series Object based on an external interval:
 ``// global Series Object variablesvar xStudy1 = null;var xStudy2 = null;var bInit = false; // Initialization flagfunction main() { if (bInit == false) { xStudy1 = stochK(14, 1, 3, inv(20)); // 20-min interval xStudy2 = stochD(14, 1, 3, inv(20)); // 20-min interval bInit = true; } // retrieve single values for conditional statements var nValue1_0 = xStudy1.getValue(0); // Current Bar Index value var nValue1_1 = xStudy1.getValue(-1); // Prior Bar Index value // Synchronized Series plot return new Array(getSeries(xStudy1), getSeries(xStudy2) ); }``

Initialize a Series Object based on an external symbol and interval:
 ``// global Series Object variablesvar xStudy1 = null;var xStudy2 = null;var bInit = false; // Initialization flagfunction main() { if (bInit == false) { xStudy1 = stochK(14, 1, 3, sym("IBM,20")); // IBM 20-min interval xStudy2 = stochD(``14, 1, 3, sym("IBM,20")``); // ``IBM ``20-min interval bInit = true; } // retrieve single values for conditional statements var nValue1_0 = xStudy1.getValue(0); // Current Bar Index value var nValue1_1 = xStudy1.getValue(-1); // Prior Bar Index value // Synchronized Series plot return new Array(getSeries(xStudy1), getSeries(xStudy2) ); }``